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/>.
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 "lib/warnings.h"
35 #include <dcp/locale_convert.h>
36 DCPOMATIC_DISABLE_WARNINGS
37 #include <wx/spinctrl.h>
38 #include <wx/splash.h>
39 #include <wx/progdlg.h>
40 #include <wx/filepicker.h>
41 DCPOMATIC_ENABLE_WARNINGS
42 #include <boost/thread.hpp>
47 using boost::shared_ptr;
48 using boost::optional;
49 using dcp::locale_convert;
50 using namespace dcpomatic;
54 create_label (wxWindow* p, wxString t, bool left)
56 create_label (wxWindow* p, wxString t, bool)
64 return new StaticText (p, t);
67 /** Add a wxStaticText to a wxSizer, aligning it at vertical centre.
68 * @param s Sizer to add to.
69 * @param p Parent window for the wxStaticText.
70 * @param t Text for the wxStaticText.
71 * @param left true if this label is a `left label'; ie the sort
72 * of label which should be right-aligned on OS X.
73 * @param prop Proportion to pass when calling Add() on the wxSizer.
76 add_label_to_sizer (wxSizer* s, wxWindow* p, wxString t, bool left, int prop, int flags)
80 flags |= wxALIGN_RIGHT;
83 wxStaticText* m = create_label (p, t, left);
84 s->Add (m, prop, flags, 6);
90 add_label_to_sizer (wxSizer* s, wxStaticText* t, bool left, int prop, int flags)
92 add_label_to_sizer (wxSizer* s, wxStaticText* t, bool, int prop, int flags)
97 flags |= wxALIGN_RIGHT;
100 s->Add (t, prop, flags, 6);
105 add_label_to_sizer (wxGridBagSizer* s, wxWindow* p, wxString t, bool left, wxGBPosition pos, wxGBSpan span)
107 int flags = wxALIGN_CENTER_VERTICAL | wxLEFT | wxRIGHT;
110 flags |= wxALIGN_RIGHT;
113 wxStaticText* m = create_label (p, t, left);
114 s->Add (m, pos, span, flags);
120 add_label_to_sizer (wxGridBagSizer* s, wxStaticText* t, bool left, wxGBPosition pos, wxGBSpan span)
122 add_label_to_sizer (wxGridBagSizer* s, wxStaticText* t, bool, wxGBPosition pos, wxGBSpan span)
125 int flags = wxALIGN_CENTER_VERTICAL | wxLEFT | wxRIGHT;
128 flags |= wxALIGN_RIGHT;
131 s->Add (t, pos, span, flags);
135 /** Pop up an error dialogue box.
136 * @param parent Parent.
138 * @param e Extended message.
141 error_dialog (wxWindow* parent, wxString m, optional<wxString> e)
143 wxMessageDialog* d = new wxMessageDialog (parent, m, _("DCP-o-matic"), wxOK | wxICON_ERROR);
146 em[0] = wxToupper (em[0]);
147 d->SetExtendedMessage (em);
153 /** Pop up an error dialogue box.
154 * @param parent Parent.
158 message_dialog (wxWindow* parent, wxString m)
160 wxMessageDialog* d = new wxMessageDialog (parent, m, _("DCP-o-matic"), wxOK | wxICON_INFORMATION);
165 /** @return true if the user answered "yes" */
167 confirm_dialog (wxWindow* parent, wxString m)
169 wxMessageDialog* d = new wxMessageDialog (parent, m, _("DCP-o-matic"), wxYES_NO | wxICON_QUESTION);
170 int const r = d->ShowModal ();
172 return r == wxID_YES;
176 /** @param s wxWidgets string.
177 * @return Corresponding STL string.
180 wx_to_std (wxString s)
182 return string (s.ToUTF8 ());
185 /** @param s STL string.
186 * @return Corresponding wxWidgets string.
191 return wxString (s.c_str(), wxConvUTF8);
195 string_client_data (wxClientData* o)
197 return wx_to_std (dynamic_cast<wxStringClientData*>(o)->GetData());
201 checked_set (FilePickerCtrl* widget, boost::filesystem::path value)
203 if (widget->GetPath() != std_to_wx (value.string())) {
205 /* Hack to make wxWidgets clear the control when we are passed
210 widget->SetPath (std_to_wx (value.string()));
215 checked_set (wxDirPickerCtrl* 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 (wxSpinCtrl* widget, int value)
231 if (widget->GetValue() != value) {
232 widget->SetValue (value);
237 checked_set (wxSpinCtrlDouble* widget, double value)
239 /* XXX: completely arbitrary epsilon */
240 if (fabs (widget->GetValue() - value) > 1e-16) {
241 widget->SetValue (value);
246 checked_set (wxChoice* widget, int value)
248 if (widget->GetSelection() != value) {
249 widget->SetSelection (value);
254 checked_set (wxChoice* widget, string value)
257 if (widget->GetSelection() != -1) {
258 o = widget->GetClientObject (widget->GetSelection ());
261 if (!o || string_client_data(o) != value) {
262 for (unsigned int i = 0; i < widget->GetCount(); ++i) {
263 if (string_client_data (widget->GetClientObject (i)) == value) {
264 widget->SetSelection (i);
271 checked_set (wxChoice* widget, vector<pair<string, string> > items)
273 vector<pair<string, string> > current;
274 for (unsigned int i = 0; i < widget->GetCount(); ++i) {
277 wx_to_std (widget->GetString (i)),
278 string_client_data (widget->GetClientObject (i))
283 if (current == items) {
288 for (vector<pair<string, string> >::const_iterator i = items.begin(); i != items.end(); ++i) {
289 widget->Append (std_to_wx (i->first), new wxStringClientData (std_to_wx (i->second)));
294 checked_set (wxTextCtrl* widget, string value)
296 if (widget->GetValue() != std_to_wx (value)) {
297 widget->ChangeValue (std_to_wx (value));
302 checked_set (PasswordEntry* entry, string value)
304 if (entry->get() != value) {
310 checked_set (wxTextCtrl* widget, wxString value)
312 if (widget->GetValue() != value) {
313 widget->ChangeValue (value);
318 checked_set (wxStaticText* widget, string value)
320 if (widget->GetLabel() != std_to_wx (value)) {
321 widget->SetLabel (std_to_wx (value));
326 checked_set (wxStaticText* widget, wxString value)
328 if (widget->GetLabel() != value) {
329 widget->SetLabel (value);
334 checked_set (wxCheckBox* widget, bool value)
336 if (widget->GetValue() != value) {
337 widget->SetValue (value);
342 checked_set (wxRadioButton* widget, bool value)
344 if (widget->GetValue() != value) {
345 widget->SetValue (value);
350 dcpomatic_setup_i18n ()
352 int language = wxLANGUAGE_DEFAULT;
354 boost::optional<string> config_lang = Config::instance()->language ();
355 if (config_lang && !config_lang->empty ()) {
356 wxLanguageInfo const * li = wxLocale::FindLanguageInfo (std_to_wx (config_lang.get ()));
358 language = li->Language;
362 wxLocale* locale = 0;
363 if (wxLocale::IsAvailable (language)) {
364 locale = new wxLocale (language, wxLOCALE_LOAD_DEFAULT);
366 #ifdef DCPOMATIC_WINDOWS
367 locale->AddCatalogLookupPathPrefix (std_to_wx (mo_path().string()));
370 #ifdef DCPOMATIC_LINUX
371 locale->AddCatalogLookupPathPrefix (LINUX_LOCALE_PREFIX);
373 /* We have to include the wxWidgets .mo in our distribution,
374 so we rename it to avoid clashes with any other installation
377 locale->AddCatalog (wxT ("dcpomatic2-wxstd"));
379 /* Fedora 29 (at least) installs wxstd3.mo instead of wxstd.mo */
380 locale->AddCatalog (wxT ("wxstd3"));
383 locale->AddCatalog (wxT ("libdcpomatic2-wx"));
384 locale->AddCatalog (wxT ("dcpomatic2"));
386 if (!locale->IsOk()) {
388 locale = new wxLocale (wxLANGUAGE_ENGLISH);
393 dcpomatic_setup_gettext_i18n (wx_to_std (locale->GetCanonicalName ()));
398 wx_get (wxSpinCtrl* w)
400 return w->GetValue ();
406 return w->GetSelection ();
410 wx_get (wxSpinCtrlDouble* w)
412 return w->GetValue ();
415 /** @param s String of the form Context|String
416 * @return translation, or String if no translation is available.
419 context_translation (wxString s)
421 wxString t = wxGetTranslation (s);
423 /* No translation; strip the context */
424 int c = t.Find (wxT ("|"));
425 if (c != wxNOT_FOUND) {
434 time_to_timecode (DCPTime t, double fps)
436 double w = t.seconds ();
437 int const h = (w / 3600);
439 int const m = (w / 60);
441 int const s = floor (w);
443 int const f = lrint (w * fps);
444 return wxString::Format (wxT("%02d:%02d:%02d.%02d"), h, m, s, f);
448 setup_audio_channels_choice (wxChoice* choice, int minimum)
450 vector<pair<string, string> > items;
451 for (int i = minimum; i <= 16; i += 2) {
453 items.push_back (make_pair (wx_to_std (_("2 - stereo")), locale_convert<string> (i)));
455 items.push_back (make_pair (wx_to_std (_("4 - L/C/R/Lfe")), locale_convert<string> (i)));
457 items.push_back (make_pair (wx_to_std (_("6 - 5.1")), locale_convert<string> (i)));
459 items.push_back (make_pair (wx_to_std (_("8 - 5.1/HI/VI")), locale_convert<string> (i)));
460 } else if (i == 12) {
461 items.push_back (make_pair (wx_to_std (_("12 - 7.1/HI/VI")), locale_convert<string> (i)));
463 items.push_back (make_pair (locale_convert<string> (i), locale_convert<string> (i)));
467 checked_set (choice, items);
473 wxSplashScreen* splash = 0;
476 if (bitmap.LoadFile(bitmap_path("splash"), wxBITMAP_TYPE_PNG)) {
477 splash = new wxSplashScreen (bitmap, wxSPLASH_CENTRE_ON_SCREEN | wxSPLASH_NO_TIMEOUT, 0, 0, -1);
480 } catch (boost::filesystem::filesystem_error& e) {
481 /* Maybe we couldn't find the splash image; never mind */
488 calculate_mark_interval (double mark_interval)
490 if (mark_interval > 5) {
491 mark_interval -= lrint (mark_interval) % 5;
493 if (mark_interval > 10) {
494 mark_interval -= lrint (mark_interval) % 10;
496 if (mark_interval > 60) {
497 mark_interval -= lrint (mark_interval) % 60;
499 if (mark_interval > 3600) {
500 mark_interval -= lrint (mark_interval) % 3600;
503 if (mark_interval < 1) {
507 return mark_interval;
511 /** @return false if the task was cancelled */
513 display_progress (wxString title, wxString task)
515 JobManager* jm = JobManager::instance ();
517 wxProgressDialog progress (title, task, 100, 0, wxPD_CAN_ABORT);
521 while (jm->work_to_do()) {
522 dcpomatic_sleep_seconds (1);
523 if (!progress.Pulse()) {
524 /* user pressed cancel */
525 BOOST_FOREACH (shared_ptr<Job> i, jm->get()) {
538 get_offsets (vector<Offset>& offsets)
540 offsets.push_back (Offset(_("UTC-11"), -11, 0));
541 offsets.push_back (Offset(_("UTC-10"), -10, 0));
542 offsets.push_back (Offset(_("UTC-9"), -9, 0));
543 offsets.push_back (Offset(_("UTC-8"), -8, 0));
544 offsets.push_back (Offset(_("UTC-7"), -7, 0));
545 offsets.push_back (Offset(_("UTC-6"), -6, 0));
546 offsets.push_back (Offset(_("UTC-5"), -5, 0));
547 offsets.push_back (Offset(_("UTC-4:30"), -4, 30));
548 offsets.push_back (Offset(_("UTC-4"), -4, 0));
549 offsets.push_back (Offset(_("UTC-3:30"), -3, 30));
550 offsets.push_back (Offset(_("UTC-3"), -3, 0));
551 offsets.push_back (Offset(_("UTC-2"), -2, 0));
552 offsets.push_back (Offset(_("UTC-1"), -1, 0));
553 int utc = offsets.size();
554 offsets.push_back (Offset(_("UTC") , 0, 0));
555 offsets.push_back (Offset(_("UTC+1"), 1, 0));
556 offsets.push_back (Offset(_("UTC+2"), 2, 0));
557 offsets.push_back (Offset(_("UTC+3"), 3, 0));
558 offsets.push_back (Offset(_("UTC+4"), 4, 0));
559 offsets.push_back (Offset(_("UTC+5"), 5, 0));
560 offsets.push_back (Offset(_("UTC+5:30"), 5, 30));
561 offsets.push_back (Offset(_("UTC+6"), 6, 0));
562 offsets.push_back (Offset(_("UTC+7"), 7, 0));
563 offsets.push_back (Offset(_("UTC+8"), 8, 0));
564 offsets.push_back (Offset(_("UTC+9"), 9, 0));
565 offsets.push_back (Offset(_("UTC+9:30"), 9, 30));
566 offsets.push_back (Offset(_("UTC+10"), 10, 0));
567 offsets.push_back (Offset(_("UTC+11"), 11, 0));
568 offsets.push_back (Offset(_("UTC+12"), 12, 0));
575 bitmap_path (string name)
577 boost::filesystem::path base;
579 #ifdef DCPOMATIC_DEBUG
580 /* Hack to allow Linux and OS X to find icons when running from the source tree */
581 char* path = getenv ("DCPOMATIC_GRAPHICS");
585 base = resources_path();
588 base = resources_path();
591 boost::filesystem::path p = base / String::compose("%1.png", name);
592 return std_to_wx (p.string());
597 small_button_size (wxWindow* parent, wxString text)
599 wxClientDC dc (parent);
600 wxSize size = dc.GetTextExtent (text);