This repository has been archived by the owner on Jan 13, 2020. It is now read-only.
/
browsedialog.cpp
376 lines (328 loc) · 8.77 KB
/
browsedialog.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
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
#include "browsedialog.h"
#include <algorithm>
#include "filelister.h"
#include "gmenu2x.h"
#include "iconbutton.h"
#include "surface.h"
#include "utilities.h"
using std::bind;
using std::max;
using std::string;
using std::tie;
using std::unique_ptr;
BrowseDialog::BrowseDialog(
GMenu2X *gmenu2x,
const string &title, const string &subtitle)
: Dialog(gmenu2x)
, title(title)
, subtitle(subtitle)
{
}
BrowseDialog::~BrowseDialog()
{
}
void BrowseDialog::initButtonBox()
{
buttonBox.clear();
// Left or Cancel to go up a directory, if directories are shown.
// Accept also goes up a directory if the selection is "..".
if (fl.getShowDirectories() && getPath() != "/") {
buttonBox.add(unique_ptr<IconButton>(new IconButton(
gmenu2x, "skin:imgs/buttons/left.png")));
if (selected < fl.size() && fl[selected] == "..") {
buttonBox.add(unique_ptr<IconButton>(new IconButton(
gmenu2x, "skin:imgs/buttons/accept.png")));
}
buttonBox.add(unique_ptr<IconButton>(new IconButton(
gmenu2x, "skin:imgs/buttons/cancel.png",
gmenu2x->tr["Up one folder"],
bind(&BrowseDialog::directoryUp, this))));
}
// Accept to enter a directory (as opposed to selecting it). -else-
// Accept to confirm the selection of a file, if files are allowed.
if (fl[selected] != "..") {
if (selected < fl.size() && fl.isDirectory(selected)) {
buttonBox.add(unique_ptr<IconButton>(new IconButton(
gmenu2x, "skin:imgs/buttons/accept.png",
gmenu2x->tr["Enter"],
bind(&BrowseDialog::directoryEnter, this))));
} else if (canSelect()) {
buttonBox.add(unique_ptr<IconButton>(new IconButton(
gmenu2x, "skin:imgs/buttons/accept.png")));
}
}
// Start to confirm the selection of a file or directory if allowed.
if (canSelect()) {
buttonBox.add(unique_ptr<IconButton>(new IconButton(
gmenu2x, "skin:imgs/buttons/start.png",
gmenu2x->tr["Select"],
bind(&BrowseDialog::confirm, this))));
}
// Cancel (if directories are hidden) or Select to exit.
if (!fl.getShowDirectories()) {
buttonBox.add(unique_ptr<IconButton>(new IconButton(
gmenu2x, "skin:imgs/buttons/cancel.png")));
}
buttonBox.add(unique_ptr<IconButton>(new IconButton(
gmenu2x, "skin:imgs/buttons/select.png",
gmenu2x->tr["Exit"],
bind(&BrowseDialog::quit, this))));
}
void BrowseDialog::initIcons()
{
iconGoUp = gmenu2x->sc.skinRes("imgs/go-up.png");
iconFolder = gmenu2x->sc.skinRes("imgs/folder.png");
iconFile = gmenu2x->sc.skinRes("imgs/file.png");
}
void BrowseDialog::initDisplay()
{
unsigned int top, height;
tie(top, height) = gmenu2x->getContentArea();
// Figure out how many items we can fit in the content area.
rowHeight = gmenu2x->font->getLineSpacing();
if (fl.getShowDirectories() && iconFolder) {
rowHeight = max(rowHeight, (unsigned int) (iconFolder->height() + 2));
}
numRows = max(height / rowHeight, 1u);
// Redistribute any leftover space.
rowHeight = height / numRows;
topBarHeight = top + (height - rowHeight * numRows) / 2;
clipRect = (SDL_Rect) {
0,
static_cast<Sint16>(topBarHeight + 1),
static_cast<Uint16>(gmenu2x->resX - 9),
static_cast<Uint16>(height - 1)
};
}
void BrowseDialog::initPath()
{
string path = getPath();
if (path.empty()) {
path = CARD_ROOT;
}
// fl.browse has to run at least once.
while (!fl.browse(path) && fl.getShowDirectories() && path != "/") {
// The given directory could not be opened; try parent.
path = parentDir(path);
}
setPath(path);
}
void BrowseDialog::initSelection()
{
selected = 0;
}
bool BrowseDialog::exec()
{
initIcons();
initDisplay();
initPath();
initSelection();
close = false;
while (!close) {
initButtonBox();
paint();
handleInput();
}
return result;
}
BrowseDialog::Action BrowseDialog::getAction(InputManager::Button button)
{
switch (button) {
case InputManager::MENU:
return BrowseDialog::ACT_CLOSE;
case InputManager::UP:
return BrowseDialog::ACT_UP;
case InputManager::DOWN:
return BrowseDialog::ACT_DOWN;
case InputManager::ALTLEFT:
return BrowseDialog::ACT_SCROLLUP;
case InputManager::ALTRIGHT:
return BrowseDialog::ACT_SCROLLDOWN;
case InputManager::LEFT:
case InputManager::CANCEL:
return BrowseDialog::ACT_GOUP;
case InputManager::ACCEPT:
return BrowseDialog::ACT_SELECT;
case InputManager::SETTINGS:
return BrowseDialog::ACT_CONFIRM;
default:
return BrowseDialog::ACT_NONE;
}
}
void BrowseDialog::handleInput()
{
InputManager::Button button = gmenu2x->input.waitForPressedButton();
BrowseDialog::Action action = getAction(button);
if (action == BrowseDialog::ACT_SELECT
&& selected < fl.size() && fl[selected] == "..") {
action = BrowseDialog::ACT_GOUP;
}
switch (action) {
case BrowseDialog::ACT_UP:
if (fl.size() > 0) {
selected = (selected == 0)
? fl.size() - 1
: selected - 1;
}
break;
case BrowseDialog::ACT_SCROLLUP:
if (fl.size() > 0) {
selected = (selected <= numRows - 2)
? 0
: selected - (numRows - 2);
}
break;
case BrowseDialog::ACT_DOWN:
if (fl.size() > 0) {
selected = (fl.size() - 1 <= selected)
? 0
: selected + 1;
}
break;
case BrowseDialog::ACT_SCROLLDOWN:
if (fl.size() > 0) {
selected = (selected + (numRows - 2) >= fl.size())
? fl.size() - 1
: selected + (numRows - 2);
}
break;
case BrowseDialog::ACT_GOUP:
if (fl.getShowDirectories()) {
directoryUp();
break;
}
/* Fall through (If not showing directories, GOUP acts as CLOSE) */
case BrowseDialog::ACT_CLOSE:
quit();
break;
case BrowseDialog::ACT_SELECT:
if (selected < fl.size() && fl.isDirectory(selected)) {
directoryEnter();
break;
}
/* Fall through (If not a directory, SELECT acts as CONFIRM) */
case BrowseDialog::ACT_CONFIRM:
if (selected < fl.size() && canSelect()) {
confirm();
}
break;
default:
break;
}
}
void BrowseDialog::directoryUp()
{
string oldDir = getPath();
if (oldDir != "/") {
string newDir = parentDir(oldDir);
setPath(newDir);
fl.browse(newDir);
// Find the position of the previous directory among the directories of
// the parent. If it's not found, select 0.
string oldName = oldDir.substr(newDir.size(), oldDir.size() - newDir.size() - 1);
auto& subdirs = fl.getDirectories();
auto it = find(subdirs.begin(), subdirs.end(), oldName);
selected = it == subdirs.end() ? 0 : it - subdirs.begin();
}
}
void BrowseDialog::directoryEnter()
{
string newDir = getPath() + fl[selected] + "/";
setPath(newDir);
fl.browse(newDir);
selected = 0;
}
void BrowseDialog::confirm()
{
result = true;
close = true;
}
void BrowseDialog::quit()
{
result = false;
close = true;
}
void BrowseDialog::paintBackground()
{
gmenu2x->bg->blit(*gmenu2x->s, 0, 0);
}
void BrowseDialog::paintIcon()
{
drawTitleIcon(*gmenu2x->s, "icons/explorer.png", true);
}
void BrowseDialog::paint()
{
OutputSurface& s = *gmenu2x->s;
unsigned int i, iY;
unsigned int firstElement, lastElement;
unsigned int offsetY;
paintBackground();
paintIcon();
writeTitle(*gmenu2x->s, title);
writeSubTitle(*gmenu2x->s, subtitle);
buttonBox.paint(*gmenu2x->s, 5, gmenu2x->resY - 1);
// TODO(MtH): I have no idea what the right value of firstElement would be,
// but originally it was undefined and that is never a good idea.
firstElement = 0;
if (selected>firstElement+numRows - 1) {
firstElement = selected-numRows + 1;
} else if (selected < firstElement) {
firstElement = selected;
}
lastElement = firstElement + numRows;
if (lastElement > fl.size())
lastElement = fl.size();
offsetY = topBarHeight + 1;
//Files & Directories
s.setClipRect(clipRect);
if (fl.size() == 0) {
gmenu2x->font->write(s, "(" + gmenu2x->tr["no items"] + ")",
4, topBarHeight + rowHeight / 2,
Font::HAlignLeft, Font::VAlignMiddle);
} else {
//Selection
iY = topBarHeight + 1 + (selected - firstElement) * rowHeight;
s.box(2, iY, gmenu2x->resX - 12, rowHeight - 1,
gmenu2x->skinConfColors[COLOR_SELECTION_BG]);
const int nameX = fl.getShowDirectories() ? 24 : 5;
for (i = firstElement; i < lastElement; i++) {
if (fl.getShowDirectories()) {
Surface *icon;
if (fl.isDirectory(i)) {
if (fl[i] == "..") {
icon = iconGoUp;
} else {
icon = iconFolder;
}
} else {
icon = iconFile;
}
if (icon) {
icon->blit(s, 5, offsetY);
}
}
gmenu2x->font->write(s, fl[i], nameX, offsetY + rowHeight / 2,
Font::HAlignLeft, Font::VAlignMiddle);
offsetY += rowHeight;
}
}
s.clearClipRect();
gmenu2x->drawScrollBar(numRows, fl.size(), firstElement);
s.flip();
}
string const& BrowseDialog::getPath() {
return path;
}
void BrowseDialog::setPath(std::string const& path)
{
this->path = path;
if (!path.empty() && path.back() != '/') {
this->path.push_back('/');
}
}
string BrowseDialog::getFile() {
return fl[selected];
}
bool BrowseDialog::canSelect() {
return selected < fl.size() && fl.isFile(selected);
}