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 "password_entry.h"
29 #include "static_text.h"
31 #include "lib/config.h"
32 #include "lib/cross.h"
34 #include "lib/job_manager.h"
36 #include "lib/version.h"
37 #include <dcp/locale_convert.h>
38 #include <dcp/warnings.h>
39 LIBDCP_DISABLE_WARNINGS
40 #include <wx/spinctrl.h>
41 #include <wx/splash.h>
42 #include <wx/progdlg.h>
43 #include <wx/filepicker.h>
46 LIBDCP_ENABLE_WARNINGS
47 #include <boost/thread.hpp>
53 using std::shared_ptr;
54 using boost::optional;
55 using dcp::locale_convert;
56 using namespace dcpomatic;
61 create_label (wxWindow* p, wxString t, bool left)
63 create_label (wxWindow* p, wxString t, bool)
71 return new StaticText (p, t);
78 setup_osx_flags (wxSizer* s, bool left, int& flags)
81 auto box = dynamic_cast<wxBoxSizer*>(s);
82 if (!box || box->GetOrientation() != wxHORIZONTAL) {
83 flags |= wxALIGN_RIGHT;
90 /** Add a wxStaticText to a wxSizer.
91 * @param s Sizer to add to.
92 * @param p Parent window for the wxStaticText.
93 * @param t Text for the wxStaticText.
94 * @param left true if this label is a `left label'; ie the sort
95 * of label which should be right-aligned on OS X.
96 * @param prop Proportion to pass when calling Add() on the wxSizer.
99 add_label_to_sizer (wxSizer* s, wxWindow* p, wxString t, bool left, int prop, int flags)
102 setup_osx_flags (s, left, flags);
104 auto m = create_label (p, t, left);
105 s->Add (m, prop, flags, DCPOMATIC_SIZER_GAP);
112 add_label_to_sizer (wxSizer* s, wxStaticText* t, bool left, int prop, int flags)
114 add_label_to_sizer (wxSizer* s, wxStaticText* t, bool, int prop, int flags)
118 setup_osx_flags (s, left, flags);
120 s->Add (t, prop, flags, DCPOMATIC_SIZER_GAP);
126 add_label_to_sizer (wxGridBagSizer* s, wxWindow* p, wxString t, bool left, wxGBPosition pos, wxGBSpan span)
128 int flags = wxALIGN_CENTER_VERTICAL | wxLEFT | wxRIGHT;
130 setup_osx_flags (s, left, flags);
132 auto m = create_label (p, t, left);
133 s->Add (m, pos, span, flags);
140 add_label_to_sizer (wxGridBagSizer* s, wxStaticText* t, bool left, wxGBPosition pos, wxGBSpan span)
142 add_label_to_sizer (wxGridBagSizer* s, wxStaticText* t, bool, wxGBPosition pos, wxGBSpan span)
145 int flags = wxALIGN_CENTER_VERTICAL | wxLEFT | wxRIGHT;
147 setup_osx_flags (s, left, flags);
149 s->Add (t, pos, span, flags);
154 /** Pop up an error dialogue box.
155 * @param parent Parent.
157 * @param e Extended message.
160 error_dialog (wxWindow* parent, wxString m, optional<wxString> e)
162 auto d = new wxMessageDialog (parent, m, _("DCP-o-matic"), wxOK | wxICON_ERROR);
165 em[0] = wxToupper (em[0]);
166 d->SetExtendedMessage (em);
173 /** Pop up an error dialogue box.
174 * @param parent Parent.
178 message_dialog (wxWindow* parent, wxString m)
180 auto d = new 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 = new wxMessageDialog (parent, m, _("DCP-o-matic"), wxYES_NO | wxICON_QUESTION);
191 int const r = d->ShowModal ();
193 return r == 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 selection_string_client_data(wxChoice* choice)
227 return string_client_data(choice->GetClientObject(choice->GetSelection()));
232 checked_set (FilePickerCtrl* widget, boost::filesystem::path value)
234 if (widget->GetPath() != std_to_wx (value.string())) {
236 /* Hack to make wxWidgets clear the control when we are passed
241 widget->SetPath (std_to_wx (value.string()));
247 checked_set (wxDirPickerCtrl* widget, boost::filesystem::path value)
249 if (widget->GetPath() != std_to_wx (value.string())) {
251 /* Hack to make wxWidgets clear the control when we are passed
256 widget->SetPath (std_to_wx (value.string()));
262 checked_set (wxSpinCtrl* widget, int value)
264 if (widget->GetValue() != value) {
265 widget->SetValue (value);
271 checked_set (wxSpinCtrlDouble* widget, double value)
273 /* XXX: completely arbitrary epsilon */
274 if (fabs (widget->GetValue() - value) > 1e-16) {
275 widget->SetValue (value);
281 checked_set (wxChoice* widget, int value)
283 if (widget->GetSelection() != value) {
284 widget->SetSelection (value);
290 checked_set (wxChoice* widget, string value)
292 wxClientData* o = nullptr;
293 if (widget->GetSelection() != -1) {
294 o = widget->GetClientObject (widget->GetSelection ());
297 if (!o || string_client_data(o) != value) {
298 for (unsigned int i = 0; i < widget->GetCount(); ++i) {
299 if (string_client_data (widget->GetClientObject (i)) == value) {
300 widget->SetSelection (i);
308 checked_set (wxChoice* widget, vector<pair<string, string>> items)
310 vector<pair<string, string>> current;
311 for (unsigned int i = 0; i < widget->GetCount(); ++i) {
314 wx_to_std(widget->GetString(i)),
315 widget->GetClientData() ? string_client_data(widget->GetClientObject(i)) : ""
320 if (current == items) {
325 for (auto i: items) {
326 widget->Append (std_to_wx(i.first), new wxStringClientData(std_to_wx(i.second)));
332 checked_set (wxTextCtrl* widget, string value)
334 if (widget->GetValue() != std_to_wx (value)) {
335 widget->ChangeValue (std_to_wx (value));
341 checked_set (PasswordEntry* entry, string value)
343 if (entry->get() != value) {
350 checked_set (wxTextCtrl* widget, wxString value)
352 if (widget->GetValue() != value) {
353 widget->ChangeValue (value);
359 checked_set (wxStaticText* widget, string value)
361 if (widget->GetLabel() != std_to_wx (value)) {
362 widget->SetLabel (std_to_wx (value));
368 checked_set (wxStaticText* widget, wxString value)
370 if (widget->GetLabel() != value) {
371 widget->SetLabel (value);
377 checked_set (wxCheckBox* widget, bool value)
379 if (widget->GetValue() != value) {
380 widget->SetValue (value);
386 checked_set (wxRadioButton* widget, bool value)
388 if (widget->GetValue() != value) {
389 widget->SetValue (value);
395 dcpomatic_setup_i18n ()
397 int language = wxLANGUAGE_DEFAULT;
399 auto config_lang = Config::instance()->language ();
400 if (config_lang && !config_lang->empty ()) {
401 auto const li = wxLocale::FindLanguageInfo (std_to_wx (config_lang.get ()));
403 language = li->Language;
407 wxLocale* locale = nullptr;
408 if (wxLocale::IsAvailable (language)) {
409 locale = new wxLocale (language, wxLOCALE_LOAD_DEFAULT);
411 #ifdef DCPOMATIC_WINDOWS
412 locale->AddCatalogLookupPathPrefix (std_to_wx (mo_path().string()));
415 #ifdef DCPOMATIC_LINUX
416 locale->AddCatalogLookupPathPrefix (LINUX_LOCALE_PREFIX);
418 /* We have to include the wxWidgets .mo in our distribution,
419 so we rename it to avoid clashes with any other installation
422 locale->AddCatalog (wxT ("dcpomatic2-wxstd"));
424 /* Fedora 29 (at least) installs wxstd3.mo instead of wxstd.mo */
425 locale->AddCatalog (wxT ("wxstd3"));
428 locale->AddCatalog (wxT ("libdcpomatic2-wx"));
429 locale->AddCatalog (wxT ("dcpomatic2"));
431 if (!locale->IsOk()) {
433 locale = new wxLocale (wxLANGUAGE_ENGLISH);
438 dcpomatic_setup_gettext_i18n (wx_to_std (locale->GetCanonicalName ()));
444 wx_get (wxSpinCtrl* w)
446 return w->GetValue ();
453 return w->GetSelection ();
458 wx_get (wxSpinCtrlDouble* w)
460 return w->GetValue ();
464 /** @param s String of the form Context|String
465 * @return translation, or String if no translation is available.
468 context_translation (wxString s)
470 auto t = wxGetTranslation (s);
472 /* No translation; strip the context */
473 int c = t.Find (wxT ("|"));
474 if (c != wxNOT_FOUND) {
484 time_to_timecode (DCPTime t, double fps)
486 auto w = t.seconds ();
487 int const h = (w / 3600);
489 int const m = (w / 60);
491 int const s = floor (w);
493 int const f = lrint (w * fps);
494 return wxString::Format (wxT("%02d:%02d:%02d.%02d"), h, m, s, f);
499 setup_audio_channels_choice (wxChoice* choice, int minimum)
501 vector<pair<string, string>> items;
502 for (int i = minimum; i <= 16; i += 2) {
504 items.push_back (make_pair(wx_to_std(_("2 - stereo")), locale_convert<string>(i)));
506 items.push_back (make_pair(wx_to_std(_("4 - L/C/R/Lfe")), locale_convert<string>(i)));
508 items.push_back (make_pair(wx_to_std(_("6 - 5.1")), locale_convert<string>(i)));
510 items.push_back (make_pair(wx_to_std(_("8 - 5.1/HI/VI")), locale_convert<string>(i)));
511 } else if (i == 12) {
512 items.push_back (make_pair(wx_to_std(_("12 - 7.1/HI/VI")), locale_convert<string>(i)));
514 items.push_back (make_pair(locale_convert<string> (i), locale_convert<string>(i)));
518 checked_set (choice, items);
525 wxSplashScreen* splash = nullptr;
528 if (bitmap.LoadFile(bitmap_path("splash.png"), wxBITMAP_TYPE_PNG)) {
530 /* This wxMemoryDC must be destroyed before bitmap can be used elsewhere */
531 wxMemoryDC dc(bitmap);
532 auto const version = wxString::Format("%s (%s)", dcpomatic_version, dcpomatic_git_commit);
533 auto screen_size = dc.GetSize();
534 auto text_size = dc.GetTextExtent(version);
535 dc.DrawText(version, (screen_size.GetWidth() - text_size.GetWidth()) / 2, 236);
537 #ifdef DCPOMATIC_WINDOWS
538 /* Having wxSTAY_ON_TOP means error dialogues hide behind the splash screen on Windows, no matter what I try */
539 splash = new wxSplashScreen (bitmap, wxSPLASH_CENTRE_ON_SCREEN | wxSPLASH_NO_TIMEOUT, 0, 0, -1, wxDefaultPosition, wxDefaultSize, wxBORDER_SIMPLE | wxFRAME_NO_TASKBAR);
541 splash = new wxSplashScreen (bitmap, wxSPLASH_CENTRE_ON_SCREEN | wxSPLASH_NO_TIMEOUT, 0, 0, -1);
545 } catch (boost::filesystem::filesystem_error& e) {
546 /* Maybe we couldn't find the splash image; never mind */
554 calculate_mark_interval (double mark_interval)
556 if (mark_interval > 5) {
557 mark_interval -= lrint (mark_interval) % 5;
559 if (mark_interval > 10) {
560 mark_interval -= lrint (mark_interval) % 10;
562 if (mark_interval > 60) {
563 mark_interval -= lrint (mark_interval) % 60;
565 if (mark_interval > 3600) {
566 mark_interval -= lrint (mark_interval) % 3600;
569 if (mark_interval < 1) {
573 return mark_interval;
577 /** @return false if the task was cancelled */
579 display_progress (wxString title, wxString task)
581 auto jm = JobManager::instance ();
583 wxProgressDialog progress (title, task, 100, 0, wxPD_CAN_ABORT);
587 while (jm->work_to_do()) {
588 dcpomatic_sleep_seconds (1);
589 if (!progress.Pulse()) {
590 /* user pressed cancel */
591 for (auto i: jm->get()) {
604 get_offsets (vector<Offset>& offsets)
606 offsets.push_back (Offset(_("UTC-11"), -11, 0));
607 offsets.push_back (Offset(_("UTC-10"), -10, 0));
608 offsets.push_back (Offset(_("UTC-9"), -9, 0));
609 offsets.push_back (Offset(_("UTC-8"), -8, 0));
610 offsets.push_back (Offset(_("UTC-7"), -7, 0));
611 offsets.push_back (Offset(_("UTC-6"), -6, 0));
612 offsets.push_back (Offset(_("UTC-5"), -5, 0));
613 offsets.push_back (Offset(_("UTC-4:30"), -4, 30));
614 offsets.push_back (Offset(_("UTC-4"), -4, 0));
615 offsets.push_back (Offset(_("UTC-3:30"), -3, 30));
616 offsets.push_back (Offset(_("UTC-3"), -3, 0));
617 offsets.push_back (Offset(_("UTC-2"), -2, 0));
618 offsets.push_back (Offset(_("UTC-1"), -1, 0));
619 int utc = offsets.size();
620 offsets.push_back (Offset(_("UTC") , 0, 0));
621 offsets.push_back (Offset(_("UTC+1"), 1, 0));
622 offsets.push_back (Offset(_("UTC+2"), 2, 0));
623 offsets.push_back (Offset(_("UTC+3"), 3, 0));
624 offsets.push_back (Offset(_("UTC+4"), 4, 0));
625 offsets.push_back (Offset(_("UTC+5"), 5, 0));
626 offsets.push_back (Offset(_("UTC+5:30"), 5, 30));
627 offsets.push_back (Offset(_("UTC+6"), 6, 0));
628 offsets.push_back (Offset(_("UTC+7"), 7, 0));
629 offsets.push_back (Offset(_("UTC+8"), 8, 0));
630 offsets.push_back (Offset(_("UTC+9"), 9, 0));
631 offsets.push_back (Offset(_("UTC+9:30"), 9, 30));
632 offsets.push_back (Offset(_("UTC+10"), 10, 0));
633 offsets.push_back (Offset(_("UTC+11"), 11, 0));
634 offsets.push_back (Offset(_("UTC+12"), 12, 0));
641 bitmap_path (string name)
643 boost::filesystem::path base;
645 #ifdef DCPOMATIC_DEBUG
646 /* Hack to allow Linux and OS X to find icons when running from the source tree */
647 char* path = getenv ("DCPOMATIC_GRAPHICS");
651 base = resources_path();
654 base = resources_path();
657 auto p = base / name;
658 return std_to_wx (p.string());
663 icon_path(string name)
665 return gui_is_dark() ? bitmap_path(String::compose("%1_white.png", name)) : bitmap_path(String::compose("%1_black.png", name));
670 small_button_size (wxWindow* parent, wxString text)
672 wxClientDC dc (parent);
673 auto size = dc.GetTextExtent (text);
683 #if defined(DCPOMATIC_OSX) && wxCHECK_VERSION(3, 1, 0)
684 auto appearance = wxSystemSettings::GetAppearance();
685 return appearance.IsDark();
692 #if wxCHECK_VERSION(3,1,0)
694 dpi_scale_factor (wxWindow* window)
696 return window->GetDPIScaleFactor();
700 dpi_scale_factor (wxWindow*)
709 search_ctrl_height ()
720 report_config_load_failure(wxWindow* parent, Config::LoadFailure what)
723 case Config::LoadFailure::CONFIG:
724 message_dialog(parent, _("The existing configuration failed to load. Default values will be used instead. These may take a short time to create."));
726 case Config::LoadFailure::CINEMAS:
729 _(wxString::Format("The cinemas list for creating KDMs (cinemas.xml) failed to load. Please check the numbered backup files in %s",
730 std_to_wx(Config::instance()->cinemas_file().parent_path().string())))
733 case Config::LoadFailure::DKDM_RECIPIENTS:
736 _(wxString::Format("The recipients list for creating DKDMs (dkdm_recipients.xml) failed to load. Please check the numbered backup files in %s",
737 std_to_wx(Config::instance()->dkdm_recipients_file().parent_path().string())))
744 static AudioAPI wasapi(_("WASAPI"), "wasapi", RtAudio::WINDOWS_WASAPI);
745 static AudioAPI asio(_("ASIO"), "asio", RtAudio::WINDOWS_ASIO);
746 static AudioAPI pulse(_("PulseAudio"), "pulse", RtAudio::LINUX_PULSE);
747 static AudioAPI alsa(_("ALSA"), "alsa", RtAudio::LINUX_ALSA);
748 static AudioAPI core(_("CoreAudio"), "core", RtAudio::MACOSX_CORE);
754 #ifdef DCPOMATIC_WINDOWS
755 return { wasapi, asio };
758 #ifdef DCPOMATIC_LINUX
759 return { pulse, alsa };
768 id_to_audio_api(string id)
770 const auto apis = audio_apis();
771 auto iter = std::find_if(apis.begin(), apis.end(), [&id](AudioAPI const& api) { return api.id() == id; });
772 if (iter == apis.end()) {
781 id_to_audio_api(optional<string> id)
783 const auto apis = audio_apis();
788 return id_to_audio_api(*id);
793 index_to_audio_api(int index)
795 const auto apis = audio_apis();
796 if (index < 0 || index >= static_cast<int>(apis.size())) {