Merge master.
[dcpomatic.git] / src / wx / wx_util.cc
1 /*
2     Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 /** @file src/wx/wx_util.cc
21  *  @brief Some utility functions and classes.
22  */
23
24 #include <boost/thread.hpp>
25 #include <wx/filepicker.h>
26 #include <wx/spinctrl.h>
27 #include "wx_util.h"
28 #include "config.h"
29 #include "util.h"
30
31 using namespace std;
32 using namespace boost;
33
34 /** Add a wxStaticText to a wxSizer, aligning it at vertical centre.
35  *  @param s Sizer to add to.
36  *  @param p Parent window for the wxStaticText.
37  *  @param t Text for the wxStaticText.
38  *  @param left true if this label is a `left label'; ie the sort
39  *  of label which should be right-aligned on OS X.
40  *  @param prop Proportion to pass when calling Add() on the wxSizer.
41  */
42 wxStaticText *
43 add_label_to_sizer (wxSizer* s, wxWindow* p, wxString t, bool left, int prop)
44 {
45         int flags = wxALIGN_CENTER_VERTICAL | wxLEFT | wxRIGHT;
46 #ifdef __WXOSX__
47         if (left) {
48                 flags |= wxALIGN_RIGHT;
49                 t += wxT (":");
50         }
51 #endif  
52         wxStaticText* m = new wxStaticText (p, wxID_ANY, t);
53         s->Add (m, prop, flags, 6);
54         return m;
55 }
56
57 wxStaticText *
58 add_label_to_grid_bag_sizer (wxGridBagSizer* s, wxWindow* p, wxString t, bool left, wxGBPosition pos, wxGBSpan span)
59 {
60         int flags = wxALIGN_CENTER_VERTICAL | wxLEFT | wxRIGHT;
61 #ifdef __WXOSX__
62         if (left) {
63                 flags |= wxALIGN_RIGHT;
64                 t += wxT (":");
65         }
66 #endif  
67         wxStaticText* m = new wxStaticText (p, wxID_ANY, t);
68         s->Add (m, pos, span, flags);
69         return m;
70 }
71
72 /** Pop up an error dialogue box.
73  *  @param parent Parent.
74  *  @param m Message.
75  */
76 void
77 error_dialog (wxWindow* parent, wxString m)
78 {
79         wxMessageDialog* d = new wxMessageDialog (parent, m, _("DCP-o-matic"), wxOK);
80         d->ShowModal ();
81         d->Destroy ();
82 }
83
84 bool
85 confirm_dialog (wxWindow* parent, wxString m)
86 {
87         wxMessageDialog* d = new wxMessageDialog (parent, m, _("DCP-o-matic"), wxYES_NO | wxICON_QUESTION);
88         int const r = d->ShowModal ();
89         d->Destroy ();
90         return r == wxID_YES;
91 }
92         
93
94 /** @param s wxWidgets string.
95  *  @return Corresponding STL string.
96  */
97 string
98 wx_to_std (wxString s)
99 {
100         return string (s.mb_str ());
101 }
102
103 /** @param s STL string.
104  *  @return Corresponding wxWidgets string.
105  */
106 wxString
107 std_to_wx (string s)
108 {
109         return wxString (s.c_str(), wxConvUTF8);
110 }
111
112 int const ThreadedStaticText::_update_event_id = 10000;
113
114 /** @param parent Parent for the wxStaticText.
115  *  @param initial Initial text for the wxStaticText while the computation is being run.
116  *  @param fn Function which works out what the wxStaticText content should be and returns it.
117  */
118 ThreadedStaticText::ThreadedStaticText (wxWindow* parent, wxString initial, function<string ()> fn)
119         : wxStaticText (parent, wxID_ANY, initial)
120 {
121         Connect (_update_event_id, wxEVT_COMMAND_TEXT_UPDATED, wxCommandEventHandler (ThreadedStaticText::thread_finished), 0, this);
122         _thread = new thread (bind (&ThreadedStaticText::run, this, fn));
123 }
124
125 ThreadedStaticText::~ThreadedStaticText ()
126 {
127         _thread->interrupt ();
128         _thread->join ();
129         delete _thread;
130 }
131
132 /** Run our thread and post the result to the GUI thread via AddPendingEvent */
133 void
134 ThreadedStaticText::run (function<string ()> fn)
135 {
136         wxCommandEvent ev (wxEVT_COMMAND_TEXT_UPDATED, _update_event_id);
137         ev.SetString (std_to_wx (fn ()));
138         GetEventHandler()->AddPendingEvent (ev);
139 }
140
141 /** Called in the GUI thread when our worker thread has finished */
142 void
143 ThreadedStaticText::thread_finished (wxCommandEvent& ev)
144 {
145         SetLabel (ev.GetString ());
146 }
147
148 string
149 string_client_data (wxClientData* o)
150 {
151         return wx_to_std (dynamic_cast<wxStringClientData*>(o)->GetData());
152 }
153
154 void
155 checked_set (wxFilePickerCtrl* widget, string value)
156 {
157         if (widget->GetPath() != std_to_wx (value)) {
158                 if (value.empty()) {
159                         /* Hack to make wxWidgets clear the control when we are passed
160                            an empty value.
161                         */
162                         value = " ";
163                 }
164                 widget->SetPath (std_to_wx (value));
165         }
166 }
167
168 void
169 checked_set (wxSpinCtrl* widget, int value)
170 {
171         if (widget->GetValue() != value) {
172                 widget->SetValue (value);
173         }
174 }
175
176 void
177 checked_set (wxChoice* widget, int value)
178 {
179         if (widget->GetSelection() != value) {
180                 widget->SetSelection (value);
181         }
182 }
183
184 void
185 checked_set (wxChoice* widget, string value)
186 {
187         wxClientData* o = 0;
188         if (widget->GetSelection() != -1) {
189                 o = widget->GetClientObject (widget->GetSelection ());
190         }
191         
192         if (!o || string_client_data(o) != value) {
193                 for (unsigned int i = 0; i < widget->GetCount(); ++i) {
194                         if (string_client_data (widget->GetClientObject (i)) == value) {
195                                 widget->SetSelection (i);
196                         }
197                 }
198         }
199 }
200
201 void
202 checked_set (wxTextCtrl* widget, string value)
203 {
204         if (widget->GetValue() != std_to_wx (value)) {
205                 widget->ChangeValue (std_to_wx (value));
206         }
207 }
208
209 void
210 checked_set (wxStaticText* widget, string value)
211 {
212         if (widget->GetLabel() != std_to_wx (value)) {
213                 widget->SetLabel (std_to_wx (value));
214         }
215 }
216
217 void
218 checked_set (wxCheckBox* widget, bool value)
219 {
220         if (widget->GetValue() != value) {
221                 widget->SetValue (value);
222         }
223 }
224
225 void
226 checked_set (wxRadioButton* widget, bool value)
227 {
228         if (widget->GetValue() != value) {
229                 widget->SetValue (value);
230         }
231 }
232
233 void
234 dcpomatic_setup_i18n ()
235 {
236         int language = wxLANGUAGE_DEFAULT;
237
238         boost::optional<string> config_lang = Config::instance()->language ();
239         if (config_lang && !config_lang->empty ()) {
240                 wxLanguageInfo const * li = wxLocale::FindLanguageInfo (std_to_wx (config_lang.get ()));
241                 if (li) {
242                         language = li->Language;
243                 }
244         }
245
246         wxLocale* locale = 0;
247         if (wxLocale::IsAvailable (language)) {
248                 locale = new wxLocale (language, wxLOCALE_LOAD_DEFAULT);
249
250 #ifdef DCPOMATIC_WINDOWS
251                 locale->AddCatalogLookupPathPrefix (std_to_wx (mo_path().string()));
252 #endif          
253
254                 locale->AddCatalog (wxT ("libdcpomatic-wx"));
255                 locale->AddCatalog (wxT ("dcpomatic"));
256                 
257                 if (!locale->IsOk()) {
258                         delete locale;
259                         locale = new wxLocale (wxLANGUAGE_ENGLISH);
260                         language = wxLANGUAGE_ENGLISH;
261                 }
262         }
263
264         if (locale) {
265                 dcpomatic_setup_gettext_i18n (wx_to_std (locale->GetCanonicalName ()));
266         }
267 }