GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/widgets/label.cpp Lines: 102 126 81.0 %
Date: 2018-11-12 Branches: 43 91 47.3 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2009  Aethyra Development Team
4
 *  Copyright (C) 2011-2018  The ManaPlus Developers
5
 *
6
 *  This file is part of The ManaPlus Client.
7
 *
8
 *  This program is free software; you can redistribute it and/or modify
9
 *  it under the terms of the GNU General Public License as published by
10
 *  the Free Software Foundation; either version 2 of the License, or
11
 *  any later version.
12
 *
13
 *  This program is distributed in the hope that it will be useful,
14
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 *  GNU General Public License for more details.
17
 *
18
 *  You should have received a copy of the GNU General Public License
19
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 */
21
22
/*      _______   __   __   __   ______   __   __   _______   __   __
23
 *     / _____/\ / /\ / /\ / /\ / ____/\ / /\ / /\ / ___  /\ /  |\/ /\
24
 *    / /\____\// / // / // / // /\___\// /_// / // /\_/ / // , |/ / /
25
 *   / / /__   / / // / // / // / /    / ___  / // ___  / // /| ' / /
26
 *  / /_// /\ / /_// / // / // /_/_   / / // / // /\_/ / // / |  / /
27
 * /______/ //______/ //_/ //_____/\ /_/ //_/ //_/ //_/ //_/ /|_/ /
28
 * \______\/ \______\/ \_\/ \_____\/ \_\/ \_\/ \_\/ \_\/ \_\/ \_\/
29
 *
30
 * Copyright (c) 2004 - 2008 Olof Naessén and Per Larsson
31
 *
32
 *
33
 * Per Larsson a.k.a finalman
34
 * Olof Naessén a.k.a jansem/yakslem
35
 *
36
 * Visit: http://guichan.sourceforge.net
37
 *
38
 * License: (BSD)
39
 * Redistribution and use in source and binary forms, with or without
40
 * modification, are permitted provided that the following conditions
41
 * are met:
42
 * 1. Redistributions of source code must retain the above copyright
43
 *    notice, this list of conditions and the following disclaimer.
44
 * 2. Redistributions in binary form must reproduce the above copyright
45
 *    notice, this list of conditions and the following disclaimer in
46
 *    the documentation and/or other materials provided with the
47
 *    distribution.
48
 * 3. Neither the name of Guichan nor the names of its contributors may
49
 *    be used to endorse or promote products derived from this software
50
 *    without specific prior written permission.
51
 *
52
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
53
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
54
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
55
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
56
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
57
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
58
 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
59
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
60
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
61
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
62
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
63
 */
64
65
#include "gui/widgets/label.h"
66
67
#include "gui/gui.h"
68
#include "gui/skin.h"
69
70
#include "gui/fonts/font.h"
71
72
#include "debug.h"
73
74
Skin *Label::mSkin = nullptr;
75
int Label::mInstances = 0;
76
77
126
Label::Label(const Widget2 *const widget) :
78
    Widget(widget),
79
    WidgetListener(),
80
    ToolTipListener(),
81
    mCaption(),
82
    mTextChunk(),
83
    mAlignment(Graphics::LEFT),
84
    mPadding(0),
85

378
    mTextChanged(true)
86
{
87
126
    init();
88
126
}
89
90
485
Label::Label(const Widget2 *const widget,
91
485
             const std::string &caption) :
92
    Widget(widget),
93
    WidgetListener(),
94
    ToolTipListener(),
95
    mCaption(caption),
96
    mTextChunk(),
97
    mAlignment(Graphics::LEFT),
98
    mPadding(0),
99

1455
    mTextChanged(true)
100
{
101
485
    const Font *const font = getFont();
102

485
    setWidth(font->getWidth(caption));
103

485
    setHeight(font->getHeight());
104
485
    init();
105
485
}
106
107
3666
Label::~Label()
108
{
109
611
    if (mWindow != nullptr)
110
607
        mWindow->removeWidgetListener(this);
111
112
611
    if (gui != nullptr)
113
611
        gui->removeDragged(this);
114
115
611
    mInstances --;
116
611
    if (mInstances == 0)
117
    {
118
39
        if (theme != nullptr)
119
39
            theme->unload(mSkin);
120
    }
121
611
    removeMouseListener(this);
122
611
    mTextChunk.deleteImage();
123
1222
}
124
125
611
void Label::init()
126
{
127
611
    addMouseListener(this);
128
611
    mAllowLogic = false;
129
1222
    mForegroundColor = getThemeColor(ThemeColorId::LABEL, 255U);
130
1222
    mForegroundColor2 = getThemeColor(ThemeColorId::LABEL_OUTLINE, 255U);
131
611
    if (mInstances == 0)
132
    {
133
39
        if (theme != nullptr)
134
        {
135

273
            mSkin = theme->load("label.xml",
136
                "",
137
                true,
138
39
                Theme::getThemePath());
139
        }
140
    }
141
611
    mInstances ++;
142
143
611
    if (mSkin != nullptr)
144
1222
        mPadding = mSkin->getPadding();
145
    else
146
        mPadding = 0;
147
1222
    setSelectable(false);
148
611
}
149
150
60
void Label::draw(Graphics *const graphics)
151
{
152
    BLOCK_START("Label::draw")
153
    int textX;
154
60
    const Rect &rect = mDimension;
155
60
    const int textY = rect.height / 2 - getFont()->getHeight() / 2;
156
60
    Font *const font = getFont();
157
158
60
    switch (mAlignment)
159
    {
160
        case Graphics::LEFT:
161
        default:
162
45
            textX = mPadding;
163
45
            break;
164
        case Graphics::CENTER:
165
15
            textX = (rect.width - font->getWidth(mCaption)) / 2;
166
15
            break;
167
        case Graphics::RIGHT:
168
            if (rect.width > mPadding)
169
                textX = rect.width - mPadding - font->getWidth(mCaption);
170
            else
171
                textX = 0;
172
            break;
173
    }
174
175
60
    if (mTextChanged)
176
    {
177
60
        mTextChunk.textFont = font;
178
60
        mTextChunk.deleteImage();
179
120
        mTextChunk.text = mCaption;
180
60
        mTextChunk.color = mForegroundColor;
181
60
        mTextChunk.color2 = mForegroundColor2;
182
60
        font->generate(mTextChunk);
183
60
        mTextChanged = false;
184
    }
185
186
60
    const Image *const image = mTextChunk.img;
187
60
    if (image != nullptr)
188
47
        graphics->drawImage(image, textX, textY);
189
    BLOCK_END("Label::draw")
190
60
}
191
192
void Label::safeDraw(Graphics *const graphics)
193
{
194
    Label::draw(graphics);
195
}
196
197
172
void Label::adjustSize()
198
{
199
172
    const Font *const font = getFont();
200
172
    const int pad2 = 2 * mPadding;
201
172
    setWidth(font->getWidth(mCaption) + pad2);
202
172
    setHeight(font->getHeight() + pad2);
203
172
}
204
205
void Label::setForegroundColor(const Color &color)
206
{
207
    if (mForegroundColor != color || mForegroundColor2 != color)
208
        mTextChanged = true;
209
//    logger->log("Label::setForegroundColor: " + mCaption);
210
    mForegroundColor = color;
211
    mForegroundColor2 = color;
212
}
213
214
68
void Label::setForegroundColorAll(const Color &color1,
215
                                  const Color &color2)
216
{
217

138
    if (mForegroundColor != color1 || mForegroundColor2 != color2)
218
66
        mTextChanged = true;
219
//    logger->log("Label::setForegroundColorAll: " + mCaption);
220
68
    mForegroundColor = color1;
221
68
    mForegroundColor2 = color2;
222
68
}
223
224
9
void Label::resizeTo(const int maxSize, const int minSize)
225
{
226
9
    const Font *const font = getFont();
227
9
    const int pad2 = 2 * mPadding;
228
9
    setHeight(font->getHeight() + pad2);
229
230
9
    if (font->getWidth(mCaption) + pad2 > maxSize)
231
    {
232
        const int dots = font->getWidth("...");
233
        if (dots > maxSize)
234
        {
235
            setWidth(maxSize);
236
            return;
237
        }
238
        const size_t szChars = mCaption.size();
239
        for (size_t f = 1; f < szChars - 1; f ++)
240
        {
241
            const std::string text = mCaption.substr(0, szChars - f);
242
            const int width = font->getWidth(text) + dots + pad2;
243
            if (width <= maxSize)
244
            {
245
                setCaption(text + "...");
246
                setWidth(width);
247
                return;
248
            }
249
        }
250
        setWidth(maxSize);
251
    }
252
    else
253
    {
254
9
        int sz = font->getWidth(mCaption) + pad2;
255
9
        if (sz < minSize)
256
            sz = minSize;
257
9
        setWidth(sz);
258
    }
259
}
260
261
158
void Label::setCaption(const std::string& caption)
262
{
263
316
    if (caption != mCaption)
264
129
        mTextChanged = true;
265
316
    mCaption = caption;
266
158
}
267
268
619
void Label::setParent(Widget *const widget)
269
{
270
619
    if (mWindow != nullptr)
271
611
        mWindow->addWidgetListener(this);
272
1238
    Widget::setParent(widget);
273
619
}
274
275
12
void Label::setWindow(Widget *const widget)
276
{
277

12
    if ((widget == nullptr) && (mWindow != nullptr))
278
    {
279
6
        mWindow->removeWidgetListener(this);
280
6
        mWindow = nullptr;
281
    }
282
    else
283
    {
284
6
        Widget2::setWindow(widget);
285
    }
286
12
}
287
288
1
void Label::widgetHidden(const Event &event A_UNUSED)
289
{
290
1
    mTextChanged = true;
291
1
    mTextChunk.deleteImage();
292
1
}
293
294
127
void Label::finalCleanup()
295
{
296
127
    mSkin = nullptr;
297
127
}