GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/widgets/checkbox.cpp Lines: 88 126 69.8 %
Date: 2021-03-17 Branches: 47 100 47.0 %

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

1220
    mTextChanged(true)
108
{
109
305
    setCaption(caption);
110
305
    mAllowLogic = false;
111
112
305
    setFocusable(true);
113
305
    addMouseListener(this);
114
305
    addKeyListener(this);
115
116
610
    mForegroundColor2 = getThemeColor(ThemeColorId::CHECKBOX_OUTLINE, 255U);
117
305
    if (instances == 0)
118
    {
119
7
        if (theme != nullptr)
120
        {
121

49
            mSkin = theme->load("checkbox.xml",
122
                "",
123
                true,
124
7
                Theme::getThemePath());
125
7
            updateAlpha();
126
        }
127
    }
128
129
305
    instances++;
130
131
305
    if (!eventId.empty())
132
284
        setActionEventId(eventId);
133
134
305
    if (listener != nullptr)
135
284
        addActionListener(listener);
136
137
610
    mForegroundColor = getThemeColor(ThemeColorId::CHECKBOX, 255U);
138
305
    if (mSkin != nullptr)
139
    {
140
610
        mPadding = mSkin->getPadding();
141

1220
        mImagePadding = mSkin->getOption("imagePadding");
142

1220
        mImageSize = mSkin->getOption("imageSize");
143

1220
        mSpacing = mSkin->getOption("spacing");
144

1220
        mDrawBox = (mSkin->getOption("drawBox", 1) != 0);
145
305
        mTextX = mPadding + mImageSize + mSpacing;
146
    }
147
305
    adjustSize();
148
305
}
149
150
2135
CheckBox::~CheckBox()
151
{
152
305
    if (mWindow != nullptr)
153
305
        mWindow->removeWidgetListener(this);
154
155
305
    if (gui != nullptr)
156
305
        gui->removeDragged(this);
157
158
305
    instances--;
159
160
305
    if (instances == 0)
161
    {
162
7
        if (theme != nullptr)
163
7
            theme->unload(mSkin);
164
    }
165
610
}
166
167
5
void CheckBox::draw(Graphics *const graphics)
168
{
169
    BLOCK_START("CheckBox::draw")
170
5
    drawBox(graphics);
171
172
5
    Font *const font = getFont();
173
174
5
    if (mTextChanged)
175
    {
176
5
        mTextChunk.textFont = font;
177
5
        mTextChunk.deleteImage();
178
10
        mTextChunk.text = mCaption;
179
5
        mTextChunk.color = mForegroundColor;
180
5
        mTextChunk.color2 = mForegroundColor2;
181
5
        font->generate(mTextChunk);
182
5
        mTextChanged = false;
183
    }
184
185
5
    const Image *const image = mTextChunk.img;
186
5
    if (image != nullptr)
187
5
        graphics->drawImage(image, mTextX, mPadding);
188
189
    BLOCK_END("CheckBox::draw")
190
5
}
191
192
void CheckBox::safeDraw(Graphics *const graphics)
193
{
194
    CheckBox::draw(graphics);
195
}
196
197
12
void CheckBox::updateAlpha()
198
{
199
    const float alpha = std::max(settings.guiAlpha,
200
36
        theme->getMinimumOpacity());
201
202
12
    if (mAlpha != alpha)
203
    {
204
        mAlpha = alpha;
205
        if (mSkin != nullptr)
206
        {
207
            const ImageRect &rect = mSkin->getBorder();
208
            for (int a = 0; a < 6; a ++)
209
            {
210
                Image *const image = rect.grid[a];
211
                if (image != nullptr)
212
                    image->setAlpha(mAlpha);
213
            }
214
        }
215
    }
216
12
}
217
218
5
void CheckBox::drawBox(Graphics *const graphics)
219
{
220

5
    if ((mSkin == nullptr) || !mDrawBox)
221
        return;
222
223
10
    const ImageRect &rect = mSkin->getBorder();
224
5
    int index = 0;
225
226

5
    if (mEnabled && mVisible == Visible_true)
227
    {
228
5
        if (mSelected)
229
        {
230
3
            if (mHasMouse)
231
                index = 5;
232
            else
233
3
                index = 1;
234
        }
235
        else
236
        {
237
2
            if (mHasMouse)
238
                index = 4;
239
            else
240
2
                index = 0;
241
        }
242
    }
243
    else
244
    {
245
        if (mSelected)
246
            index = 3;
247
        else
248
            index = 2;
249
    }
250
5
    const Image *const box = rect.grid[index];
251
252
5
    updateAlpha();
253
254
5
    if (box != nullptr)
255
    {
256
5
        graphics->drawImage(box,
257
            mImagePadding,
258
15
            (getHeight() - mImageSize) / 2);
259
    }
260
}
261
262
void CheckBox::mouseEntered(MouseEvent& event A_UNUSED)
263
{
264
    mHasMouse = true;
265
}
266
267
void CheckBox::mouseExited(MouseEvent& event)
268
{
269
    mHasMouse = false;
270
    ToolTipListener::mouseExited(event);
271
}
272
273
void CheckBox::keyPressed(KeyEvent& event)
274
{
275
    const InputActionT action = event.getActionId();
276
277
    if (action == InputAction::GUI_SELECT)
278
    {
279
        toggleSelected();
280
        event.consume();
281
    }
282
}
283
284
305
void CheckBox::adjustSize()
285
{
286
305
    setHeight(getFont()->getHeight() + 2 * mPadding);
287
610
    setWidth(mImagePadding + mImageSize + mSpacing
288
610
        + getFont()->getWidth(mCaption) + mPadding);
289
305
}
290
291
void CheckBox::mouseClicked(MouseEvent& event)
292
{
293
    if (event.getButton() == MouseButton::LEFT)
294
    {
295
        toggleSelected();
296
        event.consume();
297
    }
298
}
299
300
void CheckBox::mouseDragged(MouseEvent& event)
301
{
302
    event.consume();
303
}
304
305
void CheckBox::toggleSelected()
306
{
307
    mSelected = !mSelected;
308
    distributeActionEvent();
309
}
310
311
305
void CheckBox::setCaption(const std::string& caption)
312
{
313
610
    if (caption != mCaption)
314
305
        mTextChanged = true;
315
610
    mCaption = caption;
316
305
}
317
318
305
void CheckBox::setParent(Widget *widget)
319
{
320
305
    if (mWindow != nullptr)
321
305
        mWindow->addWidgetListener(this);
322
610
    Widget::setParent(widget);
323
305
}
324
325
void CheckBox::widgetHidden(const Event &event A_UNUSED)
326
{
327
    mTextChanged = true;
328
    mTextChunk.deleteImage();
329
}
330
331
void CheckBox::setWindow(Widget *const widget)
332
{
333
    if ((widget == nullptr) && (mWindow != nullptr))
334
    {
335
        mWindow->removeWidgetListener(this);
336
        mWindow = nullptr;
337
    }
338
    else
339
    {
340
        Widget2::setWindow(widget);
341
    }
342
}