-
Notifications
You must be signed in to change notification settings - Fork 4
/
packagedialog.cpp
291 lines (250 loc) · 9.12 KB
/
packagedialog.cpp
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
/*
Copyright (C) 2013 - Voidious
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include <wx/wx.h>
#include "bbwx.h"
#include "ResourcePath.hpp"
#include "packagedialog.h"
PackageDialog::PackageDialog(const char *title, const char *buttonLabel,
PackageDialogListener *listener, MenuBarMaker *menuBarMaker)
: wxFrame(NULL, wxID_ANY, title, wxPoint(50, 50), wxDefaultSize,
wxDEFAULT_FRAME_STYLE & ~ (wxRESIZE_BORDER | wxMAXIMIZE_BOX)) {
listener_ = listener;
menuBarMaker_ = menuBarMaker;
menusInitialized_ = false;
numItems_ = 0;
#ifdef __WINDOWS__
SetIcon(wxIcon(resourcePath() + BERRYBOTS_ICO, wxBITMAP_TYPE_ICO));
// The 8-9 point default font size in Windows is much smaller than Mac/Linux.
wxFont windowFont = GetFont();
if (windowFont.GetPointSize() <= 9) {
SetFont(windowFont.Larger());
}
#elif __WXGTK__
SetIcon(wxIcon(resourcePath() + BBICON_128, wxBITMAP_TYPE_PNG));
#endif
mainPanel_ = new wxPanel(this);
mainSizer_ = new wxBoxSizer(wxHORIZONTAL);
mainSizer_->Add(mainPanel_);
wxBoxSizer *topSizer = new wxBoxSizer(wxHORIZONTAL);
itemSelect_ = new wxListBox(mainPanel_, wxID_ANY, wxDefaultPosition,
wxSize(275, 225), 0, NULL, wxLB_SORT);
topSizer->Add(itemSelect_);
topSizer->AddSpacer(5);
wxBoxSizer *rightSizer = new wxBoxSizer(wxVERTICAL);
versionLabel_ = new wxStaticText(mainPanel_, wxID_ANY, "Version:");
versionText_ = new wxTextCtrl(mainPanel_, wxID_ANY, "1.0", wxDefaultPosition,
wxSize(70, 23));
refreshButton_ = new wxButton(mainPanel_, wxID_REFRESH, " &Refresh ");
rightSizer->Add(refreshButton_, 0, wxEXPAND | wxALIGN_CENTER);
rightSizer->AddStretchSpacer(1);
#ifdef __WXOSX__
keyboardLabel_ = new wxStaticText(mainPanel_, wxID_ANY, "\u2318 hotkeys");
#else
keyboardLabel_ = new wxStaticText(mainPanel_, wxID_ANY, "ALT hotkeys");
#endif
rightSizer->Add(keyboardLabel_, 0, wxALIGN_CENTER);
rightSizer->AddStretchSpacer(1);
wxBoxSizer *versionSizer = new wxBoxSizer(wxHORIZONTAL);
versionSizer->Add(versionLabel_, 0, wxALIGN_CENTER);
versionSizer->AddSpacer(5);
versionSizer->Add(versionText_, 0, wxALIGN_CENTER);
rightSizer->Add(versionSizer);
packageLabel_.Append(" ");
packageLabel_.Append(buttonLabel);
packageLabel_.Append(" ");
modifiedPackageLabel_.Append(buttonLabel);
#ifdef __WXOSX__
modifiedPackageLabel_.Append(" \u2318K");
#else
modifiedPackageLabel_.Append(" alt-K");
#endif
packageButton_ = new wxButton(mainPanel_, wxID_ANY, packageLabel_,
wxDefaultPosition, wxDefaultSize, wxBU_EXACTFIT);
rightSizer->AddSpacer(5);
rightSizer->Add(packageButton_, 0, wxEXPAND | wxALIGN_BOTTOM);
topSizer->Add(rightSizer, 0, wxEXPAND);
borderSizer_ = new wxBoxSizer(wxHORIZONTAL);
borderSizer_->Add(topSizer, 0, wxALL, 12);
mainPanel_->SetSizerAndFit(borderSizer_);
SetSizerAndFit(mainSizer_);
versionText_->MoveAfterInTabOrder(itemSelect_);
packageButton_->MoveAfterInTabOrder(versionText_);
refreshButton_->MoveAfterInTabOrder(packageButton_);
itemSelect_->MoveAfterInTabOrder(refreshButton_);
Connect(this->GetId(), wxEVT_ACTIVATE,
wxActivateEventHandler(PackageDialog::onActivate));
Connect(this->GetId(), wxEVT_SHOW, wxShowEventHandler(PackageDialog::onShow));
Connect(this->GetId(), wxEVT_CLOSE_WINDOW,
wxCommandEventHandler(PackageDialog::onClose));
Connect(packageButton_->GetId(), wxEVT_COMMAND_BUTTON_CLICKED,
wxCommandEventHandler(PackageDialog::onPackage));
Connect(wxID_REFRESH, wxEVT_COMMAND_BUTTON_CLICKED,
wxCommandEventHandler(PackageDialog::onRefreshFiles));
Connect(itemSelect_->GetId(), wxEVT_UPDATE_UI,
wxUpdateUIEventHandler(PackageDialog::onSelectItem));
Connect(versionText_->GetId(), wxEVT_UPDATE_UI,
wxUpdateUIEventHandler(PackageDialog::onUpdateVersion));
#ifdef __WINDOWS__
// On Windows XP, not redrawing the game while interrupted (dialog shown)
// creates visual artifacts, so manually redraw it on update UI in Windows.
// The same kills the framerate in Linux/GTK. Either way works on Mac/Cocoa.
Connect(this->GetId(), wxEVT_UPDATE_UI,
wxUpdateUIEventHandler(PackageDialog::onUpdateUi));
#endif
eventFilter_ = new PackageEventFilter(this);
this->GetEventHandler()->AddFilter(eventFilter_);
}
PackageDialog::~PackageDialog() {
this->GetEventHandler()->RemoveFilter(eventFilter_);
delete eventFilter_;
}
void PackageDialog::clearItems() {
itemSelect_->Clear();
numItems_ = 0;
}
void PackageDialog::addItem(char *name) {
itemSelect_->Append(wxString(name));
numItems_++;
if (itemSelect_->GetCount() > 0) {
itemSelect_->SetFirstItem(0);
}
}
void PackageDialog::onActivate(wxActivateEvent &event) {
if (!menusInitialized_) {
this->SetMenuBar(menuBarMaker_->getNewMenuBar());
menusInitialized_ = true;
Fit();
}
}
void PackageDialog::onShow(wxShowEvent &event) {
if (event.IsShown()) {
focusItemSelect();
}
}
void PackageDialog::onClose(wxCommandEvent &event) {
listener_->onClose();
}
void PackageDialog::onPackage(wxCommandEvent &event) {
packageSelectedItem();
}
void PackageDialog::packageSelectedItem() {
wxArrayInt selectedIndex;
itemSelect_->GetSelections(selectedIndex);
wxString versionString = versionText_->GetValue();
if (selectedIndex.Count() != 0 && versionString.length() > 0) {
wxString selectedItem = itemSelect_->GetString(*(selectedIndex.begin()));
char *name = new char[selectedItem.length() + 1];
#ifdef __WINDOWS__
strcpy(name, selectedItem.c_str());
#else
strcpy(name, selectedItem.fn_str());
#endif
char *version = new char[versionString.length() + 1];
#ifdef __WINDOWS__
strcpy(version, versionString.c_str());
#else
strcpy(version, versionString.fn_str());
#endif
listener_->package(name, version, false);
delete name;
delete version;
}
}
void PackageDialog::onRefreshFiles(wxCommandEvent &event) {
refreshFiles();
}
void PackageDialog::refreshFiles() {
listener_->refreshFiles();
}
void PackageDialog::onEscape() {
listener_->onEscape();
}
void PackageDialog::onSelectItem(wxUpdateUIEvent &event) {
validateButtons();
}
void PackageDialog::onUpdateVersion(wxUpdateUIEvent &event) {
validateButtons();
}
void PackageDialog::onUpdateUi(wxUpdateUIEvent &event) {
listener_->onUpdateUi();
}
void PackageDialog::validateButtons() {
if (versionText_->GetValue().size() > 0) {
validateButtonSelectedListBox(packageButton_, itemSelect_);
} else {
packageButton_->Disable();
}
}
void PackageDialog::validateButtonSelectedListBox(wxButton *button,
wxListBox *listBox) {
wxArrayInt selectedIndex;
listBox->GetSelections(selectedIndex);
if (selectedIndex.Count() > 0) {
button->Enable();
} else {
button->Disable();
}
}
void PackageDialog::setMnemonicLabels(bool modifierDown) {
// TODO: I'd rather it look like the button was pressed when you hit the
// shortcut, if possible. For now having trouble figuring out the
// wxButton::Command() call.
if (modifierDown) {
#ifdef __WXOSX__
refreshButton_->SetLabel("&Refresh \u2318R");
#else
refreshButton_->SetLabel("&Refresh alt-R");
#endif
packageButton_->SetLabel(modifiedPackageLabel_);
} else {
refreshButton_->SetLabel(" &Refresh ");
packageButton_->SetLabel(packageLabel_);
}
}
void PackageDialog::focusItemSelect() {
itemSelect_->SetFocus();
}
PackageEventFilter::PackageEventFilter(PackageDialog *dialog) {
packageDialog_ = dialog;
}
PackageEventFilter::~PackageEventFilter() {
}
int PackageEventFilter::FilterEvent(wxEvent& event) {
bool modifierDown = false;
wxKeyEvent *keyEvent = ((wxKeyEvent*) &event);
#if defined(__WXOSX__)
modifierDown = keyEvent->ControlDown();
#elif defined(__WINDOWS__)
modifierDown = keyEvent->AltDown();
#endif
const wxEventType type = event.GetEventType();
if (type == wxEVT_KEY_DOWN && packageDialog_->IsActive()) {
packageDialog_->setMnemonicLabels(modifierDown);
int keyCode = keyEvent->GetKeyCode();
if (keyCode == WXK_ESCAPE
|| (keyEvent->GetUnicodeKey() == 'W' && keyEvent->ControlDown())) {
packageDialog_->onEscape();
return Event_Processed;
}
}
if (type == wxEVT_KEY_UP) {
packageDialog_->setMnemonicLabels(modifierDown);
} else if (type == wxEVT_KILL_FOCUS) {
packageDialog_->setMnemonicLabels(false);
}
return Event_Skip;
}