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 <boost/thread.hpp>
36 using namespace boost;
37 using dcp::locale_convert;
39 /** Add a wxStaticText to a wxSizer, aligning it at vertical centre.
40 * @param s Sizer to add to.
41 * @param p Parent window for the wxStaticText.
42 * @param t Text for the wxStaticText.
43 * @param left true if this label is a `left label'; ie the sort
44 * of label which should be right-aligned on OS X.
45 * @param prop Proportion to pass when calling Add() on the wxSizer.
49 add_label_to_sizer (wxSizer* s, wxWindow* p, wxString t, bool left, int prop, int flags)
51 add_label_to_sizer (wxSizer* s, wxWindow* p, wxString t, bool, int prop, int flags)
56 flags |= wxALIGN_RIGHT;
60 wxStaticText* m = new wxStaticText (p, wxID_ANY, t);
61 s->Add (m, prop, flags, 6);
67 add_label_to_sizer (wxGridBagSizer* s, wxWindow* p, wxString t, bool left, wxGBPosition pos, wxGBSpan span)
69 add_label_to_sizer (wxGridBagSizer* s, wxWindow* p, wxString t, bool, wxGBPosition pos, wxGBSpan span)
72 int flags = wxALIGN_CENTER_VERTICAL | wxLEFT | wxRIGHT;
75 flags |= wxALIGN_RIGHT;
79 wxStaticText* m = new wxStaticText (p, wxID_ANY, t);
80 s->Add (m, pos, span, flags);
84 /** Pop up an error dialogue box.
85 * @param parent Parent.
87 * @param e Extended message.
90 error_dialog (wxWindow* parent, wxString m, optional<wxString> e)
92 wxMessageDialog* d = new wxMessageDialog (parent, m, _("DCP-o-matic"), wxOK | wxICON_ERROR);
94 d->SetExtendedMessage (*e);
100 /** Pop up an error dialogue box.
101 * @param parent Parent.
105 message_dialog (wxWindow* parent, wxString m)
107 wxMessageDialog* d = new wxMessageDialog (parent, m, _("DCP-o-matic"), wxOK | wxICON_INFORMATION);
113 confirm_dialog (wxWindow* parent, wxString m)
115 wxMessageDialog* d = new wxMessageDialog (parent, m, _("DCP-o-matic"), wxYES_NO | wxICON_QUESTION);
116 int const r = d->ShowModal ();
118 return r == wxID_YES;
122 /** @param s wxWidgets string.
123 * @return Corresponding STL string.
126 wx_to_std (wxString s)
128 return string (s.ToUTF8 ());
131 /** @param s STL string.
132 * @return Corresponding wxWidgets string.
137 return wxString (s.c_str(), wxConvUTF8);
141 string_client_data (wxClientData* o)
143 return wx_to_std (dynamic_cast<wxStringClientData*>(o)->GetData());
147 checked_set (FilePickerCtrl* widget, boost::filesystem::path value)
149 if (widget->GetPath() != std_to_wx (value.string())) {
151 /* Hack to make wxWidgets clear the control when we are passed
156 widget->SetPath (std_to_wx (value.string()));
161 checked_set (wxSpinCtrl* widget, int value)
163 if (widget->GetValue() != value) {
164 widget->SetValue (value);
169 checked_set (wxSpinCtrlDouble* widget, double value)
171 /* XXX: completely arbitrary epsilon */
172 if (fabs (widget->GetValue() - value) > 1e-16) {
173 widget->SetValue (value);
178 checked_set (wxChoice* widget, int value)
180 if (widget->GetSelection() != value) {
181 widget->SetSelection (value);
186 checked_set (wxChoice* widget, string value)
189 if (widget->GetSelection() != -1) {
190 o = widget->GetClientObject (widget->GetSelection ());
193 if (!o || string_client_data(o) != value) {
194 for (unsigned int i = 0; i < widget->GetCount(); ++i) {
195 if (string_client_data (widget->GetClientObject (i)) == value) {
196 widget->SetSelection (i);
203 checked_set (wxChoice* widget, vector<pair<string, string> > items)
205 vector<pair<string, string> > current;
206 for (unsigned int i = 0; i < widget->GetCount(); ++i) {
209 wx_to_std (widget->GetString (i)),
210 string_client_data (widget->GetClientObject (i))
215 if (current == items) {
220 for (vector<pair<string, string> >::const_iterator i = items.begin(); i != items.end(); ++i) {
221 widget->Append (std_to_wx (i->first), new wxStringClientData (std_to_wx (i->second)));
226 checked_set (wxTextCtrl* widget, string value)
228 if (widget->GetValue() != std_to_wx (value)) {
229 widget->ChangeValue (std_to_wx (value));
234 checked_set (wxTextCtrl* widget, wxString value)
236 if (widget->GetValue() != value) {
237 widget->ChangeValue (value);
242 checked_set (wxStaticText* widget, string value)
244 if (widget->GetLabel() != std_to_wx (value)) {
245 widget->SetLabel (std_to_wx (value));
250 checked_set (wxStaticText* widget, wxString value)
252 if (widget->GetLabel() != value) {
253 widget->SetLabel (value);
258 checked_set (wxCheckBox* widget, bool value)
260 if (widget->GetValue() != value) {
261 widget->SetValue (value);
266 checked_set (wxRadioButton* widget, bool value)
268 if (widget->GetValue() != value) {
269 widget->SetValue (value);
274 dcpomatic_setup_i18n ()
276 int language = wxLANGUAGE_DEFAULT;
278 boost::optional<string> config_lang = Config::instance()->language ();
279 if (config_lang && !config_lang->empty ()) {
280 wxLanguageInfo const * li = wxLocale::FindLanguageInfo (std_to_wx (config_lang.get ()));
282 language = li->Language;
286 wxLocale* locale = 0;
287 if (wxLocale::IsAvailable (language)) {
288 locale = new wxLocale (language, wxLOCALE_LOAD_DEFAULT);
290 #ifdef DCPOMATIC_WINDOWS
291 locale->AddCatalogLookupPathPrefix (std_to_wx (mo_path().string()));
294 #ifdef DCPOMATIC_LINUX
295 locale->AddCatalogLookupPathPrefix (LINUX_LOCALE_PREFIX);
297 /* We have to include the wxWidgets .mo in our distribution,
298 so we rename it to avoid clashes with any other installation
301 locale->AddCatalog (wxT ("dcpomatic2-wxstd"));
304 locale->AddCatalog (wxT ("libdcpomatic2-wx"));
305 locale->AddCatalog (wxT ("dcpomatic2"));
307 if (!locale->IsOk()) {
309 locale = new wxLocale (wxLANGUAGE_ENGLISH);
314 dcpomatic_setup_gettext_i18n (wx_to_std (locale->GetCanonicalName ()));
319 wx_get (wxSpinCtrl* w)
321 return w->GetValue ();
327 return w->GetSelection ();
331 wx_get (wxSpinCtrlDouble* w)
333 return w->GetValue ();
336 /** @param s String of the form Context|String
337 * @return translation, or String if no translation is available.
340 context_translation (wxString s)
342 wxString t = wxGetTranslation (s);
344 /* No translation; strip the context */
345 int c = t.Find (wxT ("|"));
346 if (c != wxNOT_FOUND) {
355 time_to_timecode (DCPTime t, double fps)
357 double w = t.seconds ();
358 int const h = (w / 3600);
360 int const m = (w / 60);
362 int const s = floor (w);
364 int const f = lrint (w * fps);
365 return wxString::Format (wxT("%02d:%02d:%02d.%02d"), h, m, s, f);
369 setup_audio_channels_choice (wxChoice* choice, int minimum)
371 vector<pair<string, string> > items;
372 for (int i = minimum; i <= 16; i += 2) {
374 items.push_back (make_pair (wx_to_std (_("2 - stereo")), locale_convert<string> (i)));
376 items.push_back (make_pair (wx_to_std (_("4 - L/C/R/Lfe")), locale_convert<string> (i)));
378 items.push_back (make_pair (wx_to_std (_("6 - 5.1")), locale_convert<string> (i)));
380 items.push_back (make_pair (wx_to_std (_("8 - 5.1/HI/VI")), locale_convert<string> (i)));
381 } else if (i == 12) {
382 items.push_back (make_pair (wx_to_std (_("12 - 7.1/HI/VI")), locale_convert<string> (i)));
384 items.push_back (make_pair (locale_convert<string> (i), locale_convert<string> (i)));
388 checked_set (choice, items);
394 wxSplashScreen* splash = 0;
396 if (!Config::have_existing ("config.xml")) {
398 boost::filesystem::path p = shared_path () / "splash.png";
399 if (bitmap.LoadFile (std_to_wx (p.string ()), wxBITMAP_TYPE_PNG)) {
400 splash = new wxSplashScreen (bitmap, wxSPLASH_CENTRE_ON_SCREEN | wxSPLASH_NO_TIMEOUT, 0, 0, -1);
404 } catch (boost::filesystem::filesystem_error& e) {
405 /* Maybe we couldn't find the splash image; never mind */
411 boost::filesystem::path
412 path_from_file_dialog (wxFileDialog* dialog, string extension)
414 return boost::filesystem::path(wx_to_std(dialog->GetPath())).replace_extension(extension);
418 calculate_mark_interval (double mark_interval)
420 if (mark_interval > 5) {
421 mark_interval -= lrint (mark_interval) % 5;
423 if (mark_interval > 10) {
424 mark_interval -= lrint (mark_interval) % 10;
426 if (mark_interval > 60) {
427 mark_interval -= lrint (mark_interval) % 60;
429 if (mark_interval > 3600) {
430 mark_interval -= lrint (mark_interval) % 3600;
433 if (mark_interval < 1) {
437 return mark_interval;