comparison ext/guichan-0.8.1/include/guichan/widgets/radiobutton.hpp @ 0:4a0efb7baf70

* Datasets becomes the new trunk and retires after that :-)
author mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
date Sun, 29 Jun 2008 18:44:17 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4a0efb7baf70
1 /* _______ __ __ __ ______ __ __ _______ __ __
2 * / _____/\ / /\ / /\ / /\ / ____/\ / /\ / /\ / ___ /\ / |\/ /\
3 * / /\____\// / // / // / // /\___\// /_// / // /\_/ / // , |/ / /
4 * / / /__ / / // / // / // / / / ___ / // ___ / // /| ' / /
5 * / /_// /\ / /_// / // / // /_/_ / / // / // /\_/ / // / | / /
6 * /______/ //______/ //_/ //_____/\ /_/ //_/ //_/ //_/ //_/ /|_/ /
7 * \______\/ \______\/ \_\/ \_____\/ \_\/ \_\/ \_\/ \_\/ \_\/ \_\/
8 *
9 * Copyright (c) 2004 - 2008 Olof Naessén and Per Larsson
10 *
11 *
12 * Per Larsson a.k.a finalman
13 * Olof Naessén a.k.a jansem/yakslem
14 *
15 * Visit: http://guichan.sourceforge.net
16 *
17 * License: (BSD)
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
20 * are met:
21 * 1. Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in
25 * the documentation and/or other materials provided with the
26 * distribution.
27 * 3. Neither the name of Guichan nor the names of its contributors may
28 * be used to endorse or promote products derived from this software
29 * without specific prior written permission.
30 *
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
36 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
37 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
38 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
39 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
40 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
41 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 */
43
44 #ifndef GCN_RADIOBUTTON_HPP
45 #define GCN_RADIOBUTTON_HPP
46
47 #include <map>
48 #include <string>
49
50 #include "guichan/keylistener.hpp"
51 #include "guichan/mouselistener.hpp"
52 #include "guichan/platform.hpp"
53 #include "guichan/widget.hpp"
54
55 namespace gcn
56 {
57 /**
58 * An implementation of a radio button where a user can select or deselect
59 * the radio button and where the status of the radio button is displayed to the user.
60 * A radio button can belong to a group and when a radio button belongs to a
61 * group only one radio button can be selected in the group. A radio button is
62 * capable of displaying a caption.
63 *
64 * If a radio button's state changes an action event will be sent to all action
65 * listeners of the check box.
66 */
67 class GCN_CORE_DECLSPEC RadioButton :
68 public Widget,
69 public MouseListener,
70 public KeyListener
71 {
72 public:
73
74 /**
75 * Constructor.
76 */
77 RadioButton();
78
79 /**
80 * Constructor. The radio button will be automatically resized
81 * to fit the caption.
82 *
83 * @param caption The caption of the radio button.
84 * @param group The group the radio button should belong to.
85 * @param selected True if the radio button should be selected.
86 */
87 RadioButton(const std::string &caption,
88 const std::string &group,
89 bool selected = false);
90
91 /**
92 * Destructor.
93 */
94 virtual ~RadioButton();
95
96 /**
97 * Checks if the radio button is selected.
98 *
99 * @return True if the radio button is selecte, false otherwise.
100 * @see setSelected
101 */
102 bool isSelected() const;
103
104 /**
105 * Sets the radio button to selected or not.
106 *
107 * @param selected True if the radio button should be selected,
108 * false otherwise.
109 * @see isSelected
110 */
111 void setSelected(bool selected);
112
113 /**
114 * Gets the caption of the radio button.
115 *
116 * @return The caption of the radio button.
117 * @see setCaption
118 */
119 const std::string &getCaption() const;
120
121 /**
122 * Sets the caption of the radio button. It's advisable to call
123 * adjustSize after setting of the caption to adjust the
124 * radio button's size to fit the caption.
125 *
126 * @param caption The caption of the radio button.
127 * @see getCaption, adjustSize
128 */
129 void setCaption(const std::string caption);
130
131 /**
132 * Sets the group the radio button should belong to. Note that
133 * a radio button group is unique per application, not per Gui object
134 * as the group is stored in a static map.
135 *
136 * @param group The name of the group.
137 * @see getGroup
138 */
139 void setGroup(const std::string &group);
140
141 /**
142 * Gets the group the radio button belongs to.
143 *
144 * @return The group the radio button belongs to.
145 * @see setGroup
146 */
147 const std::string &getGroup() const;
148
149 /**
150 * Adjusts the radio button's size to fit the caption.
151 */
152 void adjustSize();
153
154
155 // Inherited from Widget
156
157 virtual void draw(Graphics* graphics);
158
159
160 // Inherited from KeyListener
161
162 virtual void keyPressed(KeyEvent& keyEvent);
163
164
165 // Inherited from MouseListener
166
167 virtual void mouseClicked(MouseEvent& mouseEvent);
168
169 virtual void mouseDragged(MouseEvent& mouseEvent);
170
171 protected:
172 /**
173 * Draws the box.
174 *
175 * @param graphics a Graphics object to draw with.
176 */
177 virtual void drawBox(Graphics *graphics);
178
179 /**
180 * True if the radio button is selected, false otherwise.
181 */
182 bool mSelected;
183
184 /**
185 * Holds the caption of the radio button.
186 */
187 std::string mCaption;
188
189 /**
190 * Holds the group of the radio button.
191 */
192 std::string mGroup;
193
194 /**
195 * Typdef.
196 */
197 typedef std::multimap<std::string, RadioButton *> GroupMap;
198
199 /**
200 * Typdef.
201 */
202 typedef GroupMap::iterator GroupIterator;
203
204 /**
205 * Holds all available radio button groups.
206 */
207 static GroupMap mGroupMap;
208 };
209 }
210
211 #endif // end GCN_RADIOBUTTON_HPP