File: BottomButtons.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 (144 lines) | stat: -rw-r--r-- 5,384 bytes parent folder | download | duplicates (7)
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
/*
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 "global/ids.h"
#include "global/ProfileKeys.h"
#include "controls/BottomButtons.h"
#include "controls/ModList.h"
#include "datastructures/FSOExecutable.h"
#include "datastructures/ResolutionMap.h"
#include "apis/FREDManager.h"
#include "apis/TCManager.h"
#include "apis/ProfileManager.h"

#include "generated/configure_launcher.h" // needed to check whether OSX is used

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


BottomButtons::BottomButtons(wxWindow* parent, wxPoint &pos, wxSize &size) : wxPanel(
	parent, wxID_ANY, pos, size) {
		this->SetMinSize(size);

		bool showFred;
		ProMan::GetProfileManager()->GlobalRead(GBL_CFG_OPT_CONFIG_FRED, &showFred, false);
#if 0
		this->close = new wxButton(this, ID_CLOSE_BUTTON, _("Close"));
#endif
		this->help = new wxButton(this, ID_HELP_BUTTON, _("Help"));
		this->fred = new wxButton(this, ID_FRED_BUTTON, _("FRED"));
		this->fred->Show(showFred);

#if 0
		this->update = new wxButton(this, ID_UPDATE_BUTTON, _("Update Available"));
		this->update->Hide();
#endif
		this->play = new wxButton(this, ID_PLAY_BUTTON, _("Play"));
		wxFont playButtonFont = this->play->GetFont();
		playButtonFont.SetWeight(wxFONTWEIGHT_BOLD);
		this->play->SetFont(playButtonFont);
		wxBoxSizer* sizer = new wxBoxSizer(wxHORIZONTAL);
#if 0
		sizer->Add(this->close);
#endif
		sizer->Add(this->help);
		sizer->AddStretchSpacer(1);
#if 0
		sizer->Add(this->update, wxSizerFlags().ReserveSpaceEvenIfHidden());
		sizer->AddStretchSpacer(1);
#endif
		sizer->Add(this->fred, wxSizerFlags().ReserveSpaceEvenIfHidden());
		sizer->Add(this->play);

		this->SetSizerAndFit(sizer);
		this->Layout();

		wxLogDebug(_T("BottomButtons is at %p."), this);

		TCManager::RegisterTCBinaryChanged(this);
		TCManager::RegisterTCChanged(this);
		TCManager::RegisterTCActiveModChanged(this);
		TCManager::RegisterTCFredBinaryChanged(this);
		FREDManager::RegisterFREDEnabledChanged(this);
		ResolutionMap::RegisterResolutionMapChanged(this);
		wxCommandEvent nullEvent;
		this->OnTCChanges(nullEvent);
}

BEGIN_EVENT_TABLE(BottomButtons, wxPanel)
EVT_COMMAND(wxID_NONE, EVT_TC_CHANGED, BottomButtons::OnTCChanges)
EVT_COMMAND(wxID_NONE, EVT_TC_ACTIVE_MOD_CHANGED, BottomButtons::OnTCChanges)
EVT_COMMAND(wxID_NONE, EVT_TC_BINARY_CHANGED, BottomButtons::OnTCChanges)
EVT_COMMAND(wxID_NONE, EVT_TC_FRED_BINARY_CHANGED, BottomButtons::OnTCChanges)
EVT_COMMAND(wxID_NONE, EVT_FRED_ENABLED_CHANGED, BottomButtons::OnFREDEnabledChanged)
EVT_COMMAND(wxID_NONE, EVT_RESOLUTION_MAP_CHANGED, BottomButtons::OnTCChanges)
END_EVENT_TABLE()

/** remove the text after ".app" in the executable name.
 A no op on other platforms*/
#if IS_APPLE
wxString FixBinaryName(const wxString& binaryName) {
	wxString fixedBinaryName(binaryName);
	// the trailing / ensures that the .app indicates an extension
	int DotAppIndex = binaryName.Find(_T(".app/"));
	if (DotAppIndex != wxNOT_FOUND) {
		fixedBinaryName = binaryName.Left(DotAppIndex + 4); // 4 so that ".app" is retained
	}
	return fixedBinaryName;
}
#else
#define FixBinaryName(binaryName) binaryName
#endif

void BottomButtons::OnTCChanges(wxCommandEvent &WXUNUSED(event)) {
	wxString tc, binary, fredBinary;
	ProMan::GetProfileManager()->ProfileRead(PRO_CFG_TC_ROOT_FOLDER, &tc, wxEmptyString);
	ProMan::GetProfileManager()->ProfileRead(PRO_CFG_TC_CURRENT_BINARY, &binary, wxEmptyString);
	if ( tc.IsEmpty() || binary.IsEmpty() || ModList::GetActiveMod() == NULL || !ResolutionMap::HasEntryForActiveMod()) {
		this->play->Disable();
	} else if ( wxFileName(tc + wxFileName::GetPathSeparator() + binary).FileExists() ) {
		this->play->Enable();
	} else {
		wxLogWarning(_("Executable %s does not exist"), FixBinaryName(binary).c_str());
		this->play->Disable();
	}
	ProMan::GetProfileManager()->ProfileRead(PRO_CFG_TC_CURRENT_FRED, &fredBinary, wxEmptyString);
	if ( this->fred == NULL ) {
		// do nothing, no button to manipulate
	} else if ( tc.IsEmpty() || fredBinary.IsEmpty() || (!wxFileName::DirExists(tc)) ||
		(!FSOExecutable::HasFSOExecutables(wxFileName(tc, wxEmptyString))) ) {
		this->fred->Disable();
#if IS_APPLE
	} else if ( wxFileName(tc + wxFileName::GetPathSeparator() + fredBinary).FileExists() ) {
#else
	} else if ( wxFileName(tc, fredBinary).FileExists() ) {
#endif
		this->fred->Enable();
	} else {
		wxLogWarning(_("FRED executable %s does not exist"), FixBinaryName(fredBinary).c_str());
		this->fred->Disable();
	}
}

void BottomButtons::OnFREDEnabledChanged(wxCommandEvent &WXUNUSED(event)) {
	bool showFred;
	ProMan::GetProfileManager()->GlobalRead(GBL_CFG_OPT_CONFIG_FRED, &showFred, false);
	
	this->fred->Show(showFred);
}