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"
33 #include "lib/config.h"
34 #include "lib/cross.h"
36 #include "lib/job_manager.h"
38 #include "lib/version.h"
39 #include <dcp/locale_convert.h>
40 #include <dcp/warnings.h>
41 LIBDCP_DISABLE_WARNINGS
42 #include <wx/spinctrl.h>
43 #include <wx/splash.h>
44 #include <wx/progdlg.h>
45 #include <wx/filepicker.h>
47 LIBDCP_ENABLE_WARNINGS
48 #include <boost/thread.hpp>
54 using std::shared_ptr;
55 using boost::optional;
56 using dcp::locale_convert;
57 using namespace dcpomatic;
62 create_label (wxWindow* p, wxString t, bool left)
64 create_label (wxWindow* p, wxString t, bool)
72 return new StaticText (p, t);
79 setup_osx_flags (wxSizer* s, bool left, int& flags)
82 auto box = dynamic_cast<wxBoxSizer*>(s);
83 if (!box || box->GetOrientation() != wxHORIZONTAL) {
84 flags |= wxALIGN_RIGHT;
91 /** Add a wxStaticText to a wxSizer.
92 * @param s Sizer to add to.
93 * @param p Parent window for the wxStaticText.
94 * @param t Text for the wxStaticText.
95 * @param left true if this label is a `left label'; ie the sort
96 * of label which should be right-aligned on OS X.
97 * @param prop Proportion to pass when calling Add() on the wxSizer.
100 add_label_to_sizer (wxSizer* s, wxWindow* p, wxString t, bool left, int prop, int flags)
103 setup_osx_flags (s, left, flags);
105 auto m = create_label (p, t, left);
106 s->Add (m, prop, flags, DCPOMATIC_SIZER_GAP);
113 add_label_to_sizer (wxSizer* s, wxStaticText* t, bool left, int prop, int flags)
115 add_label_to_sizer (wxSizer* s, wxStaticText* t, bool, int prop, int flags)
119 setup_osx_flags (s, left, flags);
121 s->Add (t, prop, flags, DCPOMATIC_SIZER_GAP);
127 add_label_to_sizer (wxGridBagSizer* s, wxWindow* p, wxString t, bool left, wxGBPosition pos, wxGBSpan span)
129 int flags = wxALIGN_CENTER_VERTICAL | wxLEFT | wxRIGHT;
131 setup_osx_flags (s, left, flags);
133 auto m = create_label (p, t, left);
134 s->Add (m, pos, span, flags);
141 add_label_to_sizer (wxGridBagSizer* s, wxStaticText* t, bool left, wxGBPosition pos, wxGBSpan span)
143 add_label_to_sizer (wxGridBagSizer* s, wxStaticText* t, bool, wxGBPosition pos, wxGBSpan span)
146 int flags = wxALIGN_CENTER_VERTICAL | wxLEFT | wxRIGHT;
148 setup_osx_flags (s, left, flags);
150 s->Add (t, pos, span, flags);
155 /** Pop up an error dialogue box.
156 * @param parent Parent.
158 * @param e Extended message.
161 error_dialog (wxWindow* parent, wxString m, optional<wxString> e)
163 auto d = new wxMessageDialog (parent, m, _("DCP-o-matic"), wxOK | wxICON_ERROR);
166 em[0] = wxToupper (em[0]);
167 d->SetExtendedMessage (em);
174 /** Pop up an error dialogue box.
175 * @param parent Parent.
179 message_dialog (wxWindow* parent, wxString m)
181 auto d = new wxMessageDialog (parent, m, _("DCP-o-matic"), wxOK | wxICON_INFORMATION);
187 /** @return true if the user answered "yes" */
189 confirm_dialog (wxWindow* parent, wxString m)
191 auto d = new wxMessageDialog (parent, m, _("DCP-o-matic"), wxYES_NO | wxICON_QUESTION);
192 int const r = d->ShowModal ();
194 return r == wxID_YES;
198 /** @param s wxWidgets string.
199 * @return Corresponding STL string.
202 wx_to_std (wxString s)
204 return string (s.ToUTF8());
208 /** @param s STL string.
209 * @return Corresponding wxWidgets string.
214 return wxString (s.c_str(), wxConvUTF8);
219 string_client_data (wxClientData* o)
221 return wx_to_std (dynamic_cast<wxStringClientData*>(o)->GetData());
226 checked_set (FilePickerCtrl* widget, boost::filesystem::path value)
228 if (widget->GetPath() != std_to_wx (value.string())) {
230 /* Hack to make wxWidgets clear the control when we are passed
235 widget->SetPath (std_to_wx (value.string()));
241 checked_set (wxDirPickerCtrl* widget, boost::filesystem::path value)
243 if (widget->GetPath() != std_to_wx (value.string())) {
245 /* Hack to make wxWidgets clear the control when we are passed
250 widget->SetPath (std_to_wx (value.string()));
256 checked_set (wxSpinCtrl* widget, int value)
258 if (widget->GetValue() != value) {
259 widget->SetValue (value);
265 checked_set (wxSpinCtrlDouble* widget, double value)
267 /* XXX: completely arbitrary epsilon */
268 if (fabs (widget->GetValue() - value) > 1e-16) {
269 widget->SetValue (value);
275 checked_set (wxChoice* widget, int value)
277 if (widget->GetSelection() != value) {
278 widget->SetSelection (value);
284 checked_set (wxChoice* widget, string value)
286 wxClientData* o = nullptr;
287 if (widget->GetSelection() != -1) {
288 o = widget->GetClientObject (widget->GetSelection ());
291 if (!o || string_client_data(o) != value) {
292 for (unsigned int i = 0; i < widget->GetCount(); ++i) {
293 if (string_client_data (widget->GetClientObject (i)) == value) {
294 widget->SetSelection (i);
302 checked_set (wxChoice* widget, vector<pair<string, string>> items)
304 vector<pair<string, string>> current;
305 for (unsigned int i = 0; i < widget->GetCount(); ++i) {
308 wx_to_std(widget->GetString(i)),
309 widget->GetClientData() ? string_client_data(widget->GetClientObject(i)) : ""
314 if (current == items) {
319 for (auto i: items) {
320 widget->Append (std_to_wx(i.first), new wxStringClientData(std_to_wx(i.second)));
326 checked_set (wxTextCtrl* widget, string value)
328 if (widget->GetValue() != std_to_wx (value)) {
329 widget->ChangeValue (std_to_wx (value));
335 checked_set (PasswordEntry* entry, string value)
337 if (entry->get() != value) {
344 checked_set (wxTextCtrl* widget, wxString value)
346 if (widget->GetValue() != value) {
347 widget->ChangeValue (value);
353 checked_set (wxStaticText* widget, string value)
355 if (widget->GetLabel() != std_to_wx (value)) {
356 widget->SetLabel (std_to_wx (value));
362 checked_set (wxStaticText* widget, wxString value)
364 if (widget->GetLabel() != value) {
365 widget->SetLabel (value);
371 checked_set (wxCheckBox* widget, bool value)
373 if (widget->GetValue() != value) {
374 widget->SetValue (value);
380 checked_set (wxRadioButton* widget, bool value)
382 if (widget->GetValue() != value) {
383 widget->SetValue (value);
389 checked_set(LanguageTagWidget* widget, dcp::LanguageTag value)
391 if (widget->get() != value) {
398 checked_set(LanguageTagWidget* widget, optional<dcp::LanguageTag> value)
400 if (widget->get() != value) {
407 checked_set(RegionSubtagWidget* widget, optional<dcp::LanguageTag::RegionSubtag> value)
409 if (widget->get() != value) {
416 dcpomatic_setup_i18n ()
418 int language = wxLANGUAGE_DEFAULT;
420 auto config_lang = Config::instance()->language ();
421 if (config_lang && !config_lang->empty ()) {
422 auto const li = wxLocale::FindLanguageInfo (std_to_wx (config_lang.get ()));
424 language = li->Language;
428 wxLocale* locale = nullptr;
429 if (wxLocale::IsAvailable (language)) {
430 locale = new wxLocale (language, wxLOCALE_LOAD_DEFAULT);
432 #ifdef DCPOMATIC_WINDOWS
433 locale->AddCatalogLookupPathPrefix (std_to_wx (mo_path().string()));
436 #ifdef DCPOMATIC_LINUX
437 locale->AddCatalogLookupPathPrefix (LINUX_LOCALE_PREFIX);
439 /* We have to include the wxWidgets .mo in our distribution,
440 so we rename it to avoid clashes with any other installation
443 locale->AddCatalog (wxT ("dcpomatic2-wxstd"));
445 /* Fedora 29 (at least) installs wxstd3.mo instead of wxstd.mo */
446 locale->AddCatalog (wxT ("wxstd3"));
449 locale->AddCatalog (wxT ("libdcpomatic2-wx"));
450 locale->AddCatalog (wxT ("dcpomatic2"));
452 if (!locale->IsOk()) {
454 locale = new wxLocale (wxLANGUAGE_ENGLISH);
459 dcpomatic_setup_gettext_i18n (wx_to_std (locale->GetCanonicalName ()));
465 wx_get (wxSpinCtrl* w)
467 return w->GetValue ();
474 return w->GetSelection ();
479 wx_get (wxSpinCtrlDouble* w)
481 return w->GetValue ();
485 /** @param s String of the form Context|String
486 * @return translation, or String if no translation is available.
489 context_translation (wxString s)
491 auto t = wxGetTranslation (s);
493 /* No translation; strip the context */
494 int c = t.Find (wxT ("|"));
495 if (c != wxNOT_FOUND) {
505 time_to_timecode (DCPTime t, double fps)
507 auto w = t.seconds ();
508 int const h = (w / 3600);
510 int const m = (w / 60);
512 int const s = floor (w);
514 int const f = lrint (w * fps);
515 return wxString::Format (wxT("%02d:%02d:%02d.%02d"), h, m, s, f);
520 setup_audio_channels_choice (wxChoice* choice, int minimum)
522 vector<pair<string, string>> items;
523 for (int i = minimum; i <= 16; i += 2) {
525 items.push_back (make_pair(wx_to_std(_("2 - stereo")), locale_convert<string>(i)));
527 items.push_back (make_pair(wx_to_std(_("4 - L/C/R/Lfe")), locale_convert<string>(i)));
529 items.push_back (make_pair(wx_to_std(_("6 - 5.1")), locale_convert<string>(i)));
531 items.push_back (make_pair(wx_to_std(_("8 - 5.1/HI/VI")), locale_convert<string>(i)));
532 } else if (i == 12) {
533 items.push_back (make_pair(wx_to_std(_("12 - 7.1/HI/VI")), locale_convert<string>(i)));
535 items.push_back (make_pair(locale_convert<string> (i), locale_convert<string>(i)));
539 checked_set (choice, items);
546 wxSplashScreen* splash = nullptr;
549 if (bitmap.LoadFile(bitmap_path("splash.png"), wxBITMAP_TYPE_PNG)) {
551 /* This wxMemoryDC must be destroyed before bitmap can be used elsewhere */
552 wxMemoryDC dc(bitmap);
553 auto const version = wxString::Format("%s (%s)", dcpomatic_version, dcpomatic_git_commit);
554 auto screen_size = dc.GetSize();
555 auto text_size = dc.GetTextExtent(version);
556 dc.DrawText(version, (screen_size.GetWidth() - text_size.GetWidth()) / 2, 236);
558 #ifdef DCPOMATIC_WINDOWS
559 /* Having wxSTAY_ON_TOP means error dialogues hide behind the splash screen on Windows, no matter what I try */
560 splash = new wxSplashScreen (bitmap, wxSPLASH_CENTRE_ON_SCREEN | wxSPLASH_NO_TIMEOUT, 0, 0, -1, wxDefaultPosition, wxDefaultSize, wxBORDER_SIMPLE | wxFRAME_NO_TASKBAR);
562 splash = new wxSplashScreen (bitmap, wxSPLASH_CENTRE_ON_SCREEN | wxSPLASH_NO_TIMEOUT, 0, 0, -1);
566 } catch (boost::filesystem::filesystem_error& e) {
567 /* Maybe we couldn't find the splash image; never mind */
575 calculate_mark_interval (double mark_interval)
577 if (mark_interval > 5) {
578 mark_interval -= lrint (mark_interval) % 5;
580 if (mark_interval > 10) {
581 mark_interval -= lrint (mark_interval) % 10;
583 if (mark_interval > 60) {
584 mark_interval -= lrint (mark_interval) % 60;
586 if (mark_interval > 3600) {
587 mark_interval -= lrint (mark_interval) % 3600;
590 if (mark_interval < 1) {
594 return mark_interval;
598 /** @return false if the task was cancelled */
600 display_progress (wxString title, wxString task)
602 auto jm = JobManager::instance ();
604 wxProgressDialog progress (title, task, 100, 0, wxPD_CAN_ABORT);
608 while (jm->work_to_do()) {
609 dcpomatic_sleep_seconds (1);
610 if (!progress.Pulse()) {
611 /* user pressed cancel */
612 for (auto i: jm->get()) {
625 get_offsets (vector<Offset>& offsets)
627 offsets.push_back (Offset(_("UTC-11"), -11, 0));
628 offsets.push_back (Offset(_("UTC-10"), -10, 0));
629 offsets.push_back (Offset(_("UTC-9"), -9, 0));
630 offsets.push_back (Offset(_("UTC-8"), -8, 0));
631 offsets.push_back (Offset(_("UTC-7"), -7, 0));
632 offsets.push_back (Offset(_("UTC-6"), -6, 0));
633 offsets.push_back (Offset(_("UTC-5"), -5, 0));
634 offsets.push_back (Offset(_("UTC-4:30"), -4, 30));
635 offsets.push_back (Offset(_("UTC-4"), -4, 0));
636 offsets.push_back (Offset(_("UTC-3:30"), -3, 30));
637 offsets.push_back (Offset(_("UTC-3"), -3, 0));
638 offsets.push_back (Offset(_("UTC-2"), -2, 0));
639 offsets.push_back (Offset(_("UTC-1"), -1, 0));
640 int utc = offsets.size();
641 offsets.push_back (Offset(_("UTC") , 0, 0));
642 offsets.push_back (Offset(_("UTC+1"), 1, 0));
643 offsets.push_back (Offset(_("UTC+2"), 2, 0));
644 offsets.push_back (Offset(_("UTC+3"), 3, 0));
645 offsets.push_back (Offset(_("UTC+4"), 4, 0));
646 offsets.push_back (Offset(_("UTC+5"), 5, 0));
647 offsets.push_back (Offset(_("UTC+5:30"), 5, 30));
648 offsets.push_back (Offset(_("UTC+6"), 6, 0));
649 offsets.push_back (Offset(_("UTC+7"), 7, 0));
650 offsets.push_back (Offset(_("UTC+8"), 8, 0));
651 offsets.push_back (Offset(_("UTC+9"), 9, 0));
652 offsets.push_back (Offset(_("UTC+9:30"), 9, 30));
653 offsets.push_back (Offset(_("UTC+10"), 10, 0));
654 offsets.push_back (Offset(_("UTC+11"), 11, 0));
655 offsets.push_back (Offset(_("UTC+12"), 12, 0));
662 bitmap_path (string name)
664 boost::filesystem::path base;
666 #ifdef DCPOMATIC_DEBUG
667 /* Hack to allow Linux and OS X to find icons when running from the source tree */
668 char* path = getenv ("DCPOMATIC_GRAPHICS");
672 base = resources_path();
675 base = resources_path();
678 auto p = base / name;
679 return std_to_wx (p.string());
684 icon_path(string name)
686 return gui_is_dark() ? bitmap_path(String::compose("%1_white.png", name)) : bitmap_path(String::compose("%1_black.png", name));
691 small_button_size (wxWindow* parent, wxString text)
693 wxClientDC dc (parent);
694 auto size = dc.GetTextExtent (text);
704 #if defined(DCPOMATIC_OSX) && wxCHECK_VERSION(3, 1, 0)
705 auto appearance = wxSystemSettings::GetAppearance();
706 return appearance.IsDark();
713 #if wxCHECK_VERSION(3,1,0)
715 dpi_scale_factor (wxWindow* window)
717 return window->GetDPIScaleFactor();
721 dpi_scale_factor (wxWindow*)
730 search_ctrl_height ()
741 report_config_load_failure(wxWindow* parent, Config::LoadFailure what)
744 case Config::LoadFailure::CONFIG:
745 message_dialog(parent, _("The existing configuration failed to load. Default values will be used instead. These may take a short time to create."));
747 case Config::LoadFailure::CINEMAS:
750 _(wxString::Format("The cinemas list for creating KDMs (cinemas.xml) failed to load. Please check the numbered backup files in %s",
751 std_to_wx(Config::instance()->cinemas_file().parent_path().string())))
754 case Config::LoadFailure::DKDM_RECIPIENTS:
757 _(wxString::Format("The recipients list for creating DKDMs (dkdm_recipients.xml) failed to load. Please check the numbered backup files in %s",
758 std_to_wx(Config::instance()->dkdm_recipients_file().parent_path().string())))