1
0
mirror of git://projects.qi-hardware.com/gmenu2x.git synced 2024-06-30 20:34:11 +03:00

Make Selector robust against an empty file list

This can happen on a directory with 0 file matches when directories are
not being shown (otherwise there is either ".." or a subdir).

Since there is nothing to be selected, the Accept button is disabled
and no cursor is shown.
This commit is contained in:
Maarten ter Huurne 2014-08-16 15:50:16 +02:00
parent 3cce71284a
commit dc39fcc01f

View File

@ -61,7 +61,9 @@ int Selector::exec(int startSelection) {
writeSubTitle(bg, link.getDescription()); writeSubTitle(bg, link.getDescription());
int x = 5; int x = 5;
x = gmenu2x->drawButton(bg, "accept", gmenu2x->tr["Select"], x); if (fl.size() != 0) {
x = gmenu2x->drawButton(bg, "accept", gmenu2x->tr["Select"], x);
}
if (showDirectories) { if (showDirectories) {
x = gmenu2x->drawButton(bg, "left", "", x); x = gmenu2x->drawButton(bg, "left", "", x);
x = gmenu2x->drawButton(bg, "cancel", gmenu2x->tr["Up one folder"], x); x = gmenu2x->drawButton(bg, "cancel", gmenu2x->tr["Up one folder"], x);
@ -96,47 +98,52 @@ int Selector::exec(int startSelection) {
bg.blit(s, 0, 0); bg.blit(s, 0, 0);
if (selected >= firstElement + nb_elements) if (fl.size() == 0) {
firstElement = selected - nb_elements + 1; gmenu2x->font->write(s, "(no items)", 4, top + lineHeight / 2,
if (selected < firstElement) Font::HAlignLeft, Font::VAlignMiddle);
firstElement = selected; } else {
if (selected >= firstElement + nb_elements)
firstElement = selected - nb_elements + 1;
if (selected < firstElement)
firstElement = selected;
//Screenshot //Screenshot
if (fl.isFile(selected)) { if (fl.isFile(selected)) {
string path = screendir + trimExtension(fl[selected]) + ".png"; string path = screendir + trimExtension(fl[selected]) + ".png";
auto screenshot = OffscreenSurface::loadImage(path, false); auto screenshot = OffscreenSurface::loadImage(path, false);
if (screenshot) { if (screenshot) {
screenshot->blitRight(s, 320, 0, 320, 240, 128u); screenshot->blitRight(s, 320, 0, 320, 240, 128u);
}
}
//Selection
int iY = top + (selected - firstElement) * lineHeight;
if (selected<fl.size())
s.box(1, iY, 309, lineHeight, gmenu2x->skinConfColors[COLOR_SELECTION_BG]);
//Files & Dirs
s.setClipRect(0, top, 311, height);
for (unsigned int i = firstElement;
i < fl.size() && i < firstElement + nb_elements; i++) {
iY = top + (i - firstElement) * lineHeight;
x = 4;
if (fl.isDirectory(i)) {
if (folderIcon) {
folderIcon->blit(s,
x, iY + (lineHeight - folderIcon->height()) / 2);
x += folderIcon->width() + 2;
} }
gmenu2x->font->write(s, fl[i],
x, iY + lineHeight / 2,
Font::HAlignLeft, Font::VAlignMiddle);
} else {
gmenu2x->font->write(s, trimExtension(fl[i]),
x, iY + lineHeight / 2,
Font::HAlignLeft, Font::VAlignMiddle);
} }
//Selection
int iY = top + (selected - firstElement) * lineHeight;
if (selected<fl.size())
s.box(1, iY, 309, lineHeight, gmenu2x->skinConfColors[COLOR_SELECTION_BG]);
//Files & Dirs
s.setClipRect(0, top, 311, height);
for (unsigned int i = firstElement;
i < fl.size() && i < firstElement + nb_elements; i++) {
iY = top + (i - firstElement) * lineHeight;
x = 4;
if (fl.isDirectory(i)) {
if (folderIcon) {
folderIcon->blit(s,
x, iY + (lineHeight - folderIcon->height()) / 2);
x += folderIcon->width() + 2;
}
gmenu2x->font->write(s, fl[i],
x, iY + lineHeight / 2,
Font::HAlignLeft, Font::VAlignMiddle);
} else {
gmenu2x->font->write(s, trimExtension(fl[i]),
x, iY + lineHeight / 2,
Font::HAlignLeft, Font::VAlignMiddle);
}
}
s.clearClipRect();
} }
s.clearClipRect();
gmenu2x->drawScrollBar(nb_elements, fl.size(), firstElement); gmenu2x->drawScrollBar(nb_elements, fl.size(), firstElement);
s.flip(); s.flip();
@ -194,18 +201,20 @@ int Selector::exec(int startSelection) {
break; break;
case InputManager::ACCEPT: case InputManager::ACCEPT:
if (fl.isFile(selected)) { if (fl.size() != 0) {
file = fl[selected]; if (fl.isFile(selected)) {
close = true; file = fl[selected];
} else { close = true;
dir = dir+fl[selected]; } else {
char *buf = realpath(dir.c_str(), NULL); dir = dir+fl[selected];
dir = (string) buf + '/'; char *buf = realpath(dir.c_str(), NULL);
free(buf); dir = (string) buf + '/';
free(buf);
selected = 0; selected = 0;
firstElement = 0; firstElement = 0;
prepare(fl); prepare(fl);
}
} }
break; break;