Supporters update.
[dcpomatic.git] / src / wx / wx_util.cc
1 /*
2     Copyright (C) 2012-2021 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 /** @file src/wx/wx_util.cc
23  *  @brief Some utility functions and classes.
24  */
25
26
27 #include "file_picker_ctrl.h"
28 #include "language_tag_widget.h"
29 #include "password_entry.h"
30 #include "region_subtag_widget.h"
31 #include "static_text.h"
32 #include "wx_util.h"
33 #include "lib/config.h"
34 #include "lib/cross.h"
35 #include "lib/job.h"
36 #include "lib/job_manager.h"
37 #include "lib/util.h"
38 #include "lib/version.h"
39 #include <dcp/locale_convert.h>
40 #include <dcp/warnings.h>
41 LIBDCP_DISABLE_WARNINGS
42 #include <wx/spinctrl.h>
43 #include <wx/splash.h>
44 #include <wx/progdlg.h>
45 #include <wx/filepicker.h>
46 #include <wx/sizer.h>
47 LIBDCP_ENABLE_WARNINGS
48 #include <boost/thread.hpp>
49
50
51 using std::string;
52 using std::vector;
53 using std::pair;
54 using std::shared_ptr;
55 using boost::optional;
56 using dcp::locale_convert;
57 using namespace dcpomatic;
58
59
60 wxStaticText *
61 #ifdef __WXOSX__
62 create_label (wxWindow* p, wxString t, bool left)
63 #else
64 create_label (wxWindow* p, wxString t, bool)
65 #endif
66 {
67 #ifdef __WXOSX__
68         if (left) {
69                 t += wxT (":");
70         }
71 #endif
72         return new StaticText (p, t);
73 }
74
75
76 #ifdef __WXOSX__
77 static
78 void
79 setup_osx_flags (wxSizer* s, bool left, int& flags)
80 {
81         if (left) {
82                 auto box = dynamic_cast<wxBoxSizer*>(s);
83                 if (!box || box->GetOrientation() != wxHORIZONTAL) {
84                         flags |= wxALIGN_RIGHT;
85                 }
86         }
87 }
88 #endif
89
90
91 /** Add a wxStaticText to a wxSizer.
92  *  @param s Sizer to add to.
93  *  @param p Parent window for the wxStaticText.
94  *  @param t Text for the wxStaticText.
95  *  @param left true if this label is a `left label'; ie the sort
96  *  of label which should be right-aligned on OS X.
97  *  @param prop Proportion to pass when calling Add() on the wxSizer.
98  */
99 wxStaticText *
100 add_label_to_sizer (wxSizer* s, wxWindow* p, wxString t, bool left, int prop, int flags)
101 {
102 #ifdef __WXOSX__
103         setup_osx_flags (s, left, flags);
104 #endif
105         auto m = create_label (p, t, left);
106         s->Add (m, prop, flags, DCPOMATIC_SIZER_GAP);
107         return m;
108 }
109
110
111 wxStaticText *
112 #ifdef __WXOSX__
113 add_label_to_sizer (wxSizer* s, wxStaticText* t, bool left, int prop, int flags)
114 #else
115 add_label_to_sizer (wxSizer* s, wxStaticText* t, bool, int prop, int flags)
116 #endif
117 {
118 #ifdef __WXOSX__
119         setup_osx_flags (s, left, flags);
120 #endif
121         s->Add (t, prop, flags, DCPOMATIC_SIZER_GAP);
122         return t;
123 }
124
125
126 wxStaticText *
127 add_label_to_sizer (wxGridBagSizer* s, wxWindow* p, wxString t, bool left, wxGBPosition pos, wxGBSpan span)
128 {
129         int flags = wxALIGN_CENTER_VERTICAL | wxLEFT | wxRIGHT;
130 #ifdef __WXOSX__
131         setup_osx_flags (s, left, flags);
132 #endif
133         auto m = create_label (p, t, left);
134         s->Add (m, pos, span, flags);
135         return m;
136 }
137
138
139 wxStaticText *
140 #ifdef __WXOSX__
141 add_label_to_sizer (wxGridBagSizer* s, wxStaticText* t, bool left, wxGBPosition pos, wxGBSpan span)
142 #else
143 add_label_to_sizer (wxGridBagSizer* s, wxStaticText* t, bool, wxGBPosition pos, wxGBSpan span)
144 #endif
145 {
146         int flags = wxALIGN_CENTER_VERTICAL | wxLEFT | wxRIGHT;
147 #ifdef __WXOSX__
148         setup_osx_flags (s, left, flags);
149 #endif
150         s->Add (t, pos, span, flags);
151         return t;
152 }
153
154
155 /** Pop up an error dialogue box.
156  *  @param parent Parent.
157  *  @param m Message.
158  *  @param e Extended message.
159  */
160 void
161 error_dialog (wxWindow* parent, wxString m, optional<wxString> e)
162 {
163         auto d = new wxMessageDialog (parent, m, _("DCP-o-matic"), wxOK | wxICON_ERROR);
164         if (e) {
165                 wxString em = *e;
166                 em[0] = wxToupper (em[0]);
167                 d->SetExtendedMessage (em);
168         }
169         d->ShowModal ();
170         d->Destroy ();
171 }
172
173
174 /** Pop up an error dialogue box.
175  *  @param parent Parent.
176  *  @param m Message.
177  */
178 void
179 message_dialog (wxWindow* parent, wxString m)
180 {
181         auto d = new wxMessageDialog (parent, m, _("DCP-o-matic"), wxOK | wxICON_INFORMATION);
182         d->ShowModal ();
183         d->Destroy ();
184 }
185
186
187 /** @return true if the user answered "yes" */
188 bool
189 confirm_dialog (wxWindow* parent, wxString m)
190 {
191         auto d = new wxMessageDialog (parent, m, _("DCP-o-matic"), wxYES_NO | wxICON_QUESTION);
192         int const r = d->ShowModal ();
193         d->Destroy ();
194         return r == wxID_YES;
195 }
196
197
198 /** @param s wxWidgets string.
199  *  @return Corresponding STL string.
200  */
201 string
202 wx_to_std (wxString s)
203 {
204         return string (s.ToUTF8());
205 }
206
207
208 /** @param s STL string.
209  *  @return Corresponding wxWidgets string.
210  */
211 wxString
212 std_to_wx (string s)
213 {
214         return wxString (s.c_str(), wxConvUTF8);
215 }
216
217
218 string
219 string_client_data (wxClientData* o)
220 {
221         return wx_to_std (dynamic_cast<wxStringClientData*>(o)->GetData());
222 }
223
224
225 void
226 checked_set (FilePickerCtrl* widget, boost::filesystem::path value)
227 {
228         if (widget->GetPath() != std_to_wx (value.string())) {
229                 if (value.empty()) {
230                         /* Hack to make wxWidgets clear the control when we are passed
231                            an empty value.
232                         */
233                         value = " ";
234                 }
235                 widget->SetPath (std_to_wx (value.string()));
236         }
237 }
238
239
240 void
241 checked_set (wxDirPickerCtrl* widget, boost::filesystem::path value)
242 {
243         if (widget->GetPath() != std_to_wx (value.string())) {
244                 if (value.empty()) {
245                         /* Hack to make wxWidgets clear the control when we are passed
246                            an empty value.
247                         */
248                         value = " ";
249                 }
250                 widget->SetPath (std_to_wx (value.string()));
251         }
252 }
253
254
255 void
256 checked_set (wxSpinCtrl* widget, int value)
257 {
258         if (widget->GetValue() != value) {
259                 widget->SetValue (value);
260         }
261 }
262
263
264 void
265 checked_set (wxSpinCtrlDouble* widget, double value)
266 {
267         /* XXX: completely arbitrary epsilon */
268         if (fabs (widget->GetValue() - value) > 1e-16) {
269                 widget->SetValue (value);
270         }
271 }
272
273
274 void
275 checked_set (wxChoice* widget, int value)
276 {
277         if (widget->GetSelection() != value) {
278                 widget->SetSelection (value);
279         }
280 }
281
282
283 void
284 checked_set (wxChoice* widget, string value)
285 {
286         wxClientData* o = nullptr;
287         if (widget->GetSelection() != -1) {
288                 o = widget->GetClientObject (widget->GetSelection ());
289         }
290
291         if (!o || string_client_data(o) != value) {
292                 for (unsigned int i = 0; i < widget->GetCount(); ++i) {
293                         if (string_client_data (widget->GetClientObject (i)) == value) {
294                                 widget->SetSelection (i);
295                         }
296                 }
297         }
298 }
299
300
301 void
302 checked_set (wxChoice* widget, vector<pair<string, string>> items)
303 {
304        vector<pair<string, string>> current;
305        for (unsigned int i = 0; i < widget->GetCount(); ++i) {
306                current.push_back (
307                        make_pair(
308                                wx_to_std(widget->GetString(i)),
309                                widget->GetClientData() ? string_client_data(widget->GetClientObject(i)) : ""
310                                )
311                        );
312        }
313
314        if (current == items) {
315                return;
316        }
317
318        widget->Clear ();
319        for (auto i: items) {
320                widget->Append (std_to_wx(i.first), new wxStringClientData(std_to_wx(i.second)));
321        }
322 }
323
324
325 void
326 checked_set (wxTextCtrl* widget, string value)
327 {
328         if (widget->GetValue() != std_to_wx (value)) {
329                 widget->ChangeValue (std_to_wx (value));
330         }
331 }
332
333
334 void
335 checked_set (PasswordEntry* entry, string value)
336 {
337         if (entry->get() != value) {
338                 entry->set(value);
339         }
340 }
341
342
343 void
344 checked_set (wxTextCtrl* widget, wxString value)
345 {
346         if (widget->GetValue() != value) {
347                 widget->ChangeValue (value);
348         }
349 }
350
351
352 void
353 checked_set (wxStaticText* widget, string value)
354 {
355         if (widget->GetLabel() != std_to_wx (value)) {
356                 widget->SetLabel (std_to_wx (value));
357         }
358 }
359
360
361 void
362 checked_set (wxStaticText* widget, wxString value)
363 {
364         if (widget->GetLabel() != value) {
365                 widget->SetLabel (value);
366         }
367 }
368
369
370 void
371 checked_set (wxCheckBox* widget, bool value)
372 {
373         if (widget->GetValue() != value) {
374                 widget->SetValue (value);
375         }
376 }
377
378
379 void
380 checked_set (wxRadioButton* widget, bool value)
381 {
382         if (widget->GetValue() != value) {
383                 widget->SetValue (value);
384         }
385 }
386
387
388 void
389 checked_set(LanguageTagWidget* widget, dcp::LanguageTag value)
390 {
391         if (widget->get() != value) {
392                 widget->set(value);
393         }
394 }
395
396
397 void
398 checked_set(LanguageTagWidget* widget, optional<dcp::LanguageTag> value)
399 {
400         if (widget->get() != value) {
401                 widget->set(value);
402         }
403 }
404
405
406 void
407 checked_set(RegionSubtagWidget* widget, optional<dcp::LanguageTag::RegionSubtag> value)
408 {
409         if (widget->get() != value) {
410                 widget->set(value);
411         }
412 }
413
414
415 void
416 dcpomatic_setup_i18n ()
417 {
418         int language = wxLANGUAGE_DEFAULT;
419
420         auto config_lang = Config::instance()->language ();
421         if (config_lang && !config_lang->empty ()) {
422                 auto const li = wxLocale::FindLanguageInfo (std_to_wx (config_lang.get ()));
423                 if (li) {
424                         language = li->Language;
425                 }
426         }
427
428         wxLocale* locale = nullptr;
429         if (wxLocale::IsAvailable (language)) {
430                 locale = new wxLocale (language, wxLOCALE_LOAD_DEFAULT);
431
432 #ifdef DCPOMATIC_WINDOWS
433                 locale->AddCatalogLookupPathPrefix (std_to_wx (mo_path().string()));
434 #endif
435
436 #ifdef DCPOMATIC_LINUX
437                 locale->AddCatalogLookupPathPrefix (LINUX_LOCALE_PREFIX);
438
439                 /* We have to include the wxWidgets .mo in our distribution,
440                    so we rename it to avoid clashes with any other installation
441                    of wxWidgets.
442                 */
443                 locale->AddCatalog (wxT ("dcpomatic2-wxstd"));
444
445                 /* Fedora 29 (at least) installs wxstd3.mo instead of wxstd.mo */
446                 locale->AddCatalog (wxT ("wxstd3"));
447 #endif
448
449                 locale->AddCatalog (wxT ("libdcpomatic2-wx"));
450                 locale->AddCatalog (wxT ("dcpomatic2"));
451
452                 if (!locale->IsOk()) {
453                         delete locale;
454                         locale = new wxLocale (wxLANGUAGE_ENGLISH);
455                 }
456         }
457
458         if (locale) {
459                 dcpomatic_setup_gettext_i18n (wx_to_std (locale->GetCanonicalName ()));
460         }
461 }
462
463
464 int
465 wx_get (wxSpinCtrl* w)
466 {
467         return w->GetValue ();
468 }
469
470
471 int
472 wx_get (wxChoice* w)
473 {
474         return w->GetSelection ();
475 }
476
477
478 double
479 wx_get (wxSpinCtrlDouble* w)
480 {
481         return w->GetValue ();
482 }
483
484
485 /** @param s String of the form Context|String
486  *  @return translation, or String if no translation is available.
487  */
488 wxString
489 context_translation (wxString s)
490 {
491         auto t = wxGetTranslation (s);
492         if (t == s) {
493                 /* No translation; strip the context */
494                 int c = t.Find (wxT ("|"));
495                 if (c != wxNOT_FOUND) {
496                         t = t.Mid (c + 1);
497                 }
498         }
499
500         return t;
501 }
502
503
504 wxString
505 time_to_timecode (DCPTime t, double fps)
506 {
507         auto w = t.seconds ();
508         int const h = (w / 3600);
509         w -= h * 3600;
510         int const m = (w / 60);
511         w -= m * 60;
512         int const s = floor (w);
513         w -= s;
514         int const f = lrint (w * fps);
515         return wxString::Format (wxT("%02d:%02d:%02d.%02d"), h, m, s, f);
516 }
517
518
519 void
520 setup_audio_channels_choice (wxChoice* choice, int minimum)
521 {
522         vector<pair<string, string>> items;
523         for (int i = minimum; i <= 16; i += 2) {
524                 if (i == 2) {
525                         items.push_back (make_pair(wx_to_std(_("2 - stereo")), locale_convert<string>(i)));
526                 } else if (i == 4) {
527                         items.push_back (make_pair(wx_to_std(_("4 - L/C/R/Lfe")), locale_convert<string>(i)));
528                 } else if (i == 6) {
529                         items.push_back (make_pair(wx_to_std(_("6 - 5.1")), locale_convert<string>(i)));
530                 } else if (i == 8) {
531                         items.push_back (make_pair(wx_to_std(_("8 - 5.1/HI/VI")), locale_convert<string>(i)));
532                 } else if (i == 12) {
533                         items.push_back (make_pair(wx_to_std(_("12 - 7.1/HI/VI")), locale_convert<string>(i)));
534                 } else {
535                         items.push_back (make_pair(locale_convert<string> (i), locale_convert<string>(i)));
536                 }
537         }
538
539         checked_set (choice, items);
540 }
541
542
543 wxSplashScreen *
544 maybe_show_splash ()
545 {
546         wxSplashScreen* splash = nullptr;
547         try {
548                 wxBitmap bitmap;
549                 if (bitmap.LoadFile(bitmap_path("splash.png"), wxBITMAP_TYPE_PNG)) {
550                         {
551                                 /* This wxMemoryDC must be destroyed before bitmap can be used elsewhere */
552                                 wxMemoryDC dc(bitmap);
553                                 auto const version = wxString::Format("%s (%s)", dcpomatic_version, dcpomatic_git_commit);
554                                 auto screen_size = dc.GetSize();
555                                 auto text_size = dc.GetTextExtent(version);
556                                 dc.DrawText(version, (screen_size.GetWidth() - text_size.GetWidth()) / 2, 236);
557                         }
558 #ifdef DCPOMATIC_WINDOWS
559                         /* Having wxSTAY_ON_TOP means error dialogues hide behind the splash screen on Windows, no matter what I try */
560                         splash = new wxSplashScreen (bitmap, wxSPLASH_CENTRE_ON_SCREEN | wxSPLASH_NO_TIMEOUT, 0, 0, -1, wxDefaultPosition, wxDefaultSize, wxBORDER_SIMPLE | wxFRAME_NO_TASKBAR);
561 #else
562                         splash = new wxSplashScreen (bitmap, wxSPLASH_CENTRE_ON_SCREEN | wxSPLASH_NO_TIMEOUT, 0, 0, -1);
563 #endif
564                         wxYield ();
565                 }
566         } catch (boost::filesystem::filesystem_error& e) {
567                 /* Maybe we couldn't find the splash image; never mind */
568         }
569
570         return splash;
571 }
572
573
574 double
575 calculate_mark_interval (double mark_interval)
576 {
577         if (mark_interval > 5) {
578                 mark_interval -= lrint (mark_interval) % 5;
579         }
580         if (mark_interval > 10) {
581                 mark_interval -= lrint (mark_interval) % 10;
582         }
583         if (mark_interval > 60) {
584                 mark_interval -= lrint (mark_interval) % 60;
585         }
586         if (mark_interval > 3600) {
587                 mark_interval -= lrint (mark_interval) % 3600;
588         }
589
590         if (mark_interval < 1) {
591                 mark_interval = 1;
592         }
593
594         return mark_interval;
595 }
596
597
598 /** @return false if the task was cancelled */
599 bool
600 display_progress (wxString title, wxString task)
601 {
602         auto jm = JobManager::instance ();
603
604         wxProgressDialog progress (title, task, 100, 0, wxPD_CAN_ABORT);
605
606         bool ok = true;
607
608         while (jm->work_to_do()) {
609                 dcpomatic_sleep_seconds (1);
610                 if (!progress.Pulse()) {
611                         /* user pressed cancel */
612                         for (auto i: jm->get()) {
613                                 i->cancel();
614                         }
615                         ok = false;
616                         break;
617                 }
618         }
619
620         return ok;
621 }
622
623
624 int
625 get_offsets (vector<Offset>& offsets)
626 {
627         offsets.push_back (Offset(_("UTC-11"),  -11,  0));
628         offsets.push_back (Offset(_("UTC-10"),  -10,  0));
629         offsets.push_back (Offset(_("UTC-9"),    -9,  0));
630         offsets.push_back (Offset(_("UTC-8"),    -8,  0));
631         offsets.push_back (Offset(_("UTC-7"),    -7,  0));
632         offsets.push_back (Offset(_("UTC-6"),    -6,  0));
633         offsets.push_back (Offset(_("UTC-5"),    -5,  0));
634         offsets.push_back (Offset(_("UTC-4:30"), -4, 30));
635         offsets.push_back (Offset(_("UTC-4"),    -4,  0));
636         offsets.push_back (Offset(_("UTC-3:30"), -3, 30));
637         offsets.push_back (Offset(_("UTC-3"),    -3,  0));
638         offsets.push_back (Offset(_("UTC-2"),    -2,  0));
639         offsets.push_back (Offset(_("UTC-1"),    -1,  0));
640         int utc = offsets.size();
641         offsets.push_back (Offset(_("UTC")  ,     0,  0));
642         offsets.push_back (Offset(_("UTC+1"),     1,  0));
643         offsets.push_back (Offset(_("UTC+2"),     2,  0));
644         offsets.push_back (Offset(_("UTC+3"),     3,  0));
645         offsets.push_back (Offset(_("UTC+4"),     4,  0));
646         offsets.push_back (Offset(_("UTC+5"),     5,  0));
647         offsets.push_back (Offset(_("UTC+5:30"),  5, 30));
648         offsets.push_back (Offset(_("UTC+6"),     6,  0));
649         offsets.push_back (Offset(_("UTC+7"),     7,  0));
650         offsets.push_back (Offset(_("UTC+8"),     8,  0));
651         offsets.push_back (Offset(_("UTC+9"),     9,  0));
652         offsets.push_back (Offset(_("UTC+9:30"),  9, 30));
653         offsets.push_back (Offset(_("UTC+10"),   10,  0));
654         offsets.push_back (Offset(_("UTC+11"),   11,  0));
655         offsets.push_back (Offset(_("UTC+12"),   12,  0));
656
657         return utc;
658 }
659
660
661 wxString
662 bitmap_path (string name)
663 {
664         boost::filesystem::path base;
665
666 #ifdef DCPOMATIC_DEBUG
667         /* Hack to allow Linux and OS X to find icons when running from the source tree */
668         char* path = getenv ("DCPOMATIC_GRAPHICS");
669         if (path) {
670                 base = path;
671         } else {
672                 base = resources_path();
673         }
674 #else
675         base = resources_path();
676 #endif
677
678         auto p = base / name;
679         return std_to_wx (p.string());
680 }
681
682
683 wxString
684 icon_path(string name)
685 {
686         return gui_is_dark() ? bitmap_path(String::compose("%1_white.png", name)) : bitmap_path(String::compose("%1_black.png", name));
687 }
688
689
690 wxSize
691 small_button_size (wxWindow* parent, wxString text)
692 {
693         wxClientDC dc (parent);
694         auto size = dc.GetTextExtent (text);
695         size.SetHeight (-1);
696         size.IncBy (32, 0);
697         return size;
698 }
699
700
701 bool
702 gui_is_dark ()
703 {
704 #if defined(DCPOMATIC_OSX) && wxCHECK_VERSION(3, 1, 0)
705         auto appearance = wxSystemSettings::GetAppearance();
706         return appearance.IsDark();
707 #else
708         return false;
709 #endif
710 }
711
712
713 #if wxCHECK_VERSION(3,1,0)
714 double
715 dpi_scale_factor (wxWindow* window)
716 {
717         return window->GetDPIScaleFactor();
718 }
719 #else
720 double
721 dpi_scale_factor (wxWindow*)
722 {
723         return 1;
724 }
725 #endif
726
727
728
729 int
730 search_ctrl_height ()
731 {
732 #ifdef __WXGTK3__
733         return 30;
734 #else
735         return -1;
736 #endif
737 }
738
739
740 void
741 report_config_load_failure(wxWindow* parent, Config::LoadFailure what)
742 {
743         switch (what) {
744         case Config::LoadFailure::CONFIG:
745                 message_dialog(parent, _("The existing configuration failed to load.  Default values will be used instead.  These may take a short time to create."));
746                 break;
747         case Config::LoadFailure::CINEMAS:
748                 message_dialog(
749                         parent,
750                         _(wxString::Format("The cinemas list for creating KDMs (cinemas.xml) failed to load.  Please check the numbered backup files in %s",
751                                            std_to_wx(Config::instance()->cinemas_file().parent_path().string())))
752                         );
753                 break;
754         case Config::LoadFailure::DKDM_RECIPIENTS:
755                 message_dialog(
756                         parent,
757                         _(wxString::Format("The recipients list for creating DKDMs (dkdm_recipients.xml) failed to load.  Please check the numbered backup files in %s",
758                                            std_to_wx(Config::instance()->dkdm_recipients_file().parent_path().string())))
759                         );
760                 break;
761         }
762 }
763