forked from zfteam/rs97-commander-sdl2
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcommander.cpp
executable file
·397 lines (379 loc) · 12.7 KB
/
commander.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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
#include <iostream>
#include <sstream>
#include "commander.h"
#include "resourceManager.h"
#include "screen.h"
#include "sdlutils.h"
#include "def.h"
#include "dialog.h"
#include "fileutils.h"
#include "viewer.h"
#include "keyboard.h"
#include <stdio.h>
#define SPLITTER_LINE_W 1
#define X_LEFT 1
#define X_RIGHT screen.w / 2 + SPLITTER_LINE_W + 1
namespace {
SDL_Surface *DrawBackground() {
SDL_Surface *bg = SDL_utils::createSurface(screen.w * screen.ppu_x, screen.h * screen.ppu_y);
// Stripes
const int stripes_h = screen.h - HEADER_H - FOOTER_H;
SDL_Rect rect = SDL_utils::Rect(0, 0, screen.w * screen.ppu_x, screen.h * screen.ppu_y);
const Uint32 bg_colors[2] = {SDL_MapRGB(bg->format, COLOR_BG_1), SDL_MapRGB(bg->format, COLOR_BG_2)};
const std::size_t num_lines = (stripes_h - 1) / LINE_HEIGHT + 1;
for (std::size_t i = 0; i < num_lines; ++i) {
rect.y = (Y_LIST + i * LINE_HEIGHT) * screen.ppu_y;
SDL_FillRect(bg, &rect, bg_colors[i % 2]);
}
// Top and bottom bars
const auto bar_color = SDL_MapRGB(bg->format, COLOR_TITLE_BG);
rect = SDL_utils::Rect(0, 0, static_cast<decltype(SDL_Rect().w)>(bg->w), Y_LIST * screen.ppu_y);
SDL_FillRect(bg, &rect, bar_color);
rect.y = bg->h - FOOTER_H * screen.ppu_y;
SDL_FillRect(bg, &rect, bar_color);
// Line in the middle
rect = SDL_utils::Rect(screen.w / 2 * screen.ppu_x, 0, SPLITTER_LINE_W * screen.ppu_x, Y_LIST * screen.ppu_y);
SDL_FillRect(bg, &rect, bg_colors[0]);
rect.y = rect.h;
rect.h = stripes_h * screen.ppu_y;
SDL_FillRect(bg, &rect, bar_color);
rect.y += rect.h;
rect.h = FOOTER_H * screen.ppu_y;
SDL_FillRect(bg, &rect, bg_colors[0]);
return bg;
}
} // namespace
CCommander::CCommander(const std::string &p_pathL, const std::string &p_pathR):
CWindow::CWindow(),
m_panelLeft(p_pathL, X_LEFT),
m_panelRight(p_pathR, X_RIGHT),
m_panelSource(NULL),
m_panelTarget(NULL),
m_background(DrawBackground())
{
m_panelSource = &m_panelLeft;
m_panelTarget = &m_panelRight;
}
CCommander::~CCommander(void)
{
SDL_FreeSurface(m_background);
}
void CCommander::render(const bool p_focus) const
{
INHIBIT(std::cout << "CCommander::render fullscreen: " << isFullScreen() << " focus: " << p_focus << std::endl;)
// Draw background image
SDL_utils::applySurface(0, 0, m_background, Globals::g_screen);
// Draw panels
m_panelLeft.render(p_focus && (m_panelSource == &m_panelLeft));
m_panelRight.render(p_focus && (m_panelSource == &m_panelRight));
}
const bool CCommander::keyPress(const SDL_Event &p_event)
{
CWindow::keyPress(p_event);
bool l_ret(false);
switch (p_event.key.keysym.sym)
{
case MYKEY_SYSTEM:
if (openSystemMenu())
m_panelSource->refresh();
l_ret = true;
break;
case MYKEY_UP:
l_ret = m_panelSource->moveCursorUp(1);
break;
case MYKEY_DOWN:
l_ret = m_panelSource->moveCursorDown(1);
break;
case MYKEY_PAGEUP:
l_ret = m_panelSource->moveCursorUp(NB_VISIBLE_LINES - 1);
break;
case MYKEY_PAGEDOWN:
l_ret = m_panelSource->moveCursorDown(NB_VISIBLE_LINES - 1);
break;
case MYKEY_LEFT:
if (m_panelSource == &m_panelRight)
{
m_panelSource = &m_panelLeft;
m_panelTarget = &m_panelRight;
l_ret = true;
}
break;
case MYKEY_RIGHT:
if (m_panelSource == &m_panelLeft)
{
m_panelSource = &m_panelRight;
m_panelTarget = &m_panelLeft;
l_ret = true;
}
break;
case MYKEY_OPEN:
if (m_panelSource->isDirectoryHighlighted())
{
// It's a dir => open it
l_ret = m_panelSource->open();
}
else
{
// It's a file => open execute menu
openExecuteMenu();
l_ret = true;
}
break;
case MYKEY_PARENT:
l_ret = m_panelSource->goToParentDir();
break;
case MYKEY_OPERATION:
// If there's no file in the select list, add current file
if (m_panelSource->getSelectList().empty() && m_panelSource->getHighlightedItem() != "..")
m_panelSource->addToSelectList(false);
if (!m_panelSource->getSelectList().empty())
{
if (openCopyMenu())
{
// Refresh file lists
m_panelSource->refresh();
m_panelTarget->refresh();
}
else
{
if (m_panelSource->getSelectList().size() == 1 && (*m_panelSource->getSelectList().begin()) == m_panelSource->getHighlightedIndex())
m_panelSource->selectNone();
}
l_ret = true;
}
break;
case MYKEY_SELECT:
l_ret = m_panelSource->addToSelectList(true);
break;
case MYKEY_TRANSFER:
if (m_panelSource->isDirectoryHighlighted() && m_panelSource->getHighlightedItem() != "..")
l_ret = m_panelTarget->open(m_panelSource->getHighlightedItemFull());
else
l_ret = m_panelTarget->open(m_panelSource->getCurrentPath());
break;
default:
break;
}
return l_ret;
}
const bool CCommander::keyHold(void)
{
bool l_ret(false);
switch(m_lastPressed)
{
case MYKEY_UP:
if (tick(SDL_GetKeyboardState(NULL)[SDL_GetScancodeFromKey(MYKEY_UP)]))
l_ret = m_panelSource->moveCursorUp(1);
break;
case MYKEY_DOWN:
if (tick(SDL_GetKeyboardState(NULL)[SDL_GetScancodeFromKey(MYKEY_DOWN)]))
l_ret = m_panelSource->moveCursorDown(1);
break;
case MYKEY_PAGEUP:
if (tick(SDL_GetKeyboardState(NULL)[SDL_GetScancodeFromKey(MYKEY_PAGEUP)]))
l_ret = m_panelSource->moveCursorUp(NB_VISIBLE_LINES - 1);
break;
case MYKEY_PAGEDOWN:
if (tick(SDL_GetKeyboardState(NULL)[SDL_GetScancodeFromKey(MYKEY_PAGEDOWN)]))
l_ret = m_panelSource->moveCursorDown(NB_VISIBLE_LINES - 1);
break;
case MYKEY_SELECT:
if (tick(SDL_GetKeyboardState(NULL)[SDL_GetScancodeFromKey(MYKEY_SELECT)]))
l_ret = m_panelSource->addToSelectList(true);
break;
default:
break;
}
return l_ret;
}
const bool CCommander::openCopyMenu(void) const
{
bool l_ret(false);
int l_dialogRetVal(0);
bool l_rename(false);
// List of selected files
std::vector<std::string> l_list;
m_panelSource->getSelectList(l_list);
// The rename option appears only if one item is selected
l_rename = (l_list.size() == 1);
{
bool l_loop(false);
std::ostringstream l_stream;
l_stream << l_list.size() << " selected:";
// File operation dialog
CDialog l_dialog(l_stream.str(), 0, Y_LIST + m_panelSource->getHighlightedIndexRelative() * LINE_HEIGHT);
l_dialog.addOption(m_panelSource == &m_panelLeft ? "Copy >" : "< Copy");
l_dialog.addOption(m_panelSource == &m_panelLeft ? "Move >" : "< Move");
if (l_rename)
l_dialog.addOption("Rename");
l_dialog.addOption("Delete");
l_dialog.addOption("Disk used");
l_dialog.init();
do
{
l_loop = false;
l_dialogRetVal = l_dialog.execute();
if (l_dialogRetVal == 3 + l_rename)
{
CDialog l_dialog2("", l_dialog.getX() + l_dialog.getImage()->w - DIALOG_BORDER, l_dialog.getY() + DIALOG_BORDER + (l_dialog.getHighlightedIndex() + 1) * LINE_HEIGHT);
l_dialog2.addOption("Yes");
l_dialog2.addOption("No");
l_dialog2.init();
if (l_dialog2.execute() != 1)
l_loop = true;
}
}
while (l_loop);
}
// Perform operation
switch (l_dialogRetVal)
{
case 1:
// Copy
File_utils::copyFile(l_list, m_panelTarget->getCurrentPath());
l_ret = true;
break;
case 2:
// Move
File_utils::moveFile(l_list, m_panelTarget->getCurrentPath());
l_ret = true;
break;
case 3:
if (l_rename)
{
// Rename
CKeyboard l_keyboard(m_panelSource->getHighlightedItem());
if (l_keyboard.execute() == 1 && !l_keyboard.getInputText().empty() && l_keyboard.getInputText() != m_panelSource->getHighlightedItem())
{
File_utils::renameFile(m_panelSource->getHighlightedItemFull(), m_panelSource->getCurrentPath() + (m_panelSource->getCurrentPath() == "/" ? "" : "/") + l_keyboard.getInputText());
l_ret = true;
}
}
else
{
// Delete
printf("About to remove file");
printf("%s", l_list.at(0).c_str());
File_utils::removeFile(l_list);
printf("Done removing file.");
l_ret = true;
}
break;
case 4:
if (l_rename)
{
// Delete
File_utils::removeFile(l_list);
l_ret = true;
}
else
// Disk used
File_utils::diskUsed(l_list);
break;
case 5:
if (l_rename)
// Disk used
File_utils::diskUsed(l_list);
break;
default:
break;
}
return l_ret;
}
const bool CCommander::openSystemMenu(void)
{
bool l_ret(false);
int l_dialogRetVal(0);
// Selection dialog
{
CDialog l_dialog("System:", 0, Y_LIST + m_panelSource->getHighlightedIndexRelative() * LINE_HEIGHT);
l_dialog.addOption("Select all");
l_dialog.addOption("Select none");
l_dialog.addOption("New directory");
l_dialog.addOption("Disk info");
l_dialog.addOption("Quit");
l_dialog.init();
l_dialogRetVal = l_dialog.execute();
}
switch (l_dialogRetVal)
{
case 1:
// Select all
m_panelSource->selectAll();
break;
case 2:
// Select none
m_panelSource->selectNone();
break;
case 3:
// New dir
{
CKeyboard l_keyboard("");
if (l_keyboard.execute() == 1 && !l_keyboard.getInputText().empty())
{
File_utils::makeDirectory(m_panelSource->getCurrentPath() + (m_panelSource->getCurrentPath() == "/" ? "" : "/") + l_keyboard.getInputText());
l_ret = true;
}
}
break;
case 4:
// Disk info
File_utils::diskInfo();
break;
case 5:
// Quit
m_retVal = -1;
break;
default:
break;
}
return l_ret;
}
void CCommander::openExecuteMenu(void) const
{
int l_dialogRetVal(0);
// Dialog
{
CDialog l_dialog(m_panelSource->getHighlightedItem() + ":", 0, Y_LIST + m_panelSource->getHighlightedIndexRelative() * LINE_HEIGHT);
l_dialog.addOption("View");
l_dialog.addOption("Execute");
l_dialog.init();
l_dialogRetVal = l_dialog.execute();
}
// Perform operation
switch (l_dialogRetVal)
{
case 1:
// View
{
// Check size
const std::string l_file(m_panelSource->getHighlightedItemFull());
INHIBIT(std::cout << "File size: " << File_utils::getFileSize(l_file) << std::endl;)
if (File_utils::getFileSize(l_file) > VIEWER_SIZE_MAX)
{
// File is too big to be viewed!
CDialog l_dialog("Error:", 0, 0);
l_dialog.addLabel("File is too big!");
l_dialog.addOption("OK");
l_dialog.init();
l_dialog.execute();
}
else
{
CViewer l_viewer(m_panelSource->getHighlightedItemFull());
l_viewer.execute();
}
}
break;
case 2:
// Execute
File_utils::executeFile(m_panelSource->getHighlightedItemFull());
break;
default:
break;
}
}
bool CCommander::isFullScreen(void) const
{
return true;
}