2010-02-04 13:33:47 +02:00
|
|
|
/***************************************************************************
|
2011-10-23 17:13:02 +03:00
|
|
|
* Copyright (C) 2006 by Massimiliano Torromeo *
|
|
|
|
* massimiliano.torromeo@gmail.com *
|
2010-02-04 13:33:47 +02:00
|
|
|
* *
|
|
|
|
* 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., *
|
|
|
|
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
|
|
|
***************************************************************************/
|
|
|
|
|
2011-10-23 18:27:29 +03:00
|
|
|
#include "selector.h"
|
|
|
|
|
|
|
|
#include "debug.h"
|
|
|
|
#include "filelister.h"
|
|
|
|
#include "gmenu2x.h"
|
|
|
|
#include "linkapp.h"
|
|
|
|
#include "menu.h"
|
2013-08-03 23:30:12 +03:00
|
|
|
#include "surface.h"
|
2011-10-23 18:27:29 +03:00
|
|
|
#include "utilities.h"
|
|
|
|
|
2010-02-04 13:33:47 +02:00
|
|
|
#include <SDL.h>
|
2011-10-23 18:00:23 +03:00
|
|
|
#include <algorithm>
|
2010-02-04 13:33:47 +02:00
|
|
|
|
|
|
|
//for browsing the filesystem
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <dirent.h>
|
2010-06-19 05:44:03 +03:00
|
|
|
#include <fstream>
|
2010-02-04 13:33:47 +02:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
2010-05-05 15:35:52 +03:00
|
|
|
Selector::Selector(GMenu2X *gmenu2x, LinkApp *link, const string &selectorDir) :
|
|
|
|
Dialog(gmenu2x)
|
|
|
|
{
|
2010-02-04 13:33:47 +02:00
|
|
|
this->link = link;
|
|
|
|
selRow = 0;
|
2013-08-16 10:16:04 +03:00
|
|
|
if (selectorDir.empty())
|
2010-02-04 13:33:47 +02:00
|
|
|
dir = link->getSelectorDir();
|
|
|
|
else
|
|
|
|
dir = selectorDir;
|
|
|
|
if (dir[dir.length()-1]!='/') dir += "/";
|
|
|
|
}
|
|
|
|
|
|
|
|
int Selector::exec(int startSelection) {
|
2014-08-13 03:24:18 +03:00
|
|
|
FileLister fl;
|
|
|
|
fl.setShowDirectories(link->getSelectorBrowser());
|
2010-02-04 13:33:47 +02:00
|
|
|
fl.setFilter(link->getSelectorFilter());
|
2014-08-13 03:24:18 +03:00
|
|
|
prepare(fl);
|
2010-02-04 13:33:47 +02:00
|
|
|
|
2014-08-10 15:21:14 +03:00
|
|
|
OffscreenSurface bg(*gmenu2x->bg);
|
2014-08-10 05:26:59 +03:00
|
|
|
drawTitleIcon(bg, link->getIconPath(), true);
|
|
|
|
writeTitle(bg, link->getTitle());
|
|
|
|
writeSubTitle(bg, link->getDescription());
|
2010-02-04 13:33:47 +02:00
|
|
|
|
|
|
|
if (link->getSelectorBrowser()) {
|
2014-08-10 05:26:59 +03:00
|
|
|
gmenu2x->drawButton(bg, "start", gmenu2x->tr["Exit"],
|
|
|
|
gmenu2x->drawButton(bg, "accept", gmenu2x->tr["Select"],
|
|
|
|
gmenu2x->drawButton(bg, "cancel", gmenu2x->tr["Up one folder"],
|
|
|
|
gmenu2x->drawButton(bg, "left", "", 5)-10)));
|
2010-02-04 13:33:47 +02:00
|
|
|
} else {
|
2014-08-10 05:26:59 +03:00
|
|
|
gmenu2x->drawButton(bg, "start", gmenu2x->tr["Exit"],
|
|
|
|
gmenu2x->drawButton(bg, "cancel", "",
|
|
|
|
gmenu2x->drawButton(bg, "accept", gmenu2x->tr["Select"], 5)) - 10);
|
2010-02-04 13:33:47 +02:00
|
|
|
}
|
|
|
|
|
2014-01-16 01:44:57 +02:00
|
|
|
unsigned int top, height;
|
|
|
|
tie(top, height) = gmenu2x->getContentArea();
|
|
|
|
|
2014-06-18 23:07:09 +03:00
|
|
|
int fontheight = gmenu2x->font->getLineSpacing();
|
2014-01-16 17:01:33 +02:00
|
|
|
if (link->getSelectorBrowser())
|
|
|
|
fontheight = constrain(fontheight, 20, 40);
|
2014-01-16 01:44:57 +02:00
|
|
|
unsigned int nb_elements = height / fontheight;
|
|
|
|
|
2011-06-03 12:46:36 +03:00
|
|
|
bg.convertToDisplayFormat();
|
|
|
|
|
2014-08-12 08:16:25 +03:00
|
|
|
unsigned int firstElement = 0;
|
|
|
|
unsigned int selected = constrain(startSelection, 0, fl.size() - 1);
|
2010-02-04 13:33:47 +02:00
|
|
|
|
2014-08-12 09:06:15 +03:00
|
|
|
auto folderIcon = gmenu2x->sc.skinRes("imgs/folder.png");
|
|
|
|
if (!folderIcon) {
|
|
|
|
folderIcon = gmenu2x->sc.addSkinRes("imgs/folder.png");
|
|
|
|
}
|
2014-08-12 08:16:25 +03:00
|
|
|
|
|
|
|
bool close = false, result = true;
|
2010-02-04 13:33:47 +02:00
|
|
|
while (!close) {
|
2014-08-10 14:25:54 +03:00
|
|
|
OutputSurface& s = *gmenu2x->s;
|
2014-08-10 05:26:59 +03:00
|
|
|
|
|
|
|
bg.blit(s, 0, 0);
|
2010-02-04 13:33:47 +02:00
|
|
|
|
2014-01-16 01:44:57 +02:00
|
|
|
if (selected >= firstElement + nb_elements)
|
|
|
|
firstElement = selected - nb_elements + 1;
|
|
|
|
if (selected < firstElement)
|
|
|
|
firstElement = selected;
|
2010-02-04 13:33:47 +02:00
|
|
|
|
|
|
|
//Screenshot
|
2014-08-12 09:06:15 +03:00
|
|
|
if (fl.isFile(selected)) {
|
2014-08-12 10:14:31 +03:00
|
|
|
string path = screendir + trimExtension(fl[selected]) + ".png";
|
2014-08-15 02:34:35 +03:00
|
|
|
auto screenshot = OffscreenSurface::loadImage(path, false);
|
2014-08-10 05:26:59 +03:00
|
|
|
if (screenshot) {
|
|
|
|
screenshot->blitRight(s, 320, 0, 320, 240, 128u);
|
|
|
|
}
|
2010-02-04 13:33:47 +02:00
|
|
|
}
|
|
|
|
|
2014-07-13 23:49:26 +03:00
|
|
|
//Selection
|
2014-08-12 08:16:25 +03:00
|
|
|
unsigned int iY = top + (selected - firstElement) * fontheight;
|
2014-07-13 23:49:26 +03:00
|
|
|
if (selected<fl.size())
|
2014-08-10 05:26:59 +03:00
|
|
|
s.box(1, iY, 309, fontheight, gmenu2x->skinConfColors[COLOR_SELECTION_BG]);
|
2014-07-13 23:49:26 +03:00
|
|
|
|
2010-02-04 13:33:47 +02:00
|
|
|
//Files & Dirs
|
2014-08-10 05:26:59 +03:00
|
|
|
s.setClipRect(0, top, 311, height);
|
2014-08-12 08:16:25 +03:00
|
|
|
for (unsigned int i = firstElement; i < fl.size()
|
2014-01-16 01:44:57 +02:00
|
|
|
&& i < firstElement + nb_elements; i++) {
|
2010-02-04 13:33:47 +02:00
|
|
|
iY = i-firstElement;
|
|
|
|
if (fl.isDirectory(i)) {
|
2014-08-12 09:06:15 +03:00
|
|
|
folderIcon->blit(s, 4, top + (iY * fontheight));
|
2014-08-10 05:26:59 +03:00
|
|
|
gmenu2x->font->write(s, fl[i], 21,
|
2014-01-16 17:01:33 +02:00
|
|
|
top + (iY * fontheight) + (fontheight / 2),
|
|
|
|
Font::HAlignLeft, Font::VAlignMiddle);
|
2010-02-04 13:33:47 +02:00
|
|
|
} else
|
2014-08-12 10:14:31 +03:00
|
|
|
gmenu2x->font->write(s, trimExtension(fl[i]), 4,
|
2014-01-16 17:01:33 +02:00
|
|
|
top + (iY * fontheight) + (fontheight / 2),
|
|
|
|
Font::HAlignLeft, Font::VAlignMiddle);
|
2010-02-04 13:33:47 +02:00
|
|
|
}
|
2014-08-10 05:26:59 +03:00
|
|
|
s.clearClipRect();
|
2010-02-04 13:33:47 +02:00
|
|
|
|
2014-01-16 01:44:57 +02:00
|
|
|
gmenu2x->drawScrollBar(nb_elements, fl.size(), firstElement);
|
2014-08-10 05:26:59 +03:00
|
|
|
s.flip();
|
2010-02-04 13:33:47 +02:00
|
|
|
|
2013-08-30 14:02:56 +03:00
|
|
|
switch (gmenu2x->input.waitForPressedButton()) {
|
|
|
|
case InputManager::SETTINGS:
|
|
|
|
close = true;
|
|
|
|
result = false;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case InputManager::UP:
|
|
|
|
if (selected == 0) selected = fl.size() -1;
|
|
|
|
else selected -= 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case InputManager::ALTLEFT:
|
2014-01-16 01:44:57 +02:00
|
|
|
if ((int)(selected - nb_elements + 1) < 0)
|
|
|
|
selected = 0;
|
|
|
|
else
|
|
|
|
selected -= nb_elements - 1;
|
2013-08-30 14:02:56 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case InputManager::DOWN:
|
|
|
|
if (selected+1>=fl.size()) selected = 0;
|
|
|
|
else selected += 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case InputManager::ALTRIGHT:
|
2014-01-16 01:44:57 +02:00
|
|
|
if (selected + nb_elements - 1 >= fl.size())
|
|
|
|
selected = fl.size() - 1;
|
|
|
|
else
|
|
|
|
selected += nb_elements - 1;
|
2013-08-30 14:02:56 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case InputManager::CANCEL:
|
|
|
|
if (!link->getSelectorBrowser()) {
|
2010-02-04 13:33:47 +02:00
|
|
|
close = true;
|
|
|
|
result = false;
|
2013-08-30 14:02:56 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case InputManager::LEFT:
|
|
|
|
if (link->getSelectorBrowser()) {
|
|
|
|
string::size_type p = dir.rfind("/", dir.size()-2);
|
2014-08-15 15:19:17 +03:00
|
|
|
if (p==string::npos || dir.length() < 2 || dir[0] != '/') {
|
2013-08-30 14:02:56 +03:00
|
|
|
close = true;
|
|
|
|
result = false;
|
|
|
|
} else {
|
|
|
|
dir = dir.substr(0,p+1);
|
|
|
|
selected = 0;
|
|
|
|
firstElement = 0;
|
2014-08-12 10:14:31 +03:00
|
|
|
prepare(fl);
|
2013-08-30 14:02:56 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case InputManager::ACCEPT:
|
|
|
|
if (fl.isFile(selected)) {
|
|
|
|
file = fl[selected];
|
|
|
|
close = true;
|
2010-02-04 13:33:47 +02:00
|
|
|
} else {
|
2013-09-07 23:47:18 +03:00
|
|
|
dir = dir+fl[selected];
|
2013-09-06 02:23:27 +03:00
|
|
|
char *buf = realpath(dir.c_str(), NULL);
|
2013-09-07 23:47:18 +03:00
|
|
|
dir = (string) buf + '/';
|
2013-09-06 02:23:27 +03:00
|
|
|
free(buf);
|
|
|
|
|
2010-02-04 13:33:47 +02:00
|
|
|
selected = 0;
|
|
|
|
firstElement = 0;
|
2014-08-12 10:14:31 +03:00
|
|
|
prepare(fl);
|
2010-02-04 13:33:47 +02:00
|
|
|
}
|
2013-08-30 14:02:56 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2010-02-04 13:33:47 +02:00
|
|
|
}
|
|
|
|
}
|
2013-08-30 14:02:56 +03:00
|
|
|
|
2010-02-04 13:33:47 +02:00
|
|
|
return result ? (int)selected : -1;
|
|
|
|
}
|
|
|
|
|
2014-08-12 10:14:31 +03:00
|
|
|
void Selector::prepare(FileLister& fl) {
|
2014-08-13 03:24:18 +03:00
|
|
|
fl.browse(dir);
|
2010-02-04 13:33:47 +02:00
|
|
|
|
2014-08-12 09:06:15 +03:00
|
|
|
screendir = dir;
|
2013-08-16 10:16:04 +03:00
|
|
|
if (!screendir.empty() && screendir[screendir.length() - 1] != '/') {
|
|
|
|
screendir += "/";
|
|
|
|
}
|
2014-07-14 00:01:20 +03:00
|
|
|
screendir += "previews/";
|
2010-02-04 13:33:47 +02:00
|
|
|
}
|