Allow building and running with wxWidgets 3.1.x as well as 3.0.x.
[dcpomatic.git] / src / wx / wx_util.cc
1 /*
2     Copyright (C) 2012-2018 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 /** @file src/wx/wx_util.cc
22  *  @brief Some utility functions and classes.
23  */
24
25 #include "wx_util.h"
26 #include "file_picker_ctrl.h"
27 #include "static_text.h"
28 #include "lib/config.h"
29 #include "lib/job_manager.h"
30 #include "lib/util.h"
31 #include "lib/cross.h"
32 #include "lib/job.h"
33 #include <dcp/locale_convert.h>
34 #include <wx/spinctrl.h>
35 #include <wx/splash.h>
36 #include <wx/progdlg.h>
37 #include <wx/filepicker.h>
38 #include <boost/thread.hpp>
39
40 using std::string;
41 using std::vector;
42 using std::pair;
43 using boost::shared_ptr;
44 using boost::optional;
45 using dcp::locale_convert;
46
47 wxStaticText *
48 #ifdef __WXOSX__
49 create_label (wxWindow* p, wxString t, bool left)
50 #else
51 create_label (wxWindow* p, wxString t, bool)
52 #endif
53 {
54 #ifdef __WXOSX__
55         if (left) {
56                 t += wxT (":");
57         }
58 #endif
59         return new StaticText (p, t);
60 }
61
62 /** Add a wxStaticText to a wxSizer, aligning it at vertical centre.
63  *  @param s Sizer to add to.
64  *  @param p Parent window for the wxStaticText.
65  *  @param t Text for the wxStaticText.
66  *  @param left true if this label is a `left label'; ie the sort
67  *  of label which should be right-aligned on OS X.
68  *  @param prop Proportion to pass when calling Add() on the wxSizer.
69  */
70 wxStaticText *
71 add_label_to_sizer (wxSizer* s, wxWindow* p, wxString t, bool left, int prop, int flags)
72 {
73 #ifdef __WXOSX__
74         if (left) {
75                 flags |= wxALIGN_RIGHT;
76         }
77 #endif
78         wxStaticText* m = create_label (p, t, left);
79         s->Add (m, prop, flags, 6);
80         return m;
81 }
82
83
84 /* Hack: this is the same but has different default flags */
85 wxStaticText *
86 add_label_to_vertical_sizer (wxSizer* s, wxWindow* p, wxString t, bool left, int prop, int flags)
87 {
88 #ifdef __WXOSX__
89         if (left) {
90                 flags |= wxALIGN_RIGHT;
91         }
92 #endif
93         wxStaticText* m = create_label (p, t, left);
94         s->Add (m, prop, flags, 6);
95         return m;
96 }
97
98
99 wxStaticText *
100 #ifdef __WXOSX__
101 add_label_to_sizer (wxSizer* s, wxStaticText* t, bool left, int prop, int flags)
102 #else
103 add_label_to_sizer (wxSizer* s, wxStaticText* t, bool, int prop, int flags)
104 #endif
105 {
106 #ifdef __WXOSX__
107         if (left) {
108                 flags |= wxALIGN_RIGHT;
109         }
110 #endif
111         s->Add (t, prop, flags, 6);
112         return t;
113 }
114
115 wxStaticText *
116 add_label_to_sizer (wxGridBagSizer* s, wxWindow* p, wxString t, bool left, wxGBPosition pos, wxGBSpan span)
117 {
118         int flags = wxALIGN_CENTER_VERTICAL | wxLEFT | wxRIGHT;
119 #ifdef __WXOSX__
120         if (left) {
121                 flags |= wxALIGN_RIGHT;
122         }
123 #endif
124         wxStaticText* m = create_label (p, t, left);
125         s->Add (m, pos, span, flags);
126         return m;
127 }
128
129 wxStaticText *
130 #ifdef __WXOSX__
131 add_label_to_sizer (wxGridBagSizer* s, wxStaticText* t, bool left, wxGBPosition pos, wxGBSpan span)
132 #else
133 add_label_to_sizer (wxGridBagSizer* s, wxStaticText* t, bool, wxGBPosition pos, wxGBSpan span)
134 #endif
135 {
136         int flags = wxALIGN_CENTER_VERTICAL | wxLEFT | wxRIGHT;
137 #ifdef __WXOSX__
138         if (left) {
139                 flags |= wxALIGN_RIGHT;
140         }
141 #endif
142         s->Add (t, pos, span, flags);
143         return t;
144 }
145
146 /** Pop up an error dialogue box.
147  *  @param parent Parent.
148  *  @param m Message.
149  *  @param e Extended message.
150  */
151 void
152 error_dialog (wxWindow* parent, wxString m, optional<wxString> e)
153 {
154         wxMessageDialog* d = new wxMessageDialog (parent, m, _("DCP-o-matic"), wxOK | wxICON_ERROR);
155         if (e) {
156                 wxString em = *e;
157                 em[0] = wxToupper (em[0]);
158                 d->SetExtendedMessage (em);
159         }
160         d->ShowModal ();
161         d->Destroy ();
162 }
163
164 /** Pop up an error dialogue box.
165  *  @param parent Parent.
166  *  @param m Message.
167  */
168 void
169 message_dialog (wxWindow* parent, wxString m)
170 {
171         wxMessageDialog* d = new wxMessageDialog (parent, m, _("DCP-o-matic"), wxOK | wxICON_INFORMATION);
172         d->ShowModal ();
173         d->Destroy ();
174 }
175
176 bool
177 confirm_dialog (wxWindow* parent, wxString m)
178 {
179         wxMessageDialog* d = new wxMessageDialog (parent, m, _("DCP-o-matic"), wxYES_NO | wxICON_QUESTION);
180         int const r = d->ShowModal ();
181         d->Destroy ();
182         return r == wxID_YES;
183 }
184
185
186 /** @param s wxWidgets string.
187  *  @return Corresponding STL string.
188  */
189 string
190 wx_to_std (wxString s)
191 {
192         return string (s.ToUTF8 ());
193 }
194
195 /** @param s STL string.
196  *  @return Corresponding wxWidgets string.
197  */
198 wxString
199 std_to_wx (string s)
200 {
201         return wxString (s.c_str(), wxConvUTF8);
202 }
203
204 string
205 string_client_data (wxClientData* o)
206 {
207         return wx_to_std (dynamic_cast<wxStringClientData*>(o)->GetData());
208 }
209
210 void
211 checked_set (FilePickerCtrl* widget, boost::filesystem::path value)
212 {
213         if (widget->GetPath() != std_to_wx (value.string())) {
214                 if (value.empty()) {
215                         /* Hack to make wxWidgets clear the control when we are passed
216                            an empty value.
217                         */
218                         value = " ";
219                 }
220                 widget->SetPath (std_to_wx (value.string()));
221         }
222 }
223
224 void
225 checked_set (wxDirPickerCtrl* widget, boost::filesystem::path value)
226 {
227         if (widget->GetPath() != std_to_wx (value.string())) {
228                 if (value.empty()) {
229                         /* Hack to make wxWidgets clear the control when we are passed
230                            an empty value.
231                         */
232                         value = " ";
233                 }
234                 widget->SetPath (std_to_wx (value.string()));
235         }
236 }
237
238 void
239 checked_set (wxSpinCtrl* widget, int value)
240 {
241         if (widget->GetValue() != value) {
242                 widget->SetValue (value);
243         }
244 }
245
246 void
247 checked_set (wxSpinCtrlDouble* widget, double value)
248 {
249         /* XXX: completely arbitrary epsilon */
250         if (fabs (widget->GetValue() - value) > 1e-16) {
251                 widget->SetValue (value);
252         }
253 }
254
255 void
256 checked_set (wxChoice* widget, int value)
257 {
258         if (widget->GetSelection() != value) {
259                 widget->SetSelection (value);
260         }
261 }
262
263 void
264 checked_set (wxChoice* widget, string value)
265 {
266         wxClientData* o = 0;
267         if (widget->GetSelection() != -1) {
268                 o = widget->GetClientObject (widget->GetSelection ());
269         }
270
271         if (!o || string_client_data(o) != value) {
272                 for (unsigned int i = 0; i < widget->GetCount(); ++i) {
273                         if (string_client_data (widget->GetClientObject (i)) == value) {
274                                 widget->SetSelection (i);
275                         }
276                 }
277         }
278 }
279
280 void
281 checked_set (wxChoice* widget, vector<pair<string, string> > items)
282 {
283        vector<pair<string, string> > current;
284        for (unsigned int i = 0; i < widget->GetCount(); ++i) {
285                current.push_back (
286                        make_pair (
287                                wx_to_std (widget->GetString (i)),
288                                string_client_data (widget->GetClientObject (i))
289                                )
290                        );
291        }
292
293        if (current == items) {
294                return;
295        }
296
297        widget->Clear ();
298        for (vector<pair<string, string> >::const_iterator i = items.begin(); i != items.end(); ++i) {
299                widget->Append (std_to_wx (i->first), new wxStringClientData (std_to_wx (i->second)));
300        }
301 }
302
303 void
304 checked_set (wxTextCtrl* widget, string value)
305 {
306         if (widget->GetValue() != std_to_wx (value)) {
307                 widget->ChangeValue (std_to_wx (value));
308         }
309 }
310
311 void
312 checked_set (wxTextCtrl* widget, wxString value)
313 {
314         if (widget->GetValue() != value) {
315                 widget->ChangeValue (value);
316         }
317 }
318
319 void
320 checked_set (wxStaticText* widget, string value)
321 {
322         if (widget->GetLabel() != std_to_wx (value)) {
323                 widget->SetLabel (std_to_wx (value));
324         }
325 }
326
327 void
328 checked_set (wxStaticText* widget, wxString value)
329 {
330         if (widget->GetLabel() != value) {
331                 widget->SetLabel (value);
332         }
333 }
334
335 void
336 checked_set (wxCheckBox* widget, bool value)
337 {
338         if (widget->GetValue() != value) {
339                 widget->SetValue (value);
340         }
341 }
342
343 void
344 checked_set (wxRadioButton* widget, bool value)
345 {
346         if (widget->GetValue() != value) {
347                 widget->SetValue (value);
348         }
349 }
350
351 void
352 dcpomatic_setup_i18n ()
353 {
354         int language = wxLANGUAGE_DEFAULT;
355
356         boost::optional<string> config_lang = Config::instance()->language ();
357         if (config_lang && !config_lang->empty ()) {
358                 wxLanguageInfo const * li = wxLocale::FindLanguageInfo (std_to_wx (config_lang.get ()));
359                 if (li) {
360                         language = li->Language;
361                 }
362         }
363
364         wxLocale* locale = 0;
365         if (wxLocale::IsAvailable (language)) {
366                 locale = new wxLocale (language, wxLOCALE_LOAD_DEFAULT);
367
368 #ifdef DCPOMATIC_WINDOWS
369                 locale->AddCatalogLookupPathPrefix (std_to_wx (mo_path().string()));
370 #endif
371
372 #ifdef DCPOMATIC_LINUX
373                 locale->AddCatalogLookupPathPrefix (LINUX_LOCALE_PREFIX);
374
375                 /* We have to include the wxWidgets .mo in our distribution,
376                    so we rename it to avoid clashes with any other installation
377                    of wxWidgets.
378                 */
379                 locale->AddCatalog (wxT ("dcpomatic2-wxstd"));
380
381                 /* Fedora 29 (at least) installs wxstd3.mo instead of wxstd.mo */
382                 locale->AddCatalog (wxT ("wxstd3"));
383 #endif
384
385                 locale->AddCatalog (wxT ("libdcpomatic2-wx"));
386                 locale->AddCatalog (wxT ("dcpomatic2"));
387
388                 if (!locale->IsOk()) {
389                         delete locale;
390                         locale = new wxLocale (wxLANGUAGE_ENGLISH);
391                 }
392         }
393
394         if (locale) {
395                 dcpomatic_setup_gettext_i18n (wx_to_std (locale->GetCanonicalName ()));
396         }
397 }
398
399 int
400 wx_get (wxSpinCtrl* w)
401 {
402         return w->GetValue ();
403 }
404
405 int
406 wx_get (wxChoice* w)
407 {
408         return w->GetSelection ();
409 }
410
411 double
412 wx_get (wxSpinCtrlDouble* w)
413 {
414         return w->GetValue ();
415 }
416
417 /** @param s String of the form Context|String
418  *  @return translation, or String if no translation is available.
419  */
420 wxString
421 context_translation (wxString s)
422 {
423         wxString t = wxGetTranslation (s);
424         if (t == s) {
425                 /* No translation; strip the context */
426                 int c = t.Find (wxT ("|"));
427                 if (c != wxNOT_FOUND) {
428                         t = t.Mid (c + 1);
429                 }
430         }
431
432         return t;
433 }
434
435 wxString
436 time_to_timecode (DCPTime t, double fps)
437 {
438         double w = t.seconds ();
439         int const h = (w / 3600);
440         w -= h * 3600;
441         int const m = (w / 60);
442         w -= m * 60;
443         int const s = floor (w);
444         w -= s;
445         int const f = lrint (w * fps);
446         return wxString::Format (wxT("%02d:%02d:%02d.%02d"), h, m, s, f);
447 }
448
449 void
450 setup_audio_channels_choice (wxChoice* choice, int minimum)
451 {
452         vector<pair<string, string> > items;
453         for (int i = minimum; i <= 16; i += 2) {
454                 if (i == 2) {
455                         items.push_back (make_pair (wx_to_std (_("2 - stereo")), locale_convert<string> (i)));
456                 } else if (i == 4) {
457                         items.push_back (make_pair (wx_to_std (_("4 - L/C/R/Lfe")), locale_convert<string> (i)));
458                 } else if (i == 6) {
459                         items.push_back (make_pair (wx_to_std (_("6 - 5.1")), locale_convert<string> (i)));
460                 } else if (i == 8) {
461                         items.push_back (make_pair (wx_to_std (_("8 - 5.1/HI/VI")), locale_convert<string> (i)));
462                 } else if (i == 12) {
463                         items.push_back (make_pair (wx_to_std (_("12 - 7.1/HI/VI")), locale_convert<string> (i)));
464                 } else {
465                         items.push_back (make_pair (locale_convert<string> (i), locale_convert<string> (i)));
466                 }
467         }
468
469         checked_set (choice, items);
470 }
471
472 wxSplashScreen *
473 maybe_show_splash ()
474 {
475         wxSplashScreen* splash = 0;
476         try {
477                 wxBitmap bitmap;
478                 boost::filesystem::path p = shared_path () / "splash.png";
479                 if (bitmap.LoadFile (std_to_wx (p.string ()), wxBITMAP_TYPE_PNG)) {
480                         splash = new wxSplashScreen (bitmap, wxSPLASH_CENTRE_ON_SCREEN | wxSPLASH_NO_TIMEOUT, 0, 0, -1);
481                         wxYield ();
482                 }
483         } catch (boost::filesystem::filesystem_error& e) {
484                 /* Maybe we couldn't find the splash image; never mind */
485         }
486
487         return splash;
488 }
489
490 double
491 calculate_mark_interval (double mark_interval)
492 {
493         if (mark_interval > 5) {
494                 mark_interval -= lrint (mark_interval) % 5;
495         }
496         if (mark_interval > 10) {
497                 mark_interval -= lrint (mark_interval) % 10;
498         }
499         if (mark_interval > 60) {
500                 mark_interval -= lrint (mark_interval) % 60;
501         }
502         if (mark_interval > 3600) {
503                 mark_interval -= lrint (mark_interval) % 3600;
504         }
505
506         if (mark_interval < 1) {
507                 mark_interval = 1;
508         }
509
510         return mark_interval;
511 }
512
513
514 /** @return false if the task was cancelled */
515 bool
516 display_progress (wxString title, wxString task)
517 {
518         JobManager* jm = JobManager::instance ();
519
520         wxProgressDialog progress (title, task, 100, 0, wxPD_CAN_ABORT);
521
522         bool ok = true;
523
524         while (jm->work_to_do()) {
525                 dcpomatic_sleep (1);
526                 if (!progress.Pulse()) {
527                         /* user pressed cancel */
528                         BOOST_FOREACH (shared_ptr<Job> i, jm->get()) {
529                                 i->cancel();
530                         }
531                         ok = false;
532                         break;
533                 }
534         }
535
536         return ok;
537 }
538
539 bool
540 report_errors_from_last_job (wxWindow* parent)
541 {
542         JobManager* jm = JobManager::instance ();
543
544         DCPOMATIC_ASSERT (!jm->get().empty());
545
546         shared_ptr<Job> last = jm->get().back();
547         if (last->finished_in_error()) {
548                 error_dialog(parent, std_to_wx(last->error_summary()) + ".\n", std_to_wx(last->error_details()));
549                 return false;
550         }
551
552         return true;
553 }