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