GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/widgets/radiobutton.cpp Lines: 106 143 74.1 %
Date: 2017-11-29 Branches: 53 106 50.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/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
2
RadioButton::GroupMap RadioButton::mGroupMap;
88
89
16
RadioButton::RadioButton(const Widget2 *const widget,
90
                         const std::string &restrict caption,
91
                         const std::string &restrict group,
92
16
                         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
96
    mTextChanged(true)
108
{
109
16
    mAllowLogic = false;
110
16
    setCaption(caption);
111
16
    setGroup(group);
112
16
    setSelected(marked);
113
114
16
    setFocusable(true);
115
16
    addMouseListener(this);
116
16
    addKeyListener(this);
117
118
32
    mForegroundColor = getThemeColor(ThemeColorId::RADIOBUTTON);
119
32
    mForegroundColor2 = getThemeColor(ThemeColorId::RADIOBUTTON_OUTLINE);
120
16
    if (instances == 0)
121
    {
122
6
        if (theme != nullptr)
123
        {
124

48
            mSkin = theme->load("radio.xml", "");
125
6
            updateAlpha();
126
        }
127
    }
128
129
16
    instances++;
130
131
16
    if (mSkin != nullptr)
132
    {
133
32
        mPadding = mSkin->getPadding();
134

64
        mImagePadding = mSkin->getOption("imagePadding");
135

64
        mImageSize = mSkin->getOption("imageSize");
136

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

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

24
    if (selected && !mGroup.empty())
290
    {
291
12
        for (GroupIterator iter = mGroupMap.lower_bound(mGroup),
292
12
             iterEnd = mGroupMap.upper_bound(mGroup);
293
20
             iter != iterEnd;
294
             ++ iter)
295
        {
296

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