GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/widgets/label.cpp Lines: 102 126 81.0 %
Date: 2020-06-04 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-2019  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
489
Label::Label(const Widget2 *const widget,
91
489
             const std::string &caption) :
92
    Widget(widget),
93
    WidgetListener(),
94
    ToolTipListener(),
95
    mCaption(caption),
96
    mTextChunk(),
97
    mAlignment(Graphics::LEFT),
98
    mPadding(0),
99

1467
    mTextChanged(true)
100
{
101
489
    const Font *const font = getFont();
102

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

489
    setHeight(font->getHeight());
104
489
    init();
105
489
}
106
107
3690
Label::~Label()
108
{
109
615
    if (mWindow != nullptr)
110
611
        mWindow->removeWidgetListener(this);
111
112
615
    if (gui != nullptr)
113
615
        gui->removeDragged(this);
114
115
615
    mInstances --;
116
615
    if (mInstances == 0)
117
    {
118
39
        if (theme != nullptr)
119
39
            theme->unload(mSkin);
120
    }
121
615
    removeMouseListener(this);
122
615
    mTextChunk.deleteImage();
123
1230
}
124
125
615
void Label::init()
126
{
127
615
    addMouseListener(this);
128
615
    mAllowLogic = false;
129
1230
    mForegroundColor = getThemeColor(ThemeColorId::LABEL, 255U);
130
1230
    mForegroundColor2 = getThemeColor(ThemeColorId::LABEL_OUTLINE, 255U);
131
615
    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
615
    mInstances ++;
142
143
615
    if (mSkin != nullptr)
144
1230
        mPadding = mSkin->getPadding();
145
    else
146
        mPadding = 0;
147
1230
    setSelectable(false);
148
615
}
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
    Font *const font = getFont();
156
157
60
    switch (mAlignment)
158
    {
159
        case Graphics::LEFT:
160
        default:
161
45
            textX = mPadding;
162
45
            break;
163
        case Graphics::CENTER:
164
15
            textX = (rect.width - font->getWidth(mCaption)) / 2;
165
15
            break;
166
        case Graphics::RIGHT:
167
            if (rect.width > mPadding)
168
                textX = rect.width - mPadding - font->getWidth(mCaption);
169
            else
170
                textX = 0;
171
            break;
172
    }
173
174
60
    if (mTextChanged)
175
    {
176
60
        mTextChunk.textFont = font;
177
60
        mTextChunk.deleteImage();
178
120
        mTextChunk.text = mCaption;
179
60
        mTextChunk.color = mForegroundColor;
180
60
        mTextChunk.color2 = mForegroundColor2;
181
60
        font->generate(mTextChunk);
182
60
        mTextChanged = false;
183
    }
184
185
60
    const Image *const image = mTextChunk.img;
186
60
    if (image != nullptr)
187
    {
188
47
        const int textY = rect.height / 2 - font->getHeight() / 2;
189
47
        graphics->drawImage(image, textX, textY);
190
    }
191
    BLOCK_END("Label::draw")
192
60
}
193
194
void Label::safeDraw(Graphics *const graphics)
195
{
196
    Label::draw(graphics);
197
}
198
199
172
void Label::adjustSize()
200
{
201
172
    const Font *const font = getFont();
202
172
    const int pad2 = 2 * mPadding;
203
172
    setWidth(font->getWidth(mCaption) + pad2);
204
172
    setHeight(font->getHeight() + pad2);
205
172
}
206
207
void Label::setForegroundColor(const Color &color)
208
{
209
    if (mForegroundColor != color || mForegroundColor2 != color)
210
        mTextChanged = true;
211
//    logger->log("Label::setForegroundColor: " + mCaption);
212
    mForegroundColor = color;
213
    mForegroundColor2 = color;
214
}
215
216
68
void Label::setForegroundColorAll(const Color &color1,
217
                                  const Color &color2)
218
{
219

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

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