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 "lib/warnings.h"
38 #include <dcp/locale_convert.h>
39 DCPOMATIC_DISABLE_WARNINGS
40 #include <wx/spinctrl.h>
41 #include <wx/splash.h>
42 #include <wx/progdlg.h>
43 #include <wx/filepicker.h>
45 DCPOMATIC_ENABLE_WARNINGS
46 #include <boost/thread.hpp>
52 using std::shared_ptr;
53 using boost::optional;
54 using dcp::locale_convert;
55 using namespace dcpomatic;
60 create_label (wxWindow* p, wxString t, bool left)
62 create_label (wxWindow* p, wxString t, bool)
70 return new StaticText (p, t);
77 setup_osx_flags (wxSizer* s, bool left, int& flags)
80 auto box = dynamic_cast<wxBoxSizer*>(s);
81 if (!box || box->GetOrientation() != wxHORIZONTAL) {
82 flags |= wxALIGN_RIGHT;
89 /** Add a wxStaticText to a wxSizer, aligning it at vertical centre.
90 * @param s Sizer to add to.
91 * @param p Parent window for the wxStaticText.
92 * @param t Text for the wxStaticText.
93 * @param left true if this label is a `left label'; ie the sort
94 * of label which should be right-aligned on OS X.
95 * @param prop Proportion to pass when calling Add() on the wxSizer.
98 add_label_to_sizer (wxSizer* s, wxWindow* p, wxString t, bool left, int prop, int flags)
101 setup_osx_flags (s, left, flags);
103 auto m = create_label (p, t, left);
104 s->Add (m, prop, flags, 6);
111 add_label_to_sizer (wxSizer* s, wxStaticText* t, bool left, int prop, int flags)
113 add_label_to_sizer (wxSizer* s, wxStaticText* t, bool, int prop, int flags)
117 setup_osx_flags (s, left, flags);
119 s->Add (t, prop, flags, 6);
125 add_label_to_sizer (wxGridBagSizer* s, wxWindow* p, wxString t, bool left, wxGBPosition pos, wxGBSpan span)
127 int flags = wxALIGN_CENTER_VERTICAL | wxLEFT | wxRIGHT;
129 setup_osx_flags (s, left, flags);
131 auto m = create_label (p, t, left);
132 s->Add (m, pos, span, flags);
139 add_label_to_sizer (wxGridBagSizer* s, wxStaticText* t, bool left, wxGBPosition pos, wxGBSpan span)
141 add_label_to_sizer (wxGridBagSizer* s, wxStaticText* t, bool, wxGBPosition pos, wxGBSpan span)
144 int flags = wxALIGN_CENTER_VERTICAL | wxLEFT | wxRIGHT;
146 setup_osx_flags (s, left, flags);
148 s->Add (t, pos, span, flags);
153 /** Pop up an error dialogue box.
154 * @param parent Parent.
156 * @param e Extended message.
159 error_dialog (wxWindow* parent, wxString m, optional<wxString> e)
161 auto d = new wxMessageDialog (parent, m, _("DCP-o-matic"), wxOK | wxICON_ERROR);
164 em[0] = wxToupper (em[0]);
165 d->SetExtendedMessage (em);
172 /** Pop up an error dialogue box.
173 * @param parent Parent.
177 message_dialog (wxWindow* parent, wxString m)
179 auto d = new wxMessageDialog (parent, m, _("DCP-o-matic"), wxOK | wxICON_INFORMATION);
185 /** @return true if the user answered "yes" */
187 confirm_dialog (wxWindow* parent, wxString m)
189 auto d = new wxMessageDialog (parent, m, _("DCP-o-matic"), wxYES_NO | wxICON_QUESTION);
190 int const r = d->ShowModal ();
192 return r == wxID_YES;
196 /** @param s wxWidgets string.
197 * @return Corresponding STL string.
200 wx_to_std (wxString s)
202 return string (s.ToUTF8());
206 /** @param s STL string.
207 * @return Corresponding wxWidgets string.
212 return wxString (s.c_str(), wxConvUTF8);
217 string_client_data (wxClientData* o)
219 return wx_to_std (dynamic_cast<wxStringClientData*>(o)->GetData());
224 checked_set (FilePickerCtrl* widget, boost::filesystem::path value)
226 if (widget->GetPath() != std_to_wx (value.string())) {
228 /* Hack to make wxWidgets clear the control when we are passed
233 widget->SetPath (std_to_wx (value.string()));
239 checked_set (wxDirPickerCtrl* widget, boost::filesystem::path value)
241 if (widget->GetPath() != std_to_wx (value.string())) {
243 /* Hack to make wxWidgets clear the control when we are passed
248 widget->SetPath (std_to_wx (value.string()));
254 checked_set (wxSpinCtrl* widget, int value)
256 if (widget->GetValue() != value) {
257 widget->SetValue (value);
263 checked_set (wxSpinCtrlDouble* widget, double value)
265 /* XXX: completely arbitrary epsilon */
266 if (fabs (widget->GetValue() - value) > 1e-16) {
267 widget->SetValue (value);
273 checked_set (wxChoice* widget, int value)
275 if (widget->GetSelection() != value) {
276 widget->SetSelection (value);
282 checked_set (wxChoice* widget, string value)
284 wxClientData* o = nullptr;
285 if (widget->GetSelection() != -1) {
286 o = widget->GetClientObject (widget->GetSelection ());
289 if (!o || string_client_data(o) != value) {
290 for (unsigned int i = 0; i < widget->GetCount(); ++i) {
291 if (string_client_data (widget->GetClientObject (i)) == value) {
292 widget->SetSelection (i);
300 checked_set (wxChoice* widget, vector<pair<string, string> > items)
302 vector<pair<string, string>> current;
303 for (unsigned int i = 0; i < widget->GetCount(); ++i) {
306 wx_to_std(widget->GetString(i)),
307 string_client_data(widget->GetClientObject(i))
312 if (current == items) {
317 for (auto i: items) {
318 widget->Append (std_to_wx(i.first), new wxStringClientData(std_to_wx(i.second)));
324 checked_set (wxTextCtrl* widget, string value)
326 if (widget->GetValue() != std_to_wx (value)) {
327 widget->ChangeValue (std_to_wx (value));
333 checked_set (PasswordEntry* entry, string value)
335 if (entry->get() != value) {
342 checked_set (wxTextCtrl* widget, wxString value)
344 if (widget->GetValue() != value) {
345 widget->ChangeValue (value);
351 checked_set (wxStaticText* widget, string value)
353 if (widget->GetLabel() != std_to_wx (value)) {
354 widget->SetLabel (std_to_wx (value));
360 checked_set (wxStaticText* widget, wxString value)
362 if (widget->GetLabel() != value) {
363 widget->SetLabel (value);
369 checked_set (wxCheckBox* widget, bool value)
371 if (widget->GetValue() != value) {
372 widget->SetValue (value);
378 checked_set (wxRadioButton* widget, bool value)
380 if (widget->GetValue() != value) {
381 widget->SetValue (value);
387 dcpomatic_setup_i18n ()
389 int language = wxLANGUAGE_DEFAULT;
391 auto config_lang = Config::instance()->language ();
392 if (config_lang && !config_lang->empty ()) {
393 auto const li = wxLocale::FindLanguageInfo (std_to_wx (config_lang.get ()));
395 language = li->Language;
399 wxLocale* locale = nullptr;
400 if (wxLocale::IsAvailable (language)) {
401 locale = new wxLocale (language, wxLOCALE_LOAD_DEFAULT);
403 #ifdef DCPOMATIC_WINDOWS
404 locale->AddCatalogLookupPathPrefix (std_to_wx (mo_path().string()));
407 #ifdef DCPOMATIC_LINUX
408 locale->AddCatalogLookupPathPrefix (LINUX_LOCALE_PREFIX);
410 /* We have to include the wxWidgets .mo in our distribution,
411 so we rename it to avoid clashes with any other installation
414 locale->AddCatalog (wxT ("dcpomatic2-wxstd"));
416 /* Fedora 29 (at least) installs wxstd3.mo instead of wxstd.mo */
417 locale->AddCatalog (wxT ("wxstd3"));
420 locale->AddCatalog (wxT ("libdcpomatic2-wx"));
421 locale->AddCatalog (wxT ("dcpomatic2"));
423 if (!locale->IsOk()) {
425 locale = new wxLocale (wxLANGUAGE_ENGLISH);
430 dcpomatic_setup_gettext_i18n (wx_to_std (locale->GetCanonicalName ()));
436 wx_get (wxSpinCtrl* w)
438 return w->GetValue ();
445 return w->GetSelection ();
450 wx_get (wxSpinCtrlDouble* w)
452 return w->GetValue ();
456 /** @param s String of the form Context|String
457 * @return translation, or String if no translation is available.
460 context_translation (wxString s)
462 auto t = wxGetTranslation (s);
464 /* No translation; strip the context */
465 int c = t.Find (wxT ("|"));
466 if (c != wxNOT_FOUND) {
476 time_to_timecode (DCPTime t, double fps)
478 auto w = t.seconds ();
479 int const h = (w / 3600);
481 int const m = (w / 60);
483 int const s = floor (w);
485 int const f = lrint (w * fps);
486 return wxString::Format (wxT("%02d:%02d:%02d.%02d"), h, m, s, f);
491 setup_audio_channels_choice (wxChoice* choice, int minimum)
493 vector<pair<string, string>> items;
494 for (int i = minimum; i <= 16; i += 2) {
496 items.push_back (make_pair(wx_to_std(_("2 - stereo")), locale_convert<string>(i)));
498 items.push_back (make_pair(wx_to_std(_("4 - L/C/R/Lfe")), locale_convert<string>(i)));
500 items.push_back (make_pair(wx_to_std(_("6 - 5.1")), locale_convert<string>(i)));
502 items.push_back (make_pair(wx_to_std(_("8 - 5.1/HI/VI")), locale_convert<string>(i)));
503 } else if (i == 12) {
504 items.push_back (make_pair(wx_to_std(_("12 - 7.1/HI/VI")), locale_convert<string>(i)));
506 items.push_back (make_pair(locale_convert<string> (i), locale_convert<string>(i)));
510 checked_set (choice, items);
517 wxSplashScreen* splash = nullptr;
520 if (bitmap.LoadFile(bitmap_path("splash"), wxBITMAP_TYPE_PNG)) {
522 /* This wxMemoryDC must be destroyed before bitmap can be used elsewhere */
523 wxMemoryDC dc(bitmap);
524 auto const version = wxString::Format("%s (%s)", dcpomatic_version, dcpomatic_git_commit);
525 auto screen_size = dc.GetSize();
526 auto text_size = dc.GetTextExtent(version);
527 dc.DrawText(version, (screen_size.GetWidth() - text_size.GetWidth()) / 2, 236);
529 #ifdef DCPOMATIC_WINDOWS
530 /* Having wxSTAY_ON_TOP means error dialogues hide behind the splash screen on Windows, no matter what I try */
531 splash = new wxSplashScreen (bitmap, wxSPLASH_CENTRE_ON_SCREEN | wxSPLASH_NO_TIMEOUT, 0, 0, -1, wxDefaultPosition, wxDefaultSize, wxBORDER_SIMPLE | wxFRAME_NO_TASKBAR);
533 splash = new wxSplashScreen (bitmap, wxSPLASH_CENTRE_ON_SCREEN | wxSPLASH_NO_TIMEOUT, 0, 0, -1);
537 } catch (boost::filesystem::filesystem_error& e) {
538 /* Maybe we couldn't find the splash image; never mind */
546 calculate_mark_interval (double mark_interval)
548 if (mark_interval > 5) {
549 mark_interval -= lrint (mark_interval) % 5;
551 if (mark_interval > 10) {
552 mark_interval -= lrint (mark_interval) % 10;
554 if (mark_interval > 60) {
555 mark_interval -= lrint (mark_interval) % 60;
557 if (mark_interval > 3600) {
558 mark_interval -= lrint (mark_interval) % 3600;
561 if (mark_interval < 1) {
565 return mark_interval;
569 /** @return false if the task was cancelled */
571 display_progress (wxString title, wxString task)
573 auto jm = JobManager::instance ();
575 wxProgressDialog progress (title, task, 100, 0, wxPD_CAN_ABORT);
579 while (jm->work_to_do()) {
580 dcpomatic_sleep_seconds (1);
581 if (!progress.Pulse()) {
582 /* user pressed cancel */
583 for (auto i: jm->get()) {
596 get_offsets (vector<Offset>& offsets)
598 offsets.push_back (Offset(_("UTC-11"), -11, 0));
599 offsets.push_back (Offset(_("UTC-10"), -10, 0));
600 offsets.push_back (Offset(_("UTC-9"), -9, 0));
601 offsets.push_back (Offset(_("UTC-8"), -8, 0));
602 offsets.push_back (Offset(_("UTC-7"), -7, 0));
603 offsets.push_back (Offset(_("UTC-6"), -6, 0));
604 offsets.push_back (Offset(_("UTC-5"), -5, 0));
605 offsets.push_back (Offset(_("UTC-4:30"), -4, 30));
606 offsets.push_back (Offset(_("UTC-4"), -4, 0));
607 offsets.push_back (Offset(_("UTC-3:30"), -3, 30));
608 offsets.push_back (Offset(_("UTC-3"), -3, 0));
609 offsets.push_back (Offset(_("UTC-2"), -2, 0));
610 offsets.push_back (Offset(_("UTC-1"), -1, 0));
611 int utc = offsets.size();
612 offsets.push_back (Offset(_("UTC") , 0, 0));
613 offsets.push_back (Offset(_("UTC+1"), 1, 0));
614 offsets.push_back (Offset(_("UTC+2"), 2, 0));
615 offsets.push_back (Offset(_("UTC+3"), 3, 0));
616 offsets.push_back (Offset(_("UTC+4"), 4, 0));
617 offsets.push_back (Offset(_("UTC+5"), 5, 0));
618 offsets.push_back (Offset(_("UTC+5:30"), 5, 30));
619 offsets.push_back (Offset(_("UTC+6"), 6, 0));
620 offsets.push_back (Offset(_("UTC+7"), 7, 0));
621 offsets.push_back (Offset(_("UTC+8"), 8, 0));
622 offsets.push_back (Offset(_("UTC+9"), 9, 0));
623 offsets.push_back (Offset(_("UTC+9:30"), 9, 30));
624 offsets.push_back (Offset(_("UTC+10"), 10, 0));
625 offsets.push_back (Offset(_("UTC+11"), 11, 0));
626 offsets.push_back (Offset(_("UTC+12"), 12, 0));
633 bitmap_path (string name)
635 boost::filesystem::path base;
637 #ifdef DCPOMATIC_DEBUG
638 /* Hack to allow Linux and OS X to find icons when running from the source tree */
639 char* path = getenv ("DCPOMATIC_GRAPHICS");
643 base = resources_path();
646 base = resources_path();
649 auto p = base / dcp::compose("%1.png", name);
650 return std_to_wx (p.string());
655 small_button_size (wxWindow* parent, wxString text)
657 wxClientDC dc (parent);
658 auto size = dc.GetTextExtent (text);
668 #if defined(DCPOMATIC_OSX) && wxCHECK_VERSION(3, 1, 0)
669 auto appearance = wxSystemSettings::GetAppearance();
670 return appearance.IsDark();