Root/src/textdialog.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 "textdialog.h"
22
23#include "gmenu2x.h"
24#include "utilities.h"
25
26#include <algorithm>
27
28using namespace std;
29
30TextDialog::TextDialog(GMenu2X& gmenu2x, const string &title, const string &description, const string &icon, const string &text)
31    : Dialog(gmenu2x)
32{
33    split(this->text, gmenu2x.font->wordWrap(text, (int) gmenu2x.resX - 15), "\n");
34    this->title = title;
35    this->description = description;
36    this->icon = icon;
37}
38
39void TextDialog::drawText(const vector<string> &text, unsigned int y,
40        unsigned int firstRow, unsigned int rowsPerPage)
41{
42    Surface& s = *gmenu2x.s;
43    const int fontHeight = gmenu2x.font->getLineSpacing();
44
45    for (unsigned i = firstRow; i < firstRow + rowsPerPage && i < text.size(); i++) {
46        const string &line = text.at(i);
47        int rowY = y + (i - firstRow) * fontHeight;
48        if (line == "----") { // horizontal ruler
49            rowY += fontHeight / 2;
50            s.box(5, rowY, gmenu2x.resX - 16, 1, 255, 255, 255, 130);
51            s.box(5, rowY+1, gmenu2x.resX - 16, 1, 0, 0, 0, 130);
52        } else {
53            gmenu2x.font->write(s, line, 5, rowY);
54        }
55    }
56
57    gmenu2x.drawScrollBar(rowsPerPage, text.size(), firstRow);
58}
59
60void TextDialog::exec() {
61    bool close = false;
62
63    OffscreenSurface bg(*gmenu2x.bg);
64
65    //link icon
66    if (!fileExists(icon))
67        drawTitleIcon(bg, "icons/ebook.png", true);
68    else
69        drawTitleIcon(bg, icon, false);
70    writeTitle(bg, title);
71    writeSubTitle(bg, description);
72
73    int x = 5;
74    x = gmenu2x.drawButton(bg, "up", "", x);
75    x = gmenu2x.drawButton(bg, "down", gmenu2x.tr["Scroll"], x);
76    x = gmenu2x.drawButton(bg, "cancel", "", x);
77    x = gmenu2x.drawButton(bg, "start", gmenu2x.tr["Exit"], x);
78    (void)x;
79
80    bg.convertToDisplayFormat();
81
82    const int fontHeight = gmenu2x.font->getLineSpacing();
83    unsigned int contentY, contentHeight;
84    tie(contentY, contentHeight) = gmenu2x.getContentArea();
85    const unsigned rowsPerPage = max(contentHeight / fontHeight, 1u);
86    const unsigned maxFirstRow =
87            text.size() < rowsPerPage ? 0 : text.size() - rowsPerPage;
88    contentY += (contentHeight % fontHeight) / 2;
89
90    unsigned firstRow = 0;
91    while (!close) {
92        OutputSurface& s = *gmenu2x.s;
93
94        bg.blit(s, 0, 0);
95        drawText(text, contentY, firstRow, rowsPerPage);
96        s.flip();
97
98        switch(gmenu2x.input.waitForPressedButton()) {
99            case InputManager::UP:
100                if (firstRow > 0) firstRow--;
101                break;
102            case InputManager::DOWN:
103                if (firstRow < maxFirstRow) firstRow++;
104                break;
105            case InputManager::ALTLEFT:
106                if (firstRow >= rowsPerPage - 1) firstRow -= rowsPerPage - 1;
107                else firstRow = 0;
108                break;
109            case InputManager::ALTRIGHT:
110                firstRow = min(firstRow + rowsPerPage - 1, maxFirstRow);
111                break;
112            case InputManager::SETTINGS:
113            case InputManager::CANCEL:
114                close = true;
115                break;
116            default:
117                break;
118        }
119    }
120}
121

Archive Download this file



interactive