2010-05-03 20:58:28 +03:00
|
|
|
#include "browsedialog.h"
|
|
|
|
|
|
|
|
#include "FastDelegate.h"
|
|
|
|
#include "filelister.h"
|
2010-07-04 04:19:52 +03:00
|
|
|
#include "gmenu2x.h"
|
2011-10-23 18:27:29 +03:00
|
|
|
#include "iconbutton.h"
|
|
|
|
#include "utilities.h"
|
2011-10-23 17:13:02 +03:00
|
|
|
|
2010-05-03 20:58:28 +03:00
|
|
|
using namespace fastdelegate;
|
2011-10-23 17:43:56 +03:00
|
|
|
using std::string;
|
2010-05-03 20:58:28 +03:00
|
|
|
|
2011-12-23 15:03:05 +02:00
|
|
|
BrowseDialog::BrowseDialog(
|
|
|
|
GMenu2X *gmenu2x, Touchscreen &ts_,
|
|
|
|
const string &title, const string &subtitle)
|
2010-07-26 05:08:56 +03:00
|
|
|
: Dialog(gmenu2x)
|
2011-12-23 15:03:05 +02:00
|
|
|
, ts(ts_)
|
2010-07-26 05:08:56 +03:00
|
|
|
, title(title)
|
|
|
|
, subtitle(subtitle)
|
|
|
|
, ts_pressed(false)
|
|
|
|
, buttonBox(gmenu2x)
|
2010-05-03 20:58:28 +03:00
|
|
|
{
|
2010-05-03 23:21:36 +03:00
|
|
|
IconButton *btn;
|
2010-05-03 20:58:28 +03:00
|
|
|
|
2011-12-23 15:03:05 +02:00
|
|
|
buttonBox.add(new IconButton(gmenu2x, ts, "skin:imgs/buttons/left.png"));
|
|
|
|
btn = new IconButton(gmenu2x, ts, "skin:imgs/buttons/cancel.png", gmenu2x->tr["Up one folder"]);
|
2010-05-03 23:21:36 +03:00
|
|
|
btn->setAction(MakeDelegate(this, &BrowseDialog::directoryUp));
|
|
|
|
buttonBox.add(btn);
|
2010-05-03 20:58:28 +03:00
|
|
|
|
2011-12-23 15:03:05 +02:00
|
|
|
btn = new IconButton(gmenu2x, ts, "skin:imgs/buttons/accept.png", gmenu2x->tr["Enter folder"]);
|
2010-05-03 23:21:36 +03:00
|
|
|
btn->setAction(MakeDelegate(this, &BrowseDialog::directoryEnter));
|
|
|
|
buttonBox.add(btn);
|
2010-05-03 20:58:28 +03:00
|
|
|
|
2011-12-23 15:03:05 +02:00
|
|
|
btn = new IconButton(gmenu2x, ts, "skin:imgs/buttons/start.png", gmenu2x->tr["Confirm"]);
|
2010-05-03 23:21:36 +03:00
|
|
|
btn->setAction(MakeDelegate(this, &BrowseDialog::confirm));
|
|
|
|
buttonBox.add(btn);
|
2010-05-03 20:58:28 +03:00
|
|
|
|
2011-12-23 15:03:05 +02:00
|
|
|
btn = new IconButton(gmenu2x, ts, "skin:imgs/buttons/select.png", gmenu2x->tr["Exit"]);
|
2011-09-18 03:21:04 +03:00
|
|
|
btn->setAction(MakeDelegate(this, &BrowseDialog::quit));
|
|
|
|
buttonBox.add(btn);
|
|
|
|
|
2010-05-03 20:58:28 +03:00
|
|
|
iconGoUp = gmenu2x->sc.skinRes("imgs/go-up.png");
|
|
|
|
iconFolder = gmenu2x->sc.skinRes("imgs/folder.png");
|
|
|
|
iconFile = gmenu2x->sc.skinRes("imgs/file.png");
|
|
|
|
}
|
|
|
|
|
2010-06-19 05:51:53 +03:00
|
|
|
BrowseDialog::~BrowseDialog()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-05-03 20:58:28 +03:00
|
|
|
bool BrowseDialog::exec()
|
|
|
|
{
|
|
|
|
if (!fl)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
string path = fl->getPath();
|
2010-08-03 18:32:54 +03:00
|
|
|
if (path.empty() || !fileExists(path) || path.compare(0, CARD_ROOT_LEN, CARD_ROOT) != 0)
|
2010-07-04 04:19:52 +03:00
|
|
|
setPath(CARD_ROOT);
|
2010-05-03 20:58:28 +03:00
|
|
|
|
|
|
|
fl->browse();
|
|
|
|
|
2012-04-10 23:50:03 +03:00
|
|
|
const int topBarHeight = gmenu2x->skinConfInt["topBarHeight"];
|
|
|
|
rowHeight = gmenu2x->font->getHeight() + 1; // gp2x=15+1 / pandora=19+1
|
|
|
|
numRows = (gmenu2x->resY - topBarHeight - 20) / rowHeight;
|
2012-04-11 00:01:16 +03:00
|
|
|
clipRect = (SDL_Rect) {
|
|
|
|
0,
|
|
|
|
static_cast<Sint16>(topBarHeight + 1),
|
|
|
|
static_cast<Uint16>(gmenu2x->resX - 9),
|
|
|
|
static_cast<Uint16>(gmenu2x->resY - topBarHeight - 25)
|
|
|
|
};
|
|
|
|
touchRect = (SDL_Rect) {
|
|
|
|
2,
|
|
|
|
static_cast<Sint16>(topBarHeight + 4),
|
|
|
|
static_cast<Uint16>(gmenu2x->resX - 12),
|
|
|
|
clipRect.h
|
|
|
|
};
|
2010-05-03 20:58:28 +03:00
|
|
|
|
|
|
|
selected = 0;
|
2010-07-26 05:29:38 +03:00
|
|
|
close = false;
|
2010-05-03 20:58:28 +03:00
|
|
|
while (!close) {
|
2011-12-23 15:03:05 +02:00
|
|
|
if (ts.available()) ts.poll();
|
2010-05-03 20:58:28 +03:00
|
|
|
|
|
|
|
paint();
|
|
|
|
|
|
|
|
handleInput();
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2011-10-23 10:11:06 +03:00
|
|
|
BrowseDialog::Action BrowseDialog::getAction(InputManager::Button button)
|
2010-05-03 20:58:28 +03:00
|
|
|
{
|
2011-10-23 10:11:06 +03:00
|
|
|
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;
|
|
|
|
}
|
2010-05-03 20:58:28 +03:00
|
|
|
}
|
|
|
|
|
2010-06-19 04:15:20 +03:00
|
|
|
void BrowseDialog::handleInput()
|
2010-05-03 20:58:28 +03:00
|
|
|
{
|
2011-10-23 10:11:06 +03:00
|
|
|
InputManager::Button button = gmenu2x->input.waitForPressedButton();
|
2010-05-03 20:58:28 +03:00
|
|
|
|
2011-10-23 10:11:06 +03:00
|
|
|
BrowseDialog::Action action;
|
2011-12-23 15:03:05 +02:00
|
|
|
if (ts_pressed && !ts.pressed()) {
|
2010-05-03 20:58:28 +03:00
|
|
|
action = BrowseDialog::ACT_SELECT;
|
|
|
|
ts_pressed = false;
|
|
|
|
} else {
|
2011-10-23 10:11:06 +03:00
|
|
|
action = getAction(button);
|
2010-05-03 20:58:28 +03:00
|
|
|
}
|
|
|
|
|
2012-04-10 23:50:03 +03:00
|
|
|
if (ts.available() && ts.pressed() && !ts.inRect(touchRect)) {
|
|
|
|
ts_pressed = false;
|
|
|
|
}
|
2010-05-03 20:58:28 +03:00
|
|
|
|
2012-04-10 23:50:03 +03:00
|
|
|
if (action == BrowseDialog::ACT_SELECT && (*fl)[selected] == "..") {
|
2010-05-03 20:58:28 +03:00
|
|
|
action = BrowseDialog::ACT_GOUP;
|
2012-04-10 23:50:03 +03:00
|
|
|
}
|
2010-05-03 20:58:28 +03:00
|
|
|
switch (action) {
|
|
|
|
case BrowseDialog::ACT_CLOSE:
|
2011-09-18 03:21:04 +03:00
|
|
|
quit();
|
2010-05-03 20:58:28 +03:00
|
|
|
break;
|
|
|
|
case BrowseDialog::ACT_UP:
|
|
|
|
if (selected == 0)
|
|
|
|
selected = fl->size() - 1;
|
|
|
|
else
|
|
|
|
selected -= 1;
|
|
|
|
break;
|
|
|
|
case BrowseDialog::ACT_SCROLLUP:
|
|
|
|
if (selected <= numRows - 2)
|
|
|
|
selected = 0;
|
|
|
|
else
|
|
|
|
selected -= numRows - 2;
|
|
|
|
break;
|
|
|
|
case BrowseDialog::ACT_DOWN:
|
|
|
|
if (fl->size() - 1 <= selected)
|
|
|
|
selected = 0;
|
|
|
|
else
|
|
|
|
selected += 1;
|
|
|
|
break;
|
|
|
|
case BrowseDialog::ACT_SCROLLDOWN:
|
|
|
|
if (selected+(numRows-2)>=fl->size())
|
|
|
|
selected = fl->size()-1;
|
|
|
|
else
|
|
|
|
selected += numRows-2;
|
|
|
|
break;
|
|
|
|
case BrowseDialog::ACT_GOUP:
|
|
|
|
directoryUp();
|
|
|
|
break;
|
|
|
|
case BrowseDialog::ACT_SELECT:
|
|
|
|
if (fl->isDirectory(selected)) {
|
|
|
|
directoryEnter();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* Falltrough */
|
|
|
|
case BrowseDialog::ACT_CONFIRM:
|
|
|
|
confirm();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2010-05-03 21:25:08 +03:00
|
|
|
|
2010-05-03 23:21:36 +03:00
|
|
|
buttonBox.handleTS();
|
2010-05-03 20:58:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
void BrowseDialog::directoryUp()
|
|
|
|
{
|
|
|
|
string path = fl->getPath();
|
|
|
|
string::size_type p = path.rfind("/");
|
|
|
|
|
2012-04-10 23:50:03 +03:00
|
|
|
if (p == path.size() - 1) {
|
2010-05-03 20:58:28 +03:00
|
|
|
p = path.rfind("/", p - 1);
|
2012-04-10 23:50:03 +03:00
|
|
|
}
|
2011-10-23 09:57:52 +03:00
|
|
|
|
2010-08-03 18:46:34 +03:00
|
|
|
if (p == string::npos || path.compare(0, 1, "/") != 0 || path.length() < 2) {
|
2011-09-18 03:21:04 +03:00
|
|
|
quit();
|
2010-05-03 20:58:28 +03:00
|
|
|
} else {
|
|
|
|
selected = 0;
|
|
|
|
setPath(path.substr(0, p));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BrowseDialog::directoryEnter()
|
|
|
|
{
|
2010-05-03 21:28:34 +03:00
|
|
|
string path = fl->getPath();
|
2012-04-10 23:50:03 +03:00
|
|
|
if (path[path.size()-1] != '/') {
|
2010-05-03 21:28:34 +03:00
|
|
|
path += "/";
|
2012-04-10 23:50:03 +03:00
|
|
|
}
|
2010-05-03 21:28:34 +03:00
|
|
|
|
|
|
|
setPath(path + fl->at(selected));
|
|
|
|
|
2010-05-03 20:58:28 +03:00
|
|
|
selected = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BrowseDialog::confirm()
|
|
|
|
{
|
2010-07-26 05:31:16 +03:00
|
|
|
result = true;
|
2010-05-03 20:58:28 +03:00
|
|
|
close = true;
|
|
|
|
}
|
|
|
|
|
2011-09-18 03:21:04 +03:00
|
|
|
void BrowseDialog::quit()
|
|
|
|
{
|
|
|
|
result = false;
|
|
|
|
close = true;
|
|
|
|
}
|
|
|
|
|
2010-05-03 20:58:28 +03:00
|
|
|
void BrowseDialog::paint()
|
|
|
|
{
|
|
|
|
unsigned int i, iY;
|
|
|
|
unsigned int firstElement, lastElement;
|
|
|
|
unsigned int offsetY;
|
|
|
|
|
|
|
|
gmenu2x->bg->blit(gmenu2x->s, 0, 0);
|
2010-05-05 15:35:52 +03:00
|
|
|
drawTitleIcon("icons/explorer.png", true);
|
|
|
|
writeTitle(title);
|
|
|
|
writeSubTitle(subtitle);
|
2010-05-03 20:58:28 +03:00
|
|
|
|
2010-05-03 23:21:36 +03:00
|
|
|
buttonBox.paint(5);
|
2010-05-03 20:58:28 +03:00
|
|
|
|
2010-06-19 04:15:20 +03:00
|
|
|
// 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;
|
2012-04-10 23:50:03 +03:00
|
|
|
if (selected>firstElement+numRows - 1) {
|
|
|
|
firstElement = selected-numRows + 1;
|
|
|
|
} else if (selected < firstElement) {
|
2010-05-03 20:58:28 +03:00
|
|
|
firstElement = selected;
|
2012-04-10 23:50:03 +03:00
|
|
|
}
|
2010-05-03 20:58:28 +03:00
|
|
|
|
|
|
|
//Selection
|
2012-04-10 23:50:03 +03:00
|
|
|
const int topBarHeight = gmenu2x->skinConfInt["topBarHeight"];
|
|
|
|
iY = topBarHeight + 1 + (selected - firstElement) * rowHeight;
|
|
|
|
gmenu2x->s->box(2, iY, gmenu2x->resX - 12, rowHeight - 1,
|
|
|
|
gmenu2x->skinConfColors[COLOR_SELECTION_BG]);
|
2010-05-03 20:58:28 +03:00
|
|
|
|
|
|
|
lastElement = firstElement + numRows;
|
|
|
|
if (lastElement > fl->size())
|
|
|
|
lastElement = fl->size();
|
|
|
|
|
2012-04-10 23:50:03 +03:00
|
|
|
offsetY = topBarHeight + 1;
|
2010-05-03 20:58:28 +03:00
|
|
|
|
|
|
|
//Files & Directories
|
|
|
|
gmenu2x->s->setClipRect(clipRect);
|
|
|
|
for (i = firstElement; i < lastElement; i++) {
|
2012-04-10 23:50:03 +03:00
|
|
|
Surface *icon;
|
2010-05-03 20:58:28 +03:00
|
|
|
if (fl->isDirectory(i)) {
|
2012-04-10 23:50:03 +03:00
|
|
|
if ((*fl)[i] == "..") {
|
2010-05-03 20:58:28 +03:00
|
|
|
icon = iconGoUp;
|
2012-04-10 23:50:03 +03:00
|
|
|
} else {
|
2010-05-03 20:58:28 +03:00
|
|
|
icon = iconFolder;
|
2012-04-10 23:50:03 +03:00
|
|
|
}
|
2010-05-03 20:58:28 +03:00
|
|
|
} else {
|
|
|
|
icon = iconFile;
|
|
|
|
}
|
|
|
|
icon->blit(gmenu2x->s, 5, offsetY);
|
2012-04-10 23:50:03 +03:00
|
|
|
gmenu2x->s->write(gmenu2x->font, (*fl)[i], 24, offsetY + 8,
|
|
|
|
ASFont::HAlignLeft, ASFont::VAlignMiddle);
|
2010-05-03 20:58:28 +03:00
|
|
|
|
2012-04-10 23:50:03 +03:00
|
|
|
if (ts.available() && ts.pressed()
|
|
|
|
&& ts.inRect(touchRect.x, offsetY + 3, touchRect.w, rowHeight)) {
|
2010-05-03 20:58:28 +03:00
|
|
|
ts_pressed = true;
|
|
|
|
selected = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
offsetY += rowHeight;
|
|
|
|
}
|
|
|
|
gmenu2x->s->clearClipRect();
|
|
|
|
|
2012-04-10 23:50:03 +03:00
|
|
|
gmenu2x->drawScrollBar(
|
|
|
|
numRows,fl->size(), firstElement, clipRect.y, clipRect.h);
|
2010-05-03 20:58:28 +03:00
|
|
|
gmenu2x->s->flip();
|
|
|
|
}
|