GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/widgets/radiobutton.cpp Lines: 105 142 73.9 %
Date: 2018-09-20 Branches: 56 110 50.9 %

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/radiobutton.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 RadioButton::instances = 0;
84
Skin *RadioButton::mSkin = nullptr;
85
float RadioButton::mAlpha = 1.0;
86
87
1
RadioButton::GroupMap RadioButton::mGroupMap;
88
89
8
RadioButton::RadioButton(const Widget2 *const widget,
90
                         const std::string &restrict caption,
91
                         const std::string &restrict group,
92
8
                         const bool marked) :
93
    Widget(widget),
94
    MouseListener(),
95
    KeyListener(),
96
    WidgetListener(),
97
    mSelected(false),
98
    mCaption(),
99
    mGroup(),
100
    mTextChunk(),
101
    mPadding(0),
102
    mImagePadding(0),
103
    mImageSize(9),
104
    mSpacing(2),
105
    mTextX(0 + 9 + 2),
106
    mHasMouse(false),
107
48
    mTextChanged(true)
108
{
109
8
    mAllowLogic = false;
110
8
    setCaption(caption);
111
8
    setGroup(group);
112
8
    setSelected(marked);
113
114
8
    setFocusable(true);
115
8
    addMouseListener(this);
116
8
    addKeyListener(this);
117
118
16
    mForegroundColor = getThemeColor(ThemeColorId::RADIOBUTTON, 255U);
119
16
    mForegroundColor2 = getThemeColor(ThemeColorId::RADIOBUTTON_OUTLINE, 255U);
120
8
    if (instances == 0)
121
    {
122
3
        if (theme != nullptr)
123
        {
124

21
            mSkin = theme->load("radio.xml",
125
                "",
126
                true,
127
3
                Theme::getThemePath());
128
3
            updateAlpha();
129
        }
130
    }
131
132
8
    instances++;
133
134
8
    if (mSkin != nullptr)
135
    {
136
16
        mPadding = mSkin->getPadding();
137

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

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

24
        mSpacing = mSkin->getOption("spacing");
140
8
        mTextX = mPadding + mImageSize + mSpacing;
141
    }
142
143
8
    adjustSize();
144
8
}
145
146
64
RadioButton::~RadioButton()
147
{
148
8
    if (mWindow != nullptr)
149
8
        mWindow->removeWidgetListener(this);
150
151
16
    setGroup(std::string());
152
153
8
    if (gui != nullptr)
154
8
        gui->removeDragged(this);
155
156
8
    instances--;
157
158
8
    if (instances == 0)
159
    {
160
3
        if (theme != nullptr)
161
3
            theme->unload(mSkin);
162
    }
163
16
}
164
165
8
void RadioButton::updateAlpha()
166
{
167
    const float alpha = std::max(settings.guiAlpha,
168
24
        theme->getMinimumOpacity());
169
170
8
    if (mAlpha != alpha)
171
    {
172
        mAlpha = alpha;
173
        if (mSkin != nullptr)
174
        {
175
            const ImageRect &rect = mSkin->getBorder();
176
            for (int a = 0; a < 4; a ++)
177
            {
178
                Image *const image = rect.grid[a];
179
                if (image != nullptr)
180
                    image->setAlpha(mAlpha);
181
            }
182
        }
183
    }
184
8
}
185
186
5
void RadioButton::drawBox(Graphics *const graphics)
187
{
188
5
    if (mSkin == nullptr)
189
        return;
190
191
10
    const ImageRect &rect = mSkin->getBorder();
192
5
    int index = 0;
193
194

5
    if (mEnabled && mVisible == Visible_true)
195
    {
196
5
        if (mSelected)
197
        {
198
3
            if (mHasMouse)
199
                index = 1;
200
            else
201
3
                index = 0;
202
        }
203
        else
204
        {
205
2
            if (mHasMouse)
206
                index = 3;
207
            else
208
2
                index = 2;
209
        }
210
    }
211
    else
212
    {
213
        if (mSelected)
214
            index = 0;
215
        else
216
            index = 2;
217
    }
218
219
5
    const Image *const box = rect.grid[index];
220
221
5
    updateAlpha();
222
223
5
    if (box != nullptr)
224
    {
225
5
        graphics->drawImage(box,
226
            mImagePadding,
227
15
            (getHeight() - mImageSize) / 2);
228
    }
229
}
230
231
5
void RadioButton::draw(Graphics *const graphics)
232
{
233
    BLOCK_START("RadioButton::draw")
234
5
    drawBox(graphics);
235
236
5
    Font *const font = getFont();
237
238
5
    if (mTextChanged)
239
    {
240
5
        mTextChunk.textFont = font;
241
5
        mTextChunk.deleteImage();
242
10
        mTextChunk.text = mCaption;
243
5
        mTextChunk.color = mForegroundColor;
244
5
        mTextChunk.color2 = mForegroundColor2;
245
5
        font->generate(mTextChunk);
246
5
        mTextChanged = false;
247
    }
248
249
5
    const Image *const image = mTextChunk.img;
250
5
    if (image != nullptr)
251
5
        graphics->drawImage(image, mTextX, mPadding);
252
253
    BLOCK_END("RadioButton::draw")
254
5
}
255
256
void RadioButton::safeDraw(Graphics *const graphics)
257
{
258
    RadioButton::draw(graphics);
259
}
260
261
void RadioButton::mouseEntered(MouseEvent& event A_UNUSED)
262
{
263
    mHasMouse = true;
264
}
265
266
void RadioButton::mouseExited(MouseEvent& event A_UNUSED)
267
{
268
    mHasMouse = false;
269
}
270
271
void RadioButton::keyPressed(KeyEvent& event)
272
{
273
    const InputActionT action = event.getActionId();
274
    if (action == InputAction::GUI_SELECT)
275
    {
276
        setSelected(true);
277
        distributeActionEvent();
278
        event.consume();
279
    }
280
}
281
282
8
void RadioButton::adjustSize()
283
{
284
8
    Font *const font = getFont();
285
8
    setHeight(font->getHeight() + 2 * mPadding);
286
16
    setWidth(mImagePadding + mImageSize + mSpacing
287
16
        + font->getWidth(mCaption) + mPadding);
288
8
}
289
290
9
void RadioButton::setSelected(const bool selected)
291
{
292

12
    if (selected && !mGroup.empty())
293
    {
294
16
        for (GroupIterator iter = mGroupMap.lower_bound(mGroup),
295
6
             iterEnd = mGroupMap.upper_bound(mGroup);
296
             iter != iterEnd;
297
             ++ iter)
298
        {
299

14
            if ((iter->second != nullptr) && iter->second->isSelected())
300
                iter->second->setSelected(false);
301
        }
302
    }
303
304
9
    mSelected = selected;
305
9
}
306
307
void RadioButton::mouseClicked(MouseEvent& event)
308
{
309
    if (event.getButton() == MouseButton::LEFT)
310
    {
311
        setSelected(true);
312
        event.consume();
313
        distributeActionEvent();
314
    }
315
}
316
317
void RadioButton::mouseDragged(MouseEvent& event)
318
{
319
    event.consume();
320
}
321
322
16
void RadioButton::setGroup(const std::string &group)
323
{
324
32
    if (!mGroup.empty())
325
    {
326
25
        for (GroupIterator iter = mGroupMap.lower_bound(mGroup),
327
16
             iterEnd = mGroupMap.upper_bound(mGroup);
328
             iter != iterEnd;
329
             ++ iter)
330
        {
331
9
            if (iter->second == this)
332
            {
333
                mGroupMap.erase(iter);
334
                break;
335
            }
336
        }
337
    }
338
339
16
    if (!group.empty())
340
32
        mGroupMap.insert(std::pair<std::string, RadioButton *>(group, this));
341
342
32
    mGroup = group;
343
16
}
344
345
8
void RadioButton::setCaption(const std::string& caption)
346
{
347
16
    if (caption != mCaption)
348
8
        mTextChanged = true;
349
16
    mCaption = caption;
350
8
}
351
352
5
void RadioButton::setParent(Widget *widget)
353
{
354
5
    if (mWindow != nullptr)
355
5
        mWindow->addWidgetListener(this);
356
10
    Widget::setParent(widget);
357
5
}
358
359
void RadioButton::widgetHidden(const Event &event A_UNUSED)
360
{
361
    mTextChanged = true;
362
    mTextChunk.deleteImage();
363
}
364
365
void RadioButton::setWindow(Widget *const widget)
366
{
367
    if ((widget == nullptr) && (mWindow != nullptr))
368
    {
369
        mWindow->removeWidgetListener(this);
370
        mWindow = nullptr;
371
    }
372
    else
373
    {
374
        Widget2::setWindow(widget);
375
    }
376
2
}