1
0
mirror of git://projects.qi-hardware.com/gmenu2x.git synced 2024-11-26 01:50:16 +02:00

Rewrote the manual paging code

Nebuleon spotted a bug in TextManualDialog where the unsigned value
'pages[page].text.size() - rowsPerPage' could wrap around at 0 for
short chapters. I decided to change a bit more than just fixing the
bug though.
This commit is contained in:
Maarten ter Huurne 2014-08-10 10:09:42 +02:00
parent 799ebb9a29
commit 0535f8273b
2 changed files with 18 additions and 16 deletions

View File

@ -23,6 +23,8 @@
#include "gmenu2x.h" #include "gmenu2x.h"
#include "utilities.h" #include "utilities.h"
#include <algorithm>
using namespace std; using namespace std;
TextDialog::TextDialog(GMenu2X *gmenu2x, const string &title, const string &description, const string &icon, const string &text) TextDialog::TextDialog(GMenu2X *gmenu2x, const string &title, const string &description, const string &icon, const string &text)
@ -79,6 +81,8 @@ void TextDialog::exec() {
unsigned int contentY, contentHeight; unsigned int contentY, contentHeight;
tie(contentY, contentHeight) = gmenu2x->getContentArea(); tie(contentY, contentHeight) = gmenu2x->getContentArea();
const unsigned rowsPerPage = contentHeight / fontHeight; const unsigned rowsPerPage = contentHeight / fontHeight;
const unsigned maxFirstRow =
text.size() < rowsPerPage ? 0 : text.size() - rowsPerPage;
contentY += (contentHeight % fontHeight) / 2; contentY += (contentHeight % fontHeight) / 2;
unsigned firstRow = 0; unsigned firstRow = 0;
@ -94,19 +98,14 @@ void TextDialog::exec() {
if (firstRow > 0) firstRow--; if (firstRow > 0) firstRow--;
break; break;
case InputManager::DOWN: case InputManager::DOWN:
if (firstRow + rowsPerPage < text.size()) firstRow++; if (firstRow < maxFirstRow) firstRow++;
break; break;
case InputManager::ALTLEFT: case InputManager::ALTLEFT:
if (firstRow >= rowsPerPage-1) firstRow -= rowsPerPage-1; if (firstRow >= rowsPerPage - 1) firstRow -= rowsPerPage - 1;
else firstRow = 0; else firstRow = 0;
break; break;
case InputManager::ALTRIGHT: case InputManager::ALTRIGHT:
if (firstRow + rowsPerPage*2 -1 < text.size()) { firstRow = min(firstRow + rowsPerPage - 1, maxFirstRow);
firstRow += rowsPerPage-1;
} else {
firstRow = text.size() < rowsPerPage ?
0 : text.size() - rowsPerPage;
}
break; break;
case InputManager::SETTINGS: case InputManager::SETTINGS:
case InputManager::CANCEL: case InputManager::CANCEL:

View File

@ -68,9 +68,6 @@ TextManualDialog::TextManualDialog(GMenu2X *gmenu2x, const string &title, const
} }
void TextManualDialog::exec() { void TextManualDialog::exec() {
bool close = false;
uint page=0;
Surface bg(gmenu2x->bg); Surface bg(gmenu2x->bg);
//link icon //link icon
@ -89,13 +86,17 @@ void TextManualDialog::exec() {
bg.convertToDisplayFormat(); bg.convertToDisplayFormat();
uint firstRow = 0, rowsPerPage = 180/gmenu2x->font->getLineSpacing();
stringstream ss; stringstream ss;
ss << pages.size(); ss << pages.size();
string spagecount; string spagecount;
ss >> spagecount; ss >> spagecount;
string pageStatus; string pageStatus;
const unsigned rowsPerPage = 180 / gmenu2x->font->getLineSpacing();
unsigned page = 0, firstRow = 0;
bool close = false;
while (!close) { while (!close) {
Surface& s = *gmenu2x->s; Surface& s = *gmenu2x->s;
@ -111,12 +112,15 @@ void TextManualDialog::exec() {
s.flip(); s.flip();
const unsigned maxFirstRow = pages[page].text.size() < rowsPerPage
? 0 : pages[page].text.size() - rowsPerPage;
switch(gmenu2x->input.waitForPressedButton()) { switch(gmenu2x->input.waitForPressedButton()) {
case InputManager::UP: case InputManager::UP:
if (firstRow > 0) firstRow--; if (firstRow > 0) firstRow--;
break; break;
case InputManager::DOWN: case InputManager::DOWN:
if (firstRow + rowsPerPage < pages[page].text.size()) firstRow++; if (firstRow < maxFirstRow) firstRow++;
break; break;
case InputManager::LEFT: case InputManager::LEFT:
if (page > 0) { if (page > 0) {
@ -131,12 +135,11 @@ void TextManualDialog::exec() {
} }
break; break;
case InputManager::ALTLEFT: case InputManager::ALTLEFT:
if (firstRow >= rowsPerPage-1) firstRow -= rowsPerPage-1; if (firstRow >= rowsPerPage - 1) firstRow -= rowsPerPage - 1;
else firstRow = 0; else firstRow = 0;
break; break;
case InputManager::ALTRIGHT: case InputManager::ALTRIGHT:
if (firstRow + rowsPerPage*2 -1 < pages[page].text.size()) firstRow += rowsPerPage-1; firstRow = min(firstRow + rowsPerPage - 1, maxFirstRow);
else firstRow = max(0ul, (unsigned long) (pages[page].text.size() - rowsPerPage));
break; break;
case InputManager::CANCEL: case InputManager::CANCEL:
case InputManager::SETTINGS: case InputManager::SETTINGS: