2 Copyright (C) 2012-2021 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
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.
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.
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/>.
22 /** @file src/wx/wx_util.cc
23 * @brief Some utility functions and classes.
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"
34 #include "wx_variant.h"
35 #include "lib/config.h"
36 #include "lib/cross.h"
38 #include "lib/job_manager.h"
40 #include "lib/version.h"
41 #include <dcp/locale_convert.h>
42 #include <dcp/warnings.h>
43 LIBDCP_DISABLE_WARNINGS
44 #include <wx/filepicker.h>
45 #include <wx/progdlg.h>
47 #include <wx/spinctrl.h>
48 #include <wx/splash.h>
49 LIBDCP_ENABLE_WARNINGS
50 #include <boost/thread.hpp>
56 using std::shared_ptr;
57 using boost::optional;
58 using dcp::locale_convert;
59 using namespace dcpomatic;
64 create_label (wxWindow* p, wxString t, bool left)
66 create_label (wxWindow* p, wxString t, bool)
74 return new StaticText (p, t);
81 setup_osx_flags (wxSizer* s, bool left, int& flags)
84 auto box = dynamic_cast<wxBoxSizer*>(s);
85 if (!box || box->GetOrientation() != wxHORIZONTAL) {
86 flags |= wxALIGN_RIGHT;
93 /** Add a wxStaticText to a wxSizer.
94 * @param s Sizer to add to.
95 * @param p Parent window for the wxStaticText.
96 * @param t Text for the wxStaticText.
97 * @param left true if this label is a `left label'; ie the sort
98 * of label which should be right-aligned on OS X.
99 * @param prop Proportion to pass when calling Add() on the wxSizer.
102 add_label_to_sizer (wxSizer* s, wxWindow* p, wxString t, bool left, int prop, int flags)
105 setup_osx_flags (s, left, flags);
107 auto m = create_label (p, t, left);
108 s->Add (m, prop, flags, DCPOMATIC_SIZER_GAP);
115 add_label_to_sizer (wxSizer* s, wxStaticText* t, bool left, int prop, int flags)
117 add_label_to_sizer (wxSizer* s, wxStaticText* t, bool, int prop, int flags)
121 setup_osx_flags (s, left, flags);
123 s->Add (t, prop, flags, DCPOMATIC_SIZER_GAP);
129 add_label_to_sizer(wxGridBagSizer* s, wxWindow* p, wxString t, bool left, wxGBPosition pos, wxGBSpan span, bool indent)
131 int flags = wxALIGN_CENTER_VERTICAL | wxLEFT;
133 setup_osx_flags (s, left, flags);
135 auto m = create_label (p, t, left);
136 s->Add(m, pos, span, flags, indent ? DCPOMATIC_SIZER_X_GAP : 0);
143 add_label_to_sizer (wxGridBagSizer* s, wxStaticText* t, bool left, wxGBPosition pos, wxGBSpan span)
145 add_label_to_sizer (wxGridBagSizer* s, wxStaticText* t, bool, wxGBPosition pos, wxGBSpan span)
148 int flags = wxALIGN_CENTER_VERTICAL | wxLEFT | wxRIGHT;
150 setup_osx_flags (s, left, flags);
152 s->Add (t, pos, span, flags);
157 /** Pop up an error dialogue box.
158 * @param parent Parent.
160 * @param e Extended message.
163 error_dialog (wxWindow* parent, wxString m, optional<wxString> e)
165 auto d = make_wx<wxMessageDialog>(parent, m, variant::wx::dcpomatic(), wxOK | wxICON_ERROR);
168 em[0] = wxToupper (em[0]);
169 d->SetExtendedMessage (em);
175 /** Pop up an error dialogue box.
176 * @param parent Parent.
180 message_dialog (wxWindow* parent, wxString m)
182 auto d = make_wx<wxMessageDialog>(parent, m, variant::wx::dcpomatic(), wxOK | wxICON_INFORMATION);
187 /** @return true if the user answered "yes" */
189 confirm_dialog (wxWindow* parent, wxString m)
191 auto d = make_wx<wxMessageDialog>(parent, m, variant::wx::dcpomatic(), wxYES_NO | wxICON_QUESTION);
192 return d->ShowModal() == wxID_YES;
196 /** @param s wxWidgets string.
197 * @return Corresponding STL string.
200 wx_to_std (wxString s)
202 return string (s.ToUTF8());
206 /** @param s STL string.
207 * @return Corresponding wxWidgets string.
212 return wxString (s.c_str(), wxConvUTF8);
217 string_client_data (wxClientData* o)
219 return wx_to_std (dynamic_cast<wxStringClientData*>(o)->GetData());
224 checked_set (FilePickerCtrl* widget, boost::filesystem::path value)
226 if (widget->path() != value) {
228 /* Hack to make wxWidgets clear the control when we are passed
233 widget->set_path(value);
239 checked_set (wxDirPickerCtrl* widget, boost::filesystem::path value)
241 if (widget->GetPath() != std_to_wx (value.string())) {
243 /* Hack to make wxWidgets clear the control when we are passed
248 widget->SetPath (std_to_wx (value.string()));
254 checked_set (wxSpinCtrl* widget, int value)
256 if (widget->GetValue() != value) {
257 widget->SetValue (value);
263 checked_set (wxSpinCtrlDouble* widget, double value)
265 /* XXX: completely arbitrary epsilon */
266 if (fabs (widget->GetValue() - value) > 1e-16) {
267 widget->SetValue (value);
273 checked_set (wxChoice* widget, int value)
275 if (widget->GetSelection() != value) {
276 widget->SetSelection (value);
282 checked_set (wxChoice* widget, string value)
284 wxClientData* o = nullptr;
285 if (widget->GetSelection() != -1) {
286 o = widget->GetClientObject (widget->GetSelection ());
289 if (!o || string_client_data(o) != value) {
290 for (unsigned int i = 0; i < widget->GetCount(); ++i) {
291 if (string_client_data (widget->GetClientObject (i)) == value) {
292 widget->SetSelection (i);
300 checked_set (wxChoice* widget, vector<pair<string, string>> items)
302 vector<pair<string, string>> current;
303 for (unsigned int i = 0; i < widget->GetCount(); ++i) {
306 wx_to_std(widget->GetString(i)),
307 widget->GetClientData() ? string_client_data(widget->GetClientObject(i)) : ""
312 if (current == items) {
317 for (auto i: items) {
318 widget->Append (std_to_wx(i.first), new wxStringClientData(std_to_wx(i.second)));
324 checked_set (wxTextCtrl* widget, string value)
326 if (widget->GetValue() != std_to_wx (value)) {
327 widget->ChangeValue (std_to_wx (value));
333 checked_set (PasswordEntry* entry, string value)
335 if (entry->get() != value) {
342 checked_set (wxTextCtrl* widget, wxString value)
344 if (widget->GetValue() != value) {
345 widget->ChangeValue (value);
351 checked_set (wxStaticText* widget, string value)
353 if (widget->GetLabel() != std_to_wx (value)) {
354 widget->SetLabel (std_to_wx (value));
360 checked_set (wxStaticText* widget, wxString value)
362 if (widget->GetLabel() != value) {
363 widget->SetLabel (value);
369 checked_set (wxCheckBox* widget, bool value)
371 if (widget->GetValue() != value) {
372 widget->SetValue (value);
378 checked_set (wxRadioButton* widget, bool value)
380 if (widget->GetValue() != value) {
381 widget->SetValue (value);
387 checked_set(LanguageTagWidget* widget, dcp::LanguageTag value)
389 if (widget->get() != value) {
396 checked_set(LanguageTagWidget* widget, optional<dcp::LanguageTag> value)
398 if (widget->get() != value) {
405 checked_set(RegionSubtagWidget* widget, optional<dcp::LanguageTag::RegionSubtag> value)
407 if (widget->get() != value) {
416 dcpomatic_setup_i18n()
418 wxLog::EnableLogging();
420 auto get_locale_value = [](CFLocaleKey key) {
421 CFLocaleRef cflocale = CFLocaleCopyCurrent();
422 auto value = (CFStringRef) CFLocaleGetValue(cflocale, key);
424 CFStringGetCString(value, buffer, sizeof(buffer), kCFStringEncodingUTF8);
426 return string(buffer);
429 auto translations = new wxTranslations();
431 auto config_lang = Config::instance()->language();
432 if (config_lang && !config_lang->empty()) {
433 translations->SetLanguage(std_to_wx(*config_lang));
435 /* We want to use the user's preferred language. It seems that if we use the wxWidgets default we will get the
436 * language for the locale, which may not be what we want (e.g. for a machine in Germany, configured for DE locale,
437 * but with the preferred language set to English).
439 * Instead, the the language code from macOS then get the corresponding canonical language string with region,
440 * which wxTranslations::SetLanguage will accept.
442 auto const language_code = get_locale_value(kCFLocaleLanguageCode);
443 /* Ideally this would be wxUILocale (as wxLocale is deprecated) but we want to keep this building
444 * with the old wxWidgets we use for the older macOS builds.
446 auto const info = wxLocale::FindLanguageInfo(std_to_wx(language_code));
448 #if wxCHECK_VERSION(3, 1, 6)
449 translations->SetLanguage(info->GetCanonicalWithRegion());
451 translations->SetLanguage(info->CanonicalName);
456 #ifdef DCPOMATIC_DEBUG
457 wxFileTranslationsLoader::AddCatalogLookupPathPrefix(wxT("build/src/wx/mo"));
458 wxFileTranslationsLoader::AddCatalogLookupPathPrefix(wxT("build/src/tools/mo"));
461 translations->AddStdCatalog();
462 translations->AddCatalog(wxT("libdcpomatic2-wx"));
463 translations->AddCatalog(wxT("dcpomatic2"));
465 wxTranslations::Set(translations);
467 dcpomatic_setup_gettext_i18n(config_lang.get_value_or(""));
473 dcpomatic_setup_i18n ()
475 int language = wxLANGUAGE_DEFAULT;
477 auto config_lang = Config::instance()->language ();
478 if (config_lang && !config_lang->empty ()) {
479 auto const li = wxLocale::FindLanguageInfo (std_to_wx (config_lang.get ()));
481 language = li->Language;
485 wxLocale* locale = nullptr;
486 if (wxLocale::IsAvailable (language)) {
487 locale = new wxLocale (language, wxLOCALE_LOAD_DEFAULT);
489 #ifdef DCPOMATIC_WINDOWS
490 locale->AddCatalogLookupPathPrefix (std_to_wx (mo_path().string()));
493 #ifdef DCPOMATIC_LINUX
494 locale->AddCatalogLookupPathPrefix (LINUX_LOCALE_PREFIX);
496 /* We have to include the wxWidgets .mo in our distribution,
497 so we rename it to avoid clashes with any other installation
500 locale->AddCatalog (wxT ("dcpomatic2-wxstd"));
502 /* Fedora 29 (at least) installs wxstd3.mo instead of wxstd.mo */
503 locale->AddCatalog (wxT ("wxstd3"));
506 locale->AddCatalog(wxT("wxstd"));
507 locale->AddCatalog(wxT("libdcpomatic2-wx"));
508 locale->AddCatalog(wxT("dcpomatic2"));
510 if (!locale->IsOk()) {
512 locale = new wxLocale (wxLANGUAGE_ENGLISH);
517 dcpomatic_setup_gettext_i18n (wx_to_std (locale->GetCanonicalName ()));
525 wx_get (wxSpinCtrl* w)
527 return w->GetValue ();
534 return w->GetSelection ();
539 wx_get (wxSpinCtrlDouble* w)
541 return w->GetValue ();
545 /** @param s String of the form Context|String
546 * @return translation, or String if no translation is available.
549 context_translation (wxString s)
551 auto t = wxGetTranslation (s);
553 /* No translation; strip the context */
554 int c = t.Find (wxT ("|"));
555 if (c != wxNOT_FOUND) {
565 time_to_timecode (DCPTime t, double fps)
567 auto w = t.seconds ();
568 int const h = (w / 3600);
570 int const m = (w / 60);
572 int const s = floor (w);
574 int const f = lrint (w * fps);
575 return wxString::Format (wxT("%02d:%02d:%02d.%02d"), h, m, s, f);
580 setup_audio_channels_choice (wxChoice* choice, int minimum)
582 vector<pair<string, string>> items;
583 for (int i = minimum; i <= 16; i += 2) {
585 items.push_back (make_pair(wx_to_std(_("2 - stereo")), locale_convert<string>(i)));
587 items.push_back (make_pair(wx_to_std(_("4 - L/C/R/Lfe")), locale_convert<string>(i)));
589 items.push_back (make_pair(wx_to_std(_("6 - 5.1")), locale_convert<string>(i)));
591 items.push_back (make_pair(wx_to_std(_("8 - 5.1/HI/VI")), locale_convert<string>(i)));
592 } else if (i == 12) {
593 items.push_back (make_pair(wx_to_std(_("12 - 7.1/HI/VI")), locale_convert<string>(i)));
595 items.push_back (make_pair(locale_convert<string> (i), locale_convert<string>(i)));
599 checked_set (choice, items);
606 wxSplashScreen* splash = nullptr;
610 if (bitmap.LoadFile(bitmap_path("splash.png"), wxBITMAP_TYPE_PNG)) {
612 /* This wxMemoryDC must be destroyed before bitmap can be used elsewhere */
613 wxMemoryDC dc(bitmap);
614 auto const version = wxString::Format("%s (%s)", dcpomatic_version, dcpomatic_git_commit);
615 auto screen_size = dc.GetSize();
616 auto text_size = dc.GetTextExtent(version);
617 dc.DrawText(version, (screen_size.GetWidth() - text_size.GetWidth()) / 2, 236);
619 #ifdef DCPOMATIC_WINDOWS
620 /* Having wxSTAY_ON_TOP means error dialogues hide behind the splash screen on Windows, no matter what I try */
621 splash = new wxSplashScreen(bitmap, wxSPLASH_CENTRE_ON_SCREEN | wxSPLASH_NO_TIMEOUT, 0, nullptr, -1, wxDefaultPosition, wxDefaultSize, wxBORDER_SIMPLE | wxFRAME_NO_TASKBAR);
623 splash = new wxSplashScreen(bitmap, wxSPLASH_CENTRE_ON_SCREEN | wxSPLASH_NO_TIMEOUT, 0, nullptr, -1);
627 } catch (boost::filesystem::filesystem_error& e) {
628 /* Maybe we couldn't find the splash image; never mind */
636 calculate_mark_interval (double mark_interval)
638 if (mark_interval > 5) {
639 mark_interval -= lrint (mark_interval) % 5;
641 if (mark_interval > 10) {
642 mark_interval -= lrint (mark_interval) % 10;
644 if (mark_interval > 60) {
645 mark_interval -= lrint (mark_interval) % 60;
647 if (mark_interval > 3600) {
648 mark_interval -= lrint (mark_interval) % 3600;
651 if (mark_interval < 1) {
655 return mark_interval;
659 /** @return false if the task was cancelled */
661 display_progress (wxString title, wxString task)
663 auto jm = JobManager::instance ();
665 wxProgressDialog progress (title, task, 100, 0, wxPD_CAN_ABORT);
669 while (jm->work_to_do()) {
670 dcpomatic_sleep_seconds (1);
671 if (!progress.Pulse()) {
672 /* user pressed cancel */
673 for (auto i: jm->get()) {
686 get_offsets (vector<Offset>& offsets)
688 offsets.push_back (Offset(_("UTC-11"), -11, 0));
689 offsets.push_back (Offset(_("UTC-10"), -10, 0));
690 offsets.push_back (Offset(_("UTC-9"), -9, 0));
691 offsets.push_back (Offset(_("UTC-8"), -8, 0));
692 offsets.push_back (Offset(_("UTC-7"), -7, 0));
693 offsets.push_back (Offset(_("UTC-6"), -6, 0));
694 offsets.push_back (Offset(_("UTC-5"), -5, 0));
695 offsets.push_back (Offset(_("UTC-4:30"), -4, 30));
696 offsets.push_back (Offset(_("UTC-4"), -4, 0));
697 offsets.push_back (Offset(_("UTC-3:30"), -3, 30));
698 offsets.push_back (Offset(_("UTC-3"), -3, 0));
699 offsets.push_back (Offset(_("UTC-2"), -2, 0));
700 offsets.push_back (Offset(_("UTC-1"), -1, 0));
701 int utc = offsets.size();
702 offsets.push_back (Offset(_("UTC") , 0, 0));
703 offsets.push_back (Offset(_("UTC+1"), 1, 0));
704 offsets.push_back (Offset(_("UTC+2"), 2, 0));
705 offsets.push_back (Offset(_("UTC+3"), 3, 0));
706 offsets.push_back (Offset(_("UTC+4"), 4, 0));
707 offsets.push_back (Offset(_("UTC+5"), 5, 0));
708 offsets.push_back (Offset(_("UTC+5:30"), 5, 30));
709 offsets.push_back (Offset(_("UTC+6"), 6, 0));
710 offsets.push_back (Offset(_("UTC+7"), 7, 0));
711 offsets.push_back (Offset(_("UTC+8"), 8, 0));
712 offsets.push_back (Offset(_("UTC+9"), 9, 0));
713 offsets.push_back (Offset(_("UTC+9:30"), 9, 30));
714 offsets.push_back (Offset(_("UTC+10"), 10, 0));
715 offsets.push_back (Offset(_("UTC+11"), 11, 0));
716 offsets.push_back (Offset(_("UTC+12"), 12, 0));
723 bitmap_path (string name)
725 boost::filesystem::path base;
727 #ifdef DCPOMATIC_DEBUG
728 /* Hack to allow Linux and OS X to find icons when running from the source tree */
729 char* path = getenv ("DCPOMATIC_GRAPHICS");
733 base = resources_path();
736 if (!boost::filesystem::exists(base / name)) {
737 base = path / boost::filesystem::path("osx/preferences");
740 base = resources_path();
743 auto p = base / name;
744 return std_to_wx (p.string());
749 icon_path(string name)
751 return gui_is_dark() ? bitmap_path(String::compose("%1_white.png", name)) : bitmap_path(String::compose("%1_black.png", name));
756 small_button_size (wxWindow* parent, wxString text)
758 wxClientDC dc (parent);
759 auto size = dc.GetTextExtent (text);
769 #if defined(DCPOMATIC_OSX) && wxCHECK_VERSION(3, 1, 0)
770 auto appearance = wxSystemSettings::GetAppearance();
771 return appearance.IsDark();
778 #if wxCHECK_VERSION(3,1,0)
780 dpi_scale_factor (wxWindow* window)
782 return window->GetDPIScaleFactor();
786 dpi_scale_factor (wxWindow*)
795 search_ctrl_height ()
806 report_config_load_failure(wxWindow* parent, Config::LoadFailure what)
809 case Config::LoadFailure::CONFIG:
810 message_dialog(parent, _("The existing configuration failed to load. Default values will be used instead. These may take a short time to create."));
812 case Config::LoadFailure::CINEMAS:
815 _(wxString::Format("The cinemas list for creating KDMs (cinemas.xml) failed to load. Please check the numbered backup files in %s",
816 std_to_wx(Config::instance()->cinemas_file().parent_path().string())))
819 case Config::LoadFailure::DKDM_RECIPIENTS:
822 _(wxString::Format("The recipients list for creating DKDMs (dkdm_recipients.xml) failed to load. Please check the numbered backup files in %s",
823 std_to_wx(Config::instance()->dkdm_recipients_file().parent_path().string())))