File: MainWindow.cpp

package info (click to toggle)
freespace2-launcher-wxlauncher 0.11.0%2Bdfsg-1
  • links: PTS, VCS
  • area: contrib
  • in suites: stretch
  • size: 2,372 kB
  • ctags: 1,443
  • sloc: cpp: 13,446; python: 797; makefile: 13; sh: 12
file content (351 lines) | stat: -rw-r--r-- 10,474 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*
Copyright (C) 2009-2010 wxLauncher Team

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

#include <wx/wx.h>
#include <wx/gdicmn.h>
#include <wx/imagpng.h>
#include <wx/imaglist.h>
#include <wx/html/htmlwin.h>
#include "global/ids.h"
#include "global/Compatibility.h"
#include "global/ProfileKeys.h"
#include "generated/configure_launcher.h"
#include "MainWindow.h"
#include "tabs/WelcomePage.h"
#include "tabs/ModsPage.h"
#include "tabs/BasicSettingsPage.h"
#include "tabs/AdvSettingsPage.h"
#include "tabs/InstallPage.h"
#include "controls/BottomButtons.h"
#include "apis/SkinManager.h"
#include "controls/Logger.h"
#include "controls/StatusBar.h"
#include "apis/HelpManager.h"
#include "apis/FREDManager.h"

#include "global/MemoryDebugging.h" // Last include for memory debugging

#define MAINWINDOW_STYLE (wxBORDER_SUNKEN | wxSYSTEM_MENU\
	| wxCAPTION | wxCLOSE_BOX | wxMINIMIZE_BOX | wxCLIP_CHILDREN)

const int WINDOW_WIDTH = TAB_AREA_WIDTH;

MainWindow::MainWindow() {
	this->Create((wxFrame*)NULL, wxID_ANY, SkinSystem::GetSkinSystem()->GetWindowTitle(),
		wxDefaultPosition, wxSize(WINDOW_WIDTH, 550), MAINWINDOW_STYLE);
	
	SkinSystem::RegisterTCSkinChanged(this);

	this->FS2_pid = 0;
	this->FRED2_pid = 0;

	this->SetFont(SkinSystem::GetSkinSystem()->GetFont());
	this->SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));

	this->SetStatusBar(new StatusBar(this));

#if IS_WIN32 || IS_LINUX
	this->SetIcon(SkinSystem::GetSkinSystem()->GetWindowIcon());
#endif

	// setup keyboard shortcuts
	wxAcceleratorEntry entries[1];
	entries[0].Set(wxACCEL_NORMAL, WXK_F3, ID_F3_PRESSED);
	wxAcceleratorTable accel(1, entries);
	SetAcceleratorTable(accel);
	
	// setup tabs

	this->mainTab = new wxNotebook();
	this->mainTab->Create(this, ID_MAINTAB, wxPoint(0,0), wxSize(WINDOW_WIDTH,-1), wxNB_TOP);

	this->mainTab->AddPage(new WelcomePage(this->mainTab), _("Welcome"), true);
	this->mainTab->AddPage(new ModsPage(this->mainTab), _("Mods"), false);
	this->mainTab->AddPage(new BasicSettingsPage(this->mainTab), _("Basic Settings"), false);
	this->mainTab->AddPage(new AdvSettingsPage(this->mainTab), _("Advanced Settings"), false);
#if 0
	this->mainTab->AddPage(new InstallPage(this->mainTab), _("Install/Update"), false);
#endif
	wxPoint bbpoint(0, -1);
	wxSize bbsize(WINDOW_WIDTH, -1);
	BottomButtons* bb = new BottomButtons(this, bbpoint, bbsize);

	wxBoxSizer* sizer = new wxBoxSizer(wxVERTICAL);

	sizer->Add(this->mainTab);
	sizer->Add(bb, wxSizerFlags().Expand());

	sizer->SetSizeHints(this);
	this->SetSizerAndFit(sizer);
	this->Layout();
	this->Center();
}

MainWindow::~MainWindow() {
	//
}

BEGIN_EVENT_TABLE(MainWindow, wxFrame)
	EVT_BUTTON(ID_CLOSE_BUTTON, MainWindow::OnQuit)
	EVT_BUTTON(ID_HELP_BUTTON, MainWindow::OnHelp)
	EVT_BUTTON(ID_FRED_BUTTON, MainWindow::OnStartFred)
	EVT_BUTTON(ID_UPDATE_BUTTON, MainWindow::OnUpdate)
	EVT_BUTTON(ID_PLAY_BUTTON, MainWindow::OnFSButton)
	EVT_BUTTON(ID_ABOUT_BUTTON, MainWindow::OnAbout)
	EVT_HELP(wxID_ANY, MainWindow::OnContextHelp)
	EVT_END_PROCESS(ID_FS2_PROCESS, MainWindow::OnFS2Exited)
	EVT_END_PROCESS(ID_FRED2_PROCESS, MainWindow::OnFRED2Exited)
	EVT_COMMAND(wxID_NONE, EVT_TC_SKIN_CHANGED, MainWindow::OnTCSkinChanged)
	EVT_MENU(ID_F3_PRESSED, MainWindow::OnF3Pressed)
END_EVENT_TABLE()

void MainWindow::OnQuit(wxCommandEvent& WXUNUSED(event)) {
	this->Destroy();
}
void MainWindow::OnHelp(wxCommandEvent& WXUNUSED(event)) {
	if (HelpManager::IsInitialized()) {
		HelpManager::OpenMainHelpPage();
	} else {
		wxLogWarning(_("Help Manager is not initialized"));
	}
}
void MainWindow::OnStartFred(wxCommandEvent& WXUNUSED(event)) {
	bool fredEnabled;
	ProMan::GetProfileManager()->GlobalRead(GBL_CFG_OPT_CONFIG_FRED, &fredEnabled, false);
	if ( fredEnabled ) {
		wxButton* fred = dynamic_cast<wxButton*>(
			wxWindow::FindWindowById(ID_FRED_BUTTON, this));
		wxCHECK_RET(fred != NULL, _T("Unable to find FRED button"));

		if (this->FRED2_pid == 0) {
			this->OnStart(fred, true);
		} else {
			this->OnKill(fred, true);
		}
	} else {
		wxLogError(_T("OnStartFred called while fredSupport is disabled!"));
	}
}
void MainWindow::OnFSButton(wxCommandEvent& WXUNUSED(event)) {
	wxButton* play = dynamic_cast<wxButton*>(
		wxWindow::FindWindowById(ID_PLAY_BUTTON, this));
	wxCHECK_RET(play != NULL, _T("Unable to find play button"));

	if (this->FS2_pid == 0) {
		this->OnStart(play);
	} else {
		this->OnKill(play);
	}
}

void MainWindow::OnStart(wxButton* button, bool startFred) {
	button->SetLabel(_("Starting"));
	button->Disable();

	const wxString defaultButtonValue((startFred)?_("FRED"):_("Play"));
	const wxString cfgBinaryPath((startFred)? PRO_CFG_TC_CURRENT_FRED : PRO_CFG_TC_CURRENT_BINARY);
	
	ProMan* p = ProMan::GetProfileManager();
	wxString folder, binary;
	if ( !p->ProfileRead(PRO_CFG_TC_ROOT_FOLDER, &folder) ) {
		wxLogError(_T("Game root folder for current profile is not set (%s)"),
			PRO_CFG_TC_ROOT_FOLDER.c_str());
		button->SetLabel(defaultButtonValue);
		button->Enable();
		return;
	}
	if ( !p->ProfileRead(cfgBinaryPath, &binary) ) {
		wxLogError(_T("No FS2 Open executable has been selected (%s)"), cfgBinaryPath.c_str());
		button->SetLabel(defaultButtonValue);
		button->Enable();
		return;
	}

#if IS_APPLE
	wxFileName path(folder + wxFileName::GetPathSeparator() + binary, wxPATH_NATIVE);
#else
	wxFileName path(folder, binary, wxPATH_NATIVE);
#endif

	if ( !path.FileExists() ) {
		wxLogError(_T("Executable %s does not exist"), path.GetFullName().c_str());
		button->SetLabel(defaultButtonValue);
		button->Enable();
		return;
	}

	if (!Compatibility::MigrateOldConfig()) {
		wxLogError(_T("Failed to migrate old config!!"));
		button->SetLabel(defaultButtonValue);
		button->Enable();
		return;
	}

	if ( ProMan::NoError != p->PushCurrentProfile() ) {
		button->SetLabel(defaultButtonValue);
		button->Enable();
		return;
	}

	if (!Compatibility::SynchronizeOldPilots(p)) {
		wxLogError(_T("Failed to synchronize old pilot files!"));
		button->SetLabel(defaultButtonValue);
		button->Enable();
		return;
	}

#if !wxCHECK_VERSION(2, 9, 2)
	wxString previousWorkingDir(::wxGetCwd());
	// hopefully this doesn't goof anything up
	if ( !::wxSetWorkingDirectory(folder) ) {
		wxLogError(_T("Unable to change working directory to %s"),
			folder.c_str());
		button->SetLabel(defaultButtonValue);
		button->Enable();
		return;
	}
#endif

	if ( startFred ) {
		this->process = new wxProcess(this, ID_FRED2_PROCESS);
	} else {
		this->process = new wxProcess(this, ID_FS2_PROCESS);
	}

	wxString command;
	// the "" correct for spaces in the path
	if (path.GetFullPath().Find(_T(" ")) != wxNOT_FOUND) {
		command = _T("\"") + path.GetFullPath() + _T("\"");
	} else {
		command = path.GetFullPath();
	}

	wxLogDebug(_T("Starting a process using '%s'"), command.c_str());

#if wxCHECK_VERSION(2, 9, 2)
	wxExecuteEnv env;
	env.cwd = folder;

	long pid = ::wxExecute(command, wxEXEC_ASYNC, this->process, &env);
#else
	long pid = ::wxExecute(command, wxEXEC_ASYNC, this->process);
#endif

	if ( pid == 0 ) {
		button->SetLabel(defaultButtonValue);
		button->Enable();
		return;
	}
	if ( startFred ) {
		this->FRED2_pid = pid;
		wxLogInfo(_T("FRED2 Open is now running..."));
	} else {
		this->FS2_pid = pid;
		wxLogInfo(_T("FS2 Open is now running..."));
	}

#if !wxCHECK_VERSION(2, 9, 2)
	if (!::wxSetWorkingDirectory(previousWorkingDir)) {
		wxLogError(_T("Unable to change back to working directory %s"),
			previousWorkingDir.c_str());
	}
#endif

	button->SetLabel(_T("Kill"));
	button->Enable();
}

void MainWindow::OnKill(wxButton* button, bool killFred) {
	button->SetLabel(_T("Stopping"));
	button->Disable();

	int ret = ::wxKill((killFred)?this->FRED2_pid:this->FS2_pid, wxSIGKILL);
	if ( ret != wxKILL_OK ) {
		wxLogError(_T("Got KillError %d"), ret);
		wxLogError(_T("Failed to kill %s process!"), killFred?_T("FRED2 Open"):_T("FS2 Open"));
	}
}

void MainWindow::OnUpdate(wxCommandEvent& WXUNUSED(event)) {
	wxMessageBox(_T("Update"));
}
void MainWindow::OnAbout(wxCommandEvent& WXUNUSED(event)) {
	wxMessageBox(_T("About"));
}

void MainWindow::OnContextHelp(wxHelpEvent& event) {
	HelpManager::OpenHelpById((WindowIDS)event.GetId());
}

void MainWindow::OnFS2Exited(wxProcessEvent &event) {
	if ( this->FS2_pid == 0 ) {
		wxLogError(_T("OnFS2Exited called before there is a process running"));
		return;
	}

	int exitCode = event.GetExitCode();

	wxLogInfo(_T("FS2 Open exited with a status of %d"), exitCode);

	delete this->process;
	this->process = NULL;
	this->FS2_pid = 0;
	
	wxButton* play = dynamic_cast<wxButton*>(
		wxWindow::FindWindowById(ID_PLAY_BUTTON, this));
	wxCHECK_RET(play != NULL, _T("Unable to find play button"));
	play->SetLabel(_T("Play"));
	play->Enable();	
}

void MainWindow::OnFRED2Exited(wxProcessEvent &event) {
	if ( this->FRED2_pid == 0 ) {
		wxLogError(_T("OnFRED2Exited called before there is a process running"));
		return;
	}

	int exitCode = event.GetExitCode();

	wxLogInfo(_T("FRED2 Open exited with a status of %d"), exitCode);

	delete this->process;
	this->process = NULL;
	this->FRED2_pid = 0;
	
	wxButton* fred = dynamic_cast<wxButton*>(
		wxWindow::FindWindowById(ID_FRED_BUTTON, this));
	wxCHECK_RET(fred != NULL, _T("Unable to find FRED button"));
	fred->SetLabel(_T("FRED"));
	fred->Enable();
}

void MainWindow::OnTCSkinChanged(wxCommandEvent& event) {
	this->SetTitle(SkinSystem::GetSkinSystem()->GetWindowTitle());
	this->SetIcon(SkinSystem::GetSkinSystem()->GetWindowIcon());
}

void MainWindow::OnF3Pressed(wxCommandEvent& WXUNUSED(event)) {
	ProMan* proman = ProMan::GetProfileManager();
	wxCHECK_RET(proman != NULL, _T("OnF3Pressed(): proman is NULL!"));
	
	bool fredEnabled;
	proman->GlobalRead(GBL_CFG_OPT_CONFIG_FRED, &fredEnabled, false);

	proman->GlobalWrite(GBL_CFG_OPT_CONFIG_FRED, !fredEnabled);
	FREDManager::GenerateFREDEnabledChanged();
}