Root/src/textmanualdialog.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 <sstream>
22#include "textmanualdialog.h"
23
24using namespace std;
25
26TextManualDialog::TextManualDialog(GMenu2X *gmenu2x, const string &title, const string &icon, vector<string> *text)
27    : TextDialog(gmenu2x,title,"",icon,text) {
28
29    //split the text in multiple pages
30    for (uint i=0; i<text->size(); i++) {
31        string line = trim(text->at(i));
32        if (line[0]=='[' && line[line.length()-1]==']') {
33            ManualPage mp;
34            mp.title = line.substr(1,line.length()-2);
35            pages.push_back(mp);
36        } else {
37            if (pages.size()==0) {
38                ManualPage mp;
39                mp.title = gmenu2x->tr["Untitled"];
40                pages.push_back(mp);
41            }
42            pages[pages.size()-1].text.push_back(text->at(i));
43        }
44    }
45    if (pages.size()==0) {
46        ManualPage mp;
47        mp.title = gmenu2x->tr["Untitled"];
48        pages.push_back(mp);
49    }
50
51    //delete first and last blank lines from each page
52    for (uint page=0; page<pages.size(); page++) {
53        if (pages[page].text.size() > 0) {
54            //first lines
55            while (trim(pages[page].text[0])=="")
56                pages[page].text.erase(pages[page].text.begin());
57            //last lines
58            while (trim(pages[page].text[pages[page].text.size()-1])=="")
59                pages[page].text.erase(pages[page].text.end());
60        }
61    }
62}
63
64void TextManualDialog::exec() {
65    bool close = false;
66    uint page=0;
67
68    Surface bg(gmenu2x->bg);
69
70    //link icon
71    if (!fileExists(icon))
72        drawTitleIcon("icons/ebook.png",true,&bg);
73    else
74        drawTitleIcon(icon,false,&bg);
75    writeTitle(title+(description.empty() ? "" : ": "+description),&bg);
76
77    gmenu2x->drawButton(&bg, "x", gmenu2x->tr["Exit"],
78    gmenu2x->drawButton(&bg, "right", gmenu2x->tr["Change page"],
79    gmenu2x->drawButton(&bg, "left", "",
80    gmenu2x->drawButton(&bg, "down", gmenu2x->tr["Scroll"],
81    gmenu2x->drawButton(&bg, "up", "", 5)-10))-10));
82
83    uint firstRow = 0, rowsPerPage = 180/gmenu2x->font->getHeight();
84    stringstream ss;
85    ss << pages.size();
86    string spagecount;
87    ss >> spagecount;
88    string pageStatus;
89    
90    while (!close) {
91        bg.blit(gmenu2x->s,0,0);
92        writeSubTitle(pages[page].title);
93        drawText(&pages[page].text, firstRow, rowsPerPage);
94
95        ss.clear();
96        ss << page+1;
97        ss >> pageStatus;
98        pageStatus = gmenu2x->tr["Page"]+": "+pageStatus+"/"+spagecount;
99        gmenu2x->s->write(gmenu2x->font, pageStatus, 310, 230, ASFont::HAlignRight, ASFont::VAlignMiddle);
100
101        gmenu2x->s->flip();
102
103        switch(gmenu2x->input.waitForPressedButton()) {
104            case UP:
105                if (firstRow > 0) firstRow--;
106                break;
107            case DOWN:
108                if (firstRow + rowsPerPage < pages[page].text.size()) firstRow++;
109                break;
110            case LEFT:
111                if (page > 0) {
112                    page--;
113                    firstRow = 0;
114                }
115                break;
116            case RIGHT:
117                if (page < pages.size() -1) {
118                    page++;
119                    firstRow = 0;
120                }
121                break;
122            case ALTLEFT:
123                if (firstRow >= rowsPerPage-1) firstRow -= rowsPerPage-1;
124                else firstRow = 0;
125                break;
126            case ALTRIGHT:
127                if (firstRow + rowsPerPage*2 -1 < pages[page].text.size()) firstRow += rowsPerPage-1;
128                else firstRow = max(0, pages[page].text.size() - rowsPerPage);
129                break;
130            case SETTINGS:
131            case CLEAR:
132                close = true;
133                break;
134            default:
135                break;
136        }
137        
138        /*
139        gmenu2x->input.update();
140        if ( gmenu2x->input[ACTION_UP ] && firstRow>0 ) firstRow--;
141        if ( gmenu2x->input[ACTION_DOWN ] && firstRow+rowsPerPage<pages[page].text.size() ) firstRow++;
142        if ( gmenu2x->input[ACTION_LEFT ] && page>0 ) { page--; firstRow=0; }
143        if ( gmenu2x->input[ACTION_RIGHT] && page<pages.size()-1 ) { page++; firstRow=0; }
144        if ( gmenu2x->input[ACTION_L ] ) {
145            if (firstRow>=rowsPerPage-1)
146                firstRow-= rowsPerPage-1;
147            else
148                firstRow = 0;
149        }
150        if ( gmenu2x->input[ACTION_R ] ) {
151            if (firstRow+rowsPerPage*2-1<pages[page].text.size())
152                firstRow+= rowsPerPage-1;
153            else
154                firstRow = max(0,pages[page].text.size()-rowsPerPage);
155        }
156        if ( gmenu2x->input[ACTION_START] || gmenu2x->input[ACTION_X] ) close = true;
157        */
158    }
159}
160

Archive Download this file



interactive