1 //
2 // Copyright 2005 The Android Open Source Project
3 //
4 // Preferences modal dialog.
5 //
6
7 // For compilers that support precompilation, include "wx/wx.h".
8 #include "wx/wxprec.h"
9 // Otherwise, include all standard headers
10 #ifndef WX_PRECOMP
11 # include "wx/wx.h"
12 #endif
13
14 #include "PrefsDialog.h"
15 #include "Preferences.h"
16 #include "MyApp.h"
17 #include "Resource.h"
18
BEGIN_EVENT_TABLE(PrefsDialog,wxDialog)19 BEGIN_EVENT_TABLE(PrefsDialog, wxDialog)
20 END_EVENT_TABLE()
21
22 /*
23 * Constructor.
24 */
25 PrefsDialog::PrefsDialog(wxWindow* parent)
26 : wxDialog(parent, IDD_PREFS, wxT("Preferences"), wxDefaultPosition,
27 wxDefaultSize, wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER ),
28 mAutoPowerOn(false),
29 mGammaCorrection(1.0),
30 mEnableSound(true),
31 mEnableFakeCamera(true),
32 mLogLevel(0)
33 {
34 LoadPreferences();
35 CreateControls();
36 }
37
38 /*
39 * Destructor. Not much to do.
40 */
~PrefsDialog()41 PrefsDialog::~PrefsDialog()
42 {
43 }
44
45 /*
46 * Create all of the pages and add them to the notebook.
47 */
CreateControls(void)48 void PrefsDialog::CreateControls(void)
49 {
50 wxBoxSizer* mainSizer = new wxBoxSizer(wxVERTICAL);
51 wxBoxSizer* okCancelSizer = new wxBoxSizer(wxHORIZONTAL);
52 mNotebook.Create(this, wxID_ANY);
53 wxPanel* page;
54
55 /* pages added to notebook are owned by notebook */
56 page = CreateSimulatorPage(&mNotebook);
57 mNotebook.AddPage(page, wxT("Simulator"), true); // selected page
58 page = CreateRuntimePage(&mNotebook);
59 mNotebook.AddPage(page, wxT("Runtime"), false);
60
61 wxButton* cancel = new wxButton(this, wxID_CANCEL, wxT("&Cancel"),
62 wxDefaultPosition, wxDefaultSize, 0);
63 okCancelSizer->Add(cancel, 0, wxALL | wxALIGN_RIGHT, kInterSpacing);
64
65 wxButton* ok = new wxButton(this, wxID_OK, wxT("&OK"),
66 wxDefaultPosition, wxDefaultSize, 0);
67 okCancelSizer->Add(ok, 0, wxALL | wxALIGN_RIGHT, kInterSpacing);
68
69 mainSizer->Add(&mNotebook, 1, wxEXPAND);
70 mainSizer->Add(okCancelSizer, 0, wxALIGN_RIGHT);
71
72 SetSizer(mainSizer);
73
74 mainSizer->Fit(this); // shrink-to-fit
75 mainSizer->SetSizeHints(this); // define minimum size
76 }
77
78 /*
79 * Load preferences from config file
80 */
LoadPreferences(void)81 void PrefsDialog::LoadPreferences(void)
82 {
83 Preferences* pPrefs = ((MyApp*)wxTheApp)->GetPrefs();
84 assert(pPrefs != NULL);
85
86 /*
87 * Load preferences.
88 */
89 mConfigFile = ((MyApp*)wxTheApp)->GetConfigFileName();
90
91 pPrefs->GetDouble("gamma", &mGammaCorrection);
92 pPrefs->GetString("debugger", /*ref*/ mDebugger);
93 pPrefs->GetString("valgrinder", /*ref*/ mValgrinder);
94 pPrefs->GetBool("auto-power-on", &mAutoPowerOn);
95 pPrefs->GetBool("enable-sound", &mEnableSound);
96 pPrefs->GetBool("enable-fake-camera", &mEnableFakeCamera);
97 }
98
99 /*
100 * Transfer data from our members to the window controls.
101 *
102 * First we have to pull the data out of the preferences database.
103 * Anything that hasn't already been added with a default value will
104 * be given a default here, which may or may not match the default
105 * behavior elsewhere. The best solution to this is to define the
106 * default when the preferences file is created or read, so that we're
107 * never left guessing here.
108 */
TransferDataToWindow(void)109 bool PrefsDialog::TransferDataToWindow(void)
110 {
111 /*
112 * Do standard dialog setup.
113 */
114 wxTextCtrl* configFileName = (wxTextCtrl*) FindWindow(IDC_SPREFS_CONFIG_NAME);
115 wxTextCtrl* debugger = (wxTextCtrl*) FindWindow(IDC_SPREFS_DEBUGGER);
116 wxTextCtrl* valgrinder = (wxTextCtrl*) FindWindow(IDC_SPREFS_VALGRINDER);
117 wxCheckBox* autoPowerOn = (wxCheckBox*) FindWindow(IDC_SPREFS_AUTO_POWER_ON);
118 wxCheckBox* enableSound = (wxCheckBox*) FindWindow(IDC_RPREFS_ENABLE_SOUND);
119 wxCheckBox* enableFakeCamera = (wxCheckBox*) FindWindow(IDC_RPREFS_ENABLE_FAKE_CAMERA);
120
121 wxTextCtrl* gamma = (wxTextCtrl*) FindWindow(IDC_RPREFS_GAMMA);
122
123 configFileName->SetValue(mConfigFile);
124 debugger->SetValue(mDebugger);
125 valgrinder->SetValue(mValgrinder);
126 autoPowerOn->SetValue(mAutoPowerOn);
127 enableSound->SetValue(mEnableSound);
128 enableFakeCamera->SetValue(mEnableFakeCamera);
129
130 wxString tmpStr;
131 tmpStr.Printf(wxT("%.3f"), mGammaCorrection);
132 gamma->SetValue(tmpStr);
133
134 return true;
135 }
136
137 /*
138 * Transfer and validate data from the window controls.
139 *
140 * This doesn't get called if the user cancels out of the dialog.
141 */
TransferDataFromControls(void)142 bool PrefsDialog::TransferDataFromControls(void)
143 {
144 /*
145 * Do standard dialog export.
146 *
147 * We should error-check all of these.
148 */
149 // configName is read-only, don't need it here
150 wxTextCtrl* debugger = (wxTextCtrl*) FindWindow(IDC_SPREFS_DEBUGGER);
151 wxTextCtrl* valgrinder = (wxTextCtrl*) FindWindow(IDC_SPREFS_VALGRINDER);
152 wxCheckBox* autoPowerOn = (wxCheckBox*) FindWindow(IDC_SPREFS_AUTO_POWER_ON);
153 wxCheckBox* enableSound = (wxCheckBox*) FindWindow(IDC_RPREFS_ENABLE_SOUND);
154 wxCheckBox* enableFakeCamera = (wxCheckBox*) FindWindow(IDC_RPREFS_ENABLE_FAKE_CAMERA);
155
156 wxTextCtrl* gamma = (wxTextCtrl*) FindWindow(IDC_RPREFS_GAMMA);
157
158 mDebugger = debugger->GetValue();
159 mValgrinder = valgrinder->GetValue();
160 mAutoPowerOn = autoPowerOn->GetValue();
161 mEnableSound = enableSound->GetValue();
162 mEnableFakeCamera = enableFakeCamera->GetValue();
163
164 wxString tmpStr;
165 tmpStr = gamma->GetValue();
166 bool toDouble = tmpStr.ToDouble(&mGammaCorrection); // returns 0.0 on err; use strtof()?
167
168 if (!toDouble || mGammaCorrection <= 0.0 || mGammaCorrection > 2.0) {
169 wxMessageBox(wxT("Bad value for gamma -- must be > 0.0 and <= 2.0"),
170 wxT("Hoser"), wxOK, this);
171 return false;
172 }
173
174 return true;
175 }
176
177 /*
178 * Transfer preferences to config file
179 */
TransferDataFromWindow(void)180 bool PrefsDialog::TransferDataFromWindow(void)
181 {
182 Preferences* pPrefs = ((MyApp*)wxTheApp)->GetPrefs();
183 assert(pPrefs != NULL);
184
185 /*
186 * Grab the information from the controls and save in member field
187 */
188
189 if (!TransferDataFromControls())
190 return false;
191
192 pPrefs->SetString("debugger", mDebugger.ToAscii());
193 pPrefs->SetString("valgrinder", mValgrinder.ToAscii());
194 pPrefs->SetBool("auto-power-on", mAutoPowerOn);
195 pPrefs->SetBool("enable-sound", mEnableSound);
196 pPrefs->SetBool("enable-fake-camera", mEnableFakeCamera);
197
198 pPrefs->SetDouble("gamma", mGammaCorrection);
199
200 return true;
201 }
202
203
204 /*
205 * Create the Simulator Preferences page.
206 */
CreateSimulatorPage(wxBookCtrlBase * parent)207 wxPanel* PrefsDialog::CreateSimulatorPage(wxBookCtrlBase* parent)
208 {
209 wxPanel* panel = new wxPanel(parent);
210
211 wxStaticText* configNameDescr = new wxStaticText(panel, wxID_STATIC,
212 wxT("Config file:"));
213 wxTextCtrl* configName = new wxTextCtrl(panel, IDC_SPREFS_CONFIG_NAME,
214 wxT(""), wxDefaultPosition, wxDefaultSize, wxTE_READONLY);
215 // make it visibly different; unfortunately this kills scroll, copy&paste
216 configName->Enable(false);
217
218 wxStaticText* debuggerDescr = new wxStaticText(panel, wxID_STATIC,
219 wxT("Debugger:"));
220 wxTextCtrl* debugger = new wxTextCtrl(panel, IDC_SPREFS_DEBUGGER);
221
222 wxStaticText* valgrinderDescr = new wxStaticText(panel, wxID_STATIC,
223 wxT("Valgrind:"));
224 wxTextCtrl* valgrinder = new wxTextCtrl(panel, IDC_SPREFS_VALGRINDER);
225
226 wxCheckBox* autoPowerOn = new wxCheckBox(panel, IDC_SPREFS_AUTO_POWER_ON,
227 wxT("Boot runtime when simulator starts"));
228
229 wxBoxSizer* sizerPanel = new wxBoxSizer(wxVERTICAL);
230 sizerPanel->Add(kMinWidth, kEdgeSpacing); // forces minimum width
231 sizerPanel->Add(configNameDescr);
232 sizerPanel->Add(configName, 0, wxEXPAND);
233 sizerPanel->AddSpacer(kInterSpacing);
234 sizerPanel->AddSpacer(kInterSpacing);
235 sizerPanel->Add(debuggerDescr);
236 sizerPanel->Add(debugger, 0, wxEXPAND);
237 sizerPanel->AddSpacer(kInterSpacing);
238 sizerPanel->Add(valgrinderDescr);
239 sizerPanel->Add(valgrinder, 0, wxEXPAND);
240 sizerPanel->AddSpacer(kInterSpacing);
241 sizerPanel->Add(autoPowerOn);
242 sizerPanel->AddSpacer(kInterSpacing);
243
244 wxBoxSizer* horizIndent = new wxBoxSizer(wxHORIZONTAL);
245 horizIndent->AddSpacer(kEdgeSpacing);
246 horizIndent->Add(sizerPanel, wxSHAPED);
247 horizIndent->AddSpacer(kEdgeSpacing);
248 panel->SetSizer(horizIndent);
249
250 return panel;
251 }
252
253 /*
254 * Create the Runtime Preferences page.
255 */
CreateRuntimePage(wxBookCtrlBase * parent)256 wxPanel* PrefsDialog::CreateRuntimePage(wxBookCtrlBase* parent)
257 {
258 wxPanel* panel = new wxPanel(parent);
259
260 wxStaticText* gammaStrDescr = new wxStaticText(panel, wxID_STATIC,
261 wxT("Gamma correction:"));
262 wxTextCtrl* gammaStr = new wxTextCtrl(panel, IDC_RPREFS_GAMMA);
263
264 wxBoxSizer* gammaSizer = new wxBoxSizer(wxHORIZONTAL);
265 gammaSizer->Add(gammaStrDescr, 0, wxALIGN_CENTER_VERTICAL);
266 gammaSizer->AddSpacer(kInterSpacing);
267 gammaSizer->Add(gammaStr);
268
269 wxBoxSizer* sizerPanel = new wxBoxSizer(wxVERTICAL);
270 sizerPanel->Add(kMinWidth, kEdgeSpacing); // forces minimum width
271 sizerPanel->Add(gammaSizer);
272 sizerPanel->AddSpacer(kInterSpacing);
273
274 wxCheckBox* enableSound = new wxCheckBox(panel, IDC_RPREFS_ENABLE_SOUND,
275 wxT("Enable Sound"));
276 sizerPanel->AddSpacer(kInterSpacing);
277 sizerPanel->Add(enableSound);
278
279 wxCheckBox* enableFakeCamera = new wxCheckBox(panel, IDC_RPREFS_ENABLE_FAKE_CAMERA,
280 wxT("Enable Fake Camera"));
281 sizerPanel->AddSpacer(kInterSpacing);
282 sizerPanel->Add(enableFakeCamera);
283
284 wxBoxSizer* horizIndent = new wxBoxSizer(wxHORIZONTAL);
285 horizIndent->AddSpacer(kEdgeSpacing);
286 horizIndent->Add(sizerPanel, wxEXPAND);
287 horizIndent->AddSpacer(kEdgeSpacing);
288 panel->SetSizer(horizIndent);
289
290 return panel;
291 }
292
293