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/>.
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);
110 add_label_to_sizer (wxSizer* s, wxStaticText* t, bool left, int prop, int flags)
112 add_label_to_sizer (wxSizer* s, wxStaticText* t, bool, int prop, int flags)
116 setup_osx_flags (s, left, flags);
118 s->Add (t, prop, flags, 6);
123 add_label_to_sizer (wxGridBagSizer* s, wxWindow* p, wxString t, bool left, wxGBPosition pos, wxGBSpan span)
125 int flags = wxALIGN_CENTER_VERTICAL | wxLEFT | wxRIGHT;
127 setup_osx_flags (s, left, flags);
129 auto m = create_label (p, t, left);
130 s->Add (m, pos, span, flags);
136 add_label_to_sizer (wxGridBagSizer* s, wxStaticText* t, bool left, wxGBPosition pos, wxGBSpan span)
138 add_label_to_sizer (wxGridBagSizer* s, wxStaticText* t, bool, wxGBPosition pos, wxGBSpan span)
141 int flags = wxALIGN_CENTER_VERTICAL | wxLEFT | wxRIGHT;
143 setup_osx_flags (s, left, flags);
145 s->Add (t, pos, span, flags);
149 /** Pop up an error dialogue box.
150 * @param parent Parent.
152 * @param e Extended message.
155 error_dialog (wxWindow* parent, wxString m, optional<wxString> e)
157 auto d = new wxMessageDialog (parent, m, _("DCP-o-matic"), wxOK | wxICON_ERROR);
160 em[0] = wxToupper (em[0]);
161 d->SetExtendedMessage (em);
167 /** Pop up an error dialogue box.
168 * @param parent Parent.
172 message_dialog (wxWindow* parent, wxString m)
174 auto d = new wxMessageDialog (parent, m, _("DCP-o-matic"), wxOK | wxICON_INFORMATION);
179 /** @return true if the user answered "yes" */
181 confirm_dialog (wxWindow* parent, wxString m)
183 wxMessageDialog* d = new wxMessageDialog (parent, m, _("DCP-o-matic"), wxYES_NO | wxICON_QUESTION);
184 int const r = d->ShowModal ();
186 return r == wxID_YES;
190 /** @param s wxWidgets string.
191 * @return Corresponding STL string.
194 wx_to_std (wxString s)
196 return string (s.ToUTF8 ());
199 /** @param s STL string.
200 * @return Corresponding wxWidgets string.
205 return wxString (s.c_str(), wxConvUTF8);
209 string_client_data (wxClientData* o)
211 return wx_to_std (dynamic_cast<wxStringClientData*>(o)->GetData());
215 checked_set (FilePickerCtrl* widget, boost::filesystem::path value)
217 if (widget->GetPath() != std_to_wx (value.string())) {
219 /* Hack to make wxWidgets clear the control when we are passed
224 widget->SetPath (std_to_wx (value.string()));
229 checked_set (wxDirPickerCtrl* widget, boost::filesystem::path value)
231 if (widget->GetPath() != std_to_wx (value.string())) {
233 /* Hack to make wxWidgets clear the control when we are passed
238 widget->SetPath (std_to_wx (value.string()));
243 checked_set (wxSpinCtrl* widget, int value)
245 if (widget->GetValue() != value) {
246 widget->SetValue (value);
251 checked_set (wxSpinCtrlDouble* widget, double value)
253 /* XXX: completely arbitrary epsilon */
254 if (fabs (widget->GetValue() - value) > 1e-16) {
255 widget->SetValue (value);
260 checked_set (wxChoice* widget, int value)
262 if (widget->GetSelection() != value) {
263 widget->SetSelection (value);
268 checked_set (wxChoice* widget, string value)
270 wxClientData* o = nullptr;
271 if (widget->GetSelection() != -1) {
272 o = widget->GetClientObject (widget->GetSelection ());
275 if (!o || string_client_data(o) != value) {
276 for (unsigned int i = 0; i < widget->GetCount(); ++i) {
277 if (string_client_data (widget->GetClientObject (i)) == value) {
278 widget->SetSelection (i);
285 checked_set (wxChoice* widget, vector<pair<string, string> > items)
287 vector<pair<string, string> > current;
288 for (unsigned int i = 0; i < widget->GetCount(); ++i) {
291 wx_to_std (widget->GetString (i)),
292 string_client_data (widget->GetClientObject (i))
297 if (current == items) {
302 for (auto i: items) {
303 widget->Append (std_to_wx(i.first), new wxStringClientData(std_to_wx(i.second)));
308 checked_set (wxTextCtrl* widget, string value)
310 if (widget->GetValue() != std_to_wx (value)) {
311 widget->ChangeValue (std_to_wx (value));
316 checked_set (PasswordEntry* entry, string value)
318 if (entry->get() != value) {
324 checked_set (wxTextCtrl* widget, wxString value)
326 if (widget->GetValue() != value) {
327 widget->ChangeValue (value);
332 checked_set (wxStaticText* widget, string value)
334 if (widget->GetLabel() != std_to_wx (value)) {
335 widget->SetLabel (std_to_wx (value));
340 checked_set (wxStaticText* widget, wxString value)
342 if (widget->GetLabel() != value) {
343 widget->SetLabel (value);
348 checked_set (wxCheckBox* widget, bool value)
350 if (widget->GetValue() != value) {
351 widget->SetValue (value);
356 checked_set (wxRadioButton* widget, bool value)
358 if (widget->GetValue() != value) {
359 widget->SetValue (value);
364 dcpomatic_setup_i18n ()
366 int language = wxLANGUAGE_DEFAULT;
368 auto config_lang = Config::instance()->language ();
369 if (config_lang && !config_lang->empty ()) {
370 auto const li = wxLocale::FindLanguageInfo (std_to_wx (config_lang.get ()));
372 language = li->Language;
376 wxLocale* locale = nullptr;
377 if (wxLocale::IsAvailable (language)) {
378 locale = new wxLocale (language, wxLOCALE_LOAD_DEFAULT);
380 #ifdef DCPOMATIC_WINDOWS
381 locale->AddCatalogLookupPathPrefix (std_to_wx (mo_path().string()));
384 #ifdef DCPOMATIC_LINUX
385 locale->AddCatalogLookupPathPrefix (LINUX_LOCALE_PREFIX);
387 /* We have to include the wxWidgets .mo in our distribution,
388 so we rename it to avoid clashes with any other installation
391 locale->AddCatalog (wxT ("dcpomatic2-wxstd"));
393 /* Fedora 29 (at least) installs wxstd3.mo instead of wxstd.mo */
394 locale->AddCatalog (wxT ("wxstd3"));
397 locale->AddCatalog (wxT ("libdcpomatic2-wx"));
398 locale->AddCatalog (wxT ("dcpomatic2"));
400 if (!locale->IsOk()) {
402 locale = new wxLocale (wxLANGUAGE_ENGLISH);
407 dcpomatic_setup_gettext_i18n (wx_to_std (locale->GetCanonicalName ()));
412 wx_get (wxSpinCtrl* w)
414 return w->GetValue ();
420 return w->GetSelection ();
424 wx_get (wxSpinCtrlDouble* w)
426 return w->GetValue ();
429 /** @param s String of the form Context|String
430 * @return translation, or String if no translation is available.
433 context_translation (wxString s)
435 auto t = wxGetTranslation (s);
437 /* No translation; strip the context */
438 int c = t.Find (wxT ("|"));
439 if (c != wxNOT_FOUND) {
448 time_to_timecode (DCPTime t, double fps)
450 auto w = t.seconds ();
451 int const h = (w / 3600);
453 int const m = (w / 60);
455 int const s = floor (w);
457 int const f = lrint (w * fps);
458 return wxString::Format (wxT("%02d:%02d:%02d.%02d"), h, m, s, f);
462 setup_audio_channels_choice (wxChoice* choice, int minimum)
464 vector<pair<string, string> > items;
465 for (int i = minimum; i <= 16; i += 2) {
467 items.push_back (make_pair (wx_to_std (_("2 - stereo")), locale_convert<string> (i)));
469 items.push_back (make_pair (wx_to_std (_("4 - L/C/R/Lfe")), locale_convert<string> (i)));
471 items.push_back (make_pair (wx_to_std (_("6 - 5.1")), locale_convert<string> (i)));
473 items.push_back (make_pair (wx_to_std (_("8 - 5.1/HI/VI")), locale_convert<string> (i)));
474 } else if (i == 12) {
475 items.push_back (make_pair (wx_to_std (_("12 - 7.1/HI/VI")), locale_convert<string> (i)));
477 items.push_back (make_pair (locale_convert<string> (i), locale_convert<string> (i)));
481 checked_set (choice, items);
488 wxSplashScreen* splash = nullptr;
491 if (bitmap.LoadFile(bitmap_path("splash"), wxBITMAP_TYPE_PNG)) {
493 /* This wxMemoryDC must be destroyed before bitmap can be used elsewhere */
494 wxMemoryDC dc(bitmap);
495 auto const version = wxString::Format("%s (%s)", dcpomatic_version, dcpomatic_git_commit);
496 auto screen_size = dc.GetSize();
497 auto text_size = dc.GetTextExtent(version);
498 dc.DrawText(version, (screen_size.GetWidth() - text_size.GetWidth()) / 2, 236);
500 #ifdef DCPOMATIC_WINDOWS
501 /* Having wxSTAY_ON_TOP means error dialogues hide behind the splash screen on Windows, no matter what I try */
502 splash = new wxSplashScreen (bitmap, wxSPLASH_CENTRE_ON_SCREEN | wxSPLASH_NO_TIMEOUT, 0, 0, -1, wxDefaultPosition, wxDefaultSize, wxBORDER_SIMPLE | wxFRAME_NO_TASKBAR);
504 splash = new wxSplashScreen (bitmap, wxSPLASH_CENTRE_ON_SCREEN | wxSPLASH_NO_TIMEOUT, 0, 0, -1);
508 } catch (boost::filesystem::filesystem_error& e) {
509 /* Maybe we couldn't find the splash image; never mind */
516 calculate_mark_interval (double mark_interval)
518 if (mark_interval > 5) {
519 mark_interval -= lrint (mark_interval) % 5;
521 if (mark_interval > 10) {
522 mark_interval -= lrint (mark_interval) % 10;
524 if (mark_interval > 60) {
525 mark_interval -= lrint (mark_interval) % 60;
527 if (mark_interval > 3600) {
528 mark_interval -= lrint (mark_interval) % 3600;
531 if (mark_interval < 1) {
535 return mark_interval;
539 /** @return false if the task was cancelled */
541 display_progress (wxString title, wxString task)
543 auto jm = JobManager::instance ();
545 wxProgressDialog progress (title, task, 100, 0, wxPD_CAN_ABORT);
549 while (jm->work_to_do()) {
550 dcpomatic_sleep_seconds (1);
551 if (!progress.Pulse()) {
552 /* user pressed cancel */
553 for (auto i: jm->get()) {
566 get_offsets (vector<Offset>& offsets)
568 offsets.push_back (Offset(_("UTC-11"), -11, 0));
569 offsets.push_back (Offset(_("UTC-10"), -10, 0));
570 offsets.push_back (Offset(_("UTC-9"), -9, 0));
571 offsets.push_back (Offset(_("UTC-8"), -8, 0));
572 offsets.push_back (Offset(_("UTC-7"), -7, 0));
573 offsets.push_back (Offset(_("UTC-6"), -6, 0));
574 offsets.push_back (Offset(_("UTC-5"), -5, 0));
575 offsets.push_back (Offset(_("UTC-4:30"), -4, 30));
576 offsets.push_back (Offset(_("UTC-4"), -4, 0));
577 offsets.push_back (Offset(_("UTC-3:30"), -3, 30));
578 offsets.push_back (Offset(_("UTC-3"), -3, 0));
579 offsets.push_back (Offset(_("UTC-2"), -2, 0));
580 offsets.push_back (Offset(_("UTC-1"), -1, 0));
581 int utc = offsets.size();
582 offsets.push_back (Offset(_("UTC") , 0, 0));
583 offsets.push_back (Offset(_("UTC+1"), 1, 0));
584 offsets.push_back (Offset(_("UTC+2"), 2, 0));
585 offsets.push_back (Offset(_("UTC+3"), 3, 0));
586 offsets.push_back (Offset(_("UTC+4"), 4, 0));
587 offsets.push_back (Offset(_("UTC+5"), 5, 0));
588 offsets.push_back (Offset(_("UTC+5:30"), 5, 30));
589 offsets.push_back (Offset(_("UTC+6"), 6, 0));
590 offsets.push_back (Offset(_("UTC+7"), 7, 0));
591 offsets.push_back (Offset(_("UTC+8"), 8, 0));
592 offsets.push_back (Offset(_("UTC+9"), 9, 0));
593 offsets.push_back (Offset(_("UTC+9:30"), 9, 30));
594 offsets.push_back (Offset(_("UTC+10"), 10, 0));
595 offsets.push_back (Offset(_("UTC+11"), 11, 0));
596 offsets.push_back (Offset(_("UTC+12"), 12, 0));
603 bitmap_path (string name)
605 boost::filesystem::path base;
607 #ifdef DCPOMATIC_DEBUG
608 /* Hack to allow Linux and OS X to find icons when running from the source tree */
609 char* path = getenv ("DCPOMATIC_GRAPHICS");
613 base = resources_path();
616 base = resources_path();
619 boost::filesystem::path p = base / String::compose("%1.png", name);
620 return std_to_wx (p.string());
625 small_button_size (wxWindow* parent, wxString text)
627 wxClientDC dc (parent);
628 auto size = dc.GetTextExtent (text);