Debian Codegraph @

code/MainWindow.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 <wx/wx.h>
20 #include <wx/gdicmn.h>
21 #include <wx/imagpng.h>
22 #include <wx/imaglist.h>
23 #include <wx/html/htmlwin.h>
24 #include "global/ids.h"
25 #include "global/Compatibility.h"
26 #include "global/ProfileKeys.h"
27 #include "generated/configure_launcher.h"
28 #include "MainWindow.h"
29 #include "tabs/WelcomePage.h"
30 #include "tabs/ModsPage.h"
31 #include "tabs/BasicSettingsPage.h"
32 #include "tabs/AdvSettingsPage.h"
33 #include "tabs/InstallPage.h"
34 #include "controls/BottomButtons.h"
35 #include "apis/SkinManager.h"
36 #include "controls/Logger.h"
37 #include "controls/StatusBar.h"
38 #include "apis/HelpManager.h"
39 #include "apis/FREDManager.h"
40
41 #include "global/MemoryDebugging.h" // Last include for memory debugging
42
43 #define MAINWINDOW_STYLE (wxBORDER_SUNKEN | wxSYSTEM_MENU\
44 | wxCAPTION | wxCLOSE_BOX | wxMINIMIZE_BOX | wxCLIP_CHILDREN)
45
46 const int WINDOW_WIDTH = TAB_AREA_WIDTH;
47
48 MainWindow::MainWindow() {
49 this->Create((wxFrame*)NULL, wxID_ANY, SkinSystem::GetSkinSystem()->GetWindowTitle(),
50 wxDefaultPosition, wxSize(WINDOW_WIDTH, 550), MAINWINDOW_STYLE);
51
52 SkinSystem::RegisterTCSkinChanged(this);
53
54 this->FS2_pid = 0;
55 this->FRED2_pid = 0;
56
57 this->SetFont(SkinSystem::GetSkinSystem()->GetFont());
58 this->SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
59
60 this->SetStatusBar(new StatusBar(this));
61
62 #if IS_WIN32 || IS_LINUX
63 this->SetIcon(SkinSystem::GetSkinSystem()->GetWindowIcon());
64 #endif
65
66 // setup keyboard shortcuts
67 wxAcceleratorEntry entries[1];
68 entries[0].Set(wxACCEL_NORMAL, WXK_F3, ID_F3_PRESSED);
69 wxAcceleratorTable accel(1, entries);
70 SetAcceleratorTable(accel);
71
72 // setup tabs
73
74 this->mainTab = new wxNotebook();
75 this->mainTab->Create(this, ID_MAINTAB, wxPoint(0,0), wxSize(WINDOW_WIDTH,-1), wxNB_TOP);
76
77 this->mainTab->AddPage(new WelcomePage(this->mainTab), _("Welcome"), true);
78 this->mainTab->AddPage(new ModsPage(this->mainTab), _("Mods"), false);
79 this->mainTab->AddPage(new BasicSettingsPage(this->mainTab), _("Basic Settings"), false);
80 this->mainTab->AddPage(new AdvSettingsPage(this->mainTab), _("Advanced Settings"), false);
81 #if 0
82 this->mainTab->AddPage(new InstallPage(this->mainTab), _("Install/Update"), false);
83 #endif
84 wxPoint bbpoint(0, -1);
85 wxSize bbsize(WINDOW_WIDTH, -1);
86 BottomButtons* bb = new BottomButtons(this, bbpoint, bbsize);
87
88 wxBoxSizer* sizer = new wxBoxSizer(wxVERTICAL);
89
90 sizer->Add(this->mainTab);
91 sizer->Add(bb, wxSizerFlags().Expand());
92
93 sizer->SetSizeHints(this);
94 this->SetSizerAndFit(sizer);
95 this->Layout();
96 this->Center();
97 }
98
99 MainWindow::~MainWindow() {
100 //
101 }
102
103 BEGIN_EVENT_TABLE(MainWindow, wxFrame)
104 EVT_BUTTON(ID_CLOSE_BUTTON, MainWindow::OnQuit)
105 EVT_BUTTON(ID_HELP_BUTTON, MainWindow::OnHelp)
106 EVT_BUTTON(ID_FRED_BUTTON, MainWindow::OnStartFred)
107 EVT_BUTTON(ID_UPDATE_BUTTON, MainWindow::OnUpdate)
108 EVT_BUTTON(ID_PLAY_BUTTON, MainWindow::OnFSButton)
109 EVT_BUTTON(ID_ABOUT_BUTTON, MainWindow::OnAbout)
110 EVT_HELP(wxID_ANY, MainWindow::OnContextHelp)
111 EVT_END_PROCESS(ID_FS2_PROCESS, MainWindow::OnFS2Exited)
112 EVT_END_PROCESS(ID_FRED2_PROCESS, MainWindow::OnFRED2Exited)
113 EVT_COMMAND(wxID_NONE, EVT_TC_SKIN_CHANGED, MainWindow::OnTCSkinChanged)
114 EVT_MENU(ID_F3_PRESSED, MainWindow::OnF3Pressed)
115 END_EVENT_TABLE()
116
117 void MainWindow::OnQuit(wxCommandEvent& WXUNUSED(event)) {
118 this->Destroy();
119 }
120 void MainWindow::OnHelp(wxCommandEvent& WXUNUSED(event)) {
121 if (HelpManager::IsInitialized()) {
122 HelpManager::OpenMainHelpPage();
123 } else {
124 wxLogWarning(_("Help Manager is not initialized"));
125 }
126 }
127 void MainWindow::OnStartFred(wxCommandEvent& WXUNUSED(event)) {
128 bool fredEnabled;
129 ProMan::GetProfileManager()->GlobalRead(GBL_CFG_OPT_CONFIG_FRED, &fredEnabled, false);
130 if ( fredEnabled ) {
131 wxButton* fred = dynamic_cast<wxButton*>(
132 wxWindow::FindWindowById(ID_FRED_BUTTON, this));
133 wxCHECK_RET(fred != NULL, _T("Unable to find FRED button"));
134
135 if (this->FRED2_pid == 0) {
136 this->OnStart(fred, true);
137 } else {
138 this->OnKill(fred, true);
139 }
140 } else {
141 wxLogError(_T("OnStartFred called while fredSupport is disabled!"));
142 }
143 }
144 void MainWindow::OnFSButton(wxCommandEvent& WXUNUSED(event)) {
145 wxButton* play = dynamic_cast<wxButton*>(
146 wxWindow::FindWindowById(ID_PLAY_BUTTON, this));
147 wxCHECK_RET(play != NULL, _T("Unable to find play button"));
148
149 if (this->FS2_pid == 0) {
150 this->OnStart(play);
151 } else {
152 this->OnKill(play);
153 }
154 }
155
156 void MainWindow::OnStart(wxButton* button, bool startFred) {
157 button->SetLabel(_("Starting"));
158 button->Disable();
159
160 const wxString defaultButtonValue((startFred)?_("FRED"):_("Play"));
161 const wxString cfgBinaryPath((startFred)? PRO_CFG_TC_CURRENT_FRED : PRO_CFG_TC_CURRENT_BINARY);
162
163 ProMan* p = ProMan::GetProfileManager();
164 wxString folder, binary;
165 if ( !p->ProfileRead(PRO_CFG_TC_ROOT_FOLDER, &folder) ) {
166 wxLogError(_T("Game root folder for current profile is not set (%s)"),
167 PRO_CFG_TC_ROOT_FOLDER.c_str());
168 button->SetLabel(defaultButtonValue);
169 button->Enable();
170 return;
171 }
172 if ( !p->ProfileRead(cfgBinaryPath, &binary) ) {
173 wxLogError(_T("No FS2 Open executable has been selected (%s)"), cfgBinaryPath.c_str());
174 button->SetLabel(defaultButtonValue);
175 button->Enable();
176 return;
177 }
178
179 #if IS_APPLE
180 wxFileName path(folder + wxFileName::GetPathSeparator() + binary, wxPATH_NATIVE);
181 #else
182 wxFileName path(folder, binary, wxPATH_NATIVE);
183 #endif
184
185 if ( !path.FileExists() ) {
186 wxLogError(_T("Executable %s does not exist"), path.GetFullName().c_str());
187 button->SetLabel(defaultButtonValue);
188 button->Enable();
189 return;
190 }
191
192 if (!Compatibility::MigrateOldConfig()) {
193 wxLogError(_T("Failed to migrate old config!!"));
194 button->SetLabel(defaultButtonValue);
195 button->Enable();
196 return;
197 }
198
199 if ( ProMan::NoError != p->PushCurrentProfile() ) {
200 button->SetLabel(defaultButtonValue);
201 button->Enable();
202 return;
203 }
204
205 if (!Compatibility::SynchronizeOldPilots(p)) {
206 wxLogError(_T("Failed to synchronize old pilot files!"));
207 button->SetLabel(defaultButtonValue);
208 button->Enable();
209 return;
210 }
211
212 #if !wxCHECK_VERSION(2, 9, 2)
213 wxString previousWorkingDir(::wxGetCwd());
214 // hopefully this doesn't goof anything up
215 if ( !::wxSetWorkingDirectory(folder) ) {
216 wxLogError(_T("Unable to change working directory to %s"),
217 folder.c_str());
218 button->SetLabel(defaultButtonValue);
219 button->Enable();
220 return;
221 }
222 #endif
223
224 if ( startFred ) {
225 this->process = new wxProcess(this, ID_FRED2_PROCESS);
226 } else {
227 this->process = new wxProcess(this, ID_FS2_PROCESS);
228 }
229
230 wxString command;
231 // the "" correct for spaces in the path
232 if (path.GetFullPath().Find(_T(" ")) != wxNOT_FOUND) {
233 command = _T("\"") + path.GetFullPath() + _T("\"");
234 } else {
235 command = path.GetFullPath();
236 }
237
238 wxLogDebug(_T("Starting a process using '%s'"), command.c_str());
239
240 #if wxCHECK_VERSION(2, 9, 2)
241 wxExecuteEnv env;
242 env.cwd = folder;
243
244 long pid = ::wxExecute(command, wxEXEC_ASYNC, this->process, &env);
245 #else
246 long pid = ::wxExecute(command, wxEXEC_ASYNC, this->process);
247 #endif
248
249 if ( pid == 0 ) {
250 button->SetLabel(defaultButtonValue);
251 button->Enable();
252 return;
253 }
254 if ( startFred ) {
255 this->FRED2_pid = pid;
256 wxLogInfo(_T("FRED2 Open is now running..."));
257 } else {
258 this->FS2_pid = pid;
259 wxLogInfo(_T("FS2 Open is now running..."));
260 }
261
262 #if !wxCHECK_VERSION(2, 9, 2)
263 if (!::wxSetWorkingDirectory(previousWorkingDir)) {
264 wxLogError(_T("Unable to change back to working directory %s"),
265 previousWorkingDir.c_str());
266 }
267 #endif
268
269 button->SetLabel(_T("Kill"));
270 button->Enable();
271 }
272
273 void MainWindow::OnKill(wxButton* button, bool killFred) {
274 button->SetLabel(_T("Stopping"));
275 button->Disable();
276
277 int ret = ::wxKill((killFred)?this->FRED2_pid:this->FS2_pid, wxSIGKILL);
278 if ( ret != wxKILL_OK ) {
279 wxLogError(_T("Got KillError %d"), ret);
280 wxLogError(_T("Failed to kill %s process!"), killFred?_T("FRED2 Open"):_T("FS2 Open"));
281 }
282 }
283
284 void MainWindow::OnUpdate(wxCommandEvent& WXUNUSED(event)) {
285 wxMessageBox(_T("Update"));
286 }
287 void MainWindow::OnAbout(wxCommandEvent& WXUNUSED(event)) {
288 wxMessageBox(_T("About"));
289 }
290
291 void MainWindow::OnContextHelp(wxHelpEvent& event) {
292 HelpManager::OpenHelpById((WindowIDS)event.GetId());
293 }
294
295 void MainWindow::OnFS2Exited(wxProcessEvent &event) {
296 if ( this->FS2_pid == 0 ) {
297 wxLogError(_T("OnFS2Exited called before there is a process running"));
298 return;
299 }
300
301 int exitCode = event.GetExitCode();
302
303 wxLogInfo(_T("FS2 Open exited with a status of %d"), exitCode);
304
305 delete this->process;
306 this->process = NULL;
307 this->FS2_pid = 0;
308
309 wxButton* play = dynamic_cast<wxButton*>(
310 wxWindow::FindWindowById(ID_PLAY_BUTTON, this));
311 wxCHECK_RET(play != NULL, _T("Unable to find play button"));
312 play->SetLabel(_T("Play"));
313 play->Enable();
314 }
315
316 void MainWindow::OnFRED2Exited(wxProcessEvent &event) {
317 if ( this->FRED2_pid == 0 ) {
318 wxLogError(_T("OnFRED2Exited called before there is a process running"));
319 return;
320 }
321
322 int exitCode = event.GetExitCode();
323
324 wxLogInfo(_T("FRED2 Open exited with a status of %d"), exitCode);
325
326 delete this->process;
327 this->process = NULL;
328 this->FRED2_pid = 0;
329
330 wxButton* fred = dynamic_cast<wxButton*>(
331 wxWindow::FindWindowById(ID_FRED_BUTTON, this));
332 wxCHECK_RET(fred != NULL, _T("Unable to find FRED button"));
333 fred->SetLabel(_T("FRED"));
334 fred->Enable();
335 }
336
337 void MainWindow::OnTCSkinChanged(wxCommandEvent& event) {
338 this->SetTitle(SkinSystem::GetSkinSystem()->GetWindowTitle());
339 this->SetIcon(SkinSystem::GetSkinSystem()->GetWindowIcon());
340 }
341
342 void MainWindow::OnF3Pressed(wxCommandEvent& WXUNUSED(event)) {
343 ProMan* proman = ProMan::GetProfileManager();
344 wxCHECK_RET(proman != NULL, _T("OnF3Pressed(): proman is NULL!"));
345
346 bool fredEnabled;
347 proman->GlobalRead(GBL_CFG_OPT_CONFIG_FRED, &fredEnabled, false);
348
349 proman->GlobalWrite(GBL_CFG_OPT_CONFIG_FRED, !fredEnabled);
350 FREDManager::GenerateFREDEnabledChanged();
351 }