2 Copyright (C) 2012-2019 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 <dcp/locale_convert.h>
35 #include <wx/spinctrl.h>
36 #include <wx/splash.h>
37 #include <wx/progdlg.h>
38 #include <wx/filepicker.h>
39 #include <boost/thread.hpp>
44 using boost::shared_ptr;
45 using boost::optional;
46 using dcp::locale_convert;
50 create_label (wxWindow* p, wxString t, bool left)
52 create_label (wxWindow* p, wxString t, bool)
60 return new StaticText (p, t);
63 /** Add a wxStaticText to a wxSizer, aligning it at vertical centre.
64 * @param s Sizer to add to.
65 * @param p Parent window for the wxStaticText.
66 * @param t Text for the wxStaticText.
67 * @param left true if this label is a `left label'; ie the sort
68 * of label which should be right-aligned on OS X.
69 * @param prop Proportion to pass when calling Add() on the wxSizer.
72 add_label_to_sizer (wxSizer* s, wxWindow* p, wxString t, bool left, int prop, int flags)
76 flags |= wxALIGN_RIGHT;
79 wxStaticText* m = create_label (p, t, left);
80 s->Add (m, prop, flags, 6);
86 add_label_to_sizer (wxSizer* s, wxStaticText* t, bool left, int prop, int flags)
88 add_label_to_sizer (wxSizer* s, wxStaticText* t, bool, int prop, int flags)
93 flags |= wxALIGN_RIGHT;
96 s->Add (t, prop, flags, 6);
101 add_label_to_sizer (wxGridBagSizer* s, wxWindow* p, wxString t, bool left, wxGBPosition pos, wxGBSpan span)
103 int flags = wxALIGN_CENTER_VERTICAL | wxLEFT | wxRIGHT;
106 flags |= wxALIGN_RIGHT;
109 wxStaticText* m = create_label (p, t, left);
110 s->Add (m, pos, span, flags);
116 add_label_to_sizer (wxGridBagSizer* s, wxStaticText* t, bool left, wxGBPosition pos, wxGBSpan span)
118 add_label_to_sizer (wxGridBagSizer* s, wxStaticText* t, bool, wxGBPosition pos, wxGBSpan span)
121 int flags = wxALIGN_CENTER_VERTICAL | wxLEFT | wxRIGHT;
124 flags |= wxALIGN_RIGHT;
127 s->Add (t, pos, span, flags);
131 /** Pop up an error dialogue box.
132 * @param parent Parent.
134 * @param e Extended message.
137 error_dialog (wxWindow* parent, wxString m, optional<wxString> e)
139 wxMessageDialog* d = new wxMessageDialog (parent, m, _("DCP-o-matic"), wxOK | wxICON_ERROR);
142 em[0] = wxToupper (em[0]);
143 d->SetExtendedMessage (em);
149 /** Pop up an error dialogue box.
150 * @param parent Parent.
154 message_dialog (wxWindow* parent, wxString m)
156 wxMessageDialog* d = new wxMessageDialog (parent, m, _("DCP-o-matic"), wxOK | wxICON_INFORMATION);
162 confirm_dialog (wxWindow* parent, wxString m)
164 wxMessageDialog* d = new wxMessageDialog (parent, m, _("DCP-o-matic"), wxYES_NO | wxICON_QUESTION);
165 int const r = d->ShowModal ();
167 return r == wxID_YES;
171 /** @param s wxWidgets string.
172 * @return Corresponding STL string.
175 wx_to_std (wxString s)
177 return string (s.ToUTF8 ());
180 /** @param s STL string.
181 * @return Corresponding wxWidgets string.
186 return wxString (s.c_str(), wxConvUTF8);
190 string_client_data (wxClientData* o)
192 return wx_to_std (dynamic_cast<wxStringClientData*>(o)->GetData());
196 checked_set (FilePickerCtrl* widget, boost::filesystem::path value)
198 if (widget->GetPath() != std_to_wx (value.string())) {
200 /* Hack to make wxWidgets clear the control when we are passed
205 widget->SetPath (std_to_wx (value.string()));
210 checked_set (wxDirPickerCtrl* 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 (wxSpinCtrl* widget, int value)
226 if (widget->GetValue() != value) {
227 widget->SetValue (value);
232 checked_set (wxSpinCtrlDouble* widget, double value)
234 /* XXX: completely arbitrary epsilon */
235 if (fabs (widget->GetValue() - value) > 1e-16) {
236 widget->SetValue (value);
241 checked_set (wxChoice* widget, int value)
243 if (widget->GetSelection() != value) {
244 widget->SetSelection (value);
249 checked_set (wxChoice* widget, string value)
252 if (widget->GetSelection() != -1) {
253 o = widget->GetClientObject (widget->GetSelection ());
256 if (!o || string_client_data(o) != value) {
257 for (unsigned int i = 0; i < widget->GetCount(); ++i) {
258 if (string_client_data (widget->GetClientObject (i)) == value) {
259 widget->SetSelection (i);
266 checked_set (wxChoice* widget, vector<pair<string, string> > items)
268 vector<pair<string, string> > current;
269 for (unsigned int i = 0; i < widget->GetCount(); ++i) {
272 wx_to_std (widget->GetString (i)),
273 string_client_data (widget->GetClientObject (i))
278 if (current == items) {
283 for (vector<pair<string, string> >::const_iterator i = items.begin(); i != items.end(); ++i) {
284 widget->Append (std_to_wx (i->first), new wxStringClientData (std_to_wx (i->second)));
289 checked_set (wxTextCtrl* widget, string value)
291 if (widget->GetValue() != std_to_wx (value)) {
292 widget->ChangeValue (std_to_wx (value));
297 checked_set (PasswordEntry* entry, string value)
299 if (entry->get() != value) {
305 checked_set (wxTextCtrl* widget, wxString value)
307 if (widget->GetValue() != value) {
308 widget->ChangeValue (value);
313 checked_set (wxStaticText* widget, string value)
315 if (widget->GetLabel() != std_to_wx (value)) {
316 widget->SetLabel (std_to_wx (value));
321 checked_set (wxStaticText* widget, wxString value)
323 if (widget->GetLabel() != value) {
324 widget->SetLabel (value);
329 checked_set (wxCheckBox* widget, bool value)
331 if (widget->GetValue() != value) {
332 widget->SetValue (value);
337 checked_set (wxRadioButton* widget, bool value)
339 if (widget->GetValue() != value) {
340 widget->SetValue (value);
345 dcpomatic_setup_i18n ()
347 int language = wxLANGUAGE_DEFAULT;
349 boost::optional<string> config_lang = Config::instance()->language ();
350 if (config_lang && !config_lang->empty ()) {
351 wxLanguageInfo const * li = wxLocale::FindLanguageInfo (std_to_wx (config_lang.get ()));
353 language = li->Language;
357 wxLocale* locale = 0;
358 if (wxLocale::IsAvailable (language)) {
359 locale = new wxLocale (language, wxLOCALE_LOAD_DEFAULT);
361 #ifdef DCPOMATIC_WINDOWS
362 locale->AddCatalogLookupPathPrefix (std_to_wx (mo_path().string()));
365 #ifdef DCPOMATIC_LINUX
366 locale->AddCatalogLookupPathPrefix (LINUX_LOCALE_PREFIX);
368 /* We have to include the wxWidgets .mo in our distribution,
369 so we rename it to avoid clashes with any other installation
372 locale->AddCatalog (wxT ("dcpomatic2-wxstd"));
374 /* Fedora 29 (at least) installs wxstd3.mo instead of wxstd.mo */
375 locale->AddCatalog (wxT ("wxstd3"));
378 locale->AddCatalog (wxT ("libdcpomatic2-wx"));
379 locale->AddCatalog (wxT ("dcpomatic2"));
381 if (!locale->IsOk()) {
383 locale = new wxLocale (wxLANGUAGE_ENGLISH);
388 dcpomatic_setup_gettext_i18n (wx_to_std (locale->GetCanonicalName ()));
393 wx_get (wxSpinCtrl* w)
395 return w->GetValue ();
401 return w->GetSelection ();
405 wx_get (wxSpinCtrlDouble* w)
407 return w->GetValue ();
410 /** @param s String of the form Context|String
411 * @return translation, or String if no translation is available.
414 context_translation (wxString s)
416 wxString t = wxGetTranslation (s);
418 /* No translation; strip the context */
419 int c = t.Find (wxT ("|"));
420 if (c != wxNOT_FOUND) {
429 time_to_timecode (DCPTime t, double fps)
431 double w = t.seconds ();
432 int const h = (w / 3600);
434 int const m = (w / 60);
436 int const s = floor (w);
438 int const f = lrint (w * fps);
439 return wxString::Format (wxT("%02d:%02d:%02d.%02d"), h, m, s, f);
443 setup_audio_channels_choice (wxChoice* choice, int minimum)
445 vector<pair<string, string> > items;
446 for (int i = minimum; i <= 16; i += 2) {
448 items.push_back (make_pair (wx_to_std (_("2 - stereo")), locale_convert<string> (i)));
450 items.push_back (make_pair (wx_to_std (_("4 - L/C/R/Lfe")), locale_convert<string> (i)));
452 items.push_back (make_pair (wx_to_std (_("6 - 5.1")), locale_convert<string> (i)));
454 items.push_back (make_pair (wx_to_std (_("8 - 5.1/HI/VI")), locale_convert<string> (i)));
455 } else if (i == 12) {
456 items.push_back (make_pair (wx_to_std (_("12 - 7.1/HI/VI")), locale_convert<string> (i)));
458 items.push_back (make_pair (locale_convert<string> (i), locale_convert<string> (i)));
462 checked_set (choice, items);
468 wxSplashScreen* splash = 0;
471 boost::filesystem::path p = shared_path () / "splash.png";
472 if (bitmap.LoadFile (std_to_wx (p.string ()), wxBITMAP_TYPE_PNG)) {
473 splash = new wxSplashScreen (bitmap, wxSPLASH_CENTRE_ON_SCREEN | wxSPLASH_NO_TIMEOUT, 0, 0, -1);
476 } catch (boost::filesystem::filesystem_error& e) {
477 /* Maybe we couldn't find the splash image; never mind */
484 calculate_mark_interval (double mark_interval)
486 if (mark_interval > 5) {
487 mark_interval -= lrint (mark_interval) % 5;
489 if (mark_interval > 10) {
490 mark_interval -= lrint (mark_interval) % 10;
492 if (mark_interval > 60) {
493 mark_interval -= lrint (mark_interval) % 60;
495 if (mark_interval > 3600) {
496 mark_interval -= lrint (mark_interval) % 3600;
499 if (mark_interval < 1) {
503 return mark_interval;
507 /** @return false if the task was cancelled */
509 display_progress (wxString title, wxString task)
511 JobManager* jm = JobManager::instance ();
513 wxProgressDialog progress (title, task, 100, 0, wxPD_CAN_ABORT);
517 while (jm->work_to_do()) {
519 if (!progress.Pulse()) {
520 /* user pressed cancel */
521 BOOST_FOREACH (shared_ptr<Job> i, jm->get()) {
533 report_errors_from_last_job (wxWindow* parent)
535 JobManager* jm = JobManager::instance ();
537 DCPOMATIC_ASSERT (!jm->get().empty());
539 shared_ptr<Job> last = jm->get().back();
540 if (last->finished_in_error()) {
541 error_dialog(parent, std_to_wx(last->error_summary()) + ".\n", std_to_wx(last->error_details()));