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 Proportion to pass when calling Add() on the wxSizer.
39 add_label_to_sizer (wxSizer* s, wxWindow* p, wxString t, int prop)
41 wxStaticText* m = new wxStaticText (p, wxID_ANY, t);
42 s->Add (m, prop, wxALIGN_CENTER_VERTICAL | wxALL, 6);
47 add_label_to_grid_bag_sizer (wxGridBagSizer* s, wxWindow* p, wxString t, wxGBPosition pos, wxGBSpan span)
49 wxStaticText* m = new wxStaticText (p, wxID_ANY, t);
50 s->Add (m, pos, span, wxALIGN_CENTER_VERTICAL | wxALL, 6);
54 /** Pop up an error dialogue box.
55 * @param parent Parent.
59 error_dialog (wxWindow* parent, wxString m)
61 wxMessageDialog* d = new wxMessageDialog (parent, m, _("DVD-o-matic"), wxOK);
66 /** @param s wxWidgets string.
67 * @return Corresponding STL string.
70 wx_to_std (wxString s)
72 return string (s.mb_str ());
75 /** @param s STL string.
76 * @return Corresponding wxWidgets string.
81 return wxString (s.c_str(), wxConvUTF8);
84 int const ThreadedStaticText::_update_event_id = 10000;
86 /** @param parent Parent for the wxStaticText.
87 * @param initial Initial text for the wxStaticText while the computation is being run.
88 * @param fn Function which works out what the wxStaticText content should be and returns it.
90 ThreadedStaticText::ThreadedStaticText (wxWindow* parent, wxString initial, function<string ()> fn)
91 : wxStaticText (parent, wxID_ANY, initial)
93 Connect (_update_event_id, wxEVT_COMMAND_TEXT_UPDATED, wxCommandEventHandler (ThreadedStaticText::thread_finished), 0, this);
94 _thread = new thread (bind (&ThreadedStaticText::run, this, fn));
97 ThreadedStaticText::~ThreadedStaticText ()
99 _thread->interrupt ();
104 /** Run our thread and post the result to the GUI thread via AddPendingEvent */
106 ThreadedStaticText::run (function<string ()> fn)
108 wxCommandEvent ev (wxEVT_COMMAND_TEXT_UPDATED, _update_event_id);
109 ev.SetString (std_to_wx (fn ()));
110 GetEventHandler()->AddPendingEvent (ev);
113 /** Called in the GUI thread when our worker thread has finished */
115 ThreadedStaticText::thread_finished (wxCommandEvent& ev)
117 SetLabel (ev.GetString ());
121 string_client_data (wxClientData* o)
123 return wx_to_std (dynamic_cast<wxStringClientData*>(o)->GetData());
127 checked_set (wxFilePickerCtrl* widget, string value)
129 if (widget->GetPath() != std_to_wx (value)) {
131 /* Hack to make wxWidgets clear the control when we are passed
136 widget->SetPath (std_to_wx (value));
141 checked_set (wxSpinCtrl* widget, int value)
143 if (widget->GetValue() != value) {
144 widget->SetValue (value);
149 checked_set (wxChoice* widget, int value)
151 if (widget->GetSelection() != value) {
152 widget->SetSelection (value);
157 checked_set (wxChoice* widget, string value)
160 if (widget->GetSelection() != -1) {
161 o = widget->GetClientObject (widget->GetSelection ());
164 if (!o || string_client_data(o) != value) {
165 for (unsigned int i = 0; i < widget->GetCount(); ++i) {
166 if (string_client_data (widget->GetClientObject (i)) == value) {
167 widget->SetSelection (i);
174 checked_set (wxTextCtrl* widget, string value)
176 if (widget->GetValue() != std_to_wx (value)) {
177 widget->ChangeValue (std_to_wx (value));
182 checked_set (wxStaticText* widget, string value)
184 if (widget->GetLabel() != std_to_wx (value)) {
185 widget->SetLabel (std_to_wx (value));
190 checked_set (wxCheckBox* widget, bool value)
192 if (widget->GetValue() != value) {
193 widget->SetValue (value);
198 checked_set (wxRadioButton* widget, bool value)
200 if (widget->GetValue() != value) {
201 widget->SetValue (value);