Debian Codegraph @

code/tabs/AdvSettingsPage.cpp

raw
1 /*
2 Copyright (C) 2009-2010 wxLauncher Team
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18
19 #include "generated/configure_launcher.h"
20
21 #include "tabs/AdvSettingsPage.h"
22
23 #include "apis/CmdLineManager.h"
24 #include "apis/FlagListManager.h"
25 #include "apis/ProfileProxy.h"
26 #include "apis/TCManager.h"
27 #include "apis/ProfileManager.h"
28 #include "apis/SkinManager.h"
29 #include "controls/LightingPresets.h"
30 #include "global/ids.h"
31 #include "global/ProfileKeys.h"
32 #include "global/Utils.h"
33
34 #include <wx/html/htmlwin.h>
35 #include <wx/tokenzr.h>
36
37 #include "global/MemoryDebugging.h" // Last include for memory debugging
38
39 const size_t TOP_SIZER_INDEX = 0;
40 const size_t TOP_LEFT_SIZER_INDEX = 0;
41 const size_t WIKI_LINK_SIZER_INDEX = 1;
42 const size_t TOP_RIGHT_SIZER_INDEX = 1;
43 const size_t BOTTOM_SIZER_INDEX = 1;
44
45 AdvSettingsPage::AdvSettingsPage(wxWindow* parent): wxPanel(parent, wxID_ANY), flagListBox(NULL) {
46 this->lightingPresets = new LightingPresets(this);
47 this->errorText =
48 new wxStaticText(this, wxID_ANY, wxEmptyString, wxDefaultPosition,
49 wxDefaultSize, wxALIGN_CENTER);
50
51 wxLogDebug(_T("AdvSettingsPage is at %p."), this);
52
53 CmdLineManager::RegisterCmdLineChanged(this);
54 CmdLineManager::RegisterCustomFlagsChanged(this);
55 FlagListManager::GetFlagListManager()->RegisterFlagFileProcessingStatusChanged(this);
56 TCManager::RegisterTCActiveModChanged(this);
57 ProfileProxy::GetProxy()->RegisterProxyReset(this);
58 ProfileProxy::GetProxy()->RegisterProxyFlagDataReady(this);
59 }
60
61 BEGIN_EVENT_TABLE(AdvSettingsPage, wxPanel)
62 EVT_COMMAND(wxID_NONE, EVT_PROXY_RESET, AdvSettingsPage::OnExeChanged)
63 EVT_COMMAND(wxID_NONE, EVT_PROXY_FLAG_DATA_READY, AdvSettingsPage::OnProxyFlagDataReady)
64 EVT_COMMAND(wxID_NONE, EVT_TC_ACTIVE_MOD_CHANGED, AdvSettingsPage::OnNeedUpdateCommandLine)
65 EVT_COMMAND(wxID_NONE, EVT_FLAG_FILE_PROCESSING_STATUS_CHANGED, AdvSettingsPage::OnFlagFileProcessingStatusChanged)
66 EVT_COMMAND(wxID_NONE, EVT_CMD_LINE_CHANGED, AdvSettingsPage::OnNeedUpdateCommandLine)
67 EVT_COMMAND(wxID_NONE, EVT_CUSTOM_FLAGS_CHANGED, AdvSettingsPage::OnNeedUpdateCustomFlags)
68 EVT_COMMAND(wxID_NONE, EVT_FLAG_LIST_BOX_READY, AdvSettingsPage::OnFlagListBoxReady)
69 EVT_TEXT(ID_CUSTOM_FLAGS_TEXT, AdvSettingsPage::OnCustomFlagsBoxChanged)
70 EVT_CHOICE(ID_SELECT_FLAG_SET, AdvSettingsPage::OnSelectFlagSet)
71 END_EVENT_TABLE()
72
73 // FIXME HACK for now, hard-code flag list box height (sigh)
74 #if IS_WIN32
75 const int FLAG_LIST_BOX_HEIGHT = 410;
76 #elif IS_LINUX
77 const int FLAG_LIST_BOX_HEIGHT = 363;
78 #elif IS_APPLE
79 const int FLAG_LIST_BOX_HEIGHT = 363;
80 #else
81 #error "One of IS_WIN32, IS_LINUX, IS_APPLE must evaluate to true"
82 #endif
83
84 void AdvSettingsPage::OnExeChanged(wxCommandEvent& event) {
85 if (this->GetSizer() != NULL) {
86 // detach it so it won't be deleted when we clear the sizer
87 this->lightingPresets->GetContainingSizer()->Detach(this->lightingPresets);
88
89 this->GetSizer()->Clear(true);
90 }
91
92 // top left components
93 this->flagListBox = new FlagListBox(this);
94 this->flagListBox->RegisterFlagListBoxReady(this);
95 FlagListManager::GetFlagListManager()->BeginFlagFileProcessing();
96
97 #if 0 // doesn't do anything
98 wxHtmlWindow* description = new wxHtmlWindow(this);
99 description->SetPage(_T("<p></p>"));
100 #endif
101
102
103 #if IS_WIN32
104 wxStaticText* wikiLinkText1 = new wxStaticText(this, wxID_ANY,
105 _T("Double-click on a flag for its online documentation, if available."));
106 #else
107 wxStaticText* wikiLinkText1 = new wxStaticText(this, wxID_ANY,
108 _T("Double-click on a flag"));
109 wxStaticText* wikiLinkText2 = new wxStaticText(this, wxID_ANY,
110 _T("for its online documentation, if available."));
111 #endif
112
113 wxBoxSizer* wikiLinkSizer = new wxBoxSizer(wxVERTICAL);
114 wikiLinkSizer->Add(wikiLinkText1, 0, wxALIGN_CENTER_HORIZONTAL);
115 #if !IS_WIN32
116 wikiLinkSizer->Add(wikiLinkText2, 0, wxALIGN_CENTER_HORIZONTAL);
117 #endif
118
119 wxBoxSizer* topLeftSizer = new wxBoxSizer(wxVERTICAL);
120 topLeftSizer->Add(this->flagListBox, wxSizerFlags().Proportion(1).Expand());
121 topLeftSizer->Add(wikiLinkSizer, 0, wxALIGN_CENTER_HORIZONTAL|wxTOP, 5);
122
123 // top right components
124 wxBoxSizer* lightingPresetsSizer = new wxBoxSizer(wxVERTICAL);
125 lightingPresetsSizer->Add(this->lightingPresets, wxSizerFlags().Proportion(1).Expand());
126
127 wxStaticText* flagSetChoiceLabel = new wxStaticText(this, wxID_ANY, _T("Flag sets:"));
128 wxChoice* flagSetChoice = new wxChoice(this, ID_SELECT_FLAG_SET);
129
130 wxBoxSizer* topRightSizer = new wxBoxSizer(wxVERTICAL);
131 topRightSizer->Add(lightingPresetsSizer, wxSizerFlags().Proportion(1).Border(wxBOTTOM, 5));
132 topRightSizer->Add(flagSetChoiceLabel, wxSizerFlags().Left().Border(wxBOTTOM, 5));
133 topRightSizer->Add(flagSetChoice, wxSizerFlags().Expand());
134
135 // putting the top sizer together
136 wxBoxSizer* topSizer = new wxBoxSizer(wxHORIZONTAL);
137 topSizer->Add(topLeftSizer, wxSizerFlags().Proportion(1).Expand());
138 topSizer->Add(topRightSizer, wxSizerFlags().Expand().Border(wxLEFT, 5));
139 #if 0
140 topSizer->Add(description, wxSizerFlags().Proportion(1).Expand());
141 #endif
142
143 #if 0 // related to functionality that isn't working yet
144 wxStaticBitmap* idealIcon = new wxStaticBitmap(this, wxID_ANY, this->skin->GetIdealIcon());
145 wxStaticText* idealLabel = new wxStaticText(this, wxID_ANY, _("= Recommended flag"));
146
147 wxBoxSizer* idealFlagsRowSizer = new wxBoxSizer(wxHORIZONTAL);
148 idealFlagsRowSizer->Add(idealIcon);
149 wxBoxSizer* idealLabelSizer = new wxBoxSizer(wxVERTICAL);
150 idealLabelSizer->AddStretchSpacer(1);
151 idealLabelSizer->Add(idealLabel);
152 idealLabelSizer->AddStretchSpacer(1);
153 idealFlagsRowSizer->Add(idealLabelSizer);
154 #endif
155
156 #if 0 // doesn't do anything
157 wxStaticBox* flagsetNotesBox = new wxStaticBox(this, wxID_ANY, _("Flag set notes"));
158 wxTextCtrl* flagsetNotes = new wxTextCtrl(this, ID_FLAG_SET_NOTES_TEXT,
159 wxEmptyString, wxDefaultPosition, wxSize(-1, TAB_AREA_HEIGHT/8),
160 wxTE_MULTILINE|wxTE_READONLY);
161 wxStaticBoxSizer* flagsetNotesSizer = new wxStaticBoxSizer(flagsetNotesBox, wxVERTICAL);
162 flagsetNotesSizer->Add(flagsetNotes, wxSizerFlags().Expand());
163 #endif
164
165 wxStaticText* customFlagsTextLabel = new wxStaticText(this, wxID_ANY, _("Custom flags:"));
166 wxTextCtrl* customFlagsText = new wxTextCtrl(this, ID_CUSTOM_FLAGS_TEXT);
167
168 wxStaticText* commandLineTextLabel = new wxStaticText(this, wxID_ANY, _("Current command line:"));
169 wxTextCtrl* commandLineText = new wxTextCtrl(this, ID_COMMAND_LINE_TEXT,
170 wxEmptyString, wxDefaultPosition, wxDefaultSize,
171 wxTE_MULTILINE|wxTE_READONLY);
172
173 wxBoxSizer* bottomSizer = new wxBoxSizer(wxVERTICAL);
174 bottomSizer->Add(customFlagsTextLabel, wxSizerFlags().Left().Border(wxTOP|wxBOTTOM, 5));
175 bottomSizer->Add(customFlagsText, wxSizerFlags().Expand().Border(wxBOTTOM, 10));
176 bottomSizer->Add(commandLineTextLabel, wxSizerFlags().Left().Border(wxBOTTOM, 5));
177 bottomSizer->Add(commandLineText, wxSizerFlags().Proportion(1).Expand());
178
179 // final layout
180 wxBoxSizer* sizer = new wxBoxSizer(wxVERTICAL);
181 sizer->Add(topSizer, wxSizerFlags().Expand().Border(wxALL, 5));
182 #if 0
183 sizer->Add(idealFlagsRowSizer, wxSizerFlags().Expand().Border(wxALL, 5));
184 sizer->Add(flagsetNotesSizer, wxSizerFlags().Expand().Border(wxLEFT|wxRIGHT|wxBOTTOM, 5));
185 #endif
186 sizer->Add(bottomSizer, wxSizerFlags().Expand().Proportion(1).Border(wxALL, 5));
187
188 this->SetSizer(sizer);
189 this->Layout();
190
191 if (!ProfileProxy::GetProxy()->IsProfileInitialized()) {
192 ProfileProxy::GetProxy()->FinishProfileInitialization();
193 }
194 }
195
196 void AdvSettingsPage::OnFlagFileProcessingStatusChanged(wxCommandEvent &event) {
197 wxASSERT(event.GetEventType() == EVT_FLAG_FILE_PROCESSING_STATUS_CHANGED);
198
199 const FlagListManager::FlagFileProcessingStatus status =
200 static_cast<FlagListManager::FlagFileProcessingStatus>(event.GetInt());
201
202 // Ignore an event with reset status, since it's meant for the proxy
203 if (status == FlagListManager::FLAG_FILE_PROCESSING_RESET) {
204 return;
205 }
206
207 this->UpdateComponents();
208
209 if (status == FlagListManager::FLAG_FILE_PROCESSING_OK) {
210 FlagFileData* flagData = FlagListManager::GetFlagListManager()->GetFlagFileData();
211 wxCHECK_RET(flagData != NULL,
212 _T("Flag file processing succeeded but could not retrieve extracted data."));
213 this->flagListBox->AcceptFlagData(flagData);
214 } else {
215 this->UpdateErrorText();
216 }
217 }
218
219 void AdvSettingsPage::OnNeedUpdateCustomFlags(wxCommandEvent &event) {
220 wxASSERT((this->flagListBox != NULL) && this->flagListBox->IsReady());
221 wxASSERT(ProfileProxy::GetProxy()->IsFlagDataReady());
222
223 wxTextCtrl* customFlagsText = dynamic_cast<wxTextCtrl*>(
224 wxWindow::FindWindowById(ID_CUSTOM_FLAGS_TEXT, this));
225 wxCHECK_RET(customFlagsText != NULL,
226 _T("Unable to find the custom flags text ctrl"));
227
228 customFlagsText->ChangeValue(ProfileProxy::GetProxy()->GetCustomFlags());
229 }
230
231 void AdvSettingsPage::OnFlagListBoxReady(wxCommandEvent &WXUNUSED(event)) {
232 wxASSERT((this->flagListBox != NULL) && this->flagListBox->IsReady());
233
234 if (ProfileProxy::GetProxy()->IsFlagDataReady()
235 && !this->flagListBox->FlagsLoaded()) {
236 this->flagListBox->LoadEnabledFlags();
237 CmdLineManager::GenerateCustomFlagsChanged();
238 CmdLineManager::GenerateCmdLineChanged();
239 }
240
241 this->UpdateComponents();
242 this->UpdateFlagSetsBox();
243 }
244
245 void AdvSettingsPage::OnProxyFlagDataReady(wxCommandEvent& event) {
246 wxASSERT((this->flagListBox != NULL) &&
247 ProfileProxy::GetProxy()->IsFlagDataReady());
248
249 if (this->flagListBox->IsReady() && !this->flagListBox->FlagsLoaded()) {
250 this->flagListBox->LoadEnabledFlags();
251 CmdLineManager::GenerateCustomFlagsChanged();
252 CmdLineManager::GenerateCmdLineChanged();
253 }
254 }
255
256 void AdvSettingsPage::UpdateComponents() {
257 wxSizer* topSizer = this->GetSizer()->GetItem(TOP_SIZER_INDEX)->GetSizer();
258 wxCHECK_RET(topSizer != NULL, _T("cannot find the top sizer"));
259
260 wxSizer* topLeftSizer = topSizer->GetItem(TOP_LEFT_SIZER_INDEX)->GetSizer();
261 wxCHECK_RET(topLeftSizer != NULL, _T("cannot find the top left sizer"));
262
263 wxCHECK_RET(this->flagListBox != NULL,
264 _T("UpdateComponents() called when flagListBox was null."));
265
266 if (this->flagListBox->IsReady()) {
267 topSizer->Show(TOP_RIGHT_SIZER_INDEX);
268 topLeftSizer->Show(WIKI_LINK_SIZER_INDEX);
269 this->GetSizer()->Show(BOTTOM_SIZER_INDEX);
270 this->flagListBox->Show();
271 this->errorText->Hide();
272 this->Layout();
273 } else {
274 topSizer->Hide(TOP_RIGHT_SIZER_INDEX);
275 topLeftSizer->Hide(WIKI_LINK_SIZER_INDEX);
276 this->GetSizer()->Hide(BOTTOM_SIZER_INDEX);
277 this->flagListBox->Hide();
278 this->errorText->Show();
279 this->Layout();
280 }
281 }
282
283 void AdvSettingsPage::UpdateErrorText() {
284 wxCHECK_RET(!FlagListManager::GetFlagListManager()->IsProcessingOK(),
285 _T("UpdateErrorText() called when processing succeeded."));
286
287 wxRect rect(0, 0, this->GetSize().x, this->GetSize().y);
288
289 wxString msg = FlagListManager::GetFlagListManager()->GetStatusMessage();
290
291 wxASSERT(!msg.IsEmpty());
292
293 this->errorText->SetLabel(msg);
294
295 this->errorText->SetFont(SkinSystem::GetSkinSystem()->GetMessageFont());
296
297 this->errorText->SetSize(rect, wxSIZE_FORCE);
298 this->errorText->Wrap(rect.width - 225); // to match mods page
299 this->errorText->Center();
300 }
301
302 void AdvSettingsPage::OnCustomFlagsBoxChanged(wxCommandEvent &WXUNUSED(event)) {
303 wxASSERT(this->flagListBox != NULL && this->flagListBox->IsReady());
304 wxASSERT(ProfileProxy::GetProxy()->IsFlagDataReady());
305
306 wxTextCtrl* customFlagsText = dynamic_cast<wxTextCtrl*>(
307 wxWindow::FindWindowById(ID_CUSTOM_FLAGS_TEXT, this));
308 wxCHECK_RET(customFlagsText != NULL,
309 _T("Unable to find the custom flags text ctrl"));
310
311 ProfileProxy::GetProxy()->SetCustomFlags(customFlagsText->GetValue(), false);
312 }
313
314 void AdvSettingsPage::UpdateFlagSetsBox() {
315 wxASSERT(this->flagListBox != NULL);
316 wxASSERT(this->flagListBox->IsReady());
317
318 wxChoice *flagSetChoice = dynamic_cast<wxChoice*>(
319 wxWindow::FindWindowById(ID_SELECT_FLAG_SET, this));
320 wxCHECK_RET(flagSetChoice != NULL,
321 _T("Unable to find the flagset choice control"));
322
323 // TODO rethink the following assertion when new mod.ini is supported
324 // the assertion also assumes that the flag sets box is never reused
325 wxASSERT(flagSetChoice->IsEmpty()); // shouldn't add sets more than once
326
327 wxArrayString flagSetsArray;
328 this->flagListBox->GetFlagSets(flagSetsArray);
329
330 // before populating the flag set choice box, let's remove the flag sets
331 // that don't make sense and can thus potentially confuse users
332
333 flagSetsArray.Remove(_T("Custom"));
334
335 flagSetChoice->Append(flagSetsArray);
336
337 wxClientDC dc(this);
338 wxArrayString flagSets = flagSetChoice->GetStrings();
339 wxFont font(this->GetFont());
340 int maxStringWidth = 0;
341 int x, y;
342
343 for (int i = 0, n = flagSets.GetCount(); i < n; ++i) {
344 dc.GetTextExtent(flagSets[i], &x, &y, NULL, NULL, &font);
345
346 if (x > maxStringWidth) {
347 maxStringWidth = x;
348 }
349 }
350
351 flagSetChoice->SetMinSize(
352 wxSize(maxStringWidth + 40, // 40 to include drop down box control
353 flagSetChoice->GetSize().GetHeight()));
354 this->Layout();
355 }
356
357 void AdvSettingsPage::OnNeedUpdateCommandLine(wxCommandEvent &WXUNUSED(event)) {
358 if ( (this->flagListBox == NULL) || !this->flagListBox->IsReady() ) {
359 // The control I need to update does not exist, do nothing
360 return;
361 }
362
363 wxTextCtrl* commandLine = dynamic_cast<wxTextCtrl*>(
364 wxWindow::FindWindowById(ID_COMMAND_LINE_TEXT, this));
365 wxCHECK_RET( commandLine != NULL, _T("Unable to find the command line view text control") );
366
367 wxString tcPath, exeName, modline;
368 wxCHECK_RET(
369 ProMan::GetProfileManager()->ProfileRead(
370 PRO_CFG_TC_ROOT_FOLDER, &tcPath),
371 _T("Could not find profile entry for root folder."));
372 wxCHECK_RET(
373 ProMan::GetProfileManager()->ProfileRead(
374 PRO_CFG_TC_CURRENT_BINARY, &exeName),
375 _T("Could not find profile entry for FSO binary."));
376 wxCHECK_RET(
377 ProMan::GetProfileManager()->ProfileRead(
378 PRO_CFG_TC_CURRENT_MODLINE, &modline),
379 _T("Could not find profile entry for mod line."));
380
381 wxString flagFileFlags(ProfileProxy::GetProxy()->GetEnabledFlagsString());
382
383 wxString lightingPresetString;
384 if (ProfileProxy::GetProxy()->HasLightingPreset()) {
385 lightingPresetString = LightingPresets::PresetNameToPresetFlagSet(
386 ProfileProxy::GetProxy()->GetLightingPresetName());
387 }
388
389 wxString customFlags(ProfileProxy::GetProxy()->GetCustomFlags());
390
391 wxString cmdLine =
392 wxString::Format(_T("%s%c%s%s%s%s%s"),
393 tcPath.c_str(),
394 wxFileName::GetPathSeparator(),
395 exeName.c_str(),
396 (modline.IsEmpty() ? wxEmptyString :
397 wxString::Format(_T(" -mod \"%s\""), modline.c_str()).c_str()),
398 (flagFileFlags.IsEmpty() ? wxEmptyString :
399 wxString::Format(_T(" %s"), flagFileFlags.c_str()).c_str()),
400 (lightingPresetString.IsEmpty() ? wxEmptyString :
401 wxString::Format(_T(" %s"), lightingPresetString.c_str()).c_str()),
402 (customFlags.IsEmpty() ? wxEmptyString :
403 wxString::Format(_T(" %s"), customFlags.c_str()).c_str()));
404
405 commandLine->ChangeValue(FormatCommandLineString(cmdLine,
406 commandLine->GetSize().GetWidth() - 30)); // 30 for scrollbar
407 }
408
409 wxString AdvSettingsPage::FormatCommandLineString(const wxString& origCmdLine,
410 const int textAreaWidth) {
411 // inspired by ModItem::InfoText::Draw()
412 wxStringTokenizer tokens(origCmdLine);
413 TextUtils::ArrayOfWords words;
414 words.Alloc(tokens.CountTokens());
415
416 wxClientDC dc(this);
417 wxFont font(this->GetFont());
418
419 FillArrayOfWordsFromTokens(tokens, dc, &font, words, true);
420
421 const int spaceWidth = dc.GetTextExtent(_T(" ")).GetWidth();
422
423 int currentWidth = 0;
424
425 wxString formattedCmdLine;
426 wxString currentLine;
427
428 bool spaceAdded = false;
429
430 for (size_t i = 0, n = words.Count(); i < n; i++) {
431 if (words[i].word.IsEmpty()) { // skip over remnants of words eliminated in tokens-to-words conversion
432 continue;
433 } else if (currentWidth + words[i].size.x + spaceWidth > textAreaWidth) {
434 // prevents trailing newline in cmdLineString (nitpicky, I know)
435 formattedCmdLine += wxString::Format(formattedCmdLine.IsEmpty() ? _T("%s") : _T("\n%s"),
436 currentLine.c_str());
437 currentLine.Empty();
438 currentWidth = 0;
439 } else {
440 if (!currentLine.IsEmpty()) {
441 currentLine.append(_T(" "));
442 spaceAdded = true;
443 }
444 }
445 currentLine.append(words[i].word);
446 currentWidth += words[i].size.x + (spaceAdded ? spaceWidth : 0);
447 spaceAdded = false;
448 }
449 if (!currentLine.IsEmpty()) {
450 formattedCmdLine += wxString::Format(formattedCmdLine.IsEmpty() ? _T("%s") : _T("\n%s"),
451 currentLine.c_str());
452 }
453
454 return formattedCmdLine;
455 }
456
457 void AdvSettingsPage::OnSelectFlagSet(wxCommandEvent &WXUNUSED(event)) {
458 wxChoice* choice = dynamic_cast<wxChoice*>(
459 wxWindow::FindWindowById(ID_SELECT_FLAG_SET, this));
460 wxCHECK_RET( choice != NULL, _T("Unable to get flagSetChoice box"));
461
462 wxString selectedSet = choice->GetString(choice->GetSelection());
463 bool ret = this->flagListBox->SetFlagSet(selectedSet);
464 if ( ret == false ) {
465 wxLogError(_T("Unable to set flag set (%s). Set does not exist"), selectedSet.c_str());
466 }
467 }
468