2 Copyright (C) 2012-2018 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 "lib/config.h"
29 #include "lib/job_manager.h"
31 #include "lib/cross.h"
33 #include <dcp/locale_convert.h>
34 #include <wx/spinctrl.h>
35 #include <wx/splash.h>
36 #include <wx/progdlg.h>
37 #include <wx/filepicker.h>
38 #include <boost/thread.hpp>
43 using boost::shared_ptr;
44 using boost::optional;
45 using dcp::locale_convert;
49 create_label (wxWindow* p, wxString t, bool left)
51 create_label (wxWindow* p, wxString t, bool)
59 return new StaticText (p, t);
62 /** Add a wxStaticText to a wxSizer, aligning it at vertical centre.
63 * @param s Sizer to add to.
64 * @param p Parent window for the wxStaticText.
65 * @param t Text for the wxStaticText.
66 * @param left true if this label is a `left label'; ie the sort
67 * of label which should be right-aligned on OS X.
68 * @param prop Proportion to pass when calling Add() on the wxSizer.
71 add_label_to_sizer (wxSizer* s, wxWindow* p, wxString t, bool left, int prop, int flags)
75 flags |= wxALIGN_RIGHT;
78 wxStaticText* m = create_label (p, t, left);
79 s->Add (m, prop, flags, 6);
85 add_label_to_sizer (wxSizer* s, wxStaticText* t, bool left, int prop, int flags)
87 add_label_to_sizer (wxSizer* s, wxStaticText* t, bool, int prop, int flags)
92 flags |= wxALIGN_RIGHT;
95 s->Add (t, prop, flags, 6);
100 add_label_to_sizer (wxGridBagSizer* s, wxWindow* p, wxString t, bool left, wxGBPosition pos, wxGBSpan span)
102 int flags = wxALIGN_CENTER_VERTICAL | wxLEFT | wxRIGHT;
105 flags |= wxALIGN_RIGHT;
108 wxStaticText* m = create_label (p, t, left);
109 s->Add (m, pos, span, flags);
115 add_label_to_sizer (wxGridBagSizer* s, wxStaticText* t, bool left, wxGBPosition pos, wxGBSpan span)
117 add_label_to_sizer (wxGridBagSizer* s, wxStaticText* t, bool, wxGBPosition pos, wxGBSpan span)
120 int flags = wxALIGN_CENTER_VERTICAL | wxLEFT | wxRIGHT;
123 flags |= wxALIGN_RIGHT;
126 s->Add (t, pos, span, flags);
130 /** Pop up an error dialogue box.
131 * @param parent Parent.
133 * @param e Extended message.
136 error_dialog (wxWindow* parent, wxString m, optional<wxString> e)
138 wxMessageDialog* d = new wxMessageDialog (parent, m, _("DCP-o-matic"), wxOK | wxICON_ERROR);
141 em[0] = wxToupper (em[0]);
142 d->SetExtendedMessage (em);
148 /** Pop up an error dialogue box.
149 * @param parent Parent.
153 message_dialog (wxWindow* parent, wxString m)
155 wxMessageDialog* d = new wxMessageDialog (parent, m, _("DCP-o-matic"), wxOK | wxICON_INFORMATION);
161 confirm_dialog (wxWindow* parent, wxString m)
163 wxMessageDialog* d = new wxMessageDialog (parent, m, _("DCP-o-matic"), wxYES_NO | wxICON_QUESTION);
164 int const r = d->ShowModal ();
166 return r == wxID_YES;
170 /** @param s wxWidgets string.
171 * @return Corresponding STL string.
174 wx_to_std (wxString s)
176 return string (s.ToUTF8 ());
179 /** @param s STL string.
180 * @return Corresponding wxWidgets string.
185 return wxString (s.c_str(), wxConvUTF8);
189 string_client_data (wxClientData* o)
191 return wx_to_std (dynamic_cast<wxStringClientData*>(o)->GetData());
195 checked_set (FilePickerCtrl* widget, boost::filesystem::path value)
197 if (widget->GetPath() != std_to_wx (value.string())) {
199 /* Hack to make wxWidgets clear the control when we are passed
204 widget->SetPath (std_to_wx (value.string()));
209 checked_set (wxDirPickerCtrl* widget, boost::filesystem::path value)
211 if (widget->GetPath() != std_to_wx (value.string())) {
213 /* Hack to make wxWidgets clear the control when we are passed
218 widget->SetPath (std_to_wx (value.string()));
223 checked_set (wxSpinCtrl* widget, int value)
225 if (widget->GetValue() != value) {
226 widget->SetValue (value);
231 checked_set (wxSpinCtrlDouble* widget, double value)
233 /* XXX: completely arbitrary epsilon */
234 if (fabs (widget->GetValue() - value) > 1e-16) {
235 widget->SetValue (value);
240 checked_set (wxChoice* widget, int value)
242 if (widget->GetSelection() != value) {
243 widget->SetSelection (value);
248 checked_set (wxChoice* widget, string value)
251 if (widget->GetSelection() != -1) {
252 o = widget->GetClientObject (widget->GetSelection ());
255 if (!o || string_client_data(o) != value) {
256 for (unsigned int i = 0; i < widget->GetCount(); ++i) {
257 if (string_client_data (widget->GetClientObject (i)) == value) {
258 widget->SetSelection (i);
265 checked_set (wxChoice* widget, vector<pair<string, string> > items)
267 vector<pair<string, string> > current;
268 for (unsigned int i = 0; i < widget->GetCount(); ++i) {
271 wx_to_std (widget->GetString (i)),
272 string_client_data (widget->GetClientObject (i))
277 if (current == items) {
282 for (vector<pair<string, string> >::const_iterator i = items.begin(); i != items.end(); ++i) {
283 widget->Append (std_to_wx (i->first), new wxStringClientData (std_to_wx (i->second)));
288 checked_set (wxTextCtrl* widget, string value)
290 if (widget->GetValue() != std_to_wx (value)) {
291 widget->ChangeValue (std_to_wx (value));
296 checked_set (wxTextCtrl* widget, wxString value)
298 if (widget->GetValue() != value) {
299 widget->ChangeValue (value);
304 checked_set (wxStaticText* widget, string value)
306 if (widget->GetLabel() != std_to_wx (value)) {
307 widget->SetLabel (std_to_wx (value));
312 checked_set (wxStaticText* widget, wxString value)
314 if (widget->GetLabel() != value) {
315 widget->SetLabel (value);
320 checked_set (wxCheckBox* widget, bool value)
322 if (widget->GetValue() != value) {
323 widget->SetValue (value);
328 checked_set (wxRadioButton* widget, bool value)
330 if (widget->GetValue() != value) {
331 widget->SetValue (value);
336 dcpomatic_setup_i18n ()
338 int language = wxLANGUAGE_DEFAULT;
340 boost::optional<string> config_lang = Config::instance()->language ();
341 if (config_lang && !config_lang->empty ()) {
342 wxLanguageInfo const * li = wxLocale::FindLanguageInfo (std_to_wx (config_lang.get ()));
344 language = li->Language;
348 wxLocale* locale = 0;
349 if (wxLocale::IsAvailable (language)) {
350 locale = new wxLocale (language, wxLOCALE_LOAD_DEFAULT);
352 #ifdef DCPOMATIC_WINDOWS
353 locale->AddCatalogLookupPathPrefix (std_to_wx (mo_path().string()));
356 #ifdef DCPOMATIC_LINUX
357 locale->AddCatalogLookupPathPrefix (LINUX_LOCALE_PREFIX);
359 /* We have to include the wxWidgets .mo in our distribution,
360 so we rename it to avoid clashes with any other installation
363 locale->AddCatalog (wxT ("dcpomatic2-wxstd"));
365 /* Fedora 29 (at least) installs wxstd3.mo instead of wxstd.mo */
366 locale->AddCatalog (wxT ("wxstd3"));
369 locale->AddCatalog (wxT ("libdcpomatic2-wx"));
370 locale->AddCatalog (wxT ("dcpomatic2"));
372 if (!locale->IsOk()) {
374 locale = new wxLocale (wxLANGUAGE_ENGLISH);
379 dcpomatic_setup_gettext_i18n (wx_to_std (locale->GetCanonicalName ()));
384 wx_get (wxSpinCtrl* w)
386 return w->GetValue ();
392 return w->GetSelection ();
396 wx_get (wxSpinCtrlDouble* w)
398 return w->GetValue ();
401 /** @param s String of the form Context|String
402 * @return translation, or String if no translation is available.
405 context_translation (wxString s)
407 wxString t = wxGetTranslation (s);
409 /* No translation; strip the context */
410 int c = t.Find (wxT ("|"));
411 if (c != wxNOT_FOUND) {
420 time_to_timecode (DCPTime t, double fps)
422 double w = t.seconds ();
423 int const h = (w / 3600);
425 int const m = (w / 60);
427 int const s = floor (w);
429 int const f = lrint (w * fps);
430 return wxString::Format (wxT("%02d:%02d:%02d.%02d"), h, m, s, f);
434 setup_audio_channels_choice (wxChoice* choice, int minimum)
436 vector<pair<string, string> > items;
437 for (int i = minimum; i <= 16; i += 2) {
439 items.push_back (make_pair (wx_to_std (_("2 - stereo")), locale_convert<string> (i)));
441 items.push_back (make_pair (wx_to_std (_("4 - L/C/R/Lfe")), locale_convert<string> (i)));
443 items.push_back (make_pair (wx_to_std (_("6 - 5.1")), locale_convert<string> (i)));
445 items.push_back (make_pair (wx_to_std (_("8 - 5.1/HI/VI")), locale_convert<string> (i)));
446 } else if (i == 12) {
447 items.push_back (make_pair (wx_to_std (_("12 - 7.1/HI/VI")), locale_convert<string> (i)));
449 items.push_back (make_pair (locale_convert<string> (i), locale_convert<string> (i)));
453 checked_set (choice, items);
459 wxSplashScreen* splash = 0;
462 boost::filesystem::path p = shared_path () / "splash.png";
463 if (bitmap.LoadFile (std_to_wx (p.string ()), wxBITMAP_TYPE_PNG)) {
464 splash = new wxSplashScreen (bitmap, wxSPLASH_CENTRE_ON_SCREEN | wxSPLASH_NO_TIMEOUT, 0, 0, -1);
467 } catch (boost::filesystem::filesystem_error& e) {
468 /* Maybe we couldn't find the splash image; never mind */
475 calculate_mark_interval (double mark_interval)
477 if (mark_interval > 5) {
478 mark_interval -= lrint (mark_interval) % 5;
480 if (mark_interval > 10) {
481 mark_interval -= lrint (mark_interval) % 10;
483 if (mark_interval > 60) {
484 mark_interval -= lrint (mark_interval) % 60;
486 if (mark_interval > 3600) {
487 mark_interval -= lrint (mark_interval) % 3600;
490 if (mark_interval < 1) {
494 return mark_interval;
498 /** @return false if the task was cancelled */
500 display_progress (wxString title, wxString task)
502 JobManager* jm = JobManager::instance ();
504 wxProgressDialog progress (title, task, 100, 0, wxPD_CAN_ABORT);
508 while (jm->work_to_do()) {
510 if (!progress.Pulse()) {
511 /* user pressed cancel */
512 BOOST_FOREACH (shared_ptr<Job> i, jm->get()) {
524 report_errors_from_last_job (wxWindow* parent)
526 JobManager* jm = JobManager::instance ();
528 DCPOMATIC_ASSERT (!jm->get().empty());
530 shared_ptr<Job> last = jm->get().back();
531 if (last->finished_in_error()) {
532 error_dialog(parent, std_to_wx(last->error_summary()) + ".\n", std_to_wx(last->error_details()));