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

Archive Download this file



interactive