GUI and metadata for external audio.
[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 Properties to pass when calling Add() on the wxSizer.
37  */
38 wxStaticText *
39 add_label_to_sizer (wxSizer* s, wxWindow* p, string t, int prop)
40 {
41         wxStaticText* m = new wxStaticText (p, wxID_ANY, std_to_wx (t));
42         s->Add (m, prop, wxALIGN_CENTER_VERTICAL | wxALL, 6);
43         return m;
44 }
45
46 /** Pop up an error dialogue box.
47  *  @param parent Parent.
48  *  @param m Message.
49  */
50 void
51 error_dialog (wxWindow* parent, string m)
52 {
53         wxMessageDialog* d = new wxMessageDialog (parent, std_to_wx (m), wxT ("DVD-o-matic"), wxOK);
54         d->ShowModal ();
55         d->Destroy ();
56 }
57
58 /** @param s wxWidgets string.
59  *  @return Corresponding STL string.
60  */
61 string
62 wx_to_std (wxString s)
63 {
64         return string (s.mb_str ());
65 }
66
67 /** @param s STL string.
68  *  @return Corresponding wxWidgets string.
69  */
70 wxString
71 std_to_wx (string s)
72 {
73         return wxString (s.c_str(), wxConvUTF8);
74 }
75
76 int const ThreadedStaticText::_update_event_id = 10000;
77
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.
81  */
82 ThreadedStaticText::ThreadedStaticText (wxWindow* parent, string initial, function<string ()> fn)
83         : wxStaticText (parent, wxID_ANY, std_to_wx (initial))
84 {
85         Connect (_update_event_id, wxEVT_COMMAND_TEXT_UPDATED, wxCommandEventHandler (ThreadedStaticText::thread_finished), 0, this);
86         _thread = new thread (bind (&ThreadedStaticText::run, this, fn));
87 }
88
89 ThreadedStaticText::~ThreadedStaticText ()
90 {
91         _thread->interrupt ();
92         _thread->join ();
93         delete _thread;
94 }
95
96 /** Run our thread and post the result to the GUI thread via AddPendingEvent */
97 void
98 ThreadedStaticText::run (function<string ()> fn)
99 {
100         wxCommandEvent ev (wxEVT_COMMAND_TEXT_UPDATED, _update_event_id);
101         ev.SetString (std_to_wx (fn ()));
102         GetEventHandler()->AddPendingEvent (ev);
103 }
104
105 /** Called in the GUI thread when our worker thread has finished */
106 void
107 ThreadedStaticText::thread_finished (wxCommandEvent& ev)
108 {
109         SetLabel (ev.GetString ());
110 }
111
112 void
113 checked_set (wxFilePickerCtrl* widget, string value)
114 {
115         if (widget->GetPath() != std_to_wx (value)) {
116                 widget->SetPath (std_to_wx (value));
117         }
118 }
119
120 void
121 checked_set (wxSpinCtrl* widget, int value)
122 {
123         if (widget->GetValue() != value) {
124                 widget->SetValue (value);
125         }
126 }
127
128 void
129 checked_set (wxComboBox* widget, int value)
130 {
131         if (widget->GetSelection() != value) {
132                 widget->SetSelection (value);
133         }
134 }
135
136 void
137 checked_set (wxTextCtrl* widget, string value)
138 {
139         if (widget->GetValue() != std_to_wx (value)) {
140                 widget->ChangeValue (std_to_wx (value));
141         }
142 }
143
144 void
145 checked_set (wxCheckBox* widget, bool value)
146 {
147         if (widget->GetValue() != value) {
148                 widget->SetValue (value);
149         }
150 }
151
152 void
153 checked_set (wxRadioButton* widget, bool value)
154 {
155         if (widget->GetValue() != value) {
156                 widget->SetValue (value);
157         }
158 }