2 Copyright (C) 2012-2016 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 "lib/config.h"
29 #include "lib/cross.h"
30 #include <dcp/locale_convert.h>
31 #include <wx/spinctrl.h>
32 #include <wx/splash.h>
33 #include <wx/filepicker.h>
34 #include <boost/thread.hpp>
37 using namespace boost;
38 using dcp::locale_convert;
42 create_label (wxWindow* p, wxString t, bool left)
44 create_label (wxWindow* p, wxString t, bool)
52 return new wxStaticText (p, wxID_ANY, t);
55 /** Add a wxStaticText to a wxSizer, aligning it at vertical centre.
56 * @param s Sizer to add to.
57 * @param p Parent window for the wxStaticText.
58 * @param t Text for the wxStaticText.
59 * @param left true if this label is a `left label'; ie the sort
60 * of label which should be right-aligned on OS X.
61 * @param prop Proportion to pass when calling Add() on the wxSizer.
64 add_label_to_sizer (wxSizer* s, wxWindow* p, wxString t, bool left, int prop, int flags)
68 flags |= wxALIGN_RIGHT;
71 wxStaticText* m = create_label (p, t, left);
72 s->Add (m, prop, flags, 6);
78 add_label_to_sizer (wxSizer* s, wxStaticText* t, bool left, int prop, int flags)
80 add_label_to_sizer (wxSizer* s, wxStaticText* t, bool, int prop, int flags)
85 flags |= wxALIGN_RIGHT;
88 s->Add (t, prop, flags, 6);
93 add_label_to_sizer (wxGridBagSizer* s, wxWindow* p, wxString t, bool left, wxGBPosition pos, wxGBSpan span)
95 int flags = wxALIGN_CENTER_VERTICAL | wxLEFT | wxRIGHT;
98 flags |= wxALIGN_RIGHT;
101 wxStaticText* m = create_label (p, t, left);
102 s->Add (m, pos, span, flags);
108 add_label_to_sizer (wxGridBagSizer* s, wxStaticText* t, bool left, wxGBPosition pos, wxGBSpan span)
110 add_label_to_sizer (wxGridBagSizer* s, wxStaticText* t, bool, wxGBPosition pos, wxGBSpan span)
113 int flags = wxALIGN_CENTER_VERTICAL | wxLEFT | wxRIGHT;
116 flags |= wxALIGN_RIGHT;
119 s->Add (t, pos, span, flags);
123 /** Pop up an error dialogue box.
124 * @param parent Parent.
126 * @param e Extended message.
129 error_dialog (wxWindow* parent, wxString m, optional<wxString> e)
131 wxMessageDialog* d = new wxMessageDialog (parent, m, _("DCP-o-matic"), wxOK | wxICON_ERROR);
133 d->SetExtendedMessage (*e);
139 /** Pop up an error dialogue box.
140 * @param parent Parent.
144 message_dialog (wxWindow* parent, wxString m)
146 wxMessageDialog* d = new wxMessageDialog (parent, m, _("DCP-o-matic"), wxOK | wxICON_INFORMATION);
152 confirm_dialog (wxWindow* parent, wxString m)
154 wxMessageDialog* d = new wxMessageDialog (parent, m, _("DCP-o-matic"), wxYES_NO | wxICON_QUESTION);
155 int const r = d->ShowModal ();
157 return r == wxID_YES;
161 /** @param s wxWidgets string.
162 * @return Corresponding STL string.
165 wx_to_std (wxString s)
167 return string (s.ToUTF8 ());
170 /** @param s STL string.
171 * @return Corresponding wxWidgets string.
176 return wxString (s.c_str(), wxConvUTF8);
180 string_client_data (wxClientData* o)
182 return wx_to_std (dynamic_cast<wxStringClientData*>(o)->GetData());
186 checked_set (FilePickerCtrl* widget, boost::filesystem::path value)
188 if (widget->GetPath() != std_to_wx (value.string())) {
190 /* Hack to make wxWidgets clear the control when we are passed
195 widget->SetPath (std_to_wx (value.string()));
200 checked_set (wxDirPickerCtrl* widget, boost::filesystem::path value)
202 if (widget->GetPath() != std_to_wx (value.string())) {
204 /* Hack to make wxWidgets clear the control when we are passed
209 widget->SetPath (std_to_wx (value.string()));
214 checked_set (wxSpinCtrl* widget, int value)
216 if (widget->GetValue() != value) {
217 widget->SetValue (value);
222 checked_set (wxSpinCtrlDouble* widget, double value)
224 /* XXX: completely arbitrary epsilon */
225 if (fabs (widget->GetValue() - value) > 1e-16) {
226 widget->SetValue (value);
231 checked_set (wxChoice* widget, int value)
233 if (widget->GetSelection() != value) {
234 widget->SetSelection (value);
239 checked_set (wxChoice* widget, string value)
242 if (widget->GetSelection() != -1) {
243 o = widget->GetClientObject (widget->GetSelection ());
246 if (!o || string_client_data(o) != value) {
247 for (unsigned int i = 0; i < widget->GetCount(); ++i) {
248 if (string_client_data (widget->GetClientObject (i)) == value) {
249 widget->SetSelection (i);
256 checked_set (wxChoice* widget, vector<pair<string, string> > items)
258 vector<pair<string, string> > current;
259 for (unsigned int i = 0; i < widget->GetCount(); ++i) {
262 wx_to_std (widget->GetString (i)),
263 string_client_data (widget->GetClientObject (i))
268 if (current == items) {
273 for (vector<pair<string, string> >::const_iterator i = items.begin(); i != items.end(); ++i) {
274 widget->Append (std_to_wx (i->first), new wxStringClientData (std_to_wx (i->second)));
279 checked_set (wxTextCtrl* widget, string value)
281 if (widget->GetValue() != std_to_wx (value)) {
282 widget->ChangeValue (std_to_wx (value));
287 checked_set (wxTextCtrl* widget, wxString value)
289 if (widget->GetValue() != value) {
290 widget->ChangeValue (value);
295 checked_set (wxStaticText* widget, string value)
297 if (widget->GetLabel() != std_to_wx (value)) {
298 widget->SetLabel (std_to_wx (value));
303 checked_set (wxStaticText* widget, wxString value)
305 if (widget->GetLabel() != value) {
306 widget->SetLabel (value);
311 checked_set (wxCheckBox* widget, bool value)
313 if (widget->GetValue() != value) {
314 widget->SetValue (value);
319 checked_set (wxRadioButton* widget, bool value)
321 if (widget->GetValue() != value) {
322 widget->SetValue (value);
327 dcpomatic_setup_i18n ()
329 int language = wxLANGUAGE_DEFAULT;
331 boost::optional<string> config_lang = Config::instance()->language ();
332 if (config_lang && !config_lang->empty ()) {
333 wxLanguageInfo const * li = wxLocale::FindLanguageInfo (std_to_wx (config_lang.get ()));
335 language = li->Language;
339 wxLocale* locale = 0;
340 if (wxLocale::IsAvailable (language)) {
341 locale = new wxLocale (language, wxLOCALE_LOAD_DEFAULT);
343 #ifdef DCPOMATIC_WINDOWS
344 locale->AddCatalogLookupPathPrefix (std_to_wx (mo_path().string()));
347 #ifdef DCPOMATIC_LINUX
348 locale->AddCatalogLookupPathPrefix (LINUX_LOCALE_PREFIX);
350 /* We have to include the wxWidgets .mo in our distribution,
351 so we rename it to avoid clashes with any other installation
354 locale->AddCatalog (wxT ("dcpomatic2-wxstd"));
357 locale->AddCatalog (wxT ("libdcpomatic2-wx"));
358 locale->AddCatalog (wxT ("dcpomatic2"));
360 if (!locale->IsOk()) {
362 locale = new wxLocale (wxLANGUAGE_ENGLISH);
367 dcpomatic_setup_gettext_i18n (wx_to_std (locale->GetCanonicalName ()));
372 wx_get (wxSpinCtrl* w)
374 return w->GetValue ();
380 return w->GetSelection ();
384 wx_get (wxSpinCtrlDouble* w)
386 return w->GetValue ();
389 /** @param s String of the form Context|String
390 * @return translation, or String if no translation is available.
393 context_translation (wxString s)
395 wxString t = wxGetTranslation (s);
397 /* No translation; strip the context */
398 int c = t.Find (wxT ("|"));
399 if (c != wxNOT_FOUND) {
408 time_to_timecode (DCPTime t, double fps)
410 double w = t.seconds ();
411 int const h = (w / 3600);
413 int const m = (w / 60);
415 int const s = floor (w);
417 int const f = lrint (w * fps);
418 return wxString::Format (wxT("%02d:%02d:%02d.%02d"), h, m, s, f);
422 setup_audio_channels_choice (wxChoice* choice, int minimum)
424 vector<pair<string, string> > items;
425 for (int i = minimum; i <= 16; i += 2) {
427 items.push_back (make_pair (wx_to_std (_("2 - stereo")), locale_convert<string> (i)));
429 items.push_back (make_pair (wx_to_std (_("4 - L/C/R/Lfe")), locale_convert<string> (i)));
431 items.push_back (make_pair (wx_to_std (_("6 - 5.1")), locale_convert<string> (i)));
433 items.push_back (make_pair (wx_to_std (_("8 - 5.1/HI/VI")), locale_convert<string> (i)));
434 } else if (i == 12) {
435 items.push_back (make_pair (wx_to_std (_("12 - 7.1/HI/VI")), locale_convert<string> (i)));
437 items.push_back (make_pair (locale_convert<string> (i), locale_convert<string> (i)));
441 checked_set (choice, items);
447 wxSplashScreen* splash = 0;
449 if (!Config::have_existing ("config.xml")) {
451 boost::filesystem::path p = shared_path () / "splash.png";
452 if (bitmap.LoadFile (std_to_wx (p.string ()), wxBITMAP_TYPE_PNG)) {
453 splash = new wxSplashScreen (bitmap, wxSPLASH_CENTRE_ON_SCREEN | wxSPLASH_NO_TIMEOUT, 0, 0, -1);
457 } catch (boost::filesystem::filesystem_error& e) {
458 /* Maybe we couldn't find the splash image; never mind */
464 boost::filesystem::path
465 path_from_file_dialog (wxFileDialog* dialog, string extension)
467 return boost::filesystem::path(wx_to_std(dialog->GetPath())).replace_extension(extension);
471 calculate_mark_interval (double mark_interval)
473 if (mark_interval > 5) {
474 mark_interval -= lrint (mark_interval) % 5;
476 if (mark_interval > 10) {
477 mark_interval -= lrint (mark_interval) % 10;
479 if (mark_interval > 60) {
480 mark_interval -= lrint (mark_interval) % 60;
482 if (mark_interval > 3600) {
483 mark_interval -= lrint (mark_interval) % 3600;
486 if (mark_interval < 1) {
490 return mark_interval;