GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/widgets/checkbox.h Lines: 1 1 100.0 %
Date: 2017-11-29 Branches: 0 0 0.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
#ifndef GUI_WIDGETS_CHECKBOX_H
67
#define GUI_WIDGETS_CHECKBOX_H
68
69
#include "listeners/keylistener.h"
70
#include "listeners/tooltiplistener.h"
71
72
#include "listeners/widgetlistener.h"
73
74
#include "gui/fonts/textchunk.h"
75
76
#include "gui/widgets/widget.h"
77
78
#include "localconsts.h"
79
80
class Skin;
81
82
/**
83
 * Check box widget. Same as the Guichan check box but with custom look.
84
 *
85
 * \ingroup GUI
86
 */
87
class CheckBox final : public Widget,
88
                       public ToolTipListener,
89
                       public KeyListener,
90
                       public WidgetListener
91
{
92
    public:
93
        /**
94
         * Constructor.
95
         */
96
        CheckBox(const Widget2 *const widget,
97
                 const std::string &restrict caption,
98
                 const bool selected = false,
99
                 ActionListener *const listener = nullptr,
100
                 const std::string &restrict eventId = "");
101
102
        A_DELETE_COPY(CheckBox)
103
104
        /**
105
         * Destructor.
106
         */
107
        ~CheckBox();
108
109
        /**
110
         * Draws the caption, then calls drawBox to draw the check box.
111
         */
112
        void draw(Graphics *const graphics) override final A_NONNULL(2);
113
114
        void safeDraw(Graphics *const graphics) override final A_NONNULL(2);
115
116
        /**
117
         * Update the alpha value to the checkbox components.
118
         */
119
        void updateAlpha();
120
121
        /**
122
         * Draws the check box, not the caption.
123
         */
124
        void drawBox(Graphics *const graphics) A_NONNULL(2);
125
126
        /**
127
         * Called when the mouse enteres the widget area.
128
         */
129
        void mouseEntered(MouseEvent& event) override final;
130
131
        /**
132
         * Called when the mouse leaves the widget area.
133
         */
134
        void mouseExited(MouseEvent& event) override final;
135
136
        void keyPressed(KeyEvent& event) override final;
137
138
        void adjustSize();
139
140
        /**
141
         * Checks if the check box is selected.
142
         *
143
         * @return True if the check box is selected, false otherwise.
144
         * @see setSelected
145
         */
146
        bool isSelected() const
147
        { return mSelected; }
148
149
        /**
150
         * Sets the check box to be selected or not.
151
         *
152
         * @param selected True if the check box should be set as selected.
153
         * @see isSelected
154
         */
155
        void setSelected(const bool selected)
156
12
        { mSelected = selected; }
157
158
        /**
159
         * Gets the caption of the check box.
160
         *
161
         * @return The caption of the check box.
162
         * @see setCaption
163
         */
164
        const std::string &getCaption() const
165
        { return mCaption; }
166
167
        /**
168
         * Sets the caption of the check box. It's advisable to call
169
         * adjustSize after setting of the caption to adjust the
170
         * check box's size to fit the caption.
171
         *
172
         * @param caption The caption of the check box.
173
         * @see getCaption, adjustSize
174
         */
175
        void setCaption(const std::string& caption);
176
177
        void mouseClicked(MouseEvent& event) override final;
178
179
        void mouseDragged(MouseEvent& event) override final;
180
181
        void setParent(Widget *widget) override final;
182
183
        void widgetHidden(const Event &event) override final;
184
185
        void setWindow(Widget *const widget) override final;
186
187
    private:
188
        void toggleSelected();
189
190
        /**
191
         * True if the check box is selected, false otherwise.
192
         */
193
        bool mSelected;
194
195
        /**
196
         * Holds the caption of the check box.
197
         */
198
        std::string mCaption;
199
200
        TextChunk mTextChunk;
201
202
        int mPadding;
203
        int mImagePadding;
204
        int mImageSize;
205
        int mSpacing;
206
        int mTextX;
207
        bool mHasMouse;
208
        bool mDrawBox;
209
        bool mTextChanged;
210
211
        static int instances;
212
        static Skin *mSkin;
213
        static float mAlpha;
214
};
215
216
#endif  // GUI_WIDGETS_CHECKBOX_H