2010-02-04 13:33:47 +02:00
|
|
|
/***************************************************************************
|
2011-10-23 18:00:23 +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. *
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#include "textdialog.h"
|
|
|
|
|
2011-10-23 17:13:02 +03:00
|
|
|
#include "gmenu2x.h"
|
2011-10-23 18:27:29 +03:00
|
|
|
#include "utilities.h"
|
2011-10-23 17:13:02 +03:00
|
|
|
|
2010-02-04 13:33:47 +02:00
|
|
|
using namespace std;
|
|
|
|
|
2010-05-05 15:35:52 +03:00
|
|
|
TextDialog::TextDialog(GMenu2X *gmenu2x, const string &title, const string &description, const string &icon, vector<string> *text)
|
|
|
|
: Dialog(gmenu2x)
|
|
|
|
{
|
2010-02-04 13:33:47 +02:00
|
|
|
this->text = text;
|
|
|
|
this->title = title;
|
|
|
|
this->description = description;
|
|
|
|
this->icon = icon;
|
|
|
|
preProcess();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextDialog::preProcess() {
|
2011-10-23 17:13:02 +03:00
|
|
|
unsigned i = 0;
|
2010-02-04 13:33:47 +02:00
|
|
|
|
2014-07-19 03:33:55 +03:00
|
|
|
while (i < text->size()) {
|
|
|
|
/* Clean the end of the string, allowing lines that are indented at
|
|
|
|
* the start to stay as such. */
|
|
|
|
string line = rtrim(text->at(i));
|
2010-02-04 13:33:47 +02:00
|
|
|
|
2014-07-19 03:33:55 +03:00
|
|
|
if (gmenu2x->font->getTextWidth(line) > (int) gmenu2x->resX - 15) {
|
|
|
|
/* At least one full character must fit, in order to advance. */
|
|
|
|
size_t fits = 1;
|
|
|
|
while (fits < line.length() && !isUTF8Starter(line[fits])) {
|
|
|
|
fits++;
|
2010-02-04 13:33:47 +02:00
|
|
|
}
|
2014-07-19 03:33:55 +03:00
|
|
|
size_t doesntFit = fits;
|
2010-02-04 13:33:47 +02:00
|
|
|
|
2014-07-19 03:33:55 +03:00
|
|
|
/* This preprocessing finds an upper bound on the number of
|
|
|
|
* bytes of full characters that fit on the screen, 2^n, in
|
|
|
|
* n steps. */
|
|
|
|
do {
|
|
|
|
fits = doesntFit; /* what didn't fit has been determined to fit by a previous iteration */
|
|
|
|
doesntFit = min(2 * fits, line.length());
|
|
|
|
while (doesntFit < line.length() && !isUTF8Starter(line[doesntFit])) {
|
|
|
|
doesntFit++;
|
|
|
|
}
|
|
|
|
} while (doesntFit <= line.length()
|
|
|
|
&& gmenu2x->font->getTextWidth(line.substr(0, doesntFit)) <= (int) gmenu2x->resX - 15);
|
2010-02-04 13:33:47 +02:00
|
|
|
|
2014-07-19 03:33:55 +03:00
|
|
|
/* End this loop when N characters fit but N + 1 don't. */
|
|
|
|
while (fits + 1 < doesntFit) {
|
|
|
|
size_t guess = fits + (doesntFit - fits) / 2;
|
|
|
|
if (!isUTF8Starter(line[guess]))
|
|
|
|
{
|
|
|
|
size_t oldGuess = guess;
|
|
|
|
/* Adjust the guess to the nearest UTF-8 starter that is
|
|
|
|
* not 'fits' or 'doesntFit'. */
|
|
|
|
for (size_t offset = 1; offset < (doesntFit - fits) / 2 - 1; offset++) {
|
|
|
|
if (isUTF8Starter(line[guess - offset])) {
|
|
|
|
guess -= offset;
|
|
|
|
break;
|
|
|
|
} else if (isUTF8Starter(line[guess + offset])) {
|
|
|
|
guess += offset;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* If there's no such character, exit early. */
|
|
|
|
if (guess == oldGuess) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (gmenu2x->font->getTextWidth(line.substr(0, guess)) <= (int) gmenu2x->resX - 15) {
|
|
|
|
fits = guess;
|
|
|
|
} else {
|
|
|
|
doesntFit = guess;
|
|
|
|
}
|
|
|
|
}
|
2010-02-04 13:33:47 +02:00
|
|
|
|
2014-07-19 03:33:55 +03:00
|
|
|
/* The line shall be split at the last space-separated word that
|
|
|
|
* fully fits, or otherwise at the last character that fits. */
|
|
|
|
size_t lastSpace = line.find_last_of(" \t\r", fits);
|
|
|
|
if (lastSpace != string::npos) {
|
|
|
|
fits = lastSpace;
|
2010-02-04 13:33:47 +02:00
|
|
|
}
|
2014-07-19 03:33:55 +03:00
|
|
|
|
|
|
|
/* Insert the rest in a new slot after this line.
|
|
|
|
* TODO (Nebuleon) Don't use a vector for this, because all later
|
|
|
|
* elements are moved, which is inefficient. */
|
|
|
|
text->insert(text->begin() + i + 1, ltrim(line.substr(fits)));
|
|
|
|
line = rtrim(line.substr(0, fits));
|
2010-02-04 13:33:47 +02:00
|
|
|
}
|
2014-07-19 03:33:55 +03:00
|
|
|
|
|
|
|
/* Put the trimmed whole line or the smaller split of the split line
|
|
|
|
* back into the same slot */
|
|
|
|
text->at(i) = line;
|
|
|
|
|
2010-02-04 13:33:47 +02:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-14 14:25:07 +03:00
|
|
|
void TextDialog::drawText(vector<string> *text, unsigned int y,
|
|
|
|
unsigned int firstRow, unsigned int rowsPerPage)
|
|
|
|
{
|
2014-06-18 23:07:09 +03:00
|
|
|
const int fontHeight = gmenu2x->font->getLineSpacing();
|
2013-08-14 14:25:07 +03:00
|
|
|
|
|
|
|
for (unsigned i = firstRow; i < firstRow + rowsPerPage && i < text->size(); i++) {
|
|
|
|
const string &line = text->at(i);
|
|
|
|
int rowY = y + (i - firstRow) * fontHeight;
|
|
|
|
if (line == "----") { // horizontal ruler
|
|
|
|
rowY += fontHeight / 2;
|
2014-07-17 18:58:48 +03:00
|
|
|
gmenu2x->s->box(5, rowY, gmenu2x->resX - 16, 1, 255, 255, 255, 130);
|
|
|
|
gmenu2x->s->box(5, rowY+1, gmenu2x->resX - 16, 1, 0, 0, 0, 130);
|
2010-02-04 13:33:47 +02:00
|
|
|
} else {
|
2013-08-14 14:25:07 +03:00
|
|
|
gmenu2x->font->write(gmenu2x->s, line, 5, rowY);
|
2010-02-04 13:33:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-14 11:49:09 +03:00
|
|
|
gmenu2x->drawScrollBar(rowsPerPage, text->size(), firstRow);
|
2010-02-04 13:33:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void TextDialog::exec() {
|
|
|
|
bool close = false;
|
|
|
|
|
|
|
|
Surface bg(gmenu2x->bg);
|
|
|
|
|
|
|
|
//link icon
|
|
|
|
if (!fileExists(icon))
|
2010-05-05 15:35:52 +03:00
|
|
|
drawTitleIcon("icons/ebook.png",true,&bg);
|
2010-02-04 13:33:47 +02:00
|
|
|
else
|
2010-05-05 15:35:52 +03:00
|
|
|
drawTitleIcon(icon,false,&bg);
|
|
|
|
writeTitle(title,&bg);
|
|
|
|
writeSubTitle(description,&bg);
|
2010-02-04 13:33:47 +02:00
|
|
|
|
2011-09-18 04:05:25 +03:00
|
|
|
gmenu2x->drawButton(&bg, "start", gmenu2x->tr["Exit"],
|
2011-09-18 04:40:33 +03:00
|
|
|
gmenu2x->drawButton(&bg, "cancel", "",
|
2010-02-04 13:33:47 +02:00
|
|
|
gmenu2x->drawButton(&bg, "down", gmenu2x->tr["Scroll"],
|
2011-09-18 04:05:25 +03:00
|
|
|
gmenu2x->drawButton(&bg, "up", "", 5)-10))-10);
|
2010-02-04 13:33:47 +02:00
|
|
|
|
2011-06-03 12:46:36 +03:00
|
|
|
bg.convertToDisplayFormat();
|
|
|
|
|
2014-06-18 23:07:09 +03:00
|
|
|
const int fontHeight = gmenu2x->font->getLineSpacing();
|
2013-08-14 14:25:07 +03:00
|
|
|
unsigned int contentY, contentHeight;
|
|
|
|
tie(contentY, contentHeight) = gmenu2x->getContentArea();
|
|
|
|
const unsigned rowsPerPage = contentHeight / fontHeight;
|
|
|
|
contentY += (contentHeight % fontHeight) / 2;
|
|
|
|
|
2011-10-23 17:13:02 +03:00
|
|
|
unsigned firstRow = 0;
|
2010-02-04 13:33:47 +02:00
|
|
|
while (!close) {
|
2011-10-23 09:57:52 +03:00
|
|
|
bg.blit(gmenu2x->s, 0, 0);
|
2013-08-14 14:25:07 +03:00
|
|
|
drawText(text, contentY, firstRow, rowsPerPage);
|
2010-02-04 13:33:47 +02:00
|
|
|
gmenu2x->s->flip();
|
|
|
|
|
2011-10-23 09:57:52 +03:00
|
|
|
switch(gmenu2x->input.waitForPressedButton()) {
|
|
|
|
case InputManager::UP:
|
|
|
|
if (firstRow > 0) firstRow--;
|
|
|
|
break;
|
|
|
|
case InputManager::DOWN:
|
|
|
|
if (firstRow + rowsPerPage < text->size()) firstRow++;
|
|
|
|
break;
|
|
|
|
case InputManager::ALTLEFT:
|
|
|
|
if (firstRow >= rowsPerPage-1) firstRow -= rowsPerPage-1;
|
|
|
|
else firstRow = 0;
|
|
|
|
break;
|
|
|
|
case InputManager::ALTRIGHT:
|
|
|
|
if (firstRow + rowsPerPage*2 -1 < text->size()) {
|
|
|
|
firstRow += rowsPerPage-1;
|
|
|
|
} else {
|
2014-04-17 21:52:41 +03:00
|
|
|
firstRow = text->size() < rowsPerPage ?
|
|
|
|
0 : text->size() - rowsPerPage;
|
2011-10-23 09:57:52 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case InputManager::SETTINGS:
|
|
|
|
case InputManager::CANCEL:
|
|
|
|
close = true;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2010-02-04 13:33:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|