ChangeLog.
[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 prop Proportion to pass when calling Add() on the wxSizer.
39  */
40 wxStaticText *
41 add_label_to_sizer (wxSizer* s, wxWindow* p, wxString t, int prop)
42 {
43         wxStaticText* m = new wxStaticText (p, wxID_ANY, t);
44         s->Add (m, prop, wxALIGN_CENTER_VERTICAL | wxALL, 6);
45         return m;
46 }
47
48 wxStaticText *
49 add_label_to_grid_bag_sizer (wxGridBagSizer* s, wxWindow* p, wxString t, wxGBPosition pos, wxGBSpan span)
50 {
51         wxStaticText* m = new wxStaticText (p, wxID_ANY, t);
52         s->Add (m, pos, span, wxALIGN_CENTER_VERTICAL | wxALL, 6);
53         return m;
54 }
55
56 /** Pop up an error dialogue box.
57  *  @param parent Parent.
58  *  @param m Message.
59  */
60 void
61 error_dialog (wxWindow* parent, wxString m)
62 {
63         wxMessageDialog* d = new wxMessageDialog (parent, m, _("DVD-o-matic"), wxOK);
64         d->ShowModal ();
65         d->Destroy ();
66 }
67
68 bool
69 confirm_dialog (wxWindow* parent, wxString m)
70 {
71         wxMessageDialog* d = new wxMessageDialog (parent, m, _("DVD-o-matic"), wxYES_NO | wxICON_QUESTION);
72         int const r = d->ShowModal ();
73         d->Destroy ();
74         return r == wxID_YES;
75 }
76         
77
78 /** @param s wxWidgets string.
79  *  @return Corresponding STL string.
80  */
81 string
82 wx_to_std (wxString s)
83 {
84         return string (s.mb_str ());
85 }
86
87 /** @param s STL string.
88  *  @return Corresponding wxWidgets string.
89  */
90 wxString
91 std_to_wx (string s)
92 {
93         return wxString (s.c_str(), wxConvUTF8);
94 }
95
96 int const ThreadedStaticText::_update_event_id = 10000;
97
98 /** @param parent Parent for the wxStaticText.
99  *  @param initial Initial text for the wxStaticText while the computation is being run.
100  *  @param fn Function which works out what the wxStaticText content should be and returns it.
101  */
102 ThreadedStaticText::ThreadedStaticText (wxWindow* parent, wxString initial, function<string ()> fn)
103         : wxStaticText (parent, wxID_ANY, initial)
104 {
105         Connect (_update_event_id, wxEVT_COMMAND_TEXT_UPDATED, wxCommandEventHandler (ThreadedStaticText::thread_finished), 0, this);
106         _thread = new thread (bind (&ThreadedStaticText::run, this, fn));
107 }
108
109 ThreadedStaticText::~ThreadedStaticText ()
110 {
111         _thread->interrupt ();
112         _thread->join ();
113         delete _thread;
114 }
115
116 /** Run our thread and post the result to the GUI thread via AddPendingEvent */
117 void
118 ThreadedStaticText::run (function<string ()> fn)
119 {
120         wxCommandEvent ev (wxEVT_COMMAND_TEXT_UPDATED, _update_event_id);
121         ev.SetString (std_to_wx (fn ()));
122         GetEventHandler()->AddPendingEvent (ev);
123 }
124
125 /** Called in the GUI thread when our worker thread has finished */
126 void
127 ThreadedStaticText::thread_finished (wxCommandEvent& ev)
128 {
129         SetLabel (ev.GetString ());
130 }
131
132 string
133 string_client_data (wxClientData* o)
134 {
135         return wx_to_std (dynamic_cast<wxStringClientData*>(o)->GetData());
136 }
137
138 void
139 checked_set (wxFilePickerCtrl* widget, string value)
140 {
141         if (widget->GetPath() != std_to_wx (value)) {
142                 if (value.empty()) {
143                         /* Hack to make wxWidgets clear the control when we are passed
144                            an empty value.
145                         */
146                         value = " ";
147                 }
148                 widget->SetPath (std_to_wx (value));
149         }
150 }
151
152 void
153 checked_set (wxSpinCtrl* widget, int value)
154 {
155         if (widget->GetValue() != value) {
156                 widget->SetValue (value);
157         }
158 }
159
160 void
161 checked_set (wxChoice* widget, int value)
162 {
163         if (widget->GetSelection() != value) {
164                 widget->SetSelection (value);
165         }
166 }
167
168 void
169 checked_set (wxChoice* widget, string value)
170 {
171         wxClientData* o = 0;
172         if (widget->GetSelection() != -1) {
173                 o = widget->GetClientObject (widget->GetSelection ());
174         }
175         
176         if (!o || string_client_data(o) != value) {
177                 for (unsigned int i = 0; i < widget->GetCount(); ++i) {
178                         if (string_client_data (widget->GetClientObject (i)) == value) {
179                                 widget->SetSelection (i);
180                         }
181                 }
182         }
183 }
184
185 void
186 checked_set (wxTextCtrl* widget, string value)
187 {
188         if (widget->GetValue() != std_to_wx (value)) {
189                 widget->ChangeValue (std_to_wx (value));
190         }
191 }
192
193 void
194 checked_set (wxStaticText* widget, string value)
195 {
196         if (widget->GetLabel() != std_to_wx (value)) {
197                 widget->SetLabel (std_to_wx (value));
198         }
199 }
200
201 void
202 checked_set (wxCheckBox* widget, bool value)
203 {
204         if (widget->GetValue() != value) {
205                 widget->SetValue (value);
206         }
207 }
208
209 void
210 checked_set (wxRadioButton* widget, bool value)
211 {
212         if (widget->GetValue() != value) {
213                 widget->SetValue (value);
214         }
215 }
216
217 void
218 dvdomatic_setup_i18n ()
219 {
220         int language = wxLANGUAGE_DEFAULT;
221
222         boost::optional<string> config_lang = Config::instance()->language ();
223         if (config_lang && !config_lang->empty ()) {
224                 wxLanguageInfo const * li = wxLocale::FindLanguageInfo (std_to_wx (config_lang.get ()));
225                 if (li) {
226                         language = li->Language;
227                 }
228         }
229
230         wxLocale* locale = 0;
231         if (wxLocale::IsAvailable (language)) {
232                 locale = new wxLocale (language, wxLOCALE_LOAD_DEFAULT);
233
234 #ifdef DVDOMATIC_WINDOWS
235                 locale->AddCatalogLookupPathPrefix (std_to_wx (mo_path().string()));
236 #endif          
237
238                 locale->AddCatalog (wxT ("libdvdomatic-wx"));
239                 locale->AddCatalog (wxT ("dvdomatic"));
240                 
241                 if (!locale->IsOk()) {
242                         delete locale;
243                         locale = new wxLocale (wxLANGUAGE_ENGLISH);
244                         language = wxLANGUAGE_ENGLISH;
245                 }
246         }
247
248         if (locale) {
249                 dvdomatic_setup_gettext_i18n (wx_to_std (locale->GetCanonicalName ()));
250         }
251 }