No-op; fix GPL address and use the explicit-program-name version.
[dcpomatic.git] / src / wx / wx_util.cc
1 /*
2     Copyright (C) 2012-2015 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/util.h"
29 #include <wx/spinctrl.h>
30 #include <boost/thread.hpp>
31
32 using namespace std;
33 using namespace boost;
34
35 /** Add a wxStaticText to a wxSizer, aligning it at vertical centre.
36  *  @param s Sizer to add to.
37  *  @param p Parent window for the wxStaticText.
38  *  @param t Text for the wxStaticText.
39  *  @param left true if this label is a `left label'; ie the sort
40  *  of label which should be right-aligned on OS X.
41  *  @param prop Proportion to pass when calling Add() on the wxSizer.
42  */
43 wxStaticText *
44 #ifdef __WXOSX__
45 add_label_to_sizer (wxSizer* s, wxWindow* p, wxString t, bool left, int prop)
46 #else
47 add_label_to_sizer (wxSizer* s, wxWindow* p, wxString t, bool, int prop)
48 #endif
49 {
50         int flags = wxALIGN_CENTER_VERTICAL | wxLEFT | wxRIGHT;
51 #ifdef __WXOSX__
52         if (left) {
53                 flags |= wxALIGN_RIGHT;
54                 t += wxT (":");
55         }
56 #endif
57         wxStaticText* m = new wxStaticText (p, wxID_ANY, t);
58         s->Add (m, prop, flags, 6);
59         return m;
60 }
61
62 wxStaticText *
63 #ifdef __WXOSX__
64 add_label_to_sizer (wxGridBagSizer* s, wxWindow* p, wxString t, bool left, wxGBPosition pos, wxGBSpan span)
65 #else
66 add_label_to_sizer (wxGridBagSizer* s, wxWindow* p, wxString t, bool, wxGBPosition pos, wxGBSpan span)
67 #endif
68 {
69         int flags = wxALIGN_CENTER_VERTICAL | wxLEFT | wxRIGHT;
70 #ifdef __WXOSX__
71         if (left) {
72                 flags |= wxALIGN_RIGHT;
73                 t += wxT (":");
74         }
75 #endif
76         wxStaticText* m = new wxStaticText (p, wxID_ANY, t);
77         s->Add (m, pos, span, flags);
78         return m;
79 }
80
81 /** Pop up an error dialogue box.
82  *  @param parent Parent.
83  *  @param m Message.
84  */
85 void
86 error_dialog (wxWindow* parent, wxString m)
87 {
88         wxMessageDialog* d = new wxMessageDialog (parent, m, _("DCP-o-matic"), wxOK | wxICON_ERROR);
89         d->ShowModal ();
90         d->Destroy ();
91 }
92
93 /** Pop up an error dialogue box.
94  *  @param parent Parent.
95  *  @param m Message.
96  */
97 void
98 message_dialog (wxWindow* parent, wxString m)
99 {
100         wxMessageDialog* d = new wxMessageDialog (parent, m, _("DCP-o-matic"), wxOK | wxICON_INFORMATION);
101         d->ShowModal ();
102         d->Destroy ();
103 }
104
105 bool
106 confirm_dialog (wxWindow* parent, wxString m)
107 {
108         wxMessageDialog* d = new wxMessageDialog (parent, m, _("DCP-o-matic"), wxYES_NO | wxICON_QUESTION);
109         int const r = d->ShowModal ();
110         d->Destroy ();
111         return r == wxID_YES;
112 }
113
114
115 /** @param s wxWidgets string.
116  *  @return Corresponding STL string.
117  */
118 string
119 wx_to_std (wxString s)
120 {
121         return string (s.ToUTF8 ());
122 }
123
124 /** @param s STL string.
125  *  @return Corresponding wxWidgets string.
126  */
127 wxString
128 std_to_wx (string s)
129 {
130         return wxString (s.c_str(), wxConvUTF8);
131 }
132
133 string
134 string_client_data (wxClientData* o)
135 {
136         return wx_to_std (dynamic_cast<wxStringClientData*>(o)->GetData());
137 }
138
139 void
140 checked_set (FilePickerCtrl* widget, boost::filesystem::path value)
141 {
142         if (widget->GetPath() != std_to_wx (value.string())) {
143                 if (value.empty()) {
144                         /* Hack to make wxWidgets clear the control when we are passed
145                            an empty value.
146                         */
147                         value = " ";
148                 }
149                 widget->SetPath (std_to_wx (value.string()));
150         }
151 }
152
153 void
154 checked_set (wxSpinCtrl* widget, int value)
155 {
156         if (widget->GetValue() != value) {
157                 widget->SetValue (value);
158         }
159 }
160
161 void
162 checked_set (wxSpinCtrlDouble* widget, double value)
163 {
164         /* XXX: completely arbitrary epsilon */
165         if (fabs (widget->GetValue() - value) > 1e-16) {
166                 widget->SetValue (value);
167         }
168 }
169
170 void
171 checked_set (wxChoice* widget, int value)
172 {
173         if (widget->GetSelection() != value) {
174                 widget->SetSelection (value);
175         }
176 }
177
178 void
179 checked_set (wxChoice* widget, string value)
180 {
181         wxClientData* o = 0;
182         if (widget->GetSelection() != -1) {
183                 o = widget->GetClientObject (widget->GetSelection ());
184         }
185
186         if (!o || string_client_data(o) != value) {
187                 for (unsigned int i = 0; i < widget->GetCount(); ++i) {
188                         if (string_client_data (widget->GetClientObject (i)) == value) {
189                                 widget->SetSelection (i);
190                         }
191                 }
192         }
193 }
194
195 void
196 checked_set (wxChoice* widget, vector<pair<string, string> > items)
197 {
198        vector<pair<string, string> > current;
199        for (unsigned int i = 0; i < widget->GetCount(); ++i) {
200                current.push_back (
201                        make_pair (
202                                wx_to_std (widget->GetString (i)),
203                                string_client_data (widget->GetClientObject (i))
204                                )
205                        );
206        }
207
208        if (current == items) {
209                return;
210        }
211
212        widget->Clear ();
213        for (vector<pair<string, string> >::const_iterator i = items.begin(); i != items.end(); ++i) {
214                widget->Append (std_to_wx (i->first), new wxStringClientData (std_to_wx (i->second)));
215        }
216 }
217
218 void
219 checked_set (wxTextCtrl* widget, string value)
220 {
221         if (widget->GetValue() != std_to_wx (value)) {
222                 widget->ChangeValue (std_to_wx (value));
223         }
224 }
225
226 void
227 checked_set (wxTextCtrl* widget, wxString value)
228 {
229         if (widget->GetValue() != value) {
230                 widget->ChangeValue (value);
231         }
232 }
233
234 void
235 checked_set (wxStaticText* widget, string value)
236 {
237         if (widget->GetLabel() != std_to_wx (value)) {
238                 widget->SetLabel (std_to_wx (value));
239         }
240 }
241
242 void
243 checked_set (wxStaticText* widget, wxString value)
244 {
245         if (widget->GetLabel() != value) {
246                 widget->SetLabel (value);
247         }
248 }
249
250 void
251 checked_set (wxCheckBox* widget, bool value)
252 {
253         if (widget->GetValue() != value) {
254                 widget->SetValue (value);
255         }
256 }
257
258 void
259 checked_set (wxRadioButton* widget, bool value)
260 {
261         if (widget->GetValue() != value) {
262                 widget->SetValue (value);
263         }
264 }
265
266 void
267 dcpomatic_setup_i18n ()
268 {
269         int language = wxLANGUAGE_DEFAULT;
270
271         boost::optional<string> config_lang = Config::instance()->language ();
272         if (config_lang && !config_lang->empty ()) {
273                 wxLanguageInfo const * li = wxLocale::FindLanguageInfo (std_to_wx (config_lang.get ()));
274                 if (li) {
275                         language = li->Language;
276                 }
277         }
278
279         wxLocale* locale = 0;
280         if (wxLocale::IsAvailable (language)) {
281                 locale = new wxLocale (language, wxLOCALE_LOAD_DEFAULT);
282
283 #ifdef DCPOMATIC_WINDOWS
284                 locale->AddCatalogLookupPathPrefix (std_to_wx (mo_path().string()));
285 #endif
286
287 #ifdef DCPOMATIC_LINUX
288                 locale->AddCatalogLookupPathPrefix (LINUX_LOCALE_PREFIX);
289
290                 /* We have to include the wxWidgets .mo in our distribution,
291                    so we rename it to avoid clashes with any other installation
292                    of wxWidgets.
293                 */
294                 locale->AddCatalog (wxT ("dcpomatic2-wxstd"));
295 #endif
296
297                 locale->AddCatalog (wxT ("libdcpomatic2-wx"));
298                 locale->AddCatalog (wxT ("dcpomatic2"));
299
300                 if (!locale->IsOk()) {
301                         delete locale;
302                         locale = new wxLocale (wxLANGUAGE_ENGLISH);
303                 }
304         }
305
306         if (locale) {
307                 dcpomatic_setup_gettext_i18n (wx_to_std (locale->GetCanonicalName ()));
308         }
309 }
310
311 int
312 wx_get (wxSpinCtrl* w)
313 {
314         return w->GetValue ();
315 }
316
317 int
318 wx_get (wxChoice* w)
319 {
320         return w->GetSelection ();
321 }
322
323 double
324 wx_get (wxSpinCtrlDouble* w)
325 {
326         return w->GetValue ();
327 }
328
329 /** @param s String of the form Context|String
330  *  @return translation, or String if no translation is available.
331  */
332 wxString
333 context_translation (wxString s)
334 {
335         wxString t = wxGetTranslation (s);
336         if (t == s) {
337                 /* No translation; strip the context */
338                 int c = t.Find (wxT ("|"));
339                 if (c != wxNOT_FOUND) {
340                         t = t.Mid (c + 1);
341                 }
342         }
343
344         return t;
345 }
346
347 wxString
348 time_to_timecode (DCPTime t, double fps)
349 {
350         double w = t.seconds ();
351         int const h = (w / 3600);
352         w -= h * 3600;
353         int const m = (w / 60);
354         w -= m * 60;
355         int const s = floor (w);
356         w -= s;
357         int const f = lrint (w * fps);
358         return wxString::Format (wxT("%02d:%02d:%02d.%02d"), h, m, s, f);
359 }