GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/widgets/label.cpp Lines: 103 128 80.5 %
Date: 2017-11-29 Branches: 38 81 46.9 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2009  Aethyra Development Team
4
 *  Copyright (C) 2011-2017  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
232
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

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

2574
    mTextChanged(true)
100
{
101
858
    const Font *const font = getFont();
102

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

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

608
            mSkin = theme->load("label.xml", "");
135
    }
136
1090
    mInstances ++;
137
138
1090
    if (mSkin != nullptr)
139
2180
        mPadding = mSkin->getPadding();
140
    else
141
        mPadding = 0;
142
2180
    setSelectable(false);
143
1090
}
144
145
122
void Label::draw(Graphics *const graphics)
146
{
147
    BLOCK_START("Label::draw")
148
    int textX;
149
122
    const Rect &rect = mDimension;
150
122
    const int textY = rect.height / 2 - getFont()->getHeight() / 2;
151
122
    Font *const font = getFont();
152
153
122
    switch (mAlignment)
154
    {
155
92
        case Graphics::LEFT:
156
        default:
157
92
            textX = mPadding;
158
92
            break;
159
30
        case Graphics::CENTER:
160
30
            textX = (rect.width - font->getWidth(mCaption)) / 2;
161
30
            break;
162
        case Graphics::RIGHT:
163
            if (rect.width > mPadding)
164
                textX = rect.width - mPadding - font->getWidth(mCaption);
165
            else
166
                textX = 0;
167
            break;
168
    }
169
170
122
    if (mTextChanged)
171
    {
172
122
        mTextChunk.textFont = font;
173
122
        mTextChunk.deleteImage();
174
244
        mTextChunk.text = mCaption;
175
122
        mTextChunk.color = mForegroundColor;
176
122
        mTextChunk.color2 = mForegroundColor2;
177
122
        font->generate(mTextChunk);
178
122
        mTextChanged = false;
179
    }
180
181
122
    const Image *const image = mTextChunk.img;
182
122
    if (image != nullptr)
183
96
        graphics->drawImage(image, textX, textY);
184
    BLOCK_END("Label::draw")
185
122
}
186
187
void Label::safeDraw(Graphics *const graphics)
188
{
189
    Label::draw(graphics);
190
}
191
192
262
void Label::adjustSize()
193
{
194
262
    const Font *const font = getFont();
195
262
    const int pad2 = 2 * mPadding;
196
262
    setWidth(font->getWidth(mCaption) + pad2);
197
262
    setHeight(font->getHeight() + pad2);
198
262
}
199
200
void Label::setForegroundColor(const Color &color)
201
{
202
    if (mForegroundColor != color || mForegroundColor2 != color)
203
        mTextChanged = true;
204
//    logger->log("Label::setForegroundColor: " + mCaption);
205
    mForegroundColor = color;
206
    mForegroundColor2 = color;
207
}
208
209
136
void Label::setForegroundColorAll(const Color &color1,
210
                                  const Color &color2)
211
{
212
140
    if (mForegroundColor != color1 || mForegroundColor2 != color2)
213
132
        mTextChanged = true;
214
//    logger->log("Label::setForegroundColorAll: " + mCaption);
215
136
    mForegroundColor = color1;
216
136
    mForegroundColor2 = color2;
217
136
}
218
219
18
void Label::resizeTo(const int maxSize, const int minSize)
220
{
221
18
    const Font *const font = getFont();
222
18
    const int pad2 = 2 * mPadding;
223
18
    setHeight(font->getHeight() + pad2);
224
225
18
    if (font->getWidth(mCaption) + pad2 > maxSize)
226
    {
227
        const int dots = font->getWidth("...");
228
        if (dots > maxSize)
229
        {
230
            setWidth(maxSize);
231
            return;
232
        }
233
        const size_t szChars = mCaption.size();
234
        for (size_t f = 1; f < szChars - 1; f ++)
235
        {
236
            const std::string text = mCaption.substr(0, szChars - f);
237
            const int width = font->getWidth(text) + dots + pad2;
238
            if (width <= maxSize)
239
            {
240
                setCaption(text + "...");
241
                setWidth(width);
242
                return;
243
            }
244
        }
245
        setWidth(maxSize);
246
    }
247
    else
248
    {
249
18
        int sz = font->getWidth(mCaption) + pad2;
250
18
        if (sz < minSize)
251
            sz = minSize;
252
18
        setWidth(sz);
253
    }
254
}
255
256
274
void Label::setCaption(const std::string& caption)
257
{
258
274
    if (caption != mCaption)
259
224
        mTextChanged = true;
260
548
    mCaption = caption;
261
274
}
262
263
1106
void Label::setParent(Widget *const widget)
264
{
265
1106
    if (mWindow != nullptr)
266
1090
        mWindow->addWidgetListener(this);
267
2212
    Widget::setParent(widget);
268
1106
}
269
270
24
void Label::setWindow(Widget *const widget)
271
{
272

24
    if ((widget == nullptr) && (mWindow != nullptr))
273
    {
274
12
        mWindow->removeWidgetListener(this);
275
12
        mWindow = nullptr;
276
    }
277
    else
278
    {
279
12
        Widget2::setWindow(widget);
280
    }
281
24
}
282
283
1
void Label::widgetHidden(const Event &event A_UNUSED)
284
{
285
1
    mTextChanged = true;
286
1
    mTextChunk.deleteImage();
287
1
}
288
289
248
void Label::finalCleanup()
290
{
291
248
    mSkin = nullptr;
292
248
}