Restore time zone to Cinema and improve UI to use it (#2473).
[dcpomatic.git] / src / wx / wx_util.cc
index 7a41f66e7cfa226c1bccb139f7fb88b3dc141082..e85b74cc81468ad23c57e1bba28a1f513bc56d7a 100644 (file)
 #include "static_text.h"
 #include "wx_ptr.h"
 #include "wx_util.h"
+#include "wx_variant.h"
 #include "lib/config.h"
 #include "lib/cross.h"
 #include "lib/job.h"
 #include "lib/job_manager.h"
 #include "lib/util.h"
+#include "lib/variant.h"
 #include "lib/version.h"
 #include <dcp/locale_convert.h>
 #include <dcp/warnings.h>
 LIBDCP_DISABLE_WARNINGS
-#include <wx/spinctrl.h>
-#include <wx/splash.h>
-#include <wx/progdlg.h>
 #include <wx/filepicker.h>
+#include <wx/progdlg.h>
 #include <wx/sizer.h>
+#include <wx/spinctrl.h>
+#include <wx/splash.h>
 LIBDCP_ENABLE_WARNINGS
 #include <boost/thread.hpp>
 
@@ -161,7 +163,7 @@ add_label_to_sizer (wxGridBagSizer* s, wxStaticText* t, bool, wxGBPosition pos,
 void
 error_dialog (wxWindow* parent, wxString m, optional<wxString> e)
 {
-       auto d = make_wx<wxMessageDialog>(parent, m, _("DCP-o-matic"), wxOK | wxICON_ERROR);
+       auto d = make_wx<wxMessageDialog>(parent, m, variant::wx::dcpomatic(), wxOK | wxICON_ERROR);
        if (e) {
                wxString em = *e;
                em[0] = wxToupper (em[0]);
@@ -178,7 +180,7 @@ error_dialog (wxWindow* parent, wxString m, optional<wxString> e)
 void
 message_dialog (wxWindow* parent, wxString m)
 {
-       auto d = make_wx<wxMessageDialog>(parent, m, _("DCP-o-matic"), wxOK | wxICON_INFORMATION);
+       auto d = make_wx<wxMessageDialog>(parent, m, variant::wx::dcpomatic(), wxOK | wxICON_INFORMATION);
        d->ShowModal ();
 }
 
@@ -187,7 +189,7 @@ message_dialog (wxWindow* parent, wxString m)
 bool
 confirm_dialog (wxWindow* parent, wxString m)
 {
-       auto d = make_wx<wxMessageDialog>(parent, m, _("DCP-o-matic"), wxYES_NO | wxICON_QUESTION);
+       auto d = make_wx<wxMessageDialog>(parent, m, variant::wx::dcpomatic(), wxYES_NO | wxICON_QUESTION);
        return d->ShowModal() == wxID_YES;
 }
 
@@ -222,14 +224,14 @@ string_client_data (wxClientData* o)
 void
 checked_set (FilePickerCtrl* widget, boost::filesystem::path value)
 {
-       if (widget->GetPath() != std_to_wx (value.string())) {
+       if (widget->path() != value) {
                if (value.empty()) {
                        /* Hack to make wxWidgets clear the control when we are passed
                           an empty value.
                        */
                        value = " ";
                }
-               widget->SetPath (std_to_wx (value.string()));
+               widget->set_path(value);
        }
 }
 
@@ -409,6 +411,65 @@ checked_set(RegionSubtagWidget* widget, optional<dcp::LanguageTag::RegionSubtag>
 }
 
 
+#ifdef DCPOMATIC_OSX
+
+void
+dcpomatic_setup_i18n()
+{
+       wxLog::EnableLogging();
+
+       auto get_locale_value = [](CFLocaleKey key) {
+               CFLocaleRef cflocale = CFLocaleCopyCurrent();
+               auto value = (CFStringRef) CFLocaleGetValue(cflocale, key);
+               char buffer[64];
+               CFStringGetCString(value, buffer, sizeof(buffer), kCFStringEncodingUTF8);
+               CFRelease(cflocale);
+               return string(buffer);
+       };
+
+       auto translations = new wxTranslations();
+
+       auto config_lang = Config::instance()->language();
+       if (config_lang && !config_lang->empty()) {
+               translations->SetLanguage(std_to_wx(*config_lang));
+       } else {
+               /* We want to use the user's preferred language.  It seems that if we use the wxWidgets default we will get the
+                * language for the locale, which may not be what we want (e.g. for a machine in Germany, configured for DE locale,
+                * but with the preferred language set to English).
+                *
+                * Instead, the the language code from macOS then get the corresponding canonical language string with region,
+                * which wxTranslations::SetLanguage will accept.
+                */
+               auto const language_code = get_locale_value(kCFLocaleLanguageCode);
+               /* Ideally this would be wxUILocale (as wxLocale is deprecated) but we want to keep this building
+                * with the old wxWidgets we use for the older macOS builds.
+                */
+               auto const info = wxLocale::FindLanguageInfo(std_to_wx(language_code));
+               if (info) {
+#if wxCHECK_VERSION(3, 1, 6)
+                       translations->SetLanguage(info->GetCanonicalWithRegion());
+#else
+                       translations->SetLanguage(info->CanonicalName);
+#endif
+               }
+       }
+
+#ifdef DCPOMATIC_DEBUG
+       wxFileTranslationsLoader::AddCatalogLookupPathPrefix(wxT("build/src/wx/mo"));
+       wxFileTranslationsLoader::AddCatalogLookupPathPrefix(wxT("build/src/tools/mo"));
+#endif
+
+       translations->AddStdCatalog();
+       translations->AddCatalog(wxT("libdcpomatic2-wx"));
+       translations->AddCatalog(wxT("dcpomatic2"));
+
+       wxTranslations::Set(translations);
+
+       dcpomatic_setup_gettext_i18n(config_lang.get_value_or(""));
+}
+
+#else
+
 void
 dcpomatic_setup_i18n ()
 {
@@ -443,8 +504,9 @@ dcpomatic_setup_i18n ()
                locale->AddCatalog (wxT ("wxstd3"));
 #endif
 
-               locale->AddCatalog (wxT ("libdcpomatic2-wx"));
-               locale->AddCatalog (wxT ("dcpomatic2"));
+               locale->AddCatalog(wxT("wxstd"));
+               locale->AddCatalog(wxT("libdcpomatic2-wx"));
+               locale->AddCatalog(wxT("dcpomatic2"));
 
                if (!locale->IsOk()) {
                        delete locale;
@@ -457,6 +519,8 @@ dcpomatic_setup_i18n ()
        }
 }
 
+#endif
+
 
 int
 wx_get (wxSpinCtrl* w)
@@ -537,10 +601,14 @@ setup_audio_channels_choice (wxChoice* choice, int minimum)
 }
 
 
-wx_ptr<wxSplashScreen>
+wxSplashScreen*
 maybe_show_splash ()
 {
-       wx_ptr<wxSplashScreen> splash;
+       if (!variant::show_splash()) {
+               return nullptr;
+       }
+
+       wxSplashScreen* splash = nullptr;
 
        try {
                wxBitmap bitmap;
@@ -555,9 +623,9 @@ maybe_show_splash ()
                        }
 #ifdef DCPOMATIC_WINDOWS
                        /* Having wxSTAY_ON_TOP means error dialogues hide behind the splash screen on Windows, no matter what I try */
-                       splash.reset(bitmap, wxSPLASH_CENTRE_ON_SCREEN | wxSPLASH_NO_TIMEOUT, 0, nullptr, -1, wxDefaultPosition, wxDefaultSize, wxBORDER_SIMPLE | wxFRAME_NO_TASKBAR);
+                       splash = new wxSplashScreen(bitmap, wxSPLASH_CENTRE_ON_SCREEN | wxSPLASH_NO_TIMEOUT, 0, nullptr, -1, wxDefaultPosition, wxDefaultSize, wxBORDER_SIMPLE | wxFRAME_NO_TASKBAR);
 #else
-                       splash.reset(bitmap, wxSPLASH_CENTRE_ON_SCREEN | wxSPLASH_NO_TIMEOUT, 0, nullptr, -1);
+                       splash = new wxSplashScreen(bitmap, wxSPLASH_CENTRE_ON_SCREEN | wxSPLASH_NO_TIMEOUT, 0, nullptr, -1);
 #endif
                        wxYield ();
                }
@@ -622,35 +690,35 @@ display_progress (wxString title, wxString task)
 int
 get_offsets (vector<Offset>& offsets)
 {
-       offsets.push_back (Offset(_("UTC-11"),  -11,  0));
-       offsets.push_back (Offset(_("UTC-10"),  -10,  0));
-       offsets.push_back (Offset(_("UTC-9"),    -9,  0));
-       offsets.push_back (Offset(_("UTC-8"),    -8,  0));
-       offsets.push_back (Offset(_("UTC-7"),    -7,  0));
-       offsets.push_back (Offset(_("UTC-6"),    -6,  0));
-       offsets.push_back (Offset(_("UTC-5"),    -5,  0));
-       offsets.push_back (Offset(_("UTC-4:30"), -4, 30));
-       offsets.push_back (Offset(_("UTC-4"),    -4,  0));
-       offsets.push_back (Offset(_("UTC-3:30"), -3, 30));
-       offsets.push_back (Offset(_("UTC-3"),    -3,  0));
-       offsets.push_back (Offset(_("UTC-2"),    -2,  0));
-       offsets.push_back (Offset(_("UTC-1"),    -1,  0));
+       offsets.push_back({_("UTC-11"),  dcp::UTCOffset(-11,   0)});
+       offsets.push_back({_("UTC-10"),  dcp::UTCOffset(-10,   0)});
+       offsets.push_back({_("UTC-9"),   dcp::UTCOffset( -9,   0)});
+       offsets.push_back({_("UTC-8"),   dcp::UTCOffset( -8,   0)});
+       offsets.push_back({_("UTC-7"),   dcp::UTCOffset( -7,   0)});
+       offsets.push_back({_("UTC-6"),   dcp::UTCOffset( -6,   0)});
+       offsets.push_back({_("UTC-5"),   dcp::UTCOffset( -5,   0)});
+       offsets.push_back({_("UTC-4:30"),dcp::UTCOffset( -4, -30)});
+       offsets.push_back({_("UTC-4"),   dcp::UTCOffset( -4,   0)});
+       offsets.push_back({_("UTC-3:30"),dcp::UTCOffset( -3, -30)});
+       offsets.push_back({_("UTC-3"),   dcp::UTCOffset( -3,   0)});
+       offsets.push_back({_("UTC-2"),   dcp::UTCOffset( -2,   0)});
+       offsets.push_back({_("UTC-1"),   dcp::UTCOffset( -1,   0)});
        int utc = offsets.size();
-       offsets.push_back (Offset(_("UTC")  ,     0,  0));
-       offsets.push_back (Offset(_("UTC+1"),     1,  0));
-       offsets.push_back (Offset(_("UTC+2"),     2,  0));
-       offsets.push_back (Offset(_("UTC+3"),     3,  0));
-       offsets.push_back (Offset(_("UTC+4"),     4,  0));
-       offsets.push_back (Offset(_("UTC+5"),     5,  0));
-       offsets.push_back (Offset(_("UTC+5:30"),  5, 30));
-       offsets.push_back (Offset(_("UTC+6"),     6,  0));
-       offsets.push_back (Offset(_("UTC+7"),     7,  0));
-       offsets.push_back (Offset(_("UTC+8"),     8,  0));
-       offsets.push_back (Offset(_("UTC+9"),     9,  0));
-       offsets.push_back (Offset(_("UTC+9:30"),  9, 30));
-       offsets.push_back (Offset(_("UTC+10"),   10,  0));
-       offsets.push_back (Offset(_("UTC+11"),   11,  0));
-       offsets.push_back (Offset(_("UTC+12"),   12,  0));
+       offsets.push_back({_("UTC")  ,   dcp::UTCOffset(  0,   0)});
+       offsets.push_back({_("UTC+1"),   dcp::UTCOffset(  1,   0)});
+       offsets.push_back({_("UTC+2"),   dcp::UTCOffset(  2,   0)});
+       offsets.push_back({_("UTC+3"),   dcp::UTCOffset(  3,   0)});
+       offsets.push_back({_("UTC+4"),   dcp::UTCOffset(  4,   0)});
+       offsets.push_back({_("UTC+5"),   dcp::UTCOffset(  5,   0)});
+       offsets.push_back({_("UTC+5:30"),dcp::UTCOffset(  5,  30)});
+       offsets.push_back({_("UTC+6"),   dcp::UTCOffset(  6,   0)});
+       offsets.push_back({_("UTC+7"),   dcp::UTCOffset(  7,   0)});
+       offsets.push_back({_("UTC+8"),   dcp::UTCOffset(  8,   0)});
+       offsets.push_back({_("UTC+9"),   dcp::UTCOffset(  9,   0)});
+       offsets.push_back({_("UTC+9:30"),dcp::UTCOffset(  9,  30)});
+       offsets.push_back({_("UTC+10"),  dcp::UTCOffset( 10,   0)});
+       offsets.push_back({_("UTC+11"),  dcp::UTCOffset( 11,   0)});
+       offsets.push_back({_("UTC+12"),  dcp::UTCOffset( 12,   0)});
 
        return utc;
 }
@@ -669,6 +737,10 @@ bitmap_path (string name)
        } else {
                base = resources_path();
        }
+
+       if (!boost::filesystem::exists(base / name)) {
+               base = path / boost::filesystem::path("osx/preferences");
+       }
 #else
        base = resources_path();
 #endif