2 Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
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.
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.
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.
20 /** @file src/wx/wx_util.cc
21 * @brief Some utility functions and classes.
24 #include <boost/thread.hpp>
25 #include <wx/filepicker.h>
26 #include <wx/spinctrl.h>
30 using namespace boost;
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 Properties to pass when calling Add() on the wxSizer.
39 add_label_to_sizer (wxSizer* s, wxWindow* p, string t, int prop)
41 wxStaticText* m = new wxStaticText (p, wxID_ANY, std_to_wx (t));
42 s->Add (m, prop, wxALIGN_CENTER_VERTICAL | wxALL, 6);
46 /** Pop up an error dialogue box.
47 * @param parent Parent.
51 error_dialog (wxWindow* parent, string m)
53 wxMessageDialog* d = new wxMessageDialog (parent, std_to_wx (m), wxT ("DVD-o-matic"), wxOK);
58 /** @param s wxWidgets string.
59 * @return Corresponding STL string.
62 wx_to_std (wxString s)
64 return string (s.mb_str ());
67 /** @param s STL string.
68 * @return Corresponding wxWidgets string.
73 return wxString (s.c_str(), wxConvUTF8);
76 int const ThreadedStaticText::_update_event_id = 10000;
78 /** @param parent Parent for the wxStaticText.
79 * @param initial Initial text for the wxStaticText while the computation is being run.
80 * @param fn Function which works out what the wxStaticText content should be and returns it.
82 ThreadedStaticText::ThreadedStaticText (wxWindow* parent, string initial, function<string ()> fn)
83 : wxStaticText (parent, wxID_ANY, std_to_wx (initial))
85 Connect (_update_event_id, wxEVT_COMMAND_TEXT_UPDATED, wxCommandEventHandler (ThreadedStaticText::thread_finished), 0, this);
86 _thread = new thread (bind (&ThreadedStaticText::run, this, fn));
89 ThreadedStaticText::~ThreadedStaticText ()
91 _thread->interrupt ();
96 /** Run our thread and post the result to the GUI thread via AddPendingEvent */
98 ThreadedStaticText::run (function<string ()> fn)
100 wxCommandEvent ev (wxEVT_COMMAND_TEXT_UPDATED, _update_event_id);
101 ev.SetString (std_to_wx (fn ()));
102 GetEventHandler()->AddPendingEvent (ev);
105 /** Called in the GUI thread when our worker thread has finished */
107 ThreadedStaticText::thread_finished (wxCommandEvent& ev)
109 SetLabel (ev.GetString ());
113 string_client_data (wxClientData* o)
115 return wx_to_std (dynamic_cast<wxStringClientData*>(o)->GetData());
119 checked_set (wxFilePickerCtrl* widget, string value)
121 if (widget->GetPath() != std_to_wx (value)) {
123 /* Hack to make wxWidgets clear the control when we are passed
128 widget->SetPath (std_to_wx (value));
133 checked_set (wxSpinCtrl* widget, int value)
135 if (widget->GetValue() != value) {
136 widget->SetValue (value);
141 checked_set (wxComboBox* widget, int value)
143 if (widget->GetSelection() != value) {
144 if (value == wxNOT_FOUND) {
145 /* Work around an apparent wxWidgets bug; SetSelection (wxNOT_FOUND)
146 appears not to work sometimes.
148 widget->SetValue (wxT (""));
150 widget->SetSelection (value);
156 checked_set (wxComboBox* widget, string value)
158 wxClientData* o = widget->GetClientObject (widget->GetSelection ());
160 if (!o || string_client_data(o) != value) {
161 for (unsigned int i = 0; i < widget->GetCount(); ++i) {
162 if (string_client_data (widget->GetClientObject (i)) == value) {
163 widget->SetSelection (i);
170 checked_set (wxTextCtrl* widget, string value)
172 if (widget->GetValue() != std_to_wx (value)) {
173 widget->ChangeValue (std_to_wx (value));
178 checked_set (wxCheckBox* widget, bool value)
180 if (widget->GetValue() != value) {
181 widget->SetValue (value);
186 checked_set (wxRadioButton* widget, bool value)
188 if (widget->GetValue() != value) {
189 widget->SetValue (value);