Mercurial > fife-parpg
comparison ext/guichan-0.8.2/src/widgets/radiobutton.cpp @ 378:64738befdf3b
bringing in the changes from the build_system_rework branch in preparation for the 0.3.0 release. This commit will require the Jan2010 devkit. Clients will also need to be modified to the new way to import fife.
author | vtchill@33b003aa-7bff-0310-803a-e67f0ece8222 |
---|---|
date | Mon, 11 Jan 2010 23:34:52 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
377:fe6fb0e0ed23 | 378:64738befdf3b |
---|---|
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 /* | |
45 * For comments regarding functions please see the header file. | |
46 */ | |
47 | |
48 #include "guichan/widgets/radiobutton.hpp" | |
49 | |
50 #include "guichan/font.hpp" | |
51 #include "guichan/graphics.hpp" | |
52 #include "guichan/key.hpp" | |
53 #include "guichan/mouseinput.hpp" | |
54 | |
55 namespace gcn | |
56 { | |
57 RadioButton::GroupMap RadioButton::mGroupMap; | |
58 | |
59 RadioButton::RadioButton() | |
60 { | |
61 setSelected(false); | |
62 | |
63 setFocusable(true); | |
64 addMouseListener(this); | |
65 addKeyListener(this); | |
66 } | |
67 | |
68 RadioButton::RadioButton(const std::string &caption, | |
69 const std::string &group, | |
70 bool selected) | |
71 { | |
72 setCaption(caption); | |
73 setGroup(group); | |
74 setSelected(selected); | |
75 | |
76 setFocusable(true); | |
77 addMouseListener(this); | |
78 addKeyListener(this); | |
79 | |
80 adjustSize(); | |
81 } | |
82 | |
83 RadioButton::~RadioButton() | |
84 { | |
85 // Remove us from the group list | |
86 setGroup(""); | |
87 } | |
88 | |
89 void RadioButton::draw(Graphics* graphics) | |
90 { | |
91 graphics->pushClipArea(Rectangle(1, | |
92 1, | |
93 getWidth() - 1, | |
94 getHeight() - 1)); | |
95 drawBox(graphics); | |
96 graphics->popClipArea(); | |
97 | |
98 | |
99 graphics->setFont(getFont()); | |
100 graphics->setColor(getForegroundColor()); | |
101 | |
102 if (isFocused()) | |
103 { | |
104 int fh; | |
105 | |
106 if (getHeight()%2 == 0) | |
107 { | |
108 fh = getHeight() - 4; | |
109 } | |
110 else | |
111 { | |
112 fh = getHeight() - 3; | |
113 } | |
114 | |
115 int hh = (fh + 1) / 2; | |
116 | |
117 graphics->drawLine(0, hh + 1, hh + 1, 0); | |
118 graphics->drawLine(hh + 2, 1, fh + 2, hh + 1); | |
119 graphics->drawLine(fh + 1, hh + 2, hh + 1, fh + 2); | |
120 graphics->drawLine(hh + 1, fh + 2, 1, hh + 2); | |
121 } | |
122 | |
123 int h = getHeight() + getHeight() / 2; | |
124 | |
125 graphics->drawText(getCaption(), h - 2, 0); | |
126 } | |
127 | |
128 void RadioButton::drawBox(Graphics *graphics) | |
129 { | |
130 int h; | |
131 | |
132 if (getHeight()%2 == 0) | |
133 { | |
134 h = getHeight() - 4; | |
135 } | |
136 else | |
137 { | |
138 h = getHeight() - 3; | |
139 } | |
140 | |
141 int alpha = getBaseColor().a; | |
142 Color faceColor = getBaseColor(); | |
143 faceColor.a = alpha; | |
144 Color highlightColor = faceColor + 0x303030; | |
145 highlightColor.a = alpha; | |
146 Color shadowColor = faceColor - 0x303030; | |
147 shadowColor.a = alpha; | |
148 | |
149 graphics->setColor(getBackgroundColor()); | |
150 | |
151 int i; | |
152 int hh = (h + 1) / 2; | |
153 | |
154 for (i = 1; i <= hh; ++i) | |
155 { | |
156 graphics->drawLine(hh - i + 1, | |
157 i, | |
158 hh + i - 1, | |
159 i); | |
160 } | |
161 | |
162 for (i = 1; i < hh; ++i) | |
163 { | |
164 graphics->drawLine(hh - i + 1, | |
165 h - i, | |
166 hh + i - 1, | |
167 h - i); | |
168 } | |
169 | |
170 graphics->setColor(shadowColor); | |
171 graphics->drawLine(hh, 0, 0, hh); | |
172 graphics->drawLine(hh + 1, 1, h - 1, hh - 1); | |
173 | |
174 graphics->setColor(highlightColor); | |
175 graphics->drawLine(1, hh + 1, hh, h); | |
176 graphics->drawLine(hh + 1, h - 1, h, hh); | |
177 | |
178 graphics->setColor(getForegroundColor()); | |
179 | |
180 int hhh = hh - 3; | |
181 if (mSelected) | |
182 { | |
183 for (i = 0; i < hhh; ++i) | |
184 { | |
185 graphics->drawLine(hh - i, 4 + i, hh + i, 4 + i); | |
186 } | |
187 for (i = 0; i < hhh; ++i) | |
188 { | |
189 graphics->drawLine(hh - i, h - 4 - i, hh + i, h - 4 - i); | |
190 } | |
191 | |
192 } | |
193 } | |
194 | |
195 bool RadioButton::isSelected() const | |
196 { | |
197 return mSelected; | |
198 } | |
199 | |
200 void RadioButton::setSelected(bool selected) | |
201 { | |
202 if (selected && mGroup != "") | |
203 { | |
204 GroupIterator iter, iterEnd; | |
205 iterEnd = mGroupMap.upper_bound(mGroup); | |
206 | |
207 for (iter = mGroupMap.lower_bound(mGroup); | |
208 iter != iterEnd; | |
209 iter++) | |
210 { | |
211 if (iter->second->isSelected()) | |
212 { | |
213 iter->second->setSelected(false); | |
214 } | |
215 } | |
216 } | |
217 | |
218 mSelected = selected; | |
219 } | |
220 | |
221 const std::string &RadioButton::getCaption() const | |
222 { | |
223 return mCaption; | |
224 } | |
225 | |
226 void RadioButton::setCaption(const std::string caption) | |
227 { | |
228 mCaption = caption; | |
229 } | |
230 | |
231 void RadioButton::keyPressed(KeyEvent& keyEvent) | |
232 { | |
233 Key key = keyEvent.getKey(); | |
234 | |
235 if (key.getValue() == Key::ENTER || | |
236 key.getValue() == Key::SPACE) | |
237 { | |
238 setSelected(true); | |
239 distributeActionEvent(); | |
240 keyEvent.consume(); | |
241 } | |
242 } | |
243 | |
244 void RadioButton::mouseClicked(MouseEvent& mouseEvent) | |
245 { | |
246 if (mouseEvent.getButton() == MouseEvent::LEFT) | |
247 { | |
248 setSelected(true); | |
249 distributeActionEvent(); | |
250 } | |
251 } | |
252 | |
253 void RadioButton::mouseDragged(MouseEvent& mouseEvent) | |
254 { | |
255 mouseEvent.consume(); | |
256 } | |
257 | |
258 void RadioButton::setGroup(const std::string &group) | |
259 { | |
260 if (mGroup != "") | |
261 { | |
262 GroupIterator iter, iterEnd; | |
263 iterEnd = mGroupMap.upper_bound(mGroup); | |
264 | |
265 for (iter = mGroupMap.lower_bound(mGroup); | |
266 iter != iterEnd; | |
267 iter++) | |
268 { | |
269 if (iter->second == this) | |
270 { | |
271 mGroupMap.erase(iter); | |
272 break; | |
273 } | |
274 } | |
275 } | |
276 | |
277 if (group != "") | |
278 { | |
279 mGroupMap.insert( | |
280 std::pair<std::string, RadioButton *>(group, this)); | |
281 } | |
282 | |
283 mGroup = group; | |
284 } | |
285 | |
286 const std::string &RadioButton::getGroup() const | |
287 { | |
288 return mGroup; | |
289 } | |
290 | |
291 void RadioButton::adjustSize() | |
292 { | |
293 int height = getFont()->getHeight(); | |
294 | |
295 setHeight(height); | |
296 setWidth(getFont()->getWidth(getCaption()) + height + height/2); | |
297 } | |
298 } |