Use Film/Playlist for SPL management rather than special classes.
[dcpomatic.git] / src / wx / wx_util.cc
1 /*
2     Copyright (C) 2012-2018 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
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.
10
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.
15
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/>.
18
19 */
20
21 /** @file src/wx/wx_util.cc
22  *  @brief Some utility functions and classes.
23  */
24
25 #include "wx_util.h"
26 #include "file_picker_ctrl.h"
27 #include "lib/config.h"
28 #include "lib/job_manager.h"
29 #include "lib/util.h"
30 #include "lib/cross.h"
31 #include "lib/job.h"
32 #include <dcp/locale_convert.h>
33 #include <wx/spinctrl.h>
34 #include <wx/splash.h>
35 #include <wx/progdlg.h>
36 #include <wx/filepicker.h>
37 #include <boost/thread.hpp>
38
39 using std::string;
40 using std::vector;
41 using std::pair;
42 using boost::shared_ptr;
43 using boost::optional;
44 using dcp::locale_convert;
45
46 wxStaticText *
47 #ifdef __WXOSX__
48 create_label (wxWindow* p, wxString t, bool left)
49 #else
50 create_label (wxWindow* p, wxString t, bool)
51 #endif
52 {
53 #ifdef __WXOSX__
54         if (left) {
55                 t += wxT (":");
56         }
57 #endif
58         return new wxStaticText (p, wxID_ANY, t);
59 }
60
61 /** Add a wxStaticText to a wxSizer, aligning it at vertical centre.
62  *  @param s Sizer to add to.
63  *  @param p Parent window for the wxStaticText.
64  *  @param t Text for the wxStaticText.
65  *  @param left true if this label is a `left label'; ie the sort
66  *  of label which should be right-aligned on OS X.
67  *  @param prop Proportion to pass when calling Add() on the wxSizer.
68  */
69 wxStaticText *
70 add_label_to_sizer (wxSizer* s, wxWindow* p, wxString t, bool left, int prop, int flags)
71 {
72 #ifdef __WXOSX__
73         if (left) {
74                 flags |= wxALIGN_RIGHT;
75         }
76 #endif
77         wxStaticText* m = create_label (p, t, left);
78         s->Add (m, prop, flags, 6);
79         return m;
80 }
81
82 wxStaticText *
83 #ifdef __WXOSX__
84 add_label_to_sizer (wxSizer* s, wxStaticText* t, bool left, int prop, int flags)
85 #else
86 add_label_to_sizer (wxSizer* s, wxStaticText* t, bool, int prop, int flags)
87 #endif
88 {
89 #ifdef __WXOSX__
90         if (left) {
91                 flags |= wxALIGN_RIGHT;
92         }
93 #endif
94         s->Add (t, prop, flags, 6);
95         return t;
96 }
97
98 wxStaticText *
99 add_label_to_sizer (wxGridBagSizer* s, wxWindow* p, wxString t, bool left, wxGBPosition pos, wxGBSpan span)
100 {
101         int flags = wxALIGN_CENTER_VERTICAL | wxLEFT | wxRIGHT;
102 #ifdef __WXOSX__
103         if (left) {
104                 flags |= wxALIGN_RIGHT;
105         }
106 #endif
107         wxStaticText* m = create_label (p, t, left);
108         s->Add (m, pos, span, flags);
109         return m;
110 }
111
112 wxStaticText *
113 #ifdef __WXOSX__
114 add_label_to_sizer (wxGridBagSizer* s, wxStaticText* t, bool left, wxGBPosition pos, wxGBSpan span)
115 #else
116 add_label_to_sizer (wxGridBagSizer* s, wxStaticText* t, bool, wxGBPosition pos, wxGBSpan span)
117 #endif
118 {
119         int flags = wxALIGN_CENTER_VERTICAL | wxLEFT | wxRIGHT;
120 #ifdef __WXOSX__
121         if (left) {
122                 flags |= wxALIGN_RIGHT;
123         }
124 #endif
125         s->Add (t, pos, span, flags);
126         return t;
127 }
128
129 /** Pop up an error dialogue box.
130  *  @param parent Parent.
131  *  @param m Message.
132  *  @param e Extended message.
133  */
134 void
135 error_dialog (wxWindow* parent, wxString m, optional<wxString> e)
136 {
137         wxMessageDialog* d = new wxMessageDialog (parent, m, _("DCP-o-matic"), wxOK | wxICON_ERROR);
138         if (e) {
139                 wxString em = *e;
140                 em[0] = wxToupper (em[0]);
141                 d->SetExtendedMessage (em);
142         }
143         d->ShowModal ();
144         d->Destroy ();
145 }
146
147 /** Pop up an error dialogue box.
148  *  @param parent Parent.
149  *  @param m Message.
150  */
151 void
152 message_dialog (wxWindow* parent, wxString m)
153 {
154         wxMessageDialog* d = new wxMessageDialog (parent, m, _("DCP-o-matic"), wxOK | wxICON_INFORMATION);
155         d->ShowModal ();
156         d->Destroy ();
157 }
158
159 bool
160 confirm_dialog (wxWindow* parent, wxString m)
161 {
162         wxMessageDialog* d = new wxMessageDialog (parent, m, _("DCP-o-matic"), wxYES_NO | wxICON_QUESTION);
163         int const r = d->ShowModal ();
164         d->Destroy ();
165         return r == wxID_YES;
166 }
167
168
169 /** @param s wxWidgets string.
170  *  @return Corresponding STL string.
171  */
172 string
173 wx_to_std (wxString s)
174 {
175         return string (s.ToUTF8 ());
176 }
177
178 /** @param s STL string.
179  *  @return Corresponding wxWidgets string.
180  */
181 wxString
182 std_to_wx (string s)
183 {
184         return wxString (s.c_str(), wxConvUTF8);
185 }
186
187 string
188 string_client_data (wxClientData* o)
189 {
190         return wx_to_std (dynamic_cast<wxStringClientData*>(o)->GetData());
191 }
192
193 void
194 checked_set (FilePickerCtrl* widget, boost::filesystem::path value)
195 {
196         if (widget->GetPath() != std_to_wx (value.string())) {
197                 if (value.empty()) {
198                         /* Hack to make wxWidgets clear the control when we are passed
199                            an empty value.
200                         */
201                         value = " ";
202                 }
203                 widget->SetPath (std_to_wx (value.string()));
204         }
205 }
206
207 void
208 checked_set (wxDirPickerCtrl* widget, boost::filesystem::path value)
209 {
210         if (widget->GetPath() != std_to_wx (value.string())) {
211                 if (value.empty()) {
212                         /* Hack to make wxWidgets clear the control when we are passed
213                            an empty value.
214                         */
215                         value = " ";
216                 }
217                 widget->SetPath (std_to_wx (value.string()));
218         }
219 }
220
221 void
222 checked_set (wxSpinCtrl* widget, int value)
223 {
224         if (widget->GetValue() != value) {
225                 widget->SetValue (value);
226         }
227 }
228
229 void
230 checked_set (wxSpinCtrlDouble* widget, double value)
231 {
232         /* XXX: completely arbitrary epsilon */
233         if (fabs (widget->GetValue() - value) > 1e-16) {
234                 widget->SetValue (value);
235         }
236 }
237
238 void
239 checked_set (wxChoice* widget, int value)
240 {
241         if (widget->GetSelection() != value) {
242                 widget->SetSelection (value);
243         }
244 }
245
246 void
247 checked_set (wxChoice* widget, string value)
248 {
249         wxClientData* o = 0;
250         if (widget->GetSelection() != -1) {
251                 o = widget->GetClientObject (widget->GetSelection ());
252         }
253
254         if (!o || string_client_data(o) != value) {
255                 for (unsigned int i = 0; i < widget->GetCount(); ++i) {
256                         if (string_client_data (widget->GetClientObject (i)) == value) {
257                                 widget->SetSelection (i);
258                         }
259                 }
260         }
261 }
262
263 void
264 checked_set (wxChoice* widget, vector<pair<string, string> > items)
265 {
266        vector<pair<string, string> > current;
267        for (unsigned int i = 0; i < widget->GetCount(); ++i) {
268                current.push_back (
269                        make_pair (
270                                wx_to_std (widget->GetString (i)),
271                                string_client_data (widget->GetClientObject (i))
272                                )
273                        );
274        }
275
276        if (current == items) {
277                return;
278        }
279
280        widget->Clear ();
281        for (vector<pair<string, string> >::const_iterator i = items.begin(); i != items.end(); ++i) {
282                widget->Append (std_to_wx (i->first), new wxStringClientData (std_to_wx (i->second)));
283        }
284 }
285
286 void
287 checked_set (wxTextCtrl* widget, string value)
288 {
289         if (widget->GetValue() != std_to_wx (value)) {
290                 widget->ChangeValue (std_to_wx (value));
291         }
292 }
293
294 void
295 checked_set (wxTextCtrl* widget, wxString value)
296 {
297         if (widget->GetValue() != value) {
298                 widget->ChangeValue (value);
299         }
300 }
301
302 void
303 checked_set (wxStaticText* widget, string value)
304 {
305         if (widget->GetLabel() != std_to_wx (value)) {
306                 widget->SetLabel (std_to_wx (value));
307         }
308 }
309
310 void
311 checked_set (wxStaticText* widget, wxString value)
312 {
313         if (widget->GetLabel() != value) {
314                 widget->SetLabel (value);
315         }
316 }
317
318 void
319 checked_set (wxCheckBox* widget, bool value)
320 {
321         if (widget->GetValue() != value) {
322                 widget->SetValue (value);
323         }
324 }
325
326 void
327 checked_set (wxRadioButton* widget, bool value)
328 {
329         if (widget->GetValue() != value) {
330                 widget->SetValue (value);
331         }
332 }
333
334 void
335 dcpomatic_setup_i18n ()
336 {
337         int language = wxLANGUAGE_DEFAULT;
338
339         boost::optional<string> config_lang = Config::instance()->language ();
340         if (config_lang && !config_lang->empty ()) {
341                 wxLanguageInfo const * li = wxLocale::FindLanguageInfo (std_to_wx (config_lang.get ()));
342                 if (li) {
343                         language = li->Language;
344                 }
345         }
346
347         wxLocale* locale = 0;
348         if (wxLocale::IsAvailable (language)) {
349                 locale = new wxLocale (language, wxLOCALE_LOAD_DEFAULT);
350
351 #ifdef DCPOMATIC_WINDOWS
352                 locale->AddCatalogLookupPathPrefix (std_to_wx (mo_path().string()));
353 #endif
354
355 #ifdef DCPOMATIC_LINUX
356                 locale->AddCatalogLookupPathPrefix (LINUX_LOCALE_PREFIX);
357
358                 /* We have to include the wxWidgets .mo in our distribution,
359                    so we rename it to avoid clashes with any other installation
360                    of wxWidgets.
361                 */
362                 locale->AddCatalog (wxT ("dcpomatic2-wxstd"));
363 #endif
364
365                 locale->AddCatalog (wxT ("libdcpomatic2-wx"));
366                 locale->AddCatalog (wxT ("dcpomatic2"));
367
368                 if (!locale->IsOk()) {
369                         delete locale;
370                         locale = new wxLocale (wxLANGUAGE_ENGLISH);
371                 }
372         }
373
374         if (locale) {
375                 dcpomatic_setup_gettext_i18n (wx_to_std (locale->GetCanonicalName ()));
376         }
377 }
378
379 int
380 wx_get (wxSpinCtrl* w)
381 {
382         return w->GetValue ();
383 }
384
385 int
386 wx_get (wxChoice* w)
387 {
388         return w->GetSelection ();
389 }
390
391 double
392 wx_get (wxSpinCtrlDouble* w)
393 {
394         return w->GetValue ();
395 }
396
397 /** @param s String of the form Context|String
398  *  @return translation, or String if no translation is available.
399  */
400 wxString
401 context_translation (wxString s)
402 {
403         wxString t = wxGetTranslation (s);
404         if (t == s) {
405                 /* No translation; strip the context */
406                 int c = t.Find (wxT ("|"));
407                 if (c != wxNOT_FOUND) {
408                         t = t.Mid (c + 1);
409                 }
410         }
411
412         return t;
413 }
414
415 wxString
416 time_to_timecode (DCPTime t, double fps)
417 {
418         double w = t.seconds ();
419         int const h = (w / 3600);
420         w -= h * 3600;
421         int const m = (w / 60);
422         w -= m * 60;
423         int const s = floor (w);
424         w -= s;
425         int const f = lrint (w * fps);
426         return wxString::Format (wxT("%02d:%02d:%02d.%02d"), h, m, s, f);
427 }
428
429 void
430 setup_audio_channels_choice (wxChoice* choice, int minimum)
431 {
432         vector<pair<string, string> > items;
433         for (int i = minimum; i <= 16; i += 2) {
434                 if (i == 2) {
435                         items.push_back (make_pair (wx_to_std (_("2 - stereo")), locale_convert<string> (i)));
436                 } else if (i == 4) {
437                         items.push_back (make_pair (wx_to_std (_("4 - L/C/R/Lfe")), locale_convert<string> (i)));
438                 } else if (i == 6) {
439                         items.push_back (make_pair (wx_to_std (_("6 - 5.1")), locale_convert<string> (i)));
440                 } else if (i == 8) {
441                         items.push_back (make_pair (wx_to_std (_("8 - 5.1/HI/VI")), locale_convert<string> (i)));
442                 } else if (i == 12) {
443                         items.push_back (make_pair (wx_to_std (_("12 - 7.1/HI/VI")), locale_convert<string> (i)));
444                 } else {
445                         items.push_back (make_pair (locale_convert<string> (i), locale_convert<string> (i)));
446                 }
447         }
448
449         checked_set (choice, items);
450 }
451
452 wxSplashScreen *
453 maybe_show_splash ()
454 {
455         wxSplashScreen* splash = 0;
456         try {
457                 if (!Config::have_existing ("config.xml")) {
458                         wxBitmap bitmap;
459                         boost::filesystem::path p = shared_path () / "splash.png";
460                         if (bitmap.LoadFile (std_to_wx (p.string ()), wxBITMAP_TYPE_PNG)) {
461                                 splash = new wxSplashScreen (bitmap, wxSPLASH_CENTRE_ON_SCREEN | wxSPLASH_NO_TIMEOUT, 0, 0, -1);
462                                 wxYield ();
463                         }
464                 }
465         } catch (boost::filesystem::filesystem_error& e) {
466                 /* Maybe we couldn't find the splash image; never mind */
467         }
468
469         return splash;
470 }
471
472 double
473 calculate_mark_interval (double mark_interval)
474 {
475         if (mark_interval > 5) {
476                 mark_interval -= lrint (mark_interval) % 5;
477         }
478         if (mark_interval > 10) {
479                 mark_interval -= lrint (mark_interval) % 10;
480         }
481         if (mark_interval > 60) {
482                 mark_interval -= lrint (mark_interval) % 60;
483         }
484         if (mark_interval > 3600) {
485                 mark_interval -= lrint (mark_interval) % 3600;
486         }
487
488         if (mark_interval < 1) {
489                 mark_interval = 1;
490         }
491
492         return mark_interval;
493 }
494
495
496 /** @return false if the task was cancelled */
497 bool
498 display_progress (wxString title, wxString task)
499 {
500         JobManager* jm = JobManager::instance ();
501
502         wxProgressDialog progress (title, task, 100, 0, wxPD_CAN_ABORT);
503
504         bool ok = true;
505
506         while (jm->work_to_do()) {
507                 dcpomatic_sleep (1);
508                 if (!progress.Pulse()) {
509                         /* user pressed cancel */
510                         BOOST_FOREACH (shared_ptr<Job> i, jm->get()) {
511                                 i->cancel();
512                         }
513                         ok = false;
514                         break;
515                 }
516         }
517
518         return ok;
519 }
520
521 bool
522 report_errors_from_last_job (wxWindow* parent)
523 {
524         JobManager* jm = JobManager::instance ();
525
526         DCPOMATIC_ASSERT (!jm->get().empty());
527
528         shared_ptr<Job> last = jm->get().back();
529         if (last->finished_in_error()) {
530                 error_dialog(parent, std_to_wx(last->error_summary()) + ".\n", std_to_wx(last->error_details()));
531                 return false;
532         }
533
534         return true;
535 }