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

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

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

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

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

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

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

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

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