GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/widgets/checkbox.h Lines: 1 1 100.0 %
Date: 2021-03-17 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-2019  The ManaPlus Developers
6
 *  Copyright (C) 2019-2021  Andrei Karas
7
 *
8
 *  This file is part of The ManaPlus Client.
9
 *
10
 *  This program is free software; you can redistribute it and/or modify
11
 *  it under the terms of the GNU General Public License as published by
12
 *  the Free Software Foundation; either version 2 of the License, or
13
 *  any later version.
14
 *
15
 *  This program is distributed in the hope that it will be useful,
16
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 *  GNU General Public License for more details.
19
 *
20
 *  You should have received a copy of the GNU General Public License
21
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 */
23
24
/*      _______   __   __   __   ______   __   __   _______   __   __
25
 *     / _____/\ / /\ / /\ / /\ / ____/\ / /\ / /\ / ___  /\ /  |\/ /\
26
 *    / /\____\// / // / // / // /\___\// /_// / // /\_/ / // , |/ / /
27
 *   / / /__   / / // / // / // / /    / ___  / // ___  / // /| ' / /
28
 *  / /_// /\ / /_// / // / // /_/_   / / // / // /\_/ / // / |  / /
29
 * /______/ //______/ //_/ //_____/\ /_/ //_/ //_/ //_/ //_/ /|_/ /
30
 * \______\/ \______\/ \_\/ \_____\/ \_\/ \_\/ \_\/ \_\/ \_\/ \_\/
31
 *
32
 * Copyright (c) 2004 - 2008 Olof Naessén and Per Larsson
33
 *
34
 *
35
 * Per Larsson a.k.a finalman
36
 * Olof Naessén a.k.a jansem/yakslem
37
 *
38
 * Visit: http://guichan.sourceforge.net
39
 *
40
 * License: (BSD)
41
 * Redistribution and use in source and binary forms, with or without
42
 * modification, are permitted provided that the following conditions
43
 * are met:
44
 * 1. Redistributions of source code must retain the above copyright
45
 *    notice, this list of conditions and the following disclaimer.
46
 * 2. Redistributions in binary form must reproduce the above copyright
47
 *    notice, this list of conditions and the following disclaimer in
48
 *    the documentation and/or other materials provided with the
49
 *    distribution.
50
 * 3. Neither the name of Guichan nor the names of its contributors may
51
 *    be used to endorse or promote products derived from this software
52
 *    without specific prior written permission.
53
 *
54
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
55
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
56
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
57
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
58
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
59
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
60
 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
61
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
62
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
63
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
64
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
65
 */
66
67
#ifndef GUI_WIDGETS_CHECKBOX_H
68
#define GUI_WIDGETS_CHECKBOX_H
69
70
#include "listeners/keylistener.h"
71
#include "listeners/tooltiplistener.h"
72
73
#include "listeners/widgetlistener.h"
74
75
#include "gui/fonts/textchunk.h"
76
77
#include "gui/widgets/widget.h"
78
79
#include "localconsts.h"
80
81
class Skin;
82
83
/**
84
 * Check box widget. Same as the Guichan check box but with custom look.
85
 *
86
 * \ingroup GUI
87
 */
88
class CheckBox final : public Widget,
89
                       public ToolTipListener,
90
                       public KeyListener,
91
                       public WidgetListener
92
{
93
    public:
94
        /**
95
         * Constructor.
96
         */
97
        CheckBox(const Widget2 *const widget,
98
                 const std::string &restrict caption,
99
                 const bool selected,
100
                 ActionListener *const listener,
101
                 const std::string &restrict eventId);
102
103
        A_DELETE_COPY(CheckBox)
104
105
        /**
106
         * Destructor.
107
         */
108
        ~CheckBox() override final;
109
110
        /**
111
         * Draws the caption, then calls drawBox to draw the check box.
112
         */
113
        void draw(Graphics *const graphics) override final A_NONNULL(2);
114
115
        void safeDraw(Graphics *const graphics) override final A_NONNULL(2);
116
117
        /**
118
         * Update the alpha value to the checkbox components.
119
         */
120
        void updateAlpha();
121
122
        /**
123
         * Draws the check box, not the caption.
124
         */
125
        void drawBox(Graphics *const graphics) A_NONNULL(2);
126
127
        /**
128
         * Called when the mouse enteres the widget area.
129
         */
130
        void mouseEntered(MouseEvent& event) override final;
131
132
        /**
133
         * Called when the mouse leaves the widget area.
134
         */
135
        void mouseExited(MouseEvent& event) override final;
136
137
        void keyPressed(KeyEvent& event) override final;
138
139
        void adjustSize();
140
141
        /**
142
         * Checks if the check box is selected.
143
         *
144
         * @return True if the check box is selected, false otherwise.
145
         * @see setSelected
146
         */
147
        bool isSelected() const
148
        { return mSelected; }
149
150
        /**
151
         * Sets the check box to be selected or not.
152
         *
153
         * @param selected True if the check box should be set as selected.
154
         * @see isSelected
155
         */
156
        void setSelected(const bool selected)
157
6
        { mSelected = selected; }
158
159
        /**
160
         * Gets the caption of the check box.
161
         *
162
         * @return The caption of the check box.
163
         * @see setCaption
164
         */
165
        const std::string &getCaption() const
166
        { return mCaption; }
167
168
        /**
169
         * Sets the caption of the check box. It's advisable to call
170
         * adjustSize after setting of the caption to adjust the
171
         * check box's size to fit the caption.
172
         *
173
         * @param caption The caption of the check box.
174
         * @see getCaption, adjustSize
175
         */
176
        void setCaption(const std::string& caption);
177
178
        void mouseClicked(MouseEvent& event) override final;
179
180
        void mouseDragged(MouseEvent& event) override final;
181
182
        void setParent(Widget *widget) override final;
183
184
        void widgetHidden(const Event &event) override final;
185
186
        void setWindow(Widget *const widget) override final;
187
188
    private:
189
        void toggleSelected();
190
191
        /**
192
         * True if the check box is selected, false otherwise.
193
         */
194
        bool mSelected;
195
196
        /**
197
         * Holds the caption of the check box.
198
         */
199
        std::string mCaption;
200
201
        TextChunk mTextChunk;
202
203
        int mPadding;
204
        int mImagePadding;
205
        int mImageSize;
206
        int mSpacing;
207
        int mTextX;
208
        bool mHasMouse;
209
        bool mDrawBox;
210
        bool mTextChanged;
211
212
        static int instances;
213
        static Skin *mSkin;
214
        static float mAlpha;
215
};
216
217
#endif  // GUI_WIDGETS_CHECKBOX_H