Restore time zone to Cinema and improve UI to use it (#2473).
[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 "dcpomatic_choice.h"
23 #include "kdm_timing_panel.h"
24 #include "static_text.h"
25 #include "time_picker.h"
26 #include "wx_util.h"
27 #include "lib/config.h"
28 #include <dcp/utc_offset.h>
29 #include <dcp/warnings.h>
30 LIBDCP_DISABLE_WARNINGS
31 #include <wx/datectrl.h>
32 #include <wx/dateevt.h>
33 LIBDCP_ENABLE_WARNINGS
34
35
36 using std::cout;
37 using boost::bind;
38
39
40 KDMTimingPanel::KDMTimingPanel (wxWindow* parent)
41         : wxPanel (parent, wxID_ANY)
42 {
43         auto overall_sizer = new wxBoxSizer (wxVERTICAL);
44
45 #ifdef __WXGTK3__
46         /* wxDatePickerCtrl is too small with the GTK3 backend so we need to make it bigger with some fudge factors */
47         wxClientDC dc (parent);
48         auto size = dc.GetTextExtent(wxT("99/99/9999"));
49         size.SetWidth (size.GetWidth() * 1.75);
50         size.SetHeight (-1);
51 #else
52         auto size = wxDefaultSize;
53 #endif
54
55         auto table = new wxBoxSizer (wxHORIZONTAL);
56         add_label_to_sizer (table, this, _("From"), false, 0, wxLEFT | wxRIGHT | wxALIGN_CENTRE_VERTICAL);
57         wxDateTime from;
58         from.SetToCurrent ();
59         _from_date = new wxDatePickerCtrl (this, wxID_ANY, from, wxDefaultPosition, size);
60 #ifdef DCPOMATIC_OSX
61         /* Hack to tweak alignment, which I can't get right by "proper" means for some reason */
62         table->Add (_from_date, 0, wxALIGN_CENTER_VERTICAL | wxBOTTOM, 4);
63 #else
64         table->Add (_from_date, 0, wxALIGN_CENTER_VERTICAL);
65 #endif
66
67 #ifdef __WXGTK3__
68         _from_time = new TimePickerText (this, from);
69 #else
70         _from_time = new TimePickerSpin (this, from);
71 #endif
72
73         table->Add (_from_time, 0, wxALIGN_CENTER_VERTICAL);
74
75         add_label_to_sizer (table, this, _("until"), false, 0, wxLEFT | wxRIGHT | wxALIGN_CENTRE_VERTICAL);
76         auto to = from;
77
78         auto const duration = Config::instance()->default_kdm_duration();
79         switch (duration.unit) {
80         case RoughDuration::Unit::DAYS:
81                 to.Add(wxDateSpan(0, 0, 0, duration.duration));
82                 break;
83         case RoughDuration::Unit::WEEKS:
84                 to.Add(wxDateSpan(0, 0, duration.duration, 0));
85                 break;
86         case RoughDuration::Unit::MONTHS:
87                 to.Add(wxDateSpan(0, duration.duration, 0, 0));
88                 break;
89         case RoughDuration::Unit::YEARS:
90                 to.Add(wxDateSpan(duration.duration, 0, 0, 0));
91                 break;
92         }
93
94         _until_date = new wxDatePickerCtrl (this, wxID_ANY, to, wxDefaultPosition, size);
95 #ifdef DCPOMATIC_OSX
96         /* Hack to tweak alignment, which I can't get right by "proper" means for some reason */
97         table->Add (_until_date, 0, wxALIGN_CENTER_VERTICAL | wxBOTTOM, 4);
98 #else
99         table->Add (_until_date, 0, wxALIGN_CENTER_VERTICAL);
100 #endif
101
102 #ifdef __WXGTK3__
103         _until_time = new TimePickerText (this, to);
104 #else
105         _until_time = new TimePickerSpin (this, to);
106 #endif
107
108         table->Add (_until_time, 0, wxALIGN_CENTER_VERTICAL);
109
110         add_label_to_sizer(table, this, _("UTC offset (time zone)"), true, 1, wxALIGN_CENTRE_VERTICAL);
111         _utc_offset = new Choice(this);
112         table->Add(_utc_offset, 0, wxALIGN_CENTRE_VERTICAL | wxLEFT, DCPOMATIC_SIZER_X_GAP);
113
114         overall_sizer->Add (table, 0, wxTOP, DCPOMATIC_SIZER_GAP);
115
116         _warning = new StaticText (this, wxT(""));
117         overall_sizer->Add (_warning, 0, wxTOP, DCPOMATIC_SIZER_GAP);
118         wxFont font = _warning->GetFont();
119         font.SetStyle(wxFONTSTYLE_ITALIC);
120         font.SetPointSize(font.GetPointSize() - 1);
121         _warning->SetForegroundColour (wxColour (255, 0, 0));
122         _warning->SetFont(font);
123
124         /* Default to UTC */
125         auto const sel = get_offsets(_offsets);
126         for (auto const& offset: _offsets) {
127                 _utc_offset->add_entry(offset.name);
128         }
129         _utc_offset->set(sel);
130
131         /* I said I've been to the year 3000.  Not much has changed but they live underwater.  And your In-in-in-interop DCP
132            is pretty fine.
133          */
134         _from_date->SetRange(wxDateTime(1, wxDateTime::Jan, 1900, 0, 0, 0, 0), wxDateTime(31, wxDateTime::Dec, 3000, 0, 0, 0, 0));
135         _until_date->SetRange(wxDateTime(1, wxDateTime::Jan, 1900, 0, 0, 0, 0), wxDateTime(31, wxDateTime::Dec, 3000, 0, 0, 0, 0));
136
137         _from_date->Bind (wxEVT_DATE_CHANGED, bind (&KDMTimingPanel::changed, this));
138         _until_date->Bind (wxEVT_DATE_CHANGED, bind (&KDMTimingPanel::changed, this));
139         _from_time->Changed.connect (bind (&KDMTimingPanel::changed, this));
140         _until_time->Changed.connect (bind (&KDMTimingPanel::changed, this));
141         _utc_offset->bind(&KDMTimingPanel::utc_offset_changed, this);
142
143         SetSizer (overall_sizer);
144 }
145
146
147 dcp::LocalTime
148 KDMTimingPanel::from () const
149 {
150         return local_time(_from_date, _from_time, utc_offset());
151 }
152
153
154 dcp::LocalTime
155 KDMTimingPanel::local_time(wxDatePickerCtrl* date_picker, TimePicker* time_picker, dcp::UTCOffset offset)
156 {
157         auto const date = date_picker->GetValue ();
158         return dcp::LocalTime(
159                 date.GetYear(),
160                 date.GetMonth() + 1,
161                 date.GetDay(),
162                 time_picker->hours(),
163                 time_picker->minutes(),
164                 offset
165                 );
166 }
167
168
169 dcp::LocalTime
170 KDMTimingPanel::until () const
171 {
172         return local_time(_until_date, _until_time, utc_offset());
173 }
174
175
176 bool
177 KDMTimingPanel::valid () const
178 {
179         return until() > from();
180 }
181
182
183 void
184 KDMTimingPanel::changed () const
185 {
186         if (valid ()) {
187                 _warning->SetLabel (wxT (""));
188         } else {
189                 _warning->SetLabel (_("The 'until' time must be after the 'from' time."));
190         }
191
192         TimingChanged ();
193 }
194
195
196 dcp::UTCOffset
197 KDMTimingPanel::utc_offset() const
198 {
199         auto const sel = _utc_offset->get();
200         if (!sel || *sel >= int(_offsets.size())) {
201                 return {};
202         }
203
204         return _offsets[*sel].offset;
205 }
206
207
208 void
209 KDMTimingPanel::utc_offset_changed()
210 {
211         _utc_offset_changed_once = true;
212         changed();
213 }
214
215
216 void
217 KDMTimingPanel::suggest_utc_offset(dcp::UTCOffset offset)
218 {
219         if (!_utc_offset_changed_once) {
220                 for (size_t i = 0; i < _offsets.size(); ++i) {
221                         if (_offsets[i].offset == offset) {
222                                 _utc_offset->set(i);
223                                 break;
224                         }
225                 }
226         }
227 }
228