Root/src/textmanualdialog.cpp

Source at commit 7b10f9448bb9965e94f9a37a59f0999e9c51e68f created 5 years 4 months ago.
By Maarten ter Huurne, Suppress Clang analyzer warnings about dead assignments
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, const string &text)
33    : TextDialog(gmenu2x, title, "", icon, text)
34{
35    //split the text in multiple pages
36    for (uint i=0; i<this->text.size(); i++) {
37        string line = trim(this->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(this->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 blank lines at the start and end of each page
58    for (auto& page : pages) {
59        //start lines
60        auto it = page.text.begin();
61        while (it != page.text.end() &&
62                it->find_first_not_of(" \t\r") == string::npos) ++it;
63        page.text.erase(page.text.begin(), it);
64        //end lines
65        it = page.text.end();
66        while (it != page.text.begin() &&
67                (it - 1)->find_first_not_of(" \t\r") == string::npos) --it;
68        page.text.erase(it, page.text.end());
69    }
70}
71
72void TextManualDialog::exec() {
73    OffscreenSurface bg(*gmenu2x.bg);
74
75    //link icon
76    if (!fileExists(icon))
77        drawTitleIcon(bg, "icons/ebook.png", true);
78    else
79        drawTitleIcon(bg, icon, false);
80    writeTitle(bg, title+(description.empty() ? "" : ": "+description));
81
82    int x = 5;
83    x = gmenu2x.drawButton(bg, "up", "", x);
84    x = gmenu2x.drawButton(bg, "down", gmenu2x.tr["Scroll"], x);
85    x = gmenu2x.drawButton(bg, "left", "", x);
86    x = gmenu2x.drawButton(bg, "right", gmenu2x.tr["Change page"], x);
87    x = gmenu2x.drawButton(bg, "cancel", "", x);
88    x = gmenu2x.drawButton(bg, "start", gmenu2x.tr["Exit"], x);
89    (void)x;
90
91    bg.convertToDisplayFormat();
92
93    stringstream ss;
94    ss << pages.size();
95    string spagecount;
96    ss >> spagecount;
97    string pageStatus;
98
99    const int fontHeight = gmenu2x.font->getLineSpacing();
100    unsigned int contentY, contentHeight;
101    tie(contentY, contentHeight) = gmenu2x.getContentArea();
102    const unsigned rowsPerPage = max(contentHeight / fontHeight, 1u);
103    contentY += (contentHeight % fontHeight) / 2;
104
105    unsigned page = 0, firstRow = 0;
106    bool close = false;
107
108    while (!close) {
109        OutputSurface& s = *gmenu2x.s;
110
111        bg.blit(s,0,0);
112        writeSubTitle(s, pages[page].title);
113        drawText(pages[page].text, contentY, firstRow, rowsPerPage);
114
115        ss.clear();
116        ss << page+1;
117        ss >> pageStatus;
118        pageStatus = gmenu2x.tr["Page"]+": "+pageStatus+"/"+spagecount;
119        gmenu2x.font->write(s, pageStatus, 310, 230, Font::HAlignRight, Font::VAlignMiddle);
120
121        s.flip();
122
123        const unsigned maxFirstRow = pages[page].text.size() < rowsPerPage
124                ? 0 : pages[page].text.size() - rowsPerPage;
125
126        switch(gmenu2x.input.waitForPressedButton()) {
127            case InputManager::UP:
128                if (firstRow > 0) firstRow--;
129                break;
130            case InputManager::DOWN:
131                if (firstRow < maxFirstRow) firstRow++;
132                break;
133            case InputManager::LEFT:
134                if (page > 0) {
135                    page--;
136                    firstRow = 0;
137                }
138                break;
139            case InputManager::RIGHT:
140                if (page < pages.size() -1) {
141                    page++;
142                    firstRow = 0;
143                }
144                break;
145            case InputManager::ALTLEFT:
146                if (firstRow >= rowsPerPage - 1) firstRow -= rowsPerPage - 1;
147                else firstRow = 0;
148                break;
149            case InputManager::ALTRIGHT:
150                firstRow = min(firstRow + rowsPerPage - 1, maxFirstRow);
151                break;
152            case InputManager::CANCEL:
153            case InputManager::SETTINGS:
154                close = true;
155                break;
156            default:
157                break;
158        }
159    }
160}
161

Archive Download this file



interactive