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 "lib/config.h"
35 #include "lib/cross.h"
37 #include "lib/job_manager.h"
39 #include "lib/version.h"
40 #include <dcp/locale_convert.h>
41 #include <dcp/warnings.h>
42 LIBDCP_DISABLE_WARNINGS
43 #include <wx/spinctrl.h>
44 #include <wx/splash.h>
45 #include <wx/progdlg.h>
46 #include <wx/filepicker.h>
48 LIBDCP_ENABLE_WARNINGS
49 #include <boost/thread.hpp>
55 using std::shared_ptr;
56 using boost::optional;
57 using dcp::locale_convert;
58 using namespace dcpomatic;
63 create_label (wxWindow* p, wxString t, bool left)
65 create_label (wxWindow* p, wxString t, bool)
73 return new StaticText (p, t);
80 setup_osx_flags (wxSizer* s, bool left, int& flags)
83 auto box = dynamic_cast<wxBoxSizer*>(s);
84 if (!box || box->GetOrientation() != wxHORIZONTAL) {
85 flags |= wxALIGN_RIGHT;
92 /** Add a wxStaticText to a wxSizer.
93 * @param s Sizer to add to.
94 * @param p Parent window for the wxStaticText.
95 * @param t Text for the wxStaticText.
96 * @param left true if this label is a `left label'; ie the sort
97 * of label which should be right-aligned on OS X.
98 * @param prop Proportion to pass when calling Add() on the wxSizer.
101 add_label_to_sizer (wxSizer* s, wxWindow* p, wxString t, bool left, int prop, int flags)
104 setup_osx_flags (s, left, flags);
106 auto m = create_label (p, t, left);
107 s->Add (m, prop, flags, DCPOMATIC_SIZER_GAP);
114 add_label_to_sizer (wxSizer* s, wxStaticText* t, bool left, int prop, int flags)
116 add_label_to_sizer (wxSizer* s, wxStaticText* t, bool, int prop, int flags)
120 setup_osx_flags (s, left, flags);
122 s->Add (t, prop, flags, DCPOMATIC_SIZER_GAP);
128 add_label_to_sizer(wxGridBagSizer* s, wxWindow* p, wxString t, bool left, wxGBPosition pos, wxGBSpan span, bool indent)
130 int flags = wxALIGN_CENTER_VERTICAL | wxLEFT;
132 setup_osx_flags (s, left, flags);
134 auto m = create_label (p, t, left);
135 s->Add(m, pos, span, flags, indent ? DCPOMATIC_SIZER_X_GAP : 0);
142 add_label_to_sizer (wxGridBagSizer* s, wxStaticText* t, bool left, wxGBPosition pos, wxGBSpan span)
144 add_label_to_sizer (wxGridBagSizer* s, wxStaticText* t, bool, wxGBPosition pos, wxGBSpan span)
147 int flags = wxALIGN_CENTER_VERTICAL | wxLEFT | wxRIGHT;
149 setup_osx_flags (s, left, flags);
151 s->Add (t, pos, span, flags);
156 /** Pop up an error dialogue box.
157 * @param parent Parent.
159 * @param e Extended message.
162 error_dialog (wxWindow* parent, wxString m, optional<wxString> e)
164 auto d = make_wx<wxMessageDialog>(parent, m, _("DCP-o-matic"), wxOK | wxICON_ERROR);
167 em[0] = wxToupper (em[0]);
168 d->SetExtendedMessage (em);
174 /** Pop up an error dialogue box.
175 * @param parent Parent.
179 message_dialog (wxWindow* parent, wxString m)
181 auto d = make_wx<wxMessageDialog>(parent, m, _("DCP-o-matic"), wxOK | wxICON_INFORMATION);
186 /** @return true if the user answered "yes" */
188 confirm_dialog (wxWindow* parent, wxString m)
190 auto d = make_wx<wxMessageDialog>(parent, m, _("DCP-o-matic"), wxYES_NO | wxICON_QUESTION);
191 return d->ShowModal() == wxID_YES;
195 /** @param s wxWidgets string.
196 * @return Corresponding STL string.
199 wx_to_std (wxString s)
201 return string (s.ToUTF8());
205 /** @param s STL string.
206 * @return Corresponding wxWidgets string.
211 return wxString (s.c_str(), wxConvUTF8);
216 string_client_data (wxClientData* o)
218 return wx_to_std (dynamic_cast<wxStringClientData*>(o)->GetData());
223 checked_set (FilePickerCtrl* widget, boost::filesystem::path value)
225 if (widget->GetPath() != std_to_wx (value.string())) {
227 /* Hack to make wxWidgets clear the control when we are passed
232 widget->SetPath (std_to_wx (value.string()));
238 checked_set (wxDirPickerCtrl* widget, boost::filesystem::path value)
240 if (widget->GetPath() != std_to_wx (value.string())) {
242 /* Hack to make wxWidgets clear the control when we are passed
247 widget->SetPath (std_to_wx (value.string()));
253 checked_set (wxSpinCtrl* widget, int value)
255 if (widget->GetValue() != value) {
256 widget->SetValue (value);
262 checked_set (wxSpinCtrlDouble* widget, double value)
264 /* XXX: completely arbitrary epsilon */
265 if (fabs (widget->GetValue() - value) > 1e-16) {
266 widget->SetValue (value);
272 checked_set (wxChoice* widget, int value)
274 if (widget->GetSelection() != value) {
275 widget->SetSelection (value);
281 checked_set (wxChoice* widget, string value)
283 wxClientData* o = nullptr;
284 if (widget->GetSelection() != -1) {
285 o = widget->GetClientObject (widget->GetSelection ());
288 if (!o || string_client_data(o) != value) {
289 for (unsigned int i = 0; i < widget->GetCount(); ++i) {
290 if (string_client_data (widget->GetClientObject (i)) == value) {
291 widget->SetSelection (i);
299 checked_set (wxChoice* widget, vector<pair<string, string>> items)
301 vector<pair<string, string>> current;
302 for (unsigned int i = 0; i < widget->GetCount(); ++i) {
305 wx_to_std(widget->GetString(i)),
306 widget->GetClientData() ? string_client_data(widget->GetClientObject(i)) : ""
311 if (current == items) {
316 for (auto i: items) {
317 widget->Append (std_to_wx(i.first), new wxStringClientData(std_to_wx(i.second)));
323 checked_set (wxTextCtrl* widget, string value)
325 if (widget->GetValue() != std_to_wx (value)) {
326 widget->ChangeValue (std_to_wx (value));
332 checked_set (PasswordEntry* entry, string value)
334 if (entry->get() != value) {
341 checked_set (wxTextCtrl* widget, wxString value)
343 if (widget->GetValue() != value) {
344 widget->ChangeValue (value);
350 checked_set (wxStaticText* widget, string value)
352 if (widget->GetLabel() != std_to_wx (value)) {
353 widget->SetLabel (std_to_wx (value));
359 checked_set (wxStaticText* widget, wxString value)
361 if (widget->GetLabel() != value) {
362 widget->SetLabel (value);
368 checked_set (wxCheckBox* widget, bool value)
370 if (widget->GetValue() != value) {
371 widget->SetValue (value);
377 checked_set (wxRadioButton* widget, bool value)
379 if (widget->GetValue() != value) {
380 widget->SetValue (value);
386 checked_set(LanguageTagWidget* widget, dcp::LanguageTag value)
388 if (widget->get() != value) {
395 checked_set(LanguageTagWidget* widget, optional<dcp::LanguageTag> value)
397 if (widget->get() != value) {
404 checked_set(RegionSubtagWidget* widget, optional<dcp::LanguageTag::RegionSubtag> value)
406 if (widget->get() != value) {
413 dcpomatic_setup_i18n ()
415 int language = wxLANGUAGE_DEFAULT;
417 auto config_lang = Config::instance()->language ();
418 if (config_lang && !config_lang->empty ()) {
419 auto const li = wxLocale::FindLanguageInfo (std_to_wx (config_lang.get ()));
421 language = li->Language;
425 wxLocale* locale = nullptr;
426 if (wxLocale::IsAvailable (language)) {
427 locale = new wxLocale (language, wxLOCALE_LOAD_DEFAULT);
429 #ifdef DCPOMATIC_WINDOWS
430 locale->AddCatalogLookupPathPrefix (std_to_wx (mo_path().string()));
433 #ifdef DCPOMATIC_LINUX
434 locale->AddCatalogLookupPathPrefix (LINUX_LOCALE_PREFIX);
436 /* We have to include the wxWidgets .mo in our distribution,
437 so we rename it to avoid clashes with any other installation
440 locale->AddCatalog (wxT ("dcpomatic2-wxstd"));
442 /* Fedora 29 (at least) installs wxstd3.mo instead of wxstd.mo */
443 locale->AddCatalog (wxT ("wxstd3"));
446 locale->AddCatalog(wxT("wxstd"));
447 locale->AddCatalog(wxT("libdcpomatic2-wx"));
448 locale->AddCatalog(wxT("dcpomatic2"));
450 if (!locale->IsOk()) {
452 locale = new wxLocale (wxLANGUAGE_ENGLISH);
457 dcpomatic_setup_gettext_i18n (wx_to_std (locale->GetCanonicalName ()));
463 wx_get (wxSpinCtrl* w)
465 return w->GetValue ();
472 return w->GetSelection ();
477 wx_get (wxSpinCtrlDouble* w)
479 return w->GetValue ();
483 /** @param s String of the form Context|String
484 * @return translation, or String if no translation is available.
487 context_translation (wxString s)
489 auto t = wxGetTranslation (s);
491 /* No translation; strip the context */
492 int c = t.Find (wxT ("|"));
493 if (c != wxNOT_FOUND) {
503 time_to_timecode (DCPTime t, double fps)
505 auto w = t.seconds ();
506 int const h = (w / 3600);
508 int const m = (w / 60);
510 int const s = floor (w);
512 int const f = lrint (w * fps);
513 return wxString::Format (wxT("%02d:%02d:%02d.%02d"), h, m, s, f);
518 setup_audio_channels_choice (wxChoice* choice, int minimum)
520 vector<pair<string, string>> items;
521 for (int i = minimum; i <= 16; i += 2) {
523 items.push_back (make_pair(wx_to_std(_("2 - stereo")), locale_convert<string>(i)));
525 items.push_back (make_pair(wx_to_std(_("4 - L/C/R/Lfe")), locale_convert<string>(i)));
527 items.push_back (make_pair(wx_to_std(_("6 - 5.1")), locale_convert<string>(i)));
529 items.push_back (make_pair(wx_to_std(_("8 - 5.1/HI/VI")), locale_convert<string>(i)));
530 } else if (i == 12) {
531 items.push_back (make_pair(wx_to_std(_("12 - 7.1/HI/VI")), locale_convert<string>(i)));
533 items.push_back (make_pair(locale_convert<string> (i), locale_convert<string>(i)));
537 checked_set (choice, items);
544 wxSplashScreen* splash = nullptr;
548 if (bitmap.LoadFile(bitmap_path("splash.png"), wxBITMAP_TYPE_PNG)) {
550 /* This wxMemoryDC must be destroyed before bitmap can be used elsewhere */
551 wxMemoryDC dc(bitmap);
552 auto const version = wxString::Format("%s (%s)", dcpomatic_version, dcpomatic_git_commit);
553 auto screen_size = dc.GetSize();
554 auto text_size = dc.GetTextExtent(version);
555 dc.DrawText(version, (screen_size.GetWidth() - text_size.GetWidth()) / 2, 236);
557 #ifdef DCPOMATIC_WINDOWS
558 /* Having wxSTAY_ON_TOP means error dialogues hide behind the splash screen on Windows, no matter what I try */
559 splash = new wxSplashScreen(bitmap, wxSPLASH_CENTRE_ON_SCREEN | wxSPLASH_NO_TIMEOUT, 0, nullptr, -1, wxDefaultPosition, wxDefaultSize, wxBORDER_SIMPLE | wxFRAME_NO_TASKBAR);
561 splash = new wxSplashScreen(bitmap, wxSPLASH_CENTRE_ON_SCREEN | wxSPLASH_NO_TIMEOUT, 0, nullptr, -1);
565 } catch (boost::filesystem::filesystem_error& e) {
566 /* Maybe we couldn't find the splash image; never mind */
574 calculate_mark_interval (double mark_interval)
576 if (mark_interval > 5) {
577 mark_interval -= lrint (mark_interval) % 5;
579 if (mark_interval > 10) {
580 mark_interval -= lrint (mark_interval) % 10;
582 if (mark_interval > 60) {
583 mark_interval -= lrint (mark_interval) % 60;
585 if (mark_interval > 3600) {
586 mark_interval -= lrint (mark_interval) % 3600;
589 if (mark_interval < 1) {
593 return mark_interval;
597 /** @return false if the task was cancelled */
599 display_progress (wxString title, wxString task)
601 auto jm = JobManager::instance ();
603 wxProgressDialog progress (title, task, 100, 0, wxPD_CAN_ABORT);
607 while (jm->work_to_do()) {
608 dcpomatic_sleep_seconds (1);
609 if (!progress.Pulse()) {
610 /* user pressed cancel */
611 for (auto i: jm->get()) {
624 get_offsets (vector<Offset>& offsets)
626 offsets.push_back (Offset(_("UTC-11"), -11, 0));
627 offsets.push_back (Offset(_("UTC-10"), -10, 0));
628 offsets.push_back (Offset(_("UTC-9"), -9, 0));
629 offsets.push_back (Offset(_("UTC-8"), -8, 0));
630 offsets.push_back (Offset(_("UTC-7"), -7, 0));
631 offsets.push_back (Offset(_("UTC-6"), -6, 0));
632 offsets.push_back (Offset(_("UTC-5"), -5, 0));
633 offsets.push_back (Offset(_("UTC-4:30"), -4, 30));
634 offsets.push_back (Offset(_("UTC-4"), -4, 0));
635 offsets.push_back (Offset(_("UTC-3:30"), -3, 30));
636 offsets.push_back (Offset(_("UTC-3"), -3, 0));
637 offsets.push_back (Offset(_("UTC-2"), -2, 0));
638 offsets.push_back (Offset(_("UTC-1"), -1, 0));
639 int utc = offsets.size();
640 offsets.push_back (Offset(_("UTC") , 0, 0));
641 offsets.push_back (Offset(_("UTC+1"), 1, 0));
642 offsets.push_back (Offset(_("UTC+2"), 2, 0));
643 offsets.push_back (Offset(_("UTC+3"), 3, 0));
644 offsets.push_back (Offset(_("UTC+4"), 4, 0));
645 offsets.push_back (Offset(_("UTC+5"), 5, 0));
646 offsets.push_back (Offset(_("UTC+5:30"), 5, 30));
647 offsets.push_back (Offset(_("UTC+6"), 6, 0));
648 offsets.push_back (Offset(_("UTC+7"), 7, 0));
649 offsets.push_back (Offset(_("UTC+8"), 8, 0));
650 offsets.push_back (Offset(_("UTC+9"), 9, 0));
651 offsets.push_back (Offset(_("UTC+9:30"), 9, 30));
652 offsets.push_back (Offset(_("UTC+10"), 10, 0));
653 offsets.push_back (Offset(_("UTC+11"), 11, 0));
654 offsets.push_back (Offset(_("UTC+12"), 12, 0));
661 bitmap_path (string name)
663 boost::filesystem::path base;
665 #ifdef DCPOMATIC_DEBUG
666 /* Hack to allow Linux and OS X to find icons when running from the source tree */
667 char* path = getenv ("DCPOMATIC_GRAPHICS");
671 base = resources_path();
674 base = resources_path();
677 auto p = base / name;
678 return std_to_wx (p.string());
683 icon_path(string name)
685 return gui_is_dark() ? bitmap_path(String::compose("%1_white.png", name)) : bitmap_path(String::compose("%1_black.png", name));
690 small_button_size (wxWindow* parent, wxString text)
692 wxClientDC dc (parent);
693 auto size = dc.GetTextExtent (text);
703 #if defined(DCPOMATIC_OSX) && wxCHECK_VERSION(3, 1, 0)
704 auto appearance = wxSystemSettings::GetAppearance();
705 return appearance.IsDark();
712 #if wxCHECK_VERSION(3,1,0)
714 dpi_scale_factor (wxWindow* window)
716 return window->GetDPIScaleFactor();
720 dpi_scale_factor (wxWindow*)
729 search_ctrl_height ()
740 report_config_load_failure(wxWindow* parent, Config::LoadFailure what)
743 case Config::LoadFailure::CONFIG:
744 message_dialog(parent, _("The existing configuration failed to load. Default values will be used instead. These may take a short time to create."));
746 case Config::LoadFailure::CINEMAS:
749 _(wxString::Format("The cinemas list for creating KDMs (cinemas.xml) failed to load. Please check the numbered backup files in %s",
750 std_to_wx(Config::instance()->cinemas_file().parent_path().string())))
753 case Config::LoadFailure::DKDM_RECIPIENTS:
756 _(wxString::Format("The recipients list for creating DKDMs (dkdm_recipients.xml) failed to load. Please check the numbered backup files in %s",
757 std_to_wx(Config::instance()->dkdm_recipients_file().parent_path().string())))