Merge branch 'master' into 1.0
[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
29 using namespace std;
30 using namespace boost;
31
32 /** Add a wxStaticText to a wxSizer, aligning it at vertical centre.
33  *  @param s Sizer to add to.
34  *  @param p Parent window for the wxStaticText.
35  *  @param t Text for the wxStaticText.
36  *  @param prop Proportion to pass when calling Add() on the wxSizer.
37  */
38 wxStaticText *
39 add_label_to_sizer (wxSizer* s, wxWindow* p, wxString t, int prop)
40 {
41         wxStaticText* m = new wxStaticText (p, wxID_ANY, t);
42         s->Add (m, prop, wxALIGN_CENTER_VERTICAL | wxALL, 6);
43         return m;
44 }
45
46 wxStaticText *
47 add_label_to_grid_bag_sizer (wxGridBagSizer* s, wxWindow* p, wxString t, wxGBPosition pos, wxGBSpan span)
48 {
49         wxStaticText* m = new wxStaticText (p, wxID_ANY, t);
50         s->Add (m, pos, span, wxALIGN_CENTER_VERTICAL | wxALL, 6);
51         return m;
52 }
53
54 /** Pop up an error dialogue box.
55  *  @param parent Parent.
56  *  @param m Message.
57  */
58 void
59 error_dialog (wxWindow* parent, wxString m)
60 {
61         wxMessageDialog* d = new wxMessageDialog (parent, m, _("DCP-o-matic"), wxOK);
62         d->ShowModal ();
63         d->Destroy ();
64 }
65
66 bool
67 confirm_dialog (wxWindow* parent, wxString m)
68 {
69         wxMessageDialog* d = new wxMessageDialog (parent, m, _("DVD-o-matic"), wxYES_NO | wxICON_QUESTION);
70         int const r = d->ShowModal ();
71         d->Destroy ();
72         return r == wxID_YES;
73 }
74         
75
76 /** @param s wxWidgets string.
77  *  @return Corresponding STL string.
78  */
79 string
80 wx_to_std (wxString s)
81 {
82         return string (s.mb_str ());
83 }
84
85 /** @param s STL string.
86  *  @return Corresponding wxWidgets string.
87  */
88 wxString
89 std_to_wx (string s)
90 {
91         return wxString (s.c_str(), wxConvUTF8);
92 }
93
94 int const ThreadedStaticText::_update_event_id = 10000;
95
96 /** @param parent Parent for the wxStaticText.
97  *  @param initial Initial text for the wxStaticText while the computation is being run.
98  *  @param fn Function which works out what the wxStaticText content should be and returns it.
99  */
100 ThreadedStaticText::ThreadedStaticText (wxWindow* parent, wxString initial, function<string ()> fn)
101         : wxStaticText (parent, wxID_ANY, initial)
102 {
103         Connect (_update_event_id, wxEVT_COMMAND_TEXT_UPDATED, wxCommandEventHandler (ThreadedStaticText::thread_finished), 0, this);
104         _thread = new thread (bind (&ThreadedStaticText::run, this, fn));
105 }
106
107 ThreadedStaticText::~ThreadedStaticText ()
108 {
109         _thread->interrupt ();
110         _thread->join ();
111         delete _thread;
112 }
113
114 /** Run our thread and post the result to the GUI thread via AddPendingEvent */
115 void
116 ThreadedStaticText::run (function<string ()> fn)
117 {
118         wxCommandEvent ev (wxEVT_COMMAND_TEXT_UPDATED, _update_event_id);
119         ev.SetString (std_to_wx (fn ()));
120         GetEventHandler()->AddPendingEvent (ev);
121 }
122
123 /** Called in the GUI thread when our worker thread has finished */
124 void
125 ThreadedStaticText::thread_finished (wxCommandEvent& ev)
126 {
127         SetLabel (ev.GetString ());
128 }
129
130 string
131 string_client_data (wxClientData* o)
132 {
133         return wx_to_std (dynamic_cast<wxStringClientData*>(o)->GetData());
134 }
135
136 void
137 checked_set (wxFilePickerCtrl* widget, string value)
138 {
139         if (widget->GetPath() != std_to_wx (value)) {
140                 if (value.empty()) {
141                         /* Hack to make wxWidgets clear the control when we are passed
142                            an empty value.
143                         */
144                         value = " ";
145                 }
146                 widget->SetPath (std_to_wx (value));
147         }
148 }
149
150 void
151 checked_set (wxSpinCtrl* widget, int value)
152 {
153         if (widget->GetValue() != value) {
154                 widget->SetValue (value);
155         }
156 }
157
158 void
159 checked_set (wxChoice* widget, int value)
160 {
161         if (widget->GetSelection() != value) {
162                 widget->SetSelection (value);
163         }
164 }
165
166 void
167 checked_set (wxChoice* widget, string value)
168 {
169         wxClientData* o = 0;
170         if (widget->GetSelection() != -1) {
171                 o = widget->GetClientObject (widget->GetSelection ());
172         }
173         
174         if (!o || string_client_data(o) != value) {
175                 for (unsigned int i = 0; i < widget->GetCount(); ++i) {
176                         if (string_client_data (widget->GetClientObject (i)) == value) {
177                                 widget->SetSelection (i);
178                         }
179                 }
180         }
181 }
182
183 void
184 checked_set (wxTextCtrl* widget, string value)
185 {
186         if (widget->GetValue() != std_to_wx (value)) {
187                 widget->ChangeValue (std_to_wx (value));
188         }
189 }
190
191 void
192 checked_set (wxStaticText* widget, string value)
193 {
194         if (widget->GetLabel() != std_to_wx (value)) {
195                 widget->SetLabel (std_to_wx (value));
196         }
197 }
198
199 void
200 checked_set (wxCheckBox* widget, bool value)
201 {
202         if (widget->GetValue() != value) {
203                 widget->SetValue (value);
204         }
205 }
206
207 void
208 checked_set (wxRadioButton* widget, bool value)
209 {
210         if (widget->GetValue() != value) {
211                 widget->SetValue (value);
212         }
213 }