2 Copyright (C) 2012-2020 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/>.
21 /** @file src/wx/wx_util.cc
22 * @brief Some utility functions and classes.
26 #include "file_picker_ctrl.h"
27 #include "static_text.h"
28 #include "password_entry.h"
29 #include "lib/config.h"
30 #include "lib/job_manager.h"
32 #include "lib/cross.h"
34 #include "lib/warnings.h"
35 #include <dcp/locale_convert.h>
36 DCPOMATIC_DISABLE_WARNINGS
37 #include <wx/spinctrl.h>
38 #include <wx/splash.h>
39 #include <wx/progdlg.h>
40 #include <wx/filepicker.h>
42 DCPOMATIC_ENABLE_WARNINGS
43 #include <boost/thread.hpp>
48 using std::shared_ptr;
49 using boost::optional;
50 using dcp::locale_convert;
51 using namespace dcpomatic;
55 create_label (wxWindow* p, wxString t, bool left)
57 create_label (wxWindow* p, wxString t, bool)
65 return new StaticText (p, t);
72 setup_osx_flags (wxSizer* s, bool left, int& flags)
75 auto box = dynamic_cast<wxBoxSizer*>(s);
76 if (!box || box->GetOrientation() != wxHORIZONTAL) {
77 flags |= wxALIGN_RIGHT;
84 /** Add a wxStaticText to a wxSizer, aligning it at vertical centre.
85 * @param s Sizer to add to.
86 * @param p Parent window for the wxStaticText.
87 * @param t Text for the wxStaticText.
88 * @param left true if this label is a `left label'; ie the sort
89 * of label which should be right-aligned on OS X.
90 * @param prop Proportion to pass when calling Add() on the wxSizer.
93 add_label_to_sizer (wxSizer* s, wxWindow* p, wxString t, bool left, int prop, int flags)
96 setup_osx_flags (s, left, flags);
98 auto m = create_label (p, t, left);
99 s->Add (m, prop, flags, 6);
105 add_label_to_sizer (wxSizer* s, wxStaticText* t, bool left, int prop, int flags)
107 add_label_to_sizer (wxSizer* s, wxStaticText* t, bool, int prop, int flags)
111 setup_osx_flags (s, left, flags);
113 s->Add (t, prop, flags, 6);
118 add_label_to_sizer (wxGridBagSizer* s, wxWindow* p, wxString t, bool left, wxGBPosition pos, wxGBSpan span)
120 int flags = wxALIGN_CENTER_VERTICAL | wxLEFT | wxRIGHT;
122 setup_osx_flags (s, left, flags);
124 auto m = create_label (p, t, left);
125 s->Add (m, pos, span, flags);
131 add_label_to_sizer (wxGridBagSizer* s, wxStaticText* t, bool left, wxGBPosition pos, wxGBSpan span)
133 add_label_to_sizer (wxGridBagSizer* s, wxStaticText* t, bool, wxGBPosition pos, wxGBSpan span)
136 int flags = wxALIGN_CENTER_VERTICAL | wxLEFT | wxRIGHT;
138 setup_osx_flags (s, left, flags);
140 s->Add (t, pos, span, flags);
144 /** Pop up an error dialogue box.
145 * @param parent Parent.
147 * @param e Extended message.
150 error_dialog (wxWindow* parent, wxString m, optional<wxString> e)
152 auto d = new wxMessageDialog (parent, m, _("DCP-o-matic"), wxOK | wxICON_ERROR);
155 em[0] = wxToupper (em[0]);
156 d->SetExtendedMessage (em);
162 /** Pop up an error dialogue box.
163 * @param parent Parent.
167 message_dialog (wxWindow* parent, wxString m)
169 auto d = new wxMessageDialog (parent, m, _("DCP-o-matic"), wxOK | wxICON_INFORMATION);
174 /** @return true if the user answered "yes" */
176 confirm_dialog (wxWindow* parent, wxString m)
178 wxMessageDialog* d = new wxMessageDialog (parent, m, _("DCP-o-matic"), wxYES_NO | wxICON_QUESTION);
179 int const r = d->ShowModal ();
181 return r == wxID_YES;
185 /** @param s wxWidgets string.
186 * @return Corresponding STL string.
189 wx_to_std (wxString s)
191 return string (s.ToUTF8 ());
194 /** @param s STL string.
195 * @return Corresponding wxWidgets string.
200 return wxString (s.c_str(), wxConvUTF8);
204 string_client_data (wxClientData* o)
206 return wx_to_std (dynamic_cast<wxStringClientData*>(o)->GetData());
210 checked_set (FilePickerCtrl* widget, boost::filesystem::path value)
212 if (widget->GetPath() != std_to_wx (value.string())) {
214 /* Hack to make wxWidgets clear the control when we are passed
219 widget->SetPath (std_to_wx (value.string()));
224 checked_set (wxDirPickerCtrl* 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()));
238 checked_set (wxSpinCtrl* widget, int value)
240 if (widget->GetValue() != value) {
241 widget->SetValue (value);
246 checked_set (wxSpinCtrlDouble* widget, double value)
248 /* XXX: completely arbitrary epsilon */
249 if (fabs (widget->GetValue() - value) > 1e-16) {
250 widget->SetValue (value);
255 checked_set (wxChoice* widget, int value)
257 if (widget->GetSelection() != value) {
258 widget->SetSelection (value);
263 checked_set (wxChoice* widget, string value)
265 wxClientData* o = nullptr;
266 if (widget->GetSelection() != -1) {
267 o = widget->GetClientObject (widget->GetSelection ());
270 if (!o || string_client_data(o) != value) {
271 for (unsigned int i = 0; i < widget->GetCount(); ++i) {
272 if (string_client_data (widget->GetClientObject (i)) == value) {
273 widget->SetSelection (i);
280 checked_set (wxChoice* widget, vector<pair<string, string> > items)
282 vector<pair<string, string> > current;
283 for (unsigned int i = 0; i < widget->GetCount(); ++i) {
286 wx_to_std (widget->GetString (i)),
287 string_client_data (widget->GetClientObject (i))
292 if (current == items) {
297 for (auto i: items) {
298 widget->Append (std_to_wx(i.first), new wxStringClientData(std_to_wx(i.second)));
303 checked_set (wxTextCtrl* widget, string value)
305 if (widget->GetValue() != std_to_wx (value)) {
306 widget->ChangeValue (std_to_wx (value));
311 checked_set (PasswordEntry* entry, string value)
313 if (entry->get() != value) {
319 checked_set (wxTextCtrl* widget, wxString value)
321 if (widget->GetValue() != value) {
322 widget->ChangeValue (value);
327 checked_set (wxStaticText* widget, string value)
329 if (widget->GetLabel() != std_to_wx (value)) {
330 widget->SetLabel (std_to_wx (value));
335 checked_set (wxStaticText* widget, wxString value)
337 if (widget->GetLabel() != value) {
338 widget->SetLabel (value);
343 checked_set (wxCheckBox* widget, bool value)
345 if (widget->GetValue() != value) {
346 widget->SetValue (value);
351 checked_set (wxRadioButton* widget, bool value)
353 if (widget->GetValue() != value) {
354 widget->SetValue (value);
359 dcpomatic_setup_i18n ()
361 int language = wxLANGUAGE_DEFAULT;
363 auto config_lang = Config::instance()->language ();
364 if (config_lang && !config_lang->empty ()) {
365 auto const li = wxLocale::FindLanguageInfo (std_to_wx (config_lang.get ()));
367 language = li->Language;
371 wxLocale* locale = nullptr;
372 if (wxLocale::IsAvailable (language)) {
373 locale = new wxLocale (language, wxLOCALE_LOAD_DEFAULT);
375 #ifdef DCPOMATIC_WINDOWS
376 locale->AddCatalogLookupPathPrefix (std_to_wx (mo_path().string()));
379 #ifdef DCPOMATIC_LINUX
380 locale->AddCatalogLookupPathPrefix (LINUX_LOCALE_PREFIX);
382 /* We have to include the wxWidgets .mo in our distribution,
383 so we rename it to avoid clashes with any other installation
386 locale->AddCatalog (wxT ("dcpomatic2-wxstd"));
388 /* Fedora 29 (at least) installs wxstd3.mo instead of wxstd.mo */
389 locale->AddCatalog (wxT ("wxstd3"));
392 locale->AddCatalog (wxT ("libdcpomatic2-wx"));
393 locale->AddCatalog (wxT ("dcpomatic2"));
395 if (!locale->IsOk()) {
397 locale = new wxLocale (wxLANGUAGE_ENGLISH);
402 dcpomatic_setup_gettext_i18n (wx_to_std (locale->GetCanonicalName ()));
407 wx_get (wxSpinCtrl* w)
409 return w->GetValue ();
415 return w->GetSelection ();
419 wx_get (wxSpinCtrlDouble* w)
421 return w->GetValue ();
424 /** @param s String of the form Context|String
425 * @return translation, or String if no translation is available.
428 context_translation (wxString s)
430 auto t = wxGetTranslation (s);
432 /* No translation; strip the context */
433 int c = t.Find (wxT ("|"));
434 if (c != wxNOT_FOUND) {
443 time_to_timecode (DCPTime t, double fps)
445 auto w = t.seconds ();
446 int const h = (w / 3600);
448 int const m = (w / 60);
450 int const s = floor (w);
452 int const f = lrint (w * fps);
453 return wxString::Format (wxT("%02d:%02d:%02d.%02d"), h, m, s, f);
457 setup_audio_channels_choice (wxChoice* choice, int minimum)
459 vector<pair<string, string> > items;
460 for (int i = minimum; i <= 16; i += 2) {
462 items.push_back (make_pair (wx_to_std (_("2 - stereo")), locale_convert<string> (i)));
464 items.push_back (make_pair (wx_to_std (_("4 - L/C/R/Lfe")), locale_convert<string> (i)));
466 items.push_back (make_pair (wx_to_std (_("6 - 5.1")), locale_convert<string> (i)));
468 items.push_back (make_pair (wx_to_std (_("8 - 5.1/HI/VI")), locale_convert<string> (i)));
469 } else if (i == 12) {
470 items.push_back (make_pair (wx_to_std (_("12 - 7.1/HI/VI")), locale_convert<string> (i)));
472 items.push_back (make_pair (locale_convert<string> (i), locale_convert<string> (i)));
476 checked_set (choice, items);
483 wxSplashScreen* splash = nullptr;
486 if (bitmap.LoadFile(bitmap_path("splash"), wxBITMAP_TYPE_PNG)) {
487 #ifdef DCPOMATIC_WINDOWS
488 /* Having wxSTAY_ON_TOP means error dialogues hide behind the splash screen on Windows, no matter what I try */
489 splash = new wxSplashScreen (bitmap, wxSPLASH_CENTRE_ON_SCREEN | wxSPLASH_NO_TIMEOUT, 0, 0, -1, wxDefaultPosition, wxDefaultSize, wxBORDER_SIMPLE | wxFRAME_NO_TASKBAR);
491 splash = new wxSplashScreen (bitmap, wxSPLASH_CENTRE_ON_SCREEN | wxSPLASH_NO_TIMEOUT, 0, 0, -1);
495 } catch (boost::filesystem::filesystem_error& e) {
496 /* Maybe we couldn't find the splash image; never mind */
503 calculate_mark_interval (double mark_interval)
505 if (mark_interval > 5) {
506 mark_interval -= lrint (mark_interval) % 5;
508 if (mark_interval > 10) {
509 mark_interval -= lrint (mark_interval) % 10;
511 if (mark_interval > 60) {
512 mark_interval -= lrint (mark_interval) % 60;
514 if (mark_interval > 3600) {
515 mark_interval -= lrint (mark_interval) % 3600;
518 if (mark_interval < 1) {
522 return mark_interval;
526 /** @return false if the task was cancelled */
528 display_progress (wxString title, wxString task)
530 auto jm = JobManager::instance ();
532 wxProgressDialog progress (title, task, 100, 0, wxPD_CAN_ABORT);
536 while (jm->work_to_do()) {
537 dcpomatic_sleep_seconds (1);
538 if (!progress.Pulse()) {
539 /* user pressed cancel */
540 for (auto i: jm->get()) {
553 get_offsets (vector<Offset>& offsets)
555 offsets.push_back (Offset(_("UTC-11"), -11, 0));
556 offsets.push_back (Offset(_("UTC-10"), -10, 0));
557 offsets.push_back (Offset(_("UTC-9"), -9, 0));
558 offsets.push_back (Offset(_("UTC-8"), -8, 0));
559 offsets.push_back (Offset(_("UTC-7"), -7, 0));
560 offsets.push_back (Offset(_("UTC-6"), -6, 0));
561 offsets.push_back (Offset(_("UTC-5"), -5, 0));
562 offsets.push_back (Offset(_("UTC-4:30"), -4, 30));
563 offsets.push_back (Offset(_("UTC-4"), -4, 0));
564 offsets.push_back (Offset(_("UTC-3:30"), -3, 30));
565 offsets.push_back (Offset(_("UTC-3"), -3, 0));
566 offsets.push_back (Offset(_("UTC-2"), -2, 0));
567 offsets.push_back (Offset(_("UTC-1"), -1, 0));
568 int utc = offsets.size();
569 offsets.push_back (Offset(_("UTC") , 0, 0));
570 offsets.push_back (Offset(_("UTC+1"), 1, 0));
571 offsets.push_back (Offset(_("UTC+2"), 2, 0));
572 offsets.push_back (Offset(_("UTC+3"), 3, 0));
573 offsets.push_back (Offset(_("UTC+4"), 4, 0));
574 offsets.push_back (Offset(_("UTC+5"), 5, 0));
575 offsets.push_back (Offset(_("UTC+5:30"), 5, 30));
576 offsets.push_back (Offset(_("UTC+6"), 6, 0));
577 offsets.push_back (Offset(_("UTC+7"), 7, 0));
578 offsets.push_back (Offset(_("UTC+8"), 8, 0));
579 offsets.push_back (Offset(_("UTC+9"), 9, 0));
580 offsets.push_back (Offset(_("UTC+9:30"), 9, 30));
581 offsets.push_back (Offset(_("UTC+10"), 10, 0));
582 offsets.push_back (Offset(_("UTC+11"), 11, 0));
583 offsets.push_back (Offset(_("UTC+12"), 12, 0));
590 bitmap_path (string name)
592 boost::filesystem::path base;
594 #ifdef DCPOMATIC_DEBUG
595 /* Hack to allow Linux and OS X to find icons when running from the source tree */
596 char* path = getenv ("DCPOMATIC_GRAPHICS");
600 base = resources_path();
603 base = resources_path();
606 boost::filesystem::path p = base / String::compose("%1.png", name);
607 return std_to_wx (p.string());
612 small_button_size (wxWindow* parent, wxString text)
614 wxClientDC dc (parent);
615 auto size = dc.GetTextExtent (text);