Use a separate file (in a configurable location) to store cinema / screen certificate...
[dcpomatic.git] / src / wx / wx_util.cc
1 /*
2     Copyright (C) 2012-2015 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 /** @file src/wx/wx_util.cc
21  *  @brief Some utility functions and classes.
22  */
23
24 #include "wx_util.h"
25 #include "file_picker_ctrl.h"
26 #include "lib/config.h"
27 #include "lib/util.h"
28 #include <wx/spinctrl.h>
29 #include <boost/thread.hpp>
30
31 using namespace std;
32 using namespace boost;
33
34 /** Add a wxStaticText to a wxSizer, aligning it at vertical centre.
35  *  @param s Sizer to add to.
36  *  @param p Parent window for the wxStaticText.
37  *  @param t Text for the wxStaticText.
38  *  @param left true if this label is a `left label'; ie the sort
39  *  of label which should be right-aligned on OS X.
40  *  @param prop Proportion to pass when calling Add() on the wxSizer.
41  */
42 wxStaticText *
43 #ifdef __WXOSX__
44 add_label_to_sizer (wxSizer* s, wxWindow* p, wxString t, bool left, int prop)
45 #else
46 add_label_to_sizer (wxSizer* s, wxWindow* p, wxString t, bool, int prop)
47 #endif
48 {
49         int flags = wxALIGN_CENTER_VERTICAL | wxLEFT | wxRIGHT;
50 #ifdef __WXOSX__
51         if (left) {
52                 flags |= wxALIGN_RIGHT;
53                 t += wxT (":");
54         }
55 #endif
56         wxStaticText* m = new wxStaticText (p, wxID_ANY, t);
57         s->Add (m, prop, flags, 6);
58         return m;
59 }
60
61 wxStaticText *
62 #ifdef __WXOSX__
63 add_label_to_sizer (wxGridBagSizer* s, wxWindow* p, wxString t, bool left, wxGBPosition pos, wxGBSpan span)
64 #else
65 add_label_to_sizer (wxGridBagSizer* s, wxWindow* p, wxString t, bool, wxGBPosition pos, wxGBSpan span)
66 #endif
67 {
68         int flags = wxALIGN_CENTER_VERTICAL | wxLEFT | wxRIGHT;
69 #ifdef __WXOSX__
70         if (left) {
71                 flags |= wxALIGN_RIGHT;
72                 t += wxT (":");
73         }
74 #endif
75         wxStaticText* m = new wxStaticText (p, wxID_ANY, t);
76         s->Add (m, pos, span, flags);
77         return m;
78 }
79
80 /** Pop up an error dialogue box.
81  *  @param parent Parent.
82  *  @param m Message.
83  */
84 void
85 error_dialog (wxWindow* parent, wxString m)
86 {
87         wxMessageDialog* d = new wxMessageDialog (parent, m, _("DCP-o-matic"), wxOK | wxICON_ERROR);
88         d->ShowModal ();
89         d->Destroy ();
90 }
91
92 /** Pop up an error dialogue box.
93  *  @param parent Parent.
94  *  @param m Message.
95  */
96 void
97 message_dialog (wxWindow* parent, wxString m)
98 {
99         wxMessageDialog* d = new wxMessageDialog (parent, m, _("DCP-o-matic"), wxOK | wxICON_INFORMATION);
100         d->ShowModal ();
101         d->Destroy ();
102 }
103
104 bool
105 confirm_dialog (wxWindow* parent, wxString m)
106 {
107         wxMessageDialog* d = new wxMessageDialog (parent, m, _("DCP-o-matic"), wxYES_NO | wxICON_QUESTION);
108         int const r = d->ShowModal ();
109         d->Destroy ();
110         return r == wxID_YES;
111 }
112
113
114 /** @param s wxWidgets string.
115  *  @return Corresponding STL string.
116  */
117 string
118 wx_to_std (wxString s)
119 {
120         return string (s.ToUTF8 ());
121 }
122
123 /** @param s STL string.
124  *  @return Corresponding wxWidgets string.
125  */
126 wxString
127 std_to_wx (string s)
128 {
129         return wxString (s.c_str(), wxConvUTF8);
130 }
131
132 string
133 string_client_data (wxClientData* o)
134 {
135         return wx_to_std (dynamic_cast<wxStringClientData*>(o)->GetData());
136 }
137
138 void
139 checked_set (FilePickerCtrl* widget, boost::filesystem::path value)
140 {
141         if (widget->GetPath() != std_to_wx (value.string())) {
142                 if (value.empty()) {
143                         /* Hack to make wxWidgets clear the control when we are passed
144                            an empty value.
145                         */
146                         value = " ";
147                 }
148                 widget->SetPath (std_to_wx (value.string()));
149         }
150 }
151
152 void
153 checked_set (wxSpinCtrl* widget, int value)
154 {
155         if (widget->GetValue() != value) {
156                 widget->SetValue (value);
157         }
158 }
159
160 void
161 checked_set (wxSpinCtrlDouble* widget, double value)
162 {
163         /* XXX: completely arbitrary epsilon */
164         if (fabs (widget->GetValue() - value) > 1e-16) {
165                 widget->SetValue (value);
166         }
167 }
168
169 void
170 checked_set (wxChoice* widget, int value)
171 {
172         if (widget->GetSelection() != value) {
173                 widget->SetSelection (value);
174         }
175 }
176
177 void
178 checked_set (wxChoice* widget, string value)
179 {
180         wxClientData* o = 0;
181         if (widget->GetSelection() != -1) {
182                 o = widget->GetClientObject (widget->GetSelection ());
183         }
184
185         if (!o || string_client_data(o) != value) {
186                 for (unsigned int i = 0; i < widget->GetCount(); ++i) {
187                         if (string_client_data (widget->GetClientObject (i)) == value) {
188                                 widget->SetSelection (i);
189                         }
190                 }
191         }
192 }
193
194 void
195 checked_set (wxChoice* widget, vector<pair<string, string> > items)
196 {
197        vector<pair<string, string> > current;
198        for (unsigned int i = 0; i < widget->GetCount(); ++i) {
199                current.push_back (
200                        make_pair (
201                                wx_to_std (widget->GetString (i)),
202                                string_client_data (widget->GetClientObject (i))
203                                )
204                        );
205        }
206
207        if (current == items) {
208                return;
209        }
210
211        widget->Clear ();
212        for (vector<pair<string, string> >::const_iterator i = items.begin(); i != items.end(); ++i) {
213                widget->Append (std_to_wx (i->first), new wxStringClientData (std_to_wx (i->second)));
214        }
215 }
216
217 void
218 checked_set (wxTextCtrl* widget, string value)
219 {
220         if (widget->GetValue() != std_to_wx (value)) {
221                 widget->ChangeValue (std_to_wx (value));
222         }
223 }
224
225 void
226 checked_set (wxTextCtrl* widget, wxString value)
227 {
228         if (widget->GetValue() != value) {
229                 widget->ChangeValue (value);
230         }
231 }
232
233 void
234 checked_set (wxStaticText* widget, string value)
235 {
236         if (widget->GetLabel() != std_to_wx (value)) {
237                 widget->SetLabel (std_to_wx (value));
238         }
239 }
240
241 void
242 checked_set (wxStaticText* widget, wxString value)
243 {
244         if (widget->GetLabel() != value) {
245                 widget->SetLabel (value);
246         }
247 }
248
249 void
250 checked_set (wxCheckBox* widget, bool value)
251 {
252         if (widget->GetValue() != value) {
253                 widget->SetValue (value);
254         }
255 }
256
257 void
258 checked_set (wxRadioButton* widget, bool value)
259 {
260         if (widget->GetValue() != value) {
261                 widget->SetValue (value);
262         }
263 }
264
265 void
266 dcpomatic_setup_i18n ()
267 {
268         int language = wxLANGUAGE_DEFAULT;
269
270         boost::optional<string> config_lang = Config::instance()->language ();
271         if (config_lang && !config_lang->empty ()) {
272                 wxLanguageInfo const * li = wxLocale::FindLanguageInfo (std_to_wx (config_lang.get ()));
273                 if (li) {
274                         language = li->Language;
275                 }
276         }
277
278         wxLocale* locale = 0;
279         if (wxLocale::IsAvailable (language)) {
280                 locale = new wxLocale (language, wxLOCALE_LOAD_DEFAULT);
281
282 #ifdef DCPOMATIC_WINDOWS
283                 locale->AddCatalogLookupPathPrefix (std_to_wx (mo_path().string()));
284 #endif
285
286 #ifdef DCPOMATIC_LINUX
287                 locale->AddCatalogLookupPathPrefix (LINUX_LOCALE_PREFIX);
288
289                 /* We have to include the wxWidgets .mo in our distribution,
290                    so we rename it to avoid clashes with any other installation
291                    of wxWidgets.
292                 */
293                 locale->AddCatalog (wxT ("dcpomatic2-wxstd"));
294 #endif
295
296                 locale->AddCatalog (wxT ("libdcpomatic2-wx"));
297                 locale->AddCatalog (wxT ("dcpomatic2"));
298
299                 if (!locale->IsOk()) {
300                         delete locale;
301                         locale = new wxLocale (wxLANGUAGE_ENGLISH);
302                 }
303         }
304
305         if (locale) {
306                 dcpomatic_setup_gettext_i18n (wx_to_std (locale->GetCanonicalName ()));
307         }
308 }
309
310 int
311 wx_get (wxSpinCtrl* w)
312 {
313         return w->GetValue ();
314 }
315
316 int
317 wx_get (wxChoice* w)
318 {
319         return w->GetSelection ();
320 }
321
322 double
323 wx_get (wxSpinCtrlDouble* w)
324 {
325         return w->GetValue ();
326 }
327
328 /** @param s String of the form Context|String
329  *  @return translation, or String if no translation is available.
330  */
331 wxString
332 context_translation (wxString s)
333 {
334         wxString t = wxGetTranslation (s);
335         if (t == s) {
336                 /* No translation; strip the context */
337                 int c = t.Find (wxT ("|"));
338                 if (c != wxNOT_FOUND) {
339                         t = t.Mid (c + 1);
340                 }
341         }
342
343         return t;
344 }
345
346 wxString
347 time_to_timecode (DCPTime t, double fps)
348 {
349         double w = t.seconds ();
350         int const h = (w / 3600);
351         w -= h * 3600;
352         int const m = (w / 60);
353         w -= m * 60;
354         int const s = floor (w);
355         w -= s;
356         int const f = lrint (w * fps);
357         return wxString::Format (wxT("%02d:%02d:%02d.%02d"), h, m, s, f);
358 }