GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/widgets/checkbox.cpp Lines: 87 125 69.6 %
Date: 2017-11-29 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-2017  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
606
CheckBox::CheckBox(const Widget2 *const widget,
88
                   const std::string &restrict caption,
89
                   const bool selected,
90
                   ActionListener *const listener,
91
606
                   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

2424
    mTextChanged(true)
107
{
108
606
    setCaption(caption);
109
606
    mAllowLogic = false;
110
111
606
    setFocusable(true);
112
606
    addMouseListener(this);
113
606
    addKeyListener(this);
114
115
1212
    mForegroundColor2 = getThemeColor(ThemeColorId::CHECKBOX_OUTLINE);
116
606
    if (instances == 0)
117
    {
118
14
        if (theme != nullptr)
119
        {
120

112
            mSkin = theme->load("checkbox.xml", "");
121
14
            updateAlpha();
122
        }
123
    }
124
125
606
    instances++;
126
127
606
    if (!eventId.empty())
128
564
        setActionEventId(eventId);
129
130
606
    if (listener != nullptr)
131
564
        addActionListener(listener);
132
133
1212
    mForegroundColor = getThemeColor(ThemeColorId::CHECKBOX);
134
606
    if (mSkin != nullptr)
135
    {
136
1212
        mPadding = mSkin->getPadding();
137

2424
        mImagePadding = mSkin->getOption("imagePadding");
138

2424
        mImageSize = mSkin->getOption("imageSize");
139

1818
        mSpacing = mSkin->getOption("spacing");
140

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

10
    if ((mSkin == nullptr) || !mDrawBox)
217
        return;
218
219
20
    const ImageRect &rect = mSkin->getBorder();
220
10
    int index = 0;
221
222

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