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/variant.h"
41 #include "lib/version.h"
42 #include <dcp/locale_convert.h>
43 #include <dcp/warnings.h>
44 LIBDCP_DISABLE_WARNINGS
45 #include <wx/filepicker.h>
46 #include <wx/progdlg.h>
48 #include <wx/spinctrl.h>
49 #include <wx/splash.h>
50 LIBDCP_ENABLE_WARNINGS
51 #include <boost/thread.hpp>
57 using std::shared_ptr;
58 using boost::optional;
59 using dcp::locale_convert;
60 using namespace dcpomatic;
65 create_label (wxWindow* p, wxString t, bool left)
67 create_label (wxWindow* p, wxString t, bool)
75 return new StaticText (p, t);
82 setup_osx_flags (wxSizer* s, bool left, int& flags)
85 auto box = dynamic_cast<wxBoxSizer*>(s);
86 if (!box || box->GetOrientation() != wxHORIZONTAL) {
87 flags |= wxALIGN_RIGHT;
94 /** Add a wxStaticText to a wxSizer.
95 * @param s Sizer to add to.
96 * @param p Parent window for the wxStaticText.
97 * @param t Text for the wxStaticText.
98 * @param left true if this label is a `left label'; ie the sort
99 * of label which should be right-aligned on OS X.
100 * @param prop Proportion to pass when calling Add() on the wxSizer.
103 add_label_to_sizer (wxSizer* s, wxWindow* p, wxString t, bool left, int prop, int flags)
106 setup_osx_flags (s, left, flags);
108 auto m = create_label (p, t, left);
109 s->Add (m, prop, flags, DCPOMATIC_SIZER_GAP);
116 add_label_to_sizer (wxSizer* s, wxStaticText* t, bool left, int prop, int flags)
118 add_label_to_sizer (wxSizer* s, wxStaticText* t, bool, int prop, int flags)
122 setup_osx_flags (s, left, flags);
124 s->Add (t, prop, flags, DCPOMATIC_SIZER_GAP);
130 add_label_to_sizer(wxGridBagSizer* s, wxWindow* p, wxString t, bool left, wxGBPosition pos, wxGBSpan span, bool indent)
132 int flags = wxALIGN_CENTER_VERTICAL | wxLEFT;
134 setup_osx_flags (s, left, flags);
136 auto m = create_label (p, t, left);
137 s->Add(m, pos, span, flags, indent ? DCPOMATIC_SIZER_X_GAP : 0);
144 add_label_to_sizer (wxGridBagSizer* s, wxStaticText* t, bool left, wxGBPosition pos, wxGBSpan span)
146 add_label_to_sizer (wxGridBagSizer* s, wxStaticText* t, bool, wxGBPosition pos, wxGBSpan span)
149 int flags = wxALIGN_CENTER_VERTICAL | wxLEFT | wxRIGHT;
151 setup_osx_flags (s, left, flags);
153 s->Add (t, pos, span, flags);
158 /** Pop up an error dialogue box.
159 * @param parent Parent.
161 * @param e Extended message.
164 error_dialog (wxWindow* parent, wxString m, optional<wxString> e)
166 auto d = make_wx<wxMessageDialog>(parent, m, variant::wx::dcpomatic(), wxOK | wxICON_ERROR);
169 em[0] = wxToupper (em[0]);
170 d->SetExtendedMessage (em);
176 /** Pop up an error dialogue box.
177 * @param parent Parent.
181 message_dialog (wxWindow* parent, wxString m)
183 auto d = make_wx<wxMessageDialog>(parent, m, variant::wx::dcpomatic(), wxOK | wxICON_INFORMATION);
188 /** @return true if the user answered "yes" */
190 confirm_dialog (wxWindow* parent, wxString m)
192 auto d = make_wx<wxMessageDialog>(parent, m, variant::wx::dcpomatic(), wxYES_NO | wxICON_QUESTION);
193 return d->ShowModal() == wxID_YES;
197 /** @param s wxWidgets string.
198 * @return Corresponding STL string.
201 wx_to_std (wxString s)
203 return string (s.ToUTF8());
207 /** @param s STL string.
208 * @return Corresponding wxWidgets string.
213 return wxString (s.c_str(), wxConvUTF8);
218 string_client_data (wxClientData* o)
220 return wx_to_std (dynamic_cast<wxStringClientData*>(o)->GetData());
225 checked_set (FilePickerCtrl* widget, boost::filesystem::path value)
227 if (widget->path() != value) {
229 /* Hack to make wxWidgets clear the control when we are passed
234 widget->set_path(value);
240 checked_set (wxDirPickerCtrl* widget, boost::filesystem::path value)
242 if (widget->GetPath() != std_to_wx (value.string())) {
244 /* Hack to make wxWidgets clear the control when we are passed
249 widget->SetPath (std_to_wx (value.string()));
255 checked_set (wxSpinCtrl* widget, int value)
257 if (widget->GetValue() != value) {
258 widget->SetValue (value);
264 checked_set (wxSpinCtrlDouble* widget, double value)
266 /* XXX: completely arbitrary epsilon */
267 if (fabs (widget->GetValue() - value) > 1e-16) {
268 widget->SetValue (value);
274 checked_set (wxChoice* widget, int value)
276 if (widget->GetSelection() != value) {
277 widget->SetSelection (value);
283 checked_set (wxChoice* widget, string value)
285 wxClientData* o = nullptr;
286 if (widget->GetSelection() != -1) {
287 o = widget->GetClientObject (widget->GetSelection ());
290 if (!o || string_client_data(o) != value) {
291 for (unsigned int i = 0; i < widget->GetCount(); ++i) {
292 if (string_client_data (widget->GetClientObject (i)) == value) {
293 widget->SetSelection (i);
301 checked_set (wxChoice* widget, vector<pair<string, string>> items)
303 vector<pair<string, string>> current;
304 for (unsigned int i = 0; i < widget->GetCount(); ++i) {
307 wx_to_std(widget->GetString(i)),
308 widget->GetClientData() ? string_client_data(widget->GetClientObject(i)) : ""
313 if (current == items) {
318 for (auto i: items) {
319 widget->Append (std_to_wx(i.first), new wxStringClientData(std_to_wx(i.second)));
325 checked_set (wxTextCtrl* widget, string value)
327 if (widget->GetValue() != std_to_wx (value)) {
328 widget->ChangeValue (std_to_wx (value));
334 checked_set (PasswordEntry* entry, string value)
336 if (entry->get() != value) {
343 checked_set (wxTextCtrl* widget, wxString value)
345 if (widget->GetValue() != value) {
346 widget->ChangeValue (value);
352 checked_set (wxStaticText* widget, string value)
354 if (widget->GetLabel() != std_to_wx (value)) {
355 widget->SetLabel (std_to_wx (value));
361 checked_set (wxStaticText* widget, wxString value)
363 if (widget->GetLabel() != value) {
364 widget->SetLabel (value);
370 checked_set (wxCheckBox* widget, bool value)
372 if (widget->GetValue() != value) {
373 widget->SetValue (value);
379 checked_set (wxRadioButton* widget, bool value)
381 if (widget->GetValue() != value) {
382 widget->SetValue (value);
388 checked_set(LanguageTagWidget* widget, dcp::LanguageTag value)
390 if (widget->get() != value) {
397 checked_set(LanguageTagWidget* widget, optional<dcp::LanguageTag> value)
399 if (widget->get() != value) {
406 checked_set(RegionSubtagWidget* widget, optional<dcp::LanguageTag::RegionSubtag> value)
408 if (widget->get() != value) {
417 dcpomatic_setup_i18n()
419 wxLog::EnableLogging();
421 auto get_locale_value = [](CFLocaleKey key) {
422 CFLocaleRef cflocale = CFLocaleCopyCurrent();
423 auto value = (CFStringRef) CFLocaleGetValue(cflocale, key);
425 CFStringGetCString(value, buffer, sizeof(buffer), kCFStringEncodingUTF8);
427 return string(buffer);
430 auto translations = new wxTranslations();
432 auto config_lang = Config::instance()->language();
433 if (config_lang && !config_lang->empty()) {
434 translations->SetLanguage(std_to_wx(*config_lang));
436 /* We want to use the user's preferred language. It seems that if we use the wxWidgets default we will get the
437 * language for the locale, which may not be what we want (e.g. for a machine in Germany, configured for DE locale,
438 * but with the preferred language set to English).
440 * Instead, the the language code from macOS then get the corresponding canonical language string with region,
441 * which wxTranslations::SetLanguage will accept.
443 auto const language_code = get_locale_value(kCFLocaleLanguageCode);
444 /* Ideally this would be wxUILocale (as wxLocale is deprecated) but we want to keep this building
445 * with the old wxWidgets we use for the older macOS builds.
447 auto const info = wxLocale::FindLanguageInfo(std_to_wx(language_code));
449 #if wxCHECK_VERSION(3, 1, 6)
450 translations->SetLanguage(info->GetCanonicalWithRegion());
452 translations->SetLanguage(info->CanonicalName);
457 #ifdef DCPOMATIC_DEBUG
458 wxFileTranslationsLoader::AddCatalogLookupPathPrefix(wxT("build/src/wx/mo"));
459 wxFileTranslationsLoader::AddCatalogLookupPathPrefix(wxT("build/src/tools/mo"));
462 translations->AddStdCatalog();
463 translations->AddCatalog(wxT("libdcpomatic2-wx"));
464 translations->AddCatalog(wxT("dcpomatic2"));
466 wxTranslations::Set(translations);
468 dcpomatic_setup_gettext_i18n(config_lang.get_value_or(""));
474 dcpomatic_setup_i18n ()
476 int language = wxLANGUAGE_DEFAULT;
478 auto config_lang = Config::instance()->language ();
479 if (config_lang && !config_lang->empty ()) {
480 auto const li = wxLocale::FindLanguageInfo (std_to_wx (config_lang.get ()));
482 language = li->Language;
486 wxLocale* locale = nullptr;
487 if (wxLocale::IsAvailable (language)) {
488 locale = new wxLocale (language, wxLOCALE_LOAD_DEFAULT);
490 #ifdef DCPOMATIC_WINDOWS
491 locale->AddCatalogLookupPathPrefix (std_to_wx (mo_path().string()));
494 #ifdef DCPOMATIC_LINUX
495 locale->AddCatalogLookupPathPrefix (LINUX_LOCALE_PREFIX);
497 /* We have to include the wxWidgets .mo in our distribution,
498 so we rename it to avoid clashes with any other installation
501 locale->AddCatalog (wxT ("dcpomatic2-wxstd"));
503 /* Fedora 29 (at least) installs wxstd3.mo instead of wxstd.mo */
504 locale->AddCatalog (wxT ("wxstd3"));
507 locale->AddCatalog(wxT("wxstd"));
508 locale->AddCatalog(wxT("libdcpomatic2-wx"));
509 locale->AddCatalog(wxT("dcpomatic2"));
511 if (!locale->IsOk()) {
513 locale = new wxLocale (wxLANGUAGE_ENGLISH);
518 dcpomatic_setup_gettext_i18n (wx_to_std (locale->GetCanonicalName ()));
526 wx_get (wxSpinCtrl* w)
528 return w->GetValue ();
535 return w->GetSelection ();
540 wx_get (wxSpinCtrlDouble* w)
542 return w->GetValue ();
546 /** @param s String of the form Context|String
547 * @return translation, or String if no translation is available.
550 context_translation (wxString s)
552 auto t = wxGetTranslation (s);
554 /* No translation; strip the context */
555 int c = t.Find (wxT ("|"));
556 if (c != wxNOT_FOUND) {
566 time_to_timecode (DCPTime t, double fps)
568 auto w = t.seconds ();
569 int const h = (w / 3600);
571 int const m = (w / 60);
573 int const s = floor (w);
575 int const f = lrint (w * fps);
576 return wxString::Format (wxT("%02d:%02d:%02d.%02d"), h, m, s, f);
581 setup_audio_channels_choice (wxChoice* choice, int minimum)
583 vector<pair<string, string>> items;
584 for (int i = minimum; i <= 16; i += 2) {
586 items.push_back (make_pair(wx_to_std(_("2 - stereo")), locale_convert<string>(i)));
588 items.push_back (make_pair(wx_to_std(_("4 - L/C/R/Lfe")), locale_convert<string>(i)));
590 items.push_back (make_pair(wx_to_std(_("6 - 5.1")), locale_convert<string>(i)));
592 items.push_back (make_pair(wx_to_std(_("8 - 5.1/HI/VI")), locale_convert<string>(i)));
593 } else if (i == 12) {
594 items.push_back (make_pair(wx_to_std(_("12 - 7.1/HI/VI")), locale_convert<string>(i)));
596 items.push_back (make_pair(locale_convert<string> (i), locale_convert<string>(i)));
600 checked_set (choice, items);
607 if (!variant::show_splash()) {
611 wxSplashScreen* splash = nullptr;
615 if (bitmap.LoadFile(bitmap_path("splash.png"), wxBITMAP_TYPE_PNG)) {
617 /* This wxMemoryDC must be destroyed before bitmap can be used elsewhere */
618 wxMemoryDC dc(bitmap);
619 auto const version = wxString::Format("%s (%s)", dcpomatic_version, dcpomatic_git_commit);
620 auto screen_size = dc.GetSize();
621 auto text_size = dc.GetTextExtent(version);
622 dc.DrawText(version, (screen_size.GetWidth() - text_size.GetWidth()) / 2, 236);
624 #ifdef DCPOMATIC_WINDOWS
625 /* Having wxSTAY_ON_TOP means error dialogues hide behind the splash screen on Windows, no matter what I try */
626 splash = new wxSplashScreen(bitmap, wxSPLASH_CENTRE_ON_SCREEN | wxSPLASH_NO_TIMEOUT, 0, nullptr, -1, wxDefaultPosition, wxDefaultSize, wxBORDER_SIMPLE | wxFRAME_NO_TASKBAR);
628 splash = new wxSplashScreen(bitmap, wxSPLASH_CENTRE_ON_SCREEN | wxSPLASH_NO_TIMEOUT, 0, nullptr, -1);
632 } catch (boost::filesystem::filesystem_error& e) {
633 /* Maybe we couldn't find the splash image; never mind */
641 calculate_mark_interval (double mark_interval)
643 if (mark_interval > 5) {
644 mark_interval -= lrint (mark_interval) % 5;
646 if (mark_interval > 10) {
647 mark_interval -= lrint (mark_interval) % 10;
649 if (mark_interval > 60) {
650 mark_interval -= lrint (mark_interval) % 60;
652 if (mark_interval > 3600) {
653 mark_interval -= lrint (mark_interval) % 3600;
656 if (mark_interval < 1) {
660 return mark_interval;
664 /** @return false if the task was cancelled */
666 display_progress (wxString title, wxString task)
668 auto jm = JobManager::instance ();
670 wxProgressDialog progress (title, task, 100, 0, wxPD_CAN_ABORT);
674 while (jm->work_to_do()) {
675 dcpomatic_sleep_seconds (1);
676 if (!progress.Pulse()) {
677 /* user pressed cancel */
678 for (auto i: jm->get()) {
691 get_offsets (vector<Offset>& offsets)
693 offsets.push_back({_("UTC-11"), dcp::UTCOffset(-11, 0)});
694 offsets.push_back({_("UTC-10"), dcp::UTCOffset(-10, 0)});
695 offsets.push_back({_("UTC-9"), dcp::UTCOffset( -9, 0)});
696 offsets.push_back({_("UTC-8"), dcp::UTCOffset( -8, 0)});
697 offsets.push_back({_("UTC-7"), dcp::UTCOffset( -7, 0)});
698 offsets.push_back({_("UTC-6"), dcp::UTCOffset( -6, 0)});
699 offsets.push_back({_("UTC-5"), dcp::UTCOffset( -5, 0)});
700 offsets.push_back({_("UTC-4:30"),dcp::UTCOffset( -4, -30)});
701 offsets.push_back({_("UTC-4"), dcp::UTCOffset( -4, 0)});
702 offsets.push_back({_("UTC-3:30"),dcp::UTCOffset( -3, -30)});
703 offsets.push_back({_("UTC-3"), dcp::UTCOffset( -3, 0)});
704 offsets.push_back({_("UTC-2"), dcp::UTCOffset( -2, 0)});
705 offsets.push_back({_("UTC-1"), dcp::UTCOffset( -1, 0)});
706 int utc = offsets.size();
707 offsets.push_back({_("UTC") , dcp::UTCOffset( 0, 0)});
708 offsets.push_back({_("UTC+1"), dcp::UTCOffset( 1, 0)});
709 offsets.push_back({_("UTC+2"), dcp::UTCOffset( 2, 0)});
710 offsets.push_back({_("UTC+3"), dcp::UTCOffset( 3, 0)});
711 offsets.push_back({_("UTC+4"), dcp::UTCOffset( 4, 0)});
712 offsets.push_back({_("UTC+5"), dcp::UTCOffset( 5, 0)});
713 offsets.push_back({_("UTC+5:30"),dcp::UTCOffset( 5, 30)});
714 offsets.push_back({_("UTC+6"), dcp::UTCOffset( 6, 0)});
715 offsets.push_back({_("UTC+7"), dcp::UTCOffset( 7, 0)});
716 offsets.push_back({_("UTC+8"), dcp::UTCOffset( 8, 0)});
717 offsets.push_back({_("UTC+9"), dcp::UTCOffset( 9, 0)});
718 offsets.push_back({_("UTC+9:30"),dcp::UTCOffset( 9, 30)});
719 offsets.push_back({_("UTC+10"), dcp::UTCOffset( 10, 0)});
720 offsets.push_back({_("UTC+11"), dcp::UTCOffset( 11, 0)});
721 offsets.push_back({_("UTC+12"), dcp::UTCOffset( 12, 0)});
728 bitmap_path (string name)
730 boost::filesystem::path base;
732 #ifdef DCPOMATIC_DEBUG
733 /* Hack to allow Linux and OS X to find icons when running from the source tree */
734 char* path = getenv ("DCPOMATIC_GRAPHICS");
738 base = resources_path();
741 if (!boost::filesystem::exists(base / name)) {
742 base = path / boost::filesystem::path("osx/preferences");
745 base = resources_path();
748 auto p = base / name;
749 return std_to_wx (p.string());
754 icon_path(string name)
756 return gui_is_dark() ? bitmap_path(String::compose("%1_white.png", name)) : bitmap_path(String::compose("%1_black.png", name));
761 small_button_size (wxWindow* parent, wxString text)
763 wxClientDC dc (parent);
764 auto size = dc.GetTextExtent (text);
774 #if defined(DCPOMATIC_OSX) && wxCHECK_VERSION(3, 1, 0)
775 auto appearance = wxSystemSettings::GetAppearance();
776 return appearance.IsDark();
783 #if wxCHECK_VERSION(3,1,0)
785 dpi_scale_factor (wxWindow* window)
787 return window->GetDPIScaleFactor();
791 dpi_scale_factor (wxWindow*)
800 search_ctrl_height ()
811 report_config_load_failure(wxWindow* parent, Config::LoadFailure what)
814 case Config::LoadFailure::CONFIG:
815 message_dialog(parent, _("The existing configuration failed to load. Default values will be used instead. These may take a short time to create."));
817 case Config::LoadFailure::CINEMAS:
820 _(wxString::Format("The cinemas list for creating KDMs (cinemas.xml) failed to load. Please check the numbered backup files in %s",
821 std_to_wx(Config::instance()->cinemas_file().parent_path().string())))
824 case Config::LoadFailure::DKDM_RECIPIENTS:
827 _(wxString::Format("The recipients list for creating DKDMs (dkdm_recipients.xml) failed to load. Please check the numbered backup files in %s",
828 std_to_wx(Config::instance()->dkdm_recipients_file().parent_path().string())))