summaryrefslogtreecommitdiff
path: root/src/gui/browserbox.cpp
blob: 504fb98773ba791f5b36bce284090fe1bf7322bf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/*
 *  The Mana World
 *  Copyright 2004 The Mana World Development Team
 *
 *  This file is part of The Mana World.
 *
 *  The Mana World is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  any later version.
 *
 *  The Mana World is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with The Mana World; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *  $Id$
 */

#include <guichan.hpp>
#include "../graphics.h"
#include "../main.h"
#include "../resources/resourcemanager.h"
#include "browserbox.h"
#include "gui.h"

int BrowserBox::instances = 0;
gcn::ImageFont* BrowserBox::browserFont;

BrowserBox::BrowserBox(unsigned int mode):
    gcn::Widget()
{
    mMode = mode;
    setOpaque(true);
    setHighlightMode(BOTH);
    mUseLinksAndUserColors = true;
    mSelectedLink = -1;
    setFocusable(true);
    addMouseListener(this);

    if (instances == 0)
    {
        if (useOpenGL) {
            browserFont = new gcn::ImageFont(
                    ResourceManager::getInstance()->getRealPath(
                        "graphics/gui/browserfont.png"),
                    " abcdefghijklmnopqrstuvwxyz"
                    "ABCDEFGHIJKLMNOPQRSTUVWXYZ01234567"
                    "89:@!\"$%&/=?^+*#[]{}()<>_;'.,\\|-~`");
        }
        else {
            browserFont = gui->getFont();
        }
    }

    instances++;
}

BrowserBox::~BrowserBox()
{
    instances--;

    if (instances == 0)
    {
#ifdef USE_OPENGL
        // Clean up static resource font
        delete browserFont;
#endif
    }
}

void BrowserBox::setLinkHandler(LinkHandler* linkHandler)
{
    mLinkHandler = linkHandler;
}

void BrowserBox::setOpaque(bool opaque)
{
    mOpaque = opaque;
}

void BrowserBox::setHighlightMode(unsigned int highMode)
{
    mHighMode = highMode;
}

void BrowserBox::disableLinksAndUserColors()
{
    mUseLinksAndUserColors = false;
}

void BrowserBox::addRow(const std::string& row)
{
    std::string tmp = row;
    std::string newRow;
    BROWSER_LINK bLink;
    int idx1, idx2, idx3;

    // Use links and user defined colors
    if (mUseLinksAndUserColors)
    {
        // Check for links in format "@@link|Caption@@"
        idx1 = tmp.find("@@");
        while (idx1 >= 0)
        {
            idx2 = tmp.find("|", idx1);
            idx3 = tmp.find("@@", idx2);
            bLink.link = tmp.substr(idx1 + 2, idx2 - (idx1 + 2));
            bLink.caption = tmp.substr(idx2 + 1, idx3 - (idx2 + 1));
            bLink.y1 = mTextRows.size() * browserFont->getHeight();
            bLink.y2 = bLink.y1 + browserFont->getHeight();

            newRow += tmp.substr(0, idx1);

            std::string tmp2 = newRow;
            idx1 = tmp2.find("##");
            while (idx1 >= 0)
            {
                tmp2.erase(idx1, 3);
                idx1 = tmp2.find("##");
            }
            bLink.x1 = browserFont->getWidth(tmp2) - 1;
            bLink.x2 = bLink.x1 + browserFont->getWidth(bLink.caption) + 1;

            mLinks.push_back(bLink);

            newRow += "##L" + bLink.caption;

            tmp.erase(0, idx3 + 2);
            if(tmp != "")
            {
                newRow += "##P";
            }
            idx1 = tmp.find("@@");
        }

        newRow += tmp;
    }

    // Don't use links and user defined colors
    else
    {
        newRow = row;
    }

    newRow == (newRow == "" ? " " : newRow);
    mTextRows.push_back(newRow);

    // Auto size mode
    if (mMode == AUTO_SIZE)
    {
        std::string plain = newRow;
        for (idx1 = plain.find("##"); idx1 >= 0; idx1 = plain.find("##"))
            plain.erase(idx1, 3);

        // Adjust the BrowserBox size
        int w = browserFont->getWidth(plain);
        if (w > getWidth())
            setWidth(w);
    }
    setHeight(browserFont->getHeight() * mTextRows.size());
}

void BrowserBox::clearRows()
{
    mTextRows.clear();
    mLinks.clear();
    setWidth(0);
    mSelectedLink = -1;
}

void BrowserBox::mousePress(int mx, int my, int button)
{
    if ((button == gcn::MouseInput::LEFT) && mLinkHandler)
    {
        for (unsigned int i = 0; i < mLinks.size(); i++)
        {
            if ((mx >= mLinks[i].x1) && (mx < mLinks[i].x2) &&
                    (my > mLinks[i].y1) && (my < mLinks[i].y2))
            {
                mLinkHandler->handleLink(mLinks[i].link);
                return;
            }
        }
    }
}

void BrowserBox::mouseMotion(int mx, int my)
{
    for (unsigned int i = 0; i < mLinks.size(); i++)
    {
        if ((mx >= mLinks[i].x1) && (mx < mLinks[i].x2) &&
                (my > mLinks[i].y1) && (my < mLinks[i].y2))
        {
            mSelectedLink = (int) i;
            return;
        }
        else
        {
            mSelectedLink = -1;
        }
    }
}

void BrowserBox::draw(gcn::Graphics* graphics)
{
    if (mOpaque)
    {
        graphics->setColor(gcn::Color(BGCOLOR));
        graphics->fillRectangle(gcn::Rectangle(0, 0, getWidth(), getHeight()));
    }

    if (mSelectedLink >= 0)
    {
        if ((mHighMode == BACKGROUND) || (mHighMode == BOTH))
        {
            graphics->setColor(gcn::Color(HIGHLIGHT));
            graphics->fillRectangle(gcn::Rectangle(
                        mLinks[mSelectedLink].x1,
                        mLinks[mSelectedLink].y1,
                        mLinks[mSelectedLink].x2 - mLinks[mSelectedLink].x1,
                        mLinks[mSelectedLink].y2 - mLinks[mSelectedLink].y1
                        ));
        }

        if ((mHighMode == UNDERLINE) || (mHighMode == BOTH))
        {
            graphics->setColor(gcn::Color(LINK));
            graphics->drawLine(
                        mLinks[mSelectedLink].x1,
                        mLinks[mSelectedLink].y2,
                        mLinks[mSelectedLink].x2,
                        mLinks[mSelectedLink].y2);
        }
    }

    unsigned int i, j;
    int x = 0, y = 0;
    int wrappedLines = 0;
    for (i = 0; i < mTextRows.size(); i++)
    {
        int selColor = BLACK;
        int prevColor = selColor;
        std::string row = mTextRows[i];
        x = 0;

        for (j = 0; j < row.size(); j++)
        {
            if (mUseLinksAndUserColors || (!mUseLinksAndUserColors && (j == 0)))
            {
                // Check for color change in format "##x", x = [L,P,0..9]
                if ((row.at(j) == '#') && (row.at(j + 1) == '#'))
                {
                    switch (row.at(j + 2))
                    {
                        case 'L': // Link color
                            prevColor = selColor;
                            selColor = LINK;
                            break;
                        case 'P': // Previous color
                            selColor = prevColor;
                            break;
                        case '1':
                            prevColor = selColor;
                            selColor = RED;
                            break;
                        case '2':
                            prevColor = selColor;
                            selColor = GREEN;
                            break;
                        case '3':
                            prevColor = selColor;
                            selColor = BLUE;
                            break;
                        case '4':
                            prevColor = selColor;
                            selColor = ORANGE;
                            break;
                        case '5':
                            prevColor = selColor;
                            selColor = YELLOW;
                            break;
                        case '6':
                            prevColor = selColor;
                            selColor = PINK;
                            break;
                        case '7':
                            prevColor = selColor;
                            selColor = PURPLE;
                            break;
                        case '8':
                            prevColor = selColor;
                            selColor = GRAY;
                            break;
                        case '9':
                            prevColor = selColor;
                            selColor = BROWN;
                            break;
                        case '0':
                        default:
                            prevColor = selColor;
                            selColor = BLACK;
                    }
                    j += 3;

                    if (j == row.size())
                    {
                        break;
                    }
                }
                graphics->setColor(gcn::Color(selColor));
            }

            // Check for line separators in format "---"
            if ((j <= 3) && (row.at(j) == '-') && (row.at(j + 1) == '-') &&
                    (row.at(j + 2) == '-'))
            {
                for (x = 0; x < getWidth(); x++)
                {
                    browserFont->drawGlyph(graphics, '-', x, y);
                    x += browserFont->getWidth('-') - 2;
                }
                break;
            }
            // Draw each char
            else
            {
                browserFont->drawGlyph(graphics, row.at(j), x, y);
                x += browserFont->getWidth(row.at(j));

                // Auto wrap mode
                if (mMode == AUTO_WRAP)
                {
                    unsigned int nextChar = j + 1;
                    char hyphen = '~';
                    int hyphenWidth =  browserFont->getWidth(hyphen);

                    // Wraping between words (at blank spaces)
                    if ((nextChar < row.size()) && (row.at(nextChar) == ' '))
                    {
                        int nextSpacePos = row.find(" ", (nextChar + 1));
                        if (nextSpacePos <= 0)
                        {
                            nextSpacePos = row.size() - 1;
                        }
                        int nextWordWidth = browserFont->getWidth(
                                row.substr(nextChar,
                                    (nextSpacePos - nextChar)));

                        if ((x + nextWordWidth + 10) > getWidth())
                        {
                            x = 15; // Ident in new line
                            y += browserFont->getHeight();
                            wrappedLines++;
                            j++;
                        }
                    }

                    // Wrapping looong lines (brutal force)
                    else if ((x + 2 * hyphenWidth) > getWidth())
                    {
                        browserFont->drawGlyph(graphics, hyphen,
                                getWidth() - hyphenWidth, y);
                        x = 15; // Ident in new line
                        y += browserFont->getHeight();
                        wrappedLines++;
                    }
                }
            }
        }
        y += browserFont->getHeight();
        setHeight((mTextRows.size() + wrappedLines) * browserFont->getHeight());
    }
}