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