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 "static_text.h"
32 #include "lib/config.h"
33 #include "lib/cross.h"
35 #include "lib/job_manager.h"
37 #include "lib/version.h"
38 #include <dcp/locale_convert.h>
39 #include <dcp/warnings.h>
40 LIBDCP_DISABLE_WARNINGS
41 #include <wx/spinctrl.h>
42 #include <wx/splash.h>
43 #include <wx/progdlg.h>
44 #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 checked_set (FilePickerCtrl* widget, boost::filesystem::path value)
227 if (widget->GetPath() != std_to_wx (value.string())) {
229 /* Hack to make wxWidgets clear the control when we are passed
234 widget->SetPath (std_to_wx (value.string()));
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 dcpomatic_setup_i18n ()
408 int language = wxLANGUAGE_DEFAULT;
410 auto config_lang = Config::instance()->language ();
411 if (config_lang && !config_lang->empty ()) {
412 auto const li = wxLocale::FindLanguageInfo (std_to_wx (config_lang.get ()));
414 language = li->Language;
418 wxLocale* locale = nullptr;
419 if (wxLocale::IsAvailable (language)) {
420 locale = new wxLocale (language, wxLOCALE_LOAD_DEFAULT);
422 #ifdef DCPOMATIC_WINDOWS
423 locale->AddCatalogLookupPathPrefix (std_to_wx (mo_path().string()));
426 #ifdef DCPOMATIC_LINUX
427 locale->AddCatalogLookupPathPrefix (LINUX_LOCALE_PREFIX);
429 /* We have to include the wxWidgets .mo in our distribution,
430 so we rename it to avoid clashes with any other installation
433 locale->AddCatalog (wxT ("dcpomatic2-wxstd"));
435 /* Fedora 29 (at least) installs wxstd3.mo instead of wxstd.mo */
436 locale->AddCatalog (wxT ("wxstd3"));
439 locale->AddCatalog (wxT ("libdcpomatic2-wx"));
440 locale->AddCatalog (wxT ("dcpomatic2"));
442 if (!locale->IsOk()) {
444 locale = new wxLocale (wxLANGUAGE_ENGLISH);
449 dcpomatic_setup_gettext_i18n (wx_to_std (locale->GetCanonicalName ()));
455 wx_get (wxSpinCtrl* w)
457 return w->GetValue ();
464 return w->GetSelection ();
469 wx_get (wxSpinCtrlDouble* w)
471 return w->GetValue ();
475 /** @param s String of the form Context|String
476 * @return translation, or String if no translation is available.
479 context_translation (wxString s)
481 auto t = wxGetTranslation (s);
483 /* No translation; strip the context */
484 int c = t.Find (wxT ("|"));
485 if (c != wxNOT_FOUND) {
495 time_to_timecode (DCPTime t, double fps)
497 auto w = t.seconds ();
498 int const h = (w / 3600);
500 int const m = (w / 60);
502 int const s = floor (w);
504 int const f = lrint (w * fps);
505 return wxString::Format (wxT("%02d:%02d:%02d.%02d"), h, m, s, f);
510 setup_audio_channels_choice (wxChoice* choice, int minimum)
512 vector<pair<string, string>> items;
513 for (int i = minimum; i <= 16; i += 2) {
515 items.push_back (make_pair(wx_to_std(_("2 - stereo")), locale_convert<string>(i)));
517 items.push_back (make_pair(wx_to_std(_("4 - L/C/R/Lfe")), locale_convert<string>(i)));
519 items.push_back (make_pair(wx_to_std(_("6 - 5.1")), locale_convert<string>(i)));
521 items.push_back (make_pair(wx_to_std(_("8 - 5.1/HI/VI")), locale_convert<string>(i)));
522 } else if (i == 12) {
523 items.push_back (make_pair(wx_to_std(_("12 - 7.1/HI/VI")), locale_convert<string>(i)));
525 items.push_back (make_pair(locale_convert<string> (i), locale_convert<string>(i)));
529 checked_set (choice, items);
536 wxSplashScreen* splash = nullptr;
539 if (bitmap.LoadFile(bitmap_path("splash.png"), wxBITMAP_TYPE_PNG)) {
541 /* This wxMemoryDC must be destroyed before bitmap can be used elsewhere */
542 wxMemoryDC dc(bitmap);
543 auto const version = wxString::Format("%s (%s)", dcpomatic_version, dcpomatic_git_commit);
544 auto screen_size = dc.GetSize();
545 auto text_size = dc.GetTextExtent(version);
546 dc.DrawText(version, (screen_size.GetWidth() - text_size.GetWidth()) / 2, 236);
548 #ifdef DCPOMATIC_WINDOWS
549 /* Having wxSTAY_ON_TOP means error dialogues hide behind the splash screen on Windows, no matter what I try */
550 splash = new wxSplashScreen (bitmap, wxSPLASH_CENTRE_ON_SCREEN | wxSPLASH_NO_TIMEOUT, 0, 0, -1, wxDefaultPosition, wxDefaultSize, wxBORDER_SIMPLE | wxFRAME_NO_TASKBAR);
552 splash = new wxSplashScreen (bitmap, wxSPLASH_CENTRE_ON_SCREEN | wxSPLASH_NO_TIMEOUT, 0, 0, -1);
556 } catch (boost::filesystem::filesystem_error& e) {
557 /* Maybe we couldn't find the splash image; never mind */
565 calculate_mark_interval (double mark_interval)
567 if (mark_interval > 5) {
568 mark_interval -= lrint (mark_interval) % 5;
570 if (mark_interval > 10) {
571 mark_interval -= lrint (mark_interval) % 10;
573 if (mark_interval > 60) {
574 mark_interval -= lrint (mark_interval) % 60;
576 if (mark_interval > 3600) {
577 mark_interval -= lrint (mark_interval) % 3600;
580 if (mark_interval < 1) {
584 return mark_interval;
588 /** @return false if the task was cancelled */
590 display_progress (wxString title, wxString task)
592 auto jm = JobManager::instance ();
594 wxProgressDialog progress (title, task, 100, 0, wxPD_CAN_ABORT);
598 while (jm->work_to_do()) {
599 dcpomatic_sleep_seconds (1);
600 if (!progress.Pulse()) {
601 /* user pressed cancel */
602 for (auto i: jm->get()) {
615 get_offsets (vector<Offset>& offsets)
617 offsets.push_back (Offset(_("UTC-11"), -11, 0));
618 offsets.push_back (Offset(_("UTC-10"), -10, 0));
619 offsets.push_back (Offset(_("UTC-9"), -9, 0));
620 offsets.push_back (Offset(_("UTC-8"), -8, 0));
621 offsets.push_back (Offset(_("UTC-7"), -7, 0));
622 offsets.push_back (Offset(_("UTC-6"), -6, 0));
623 offsets.push_back (Offset(_("UTC-5"), -5, 0));
624 offsets.push_back (Offset(_("UTC-4:30"), -4, 30));
625 offsets.push_back (Offset(_("UTC-4"), -4, 0));
626 offsets.push_back (Offset(_("UTC-3:30"), -3, 30));
627 offsets.push_back (Offset(_("UTC-3"), -3, 0));
628 offsets.push_back (Offset(_("UTC-2"), -2, 0));
629 offsets.push_back (Offset(_("UTC-1"), -1, 0));
630 int utc = offsets.size();
631 offsets.push_back (Offset(_("UTC") , 0, 0));
632 offsets.push_back (Offset(_("UTC+1"), 1, 0));
633 offsets.push_back (Offset(_("UTC+2"), 2, 0));
634 offsets.push_back (Offset(_("UTC+3"), 3, 0));
635 offsets.push_back (Offset(_("UTC+4"), 4, 0));
636 offsets.push_back (Offset(_("UTC+5"), 5, 0));
637 offsets.push_back (Offset(_("UTC+5:30"), 5, 30));
638 offsets.push_back (Offset(_("UTC+6"), 6, 0));
639 offsets.push_back (Offset(_("UTC+7"), 7, 0));
640 offsets.push_back (Offset(_("UTC+8"), 8, 0));
641 offsets.push_back (Offset(_("UTC+9"), 9, 0));
642 offsets.push_back (Offset(_("UTC+9:30"), 9, 30));
643 offsets.push_back (Offset(_("UTC+10"), 10, 0));
644 offsets.push_back (Offset(_("UTC+11"), 11, 0));
645 offsets.push_back (Offset(_("UTC+12"), 12, 0));
652 bitmap_path (string name)
654 boost::filesystem::path base;
656 #ifdef DCPOMATIC_DEBUG
657 /* Hack to allow Linux and OS X to find icons when running from the source tree */
658 char* path = getenv ("DCPOMATIC_GRAPHICS");
662 base = resources_path();
665 base = resources_path();
668 auto p = base / name;
669 return std_to_wx (p.string());
674 icon_path(string name)
676 return gui_is_dark() ? bitmap_path(String::compose("%1_white.png", name)) : bitmap_path(String::compose("%1_black.png", name));
681 small_button_size (wxWindow* parent, wxString text)
683 wxClientDC dc (parent);
684 auto size = dc.GetTextExtent (text);
694 #if defined(DCPOMATIC_OSX) && wxCHECK_VERSION(3, 1, 0)
695 auto appearance = wxSystemSettings::GetAppearance();
696 return appearance.IsDark();
703 #if wxCHECK_VERSION(3,1,0)
705 dpi_scale_factor (wxWindow* window)
707 return window->GetDPIScaleFactor();
711 dpi_scale_factor (wxWindow*)
720 search_ctrl_height ()
731 report_config_load_failure(wxWindow* parent, Config::LoadFailure what)
734 case Config::LoadFailure::CONFIG:
735 message_dialog(parent, _("The existing configuration failed to load. Default values will be used instead. These may take a short time to create."));
737 case Config::LoadFailure::CINEMAS:
740 _(wxString::Format("The cinemas list for creating KDMs (cinemas.xml) failed to load. Please check the numbered backup files in %s",
741 std_to_wx(Config::instance()->cinemas_file().parent_path().string())))
744 case Config::LoadFailure::DKDM_RECIPIENTS:
747 _(wxString::Format("The recipients list for creating DKDMs (dkdm_recipients.xml) failed to load. Please check the numbered backup files in %s",
748 std_to_wx(Config::instance()->dkdm_recipients_file().parent_path().string())))