Root/src/textdialog.cpp

1/***************************************************************************
2 * Copyright (C) 2006 by Massimiliano Torromeo *
3 * massimiliano.torromeo@gmail.com *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20
21#include "textdialog.h"
22
23using namespace std;
24
25TextDialog::TextDialog(GMenu2X *gmenu2x, const string &title, const string &description, const string &icon, vector<string> *text)
26    : Dialog(gmenu2x)
27{
28    this->text = text;
29    this->title = title;
30    this->description = description;
31    this->icon = icon;
32    preProcess();
33}
34
35void TextDialog::preProcess() {
36    uint i=0;
37    string row;
38
39    while (i<text->size()) {
40        //clean this row
41        row = trim(text->at(i));
42
43        //check if this row is not too long
44        if (gmenu2x->font->getTextWidth(row)>(int)gmenu2x->resX-15) {
45            vector<string> words;
46            split(words, row, " ");
47
48            uint numWords = words.size();
49            //find the maximum number of rows that can be printed on screen
50            while (gmenu2x->font->getTextWidth(row)>(int)gmenu2x->resX-15 && numWords>0) {
51                numWords--;
52                row = "";
53                for (uint x=0; x<numWords; x++)
54                    row += words[x] + " ";
55                row = trim(row);
56            }
57
58            //if numWords==0 then the string must be printed as-is, it cannot be split
59            if (numWords>0) {
60                //replace with the shorter version
61                text->at(i) = row;
62
63                //build the remaining text in another row
64                row = "";
65                for (uint x=numWords; x<words.size(); x++)
66                    row += words[x] + " ";
67                row = trim(row);
68
69                if (!row.empty())
70                    text->insert(text->begin()+i+1, row);
71            }
72        }
73        i++;
74    }
75}
76
77void TextDialog::drawText(vector<string> *text, uint firstRow, uint rowsPerPage) {
78    gmenu2x->s->setClipRect(0,41,gmenu2x->resX-10,gmenu2x->resY-60);
79
80    for (uint i=firstRow; i<firstRow+rowsPerPage && i<text->size(); i++) {
81        int rowY;
82        if (text->at(i)=="----") { //draw a line
83            rowY = 42+(int)((i-firstRow+0.5)*gmenu2x->font->getHeight());
84            gmenu2x->s->hline(5,rowY,gmenu2x->resX-16,255,255,255,130);
85            gmenu2x->s->hline(5,rowY+1,gmenu2x->resX-16,0,0,0,130);
86        } else {
87            rowY = 42+(i-firstRow)*gmenu2x->font->getHeight();
88            gmenu2x->font->write(gmenu2x->s, text->at(i), 5, rowY);
89        }
90    }
91
92    gmenu2x->s->clearClipRect();
93    gmenu2x->drawScrollBar(rowsPerPage,text->size(),firstRow,42,gmenu2x->resY-65);
94}
95
96void TextDialog::exec() {
97    bool close = false;
98
99    Surface bg(gmenu2x->bg);
100
101    //link icon
102    if (!fileExists(icon))
103        drawTitleIcon("icons/ebook.png",true,&bg);
104    else
105        drawTitleIcon(icon,false,&bg);
106    writeTitle(title,&bg);
107    writeSubTitle(description,&bg);
108
109    gmenu2x->drawButton(&bg, "x", gmenu2x->tr["Exit"],
110    gmenu2x->drawButton(&bg, "down", gmenu2x->tr["Scroll"],
111    gmenu2x->drawButton(&bg, "up", "", 5)-10));
112
113    uint firstRow = 0, rowsPerPage = (gmenu2x->resY-60)/gmenu2x->font->getHeight();
114    while (!close) {
115        bg.blit(gmenu2x->s,0,0);
116        drawText(text, firstRow, rowsPerPage);
117        gmenu2x->s->flip();
118
119        switch(gmenu2x->input.waitForPressedButton()) {
120            case UP:
121                if (firstRow > 0) firstRow--;
122                break;
123            case DOWN:
124                if (firstRow + rowsPerPage < text->size()) firstRow++;
125                break;
126            case ALTLEFT:
127                if (firstRow >= rowsPerPage-1) firstRow -= rowsPerPage-1;
128                else firstRow = 0;
129                break;
130            case ALTRIGHT:
131                if (firstRow + rowsPerPage*2 -1 < text->size()) firstRow += rowsPerPage-1;
132                else firstRow = max(0, text->size() - rowsPerPage);
133                break;
134            case SETTINGS:
135            case CLEAR:
136                close = true;
137                break;
138            default:
139                break;
140        }
141
142        /*
143        gmenu2x->input.update();
144        if ( gmenu2x->input[ACTION_UP ] && firstRow>0 ) firstRow--;
145        if ( gmenu2x->input[ACTION_DOWN] && firstRow+rowsPerPage<text->size() ) firstRow++;
146        if ( gmenu2x->input[ACTION_L ] ) {
147            if (firstRow>=rowsPerPage-1)
148                firstRow-= rowsPerPage-1;
149            else
150                firstRow = 0;
151        }
152        if ( gmenu2x->input[ACTION_R ] ) {
153            if (firstRow+rowsPerPage*2-1<text->size())
154                firstRow+= rowsPerPage-1;
155            else
156                firstRow = max(0,text->size()-rowsPerPage);
157        }
158        if ( gmenu2x->input[ACTION_START] || gmenu2x->input[ACTION_X] ) close = true;
159        */
160    }
161}
162

Archive Download this file



interactive