Debian Codegraph @

engine/guichan/focushandler.cpp

raw
1 /* _______ __ __ __ ______ __ __ _______ __ __
2 * / _____/\ / /\ / /\ / /\ / ____/\ / /\ / /\ / ___ /\ / |\/ /\
3 * / /\____\// / // / // / // /\___\// /_// / // /\_/ / // , |/ / /
4 * / / /__ / / // / // / // / / / ___ / // ___ / // /| ' / /
5 * / /_// /\ / /_// / // / // /_/_ / / // / // /\_/ / // / | / /
6 * /______/ //______/ //_/ //_____/\ /_/ //_/ //_/ //_/ //_/ /|_/ /
7 * \______\/ \______\/ \_\/ \_____\/ \_\/ \_\/ \_\/ \_\/ \_\/ \_\/
8 *
9 * Copyright (c) 2004, 2005 darkbits Js_./
10 * Per Larsson a.k.a finalman _RqZ{a<^_aa
11 * Olof Naess�n a.k.a jansem/yakslem _asww7!uY`> )\a//
12 * _Qhm`] _f "'c 1!5m
13 * Visit: http://guichan.darkbits.org )Qk<P ` _: :+' .' "{[
14 * .)j(] .d_/ '-( P . S
15 * License: (BSD) <Td/Z <fP"5(\"??"\a. .L
16 * Redistribution and use in source and _dV>ws?a-?' ._/L #'
17 * binary forms, with or without )4d[#7r, . ' )d`)[
18 * modification, are permitted provided _Q-5'5W..j/?' -?!\)cam'
19 * that the following conditions are met: j<<WP+k/);. _W=j f
20 * 1. Redistributions of source code must .$%w\/]Q . ."' . mj$
21 * retain the above copyright notice, ]E.pYY(Q]>. a J@\
22 * this list of conditions and the j(]1u<sE"L,. . ./^ ]{a
23 * following disclaimer. 4'_uomm\. )L);-4 (3=
24 * 2. Redistributions in binary form must )_]X{Z('a_"a7'<a"a, ]"[
25 * reproduce the above copyright notice, #}<]m7`Za??4,P-"'7. ).m
26 * this list of conditions and the ]d2e)Q(<Q( ?94 b- LQ/
27 * following disclaimer in the <B!</]C)d_, '(<' .f. =C+m
28 * documentation and/or other materials .Z!=J ]e []('-4f _ ) -.)m]'
29 * provided with the distribution. .w[5]' _[ /.)_-"+? _/ <W"
30 * 3. Neither the name of Guichan nor the :$we` _! + _/ . j?
31 * names of its contributors may be used =3)= _f (_yQmWW$#( "
32 * to endorse or promote products derived - W, sQQQQmZQ#Wwa]..
33 * from this software without specific (js, \[QQW$QWW#?!V"".
34 * prior written permission. ]y:.<\.. .
35 * -]n w/ ' [.
36 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT )/ )/ !
37 * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY < (; sac , '
38 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, ]^ .- %
39 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF c < r
40 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR aga< <La
41 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 5% )P'-3L
42 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR _bQf` y`..)a
43 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, ,J?4P'.P"_(\?d'.,
44 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES _Pa,)!f/<[]/ ?"
45 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT _2-..:. .r+_,.. .
46 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, ?a.<%"' " -'.a_ _,
47 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) ^
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
49 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
50 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
51 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
52 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
53 */
54
55 /*
56 * For comments regarding functions please see the header file.
57 */
58
59 #include <ctype.h> // for isascii
60 #include "guichan/focushandler.h"
61 #include "guichan/exception.h"
62
63 namespace gcn
64 {
65 FocusHandler::FocusHandler()
66 {
67 mFocusedWidget = NULL;
68 mDraggedWidget = NULL;
69 mToBeFocused = NULL;
70 mToBeDragged = NULL;
71 mModalFocusedWidget = NULL;
72 }
73
74 void FocusHandler::requestFocus(Widget* widget)
75 {
76 mToBeFocused = widget;
77 }
78
79 void FocusHandler::setFocus(Widget* widget)
80 {
81 mFocusedWidget = widget;
82 }
83
84 void FocusHandler::requestDrag(Widget* widget)
85 {
86 mToBeDragged = widget;
87 }
88
89 void FocusHandler::requestModalFocus(Widget* widget)
90 {
91 if (mModalFocusedWidget != NULL && mModalFocusedWidget != widget)
92 {
93 throw GCN_EXCEPTION("Another widget already has modal focus.");
94 }
95
96 mModalFocusedWidget = widget;
97
98 if (mFocusedWidget != NULL && !mFocusedWidget->hasModalFocus())
99 {
100 focusNone();
101 }
102
103 if (mDraggedWidget != NULL && !mDraggedWidget->hasModalFocus())
104 {
105 dragNone();
106 }
107 }
108
109 void FocusHandler::releaseModalFocus(Widget* widget)
110 {
111 if (mModalFocusedWidget == widget)
112 {
113 mModalFocusedWidget = NULL;
114 }
115 }
116
117 Widget* FocusHandler::getFocused() const
118 {
119 return mFocusedWidget;
120 }
121
122 Widget* FocusHandler::getDragged() const
123 {
124 return mDraggedWidget;
125 }
126
127 Widget* FocusHandler::getModalFocused() const
128 {
129 return mModalFocusedWidget;
130 }
131
132 void FocusHandler::focusNext()
133 {
134 int i;
135 int focusedWidget = -1;
136 for (i = 0; i < (int)mWidgets.size(); ++i)
137 {
138 if (mWidgets[i] == mFocusedWidget)
139 {
140 focusedWidget = i;
141 }
142 }
143 int focused = focusedWidget;
144
145 // i is a counter that ensures that the following loop
146 // won't get stuck in an infinite loop
147 i = (int)mWidgets.size();
148 do
149 {
150 ++focusedWidget;
151
152 if (i==0)
153 {
154 focusedWidget = -1;
155 break;
156 }
157
158 --i;
159
160 if (focusedWidget >= (int)mWidgets.size())
161 {
162 focusedWidget = 0;
163 }
164
165 if (focusedWidget == focused)
166 {
167 return;
168 }
169 }
170 while (!mWidgets.at(focusedWidget)->isFocusable());
171
172 if (focusedWidget >= 0)
173 {
174 mFocusedWidget = mWidgets.at(focusedWidget);
175 mWidgets.at(focusedWidget)->gotFocus();
176 }
177
178 if (focused >= 0)
179 {
180 mWidgets.at(focused)->lostFocus();
181 }
182 }
183
184 void FocusHandler::focusPrevious()
185 {
186 if (mWidgets.size() == 0)
187 {
188 mFocusedWidget = NULL;
189 return;
190 }
191
192 int i;
193 int focusedWidget = -1;
194 for (i = 0; i < (int)mWidgets.size(); ++i)
195 {
196 if (mWidgets[i] == mFocusedWidget)
197 {
198 focusedWidget = i;
199 }
200 }
201 int focused = focusedWidget;
202
203 // i is a counter that ensures that the following loop
204 // won't get stuck in an infinite loop
205 i = (int)mWidgets.size();
206 do
207 {
208 --focusedWidget;
209
210 if (i==0)
211 {
212 focusedWidget = -1;
213 break;
214 }
215
216 --i;
217
218 if (focusedWidget <= 0)
219 {
220 focusedWidget = mWidgets.size() - 1;
221 }
222
223 if (focusedWidget == focused)
224 {
225 return;
226 }
227 }
228 while (!mWidgets.at(focusedWidget)->isFocusable());
229
230 if (focusedWidget >= 0)
231 {
232 mFocusedWidget = mWidgets.at(focusedWidget);
233 mWidgets.at(focusedWidget)->gotFocus();
234 }
235
236 if (focused >= 0)
237 {
238 mWidgets.at(focused)->lostFocus();
239 }
240 }
241
242 bool FocusHandler::hasFocus(const Widget* widget) const
243 {
244 return mFocusedWidget == widget;
245 }
246
247 bool FocusHandler::isDragged(const Widget* widget) const
248 {
249 return mDraggedWidget == widget;
250 }
251
252 void FocusHandler::add(Widget* widget)
253 {
254 mWidgets.push_back(widget);
255 }
256
257 void FocusHandler::remove(Widget* widget)
258 {
259 if (widget == mFocusedWidget)
260 {
261 mFocusedWidget = NULL;
262 }
263 if (widget == mDraggedWidget)
264 {
265 mDraggedWidget = NULL;
266 }
267 if (widget == mToBeFocused)
268 {
269 mToBeFocused = NULL;
270 }
271 if (widget == mToBeDragged)
272 {
273 mToBeDragged = NULL;
274 }
275
276 if (hasFocus(widget))
277 {
278 mFocusedWidget = NULL;
279 mToBeFocused = NULL;
280 }
281
282 int i = 0;
283 WidgetIterator iter;
284
285 for (iter = mWidgets.begin(); iter != mWidgets.end(); ++iter)
286 {
287 ++i;
288
289 if ((*iter) == widget)
290 {
291 mWidgets.erase(iter);
292 return;
293 }
294 }
295 }
296
297 void FocusHandler::focusNone()
298 {
299
300 if (mFocusedWidget != NULL)
301 {
302 Widget* focused = mFocusedWidget;
303 mFocusedWidget = NULL;
304 focused->lostFocus();
305 }
306
307 mToBeFocused = NULL;
308 }
309
310 void FocusHandler::dragNone()
311 {
312 mDraggedWidget = NULL;
313 }
314
315 void FocusHandler::checkHotKey(const KeyInput &keyInput)
316 {
317 int keyin = keyInput.getKey().getValue();
318
319 for (int i = 0; i < (int)mWidgets.size(); ++i)
320 {
321 int hotKey = mWidgets[i]->getHotKey();
322
323 if (hotKey == 0)
324 {
325 continue;
326 }
327
328 if ((isascii(keyin) && tolower(keyin) == hotKey) || keyin == hotKey)
329 {
330 if (keyInput.getType() == KeyInput::PRESS)
331 {
332 mWidgets[i]->hotKeyPress();
333 if (mWidgets[i]->isFocusable())
334 {
335 this->requestFocus(mWidgets[i]);
336 }
337 }
338 else
339 {
340 mWidgets[i]->hotKeyRelease();
341 }
342 break;
343 }
344 }
345 }
346
347 void FocusHandler::tabNext()
348 {
349 if (mFocusedWidget != NULL)
350 {
351 if (!mFocusedWidget->isTabOutEnabled())
352 {
353 return;
354 }
355 }
356
357 if (mWidgets.size() == 0)
358 {
359 mFocusedWidget = NULL;
360 return;
361 }
362
363 int i;
364 int focusedWidget = -1;
365 for (i = 0; i < (int)mWidgets.size(); ++i)
366 {
367 if (mWidgets[i] == mFocusedWidget)
368 {
369 focusedWidget = i;
370 }
371 }
372 int focused = focusedWidget;
373 bool done = false;
374
375 // i is a counter that ensures that the following loop
376 // won't get stuck in an infinite loop
377 i = (int)mWidgets.size();
378 do
379 {
380 ++focusedWidget;
381
382 if (i==0)
383 {
384 focusedWidget = -1;
385 break;
386 }
387
388 --i;
389
390 if (focusedWidget >= (int)mWidgets.size())
391 {
392 focusedWidget = 0;
393 }
394
395 if (focusedWidget == focused)
396 {
397 return;
398 }
399
400 if (mWidgets.at(focusedWidget)->isFocusable() &&
401 mWidgets.at(focusedWidget)->isTabInEnabled() &&
402 (mModalFocusedWidget == NULL ||
403 mWidgets.at(focusedWidget)->hasModalFocus()))
404 {
405 done = true;
406 }
407 }
408 while (!done);
409
410 if (focusedWidget >= 0)
411 {
412 mFocusedWidget = mWidgets.at(focusedWidget);
413 mWidgets.at(focusedWidget)->gotFocus();
414 }
415
416 if (focused >= 0)
417 {
418 mWidgets.at(focused)->lostFocus();
419 }
420 }
421
422 void FocusHandler::tabPrevious()
423 {
424 if (mFocusedWidget != NULL)
425 {
426 if (!mFocusedWidget->isTabOutEnabled())
427 {
428 return;
429 }
430 }
431
432 if (mWidgets.size() == 0)
433 {
434 mFocusedWidget = NULL;
435 return;
436 }
437
438 int i;
439 int focusedWidget = -1;
440 for (i = 0; i < (int)mWidgets.size(); ++i)
441 {
442 if (mWidgets[i] == mFocusedWidget)
443 {
444 focusedWidget = i;
445 }
446 }
447 int focused = focusedWidget;
448 bool done = false;
449
450 // i is a counter that ensures that the following loop
451 // won't get stuck in an infinite loop
452 i = (int)mWidgets.size();
453 do
454 {
455 --focusedWidget;
456
457 if (i==0)
458 {
459 focusedWidget = -1;
460 break;
461 }
462
463 --i;
464
465 if (focusedWidget <= 0)
466 {
467 focusedWidget = mWidgets.size() - 1;
468 }
469
470 if (focusedWidget == focused)
471 {
472 return;
473 }
474
475 if (mWidgets.at(focusedWidget)->isFocusable() &&
476 mWidgets.at(focusedWidget)->isTabInEnabled() &&
477 (mModalFocusedWidget == NULL ||
478 mWidgets.at(focusedWidget)->hasModalFocus()))
479 {
480 done = true;
481 }
482 }
483 while (!done);
484
485 if (focusedWidget >= 0)
486 {
487 mFocusedWidget = mWidgets.at(focusedWidget);
488 mWidgets.at(focusedWidget)->gotFocus();
489 }
490
491 if (focused >= 0)
492 {
493 mWidgets.at(focused)->lostFocus();
494 }
495 }
496
497 void FocusHandler::applyChanges()
498 {
499 if (mToBeFocused != NULL)
500 {
501 unsigned int i = 0;
502 int toBeFocusedIndex = -1;
503 for (i = 0; i < mWidgets.size(); ++i)
504 {
505 if (mWidgets[i] == mToBeFocused)
506 {
507 toBeFocusedIndex = i;
508 break;
509 }
510 }
511
512 if (toBeFocusedIndex < 0)
513 {
514 throw GCN_EXCEPTION("Trying to focus a none existing widget.");
515 }
516
517 Widget *oldFocused = mFocusedWidget;
518
519 if (oldFocused != mToBeFocused)
520 {
521 mFocusedWidget = mWidgets.at(toBeFocusedIndex);
522
523 if (oldFocused != NULL)
524 {
525 oldFocused->lostFocus();
526 }
527
528 mWidgets.at(toBeFocusedIndex)->gotFocus();
529 }
530 mToBeFocused = NULL;
531 }
532
533 if (mToBeDragged != NULL)
534 {
535 unsigned int i = 0;
536 int toBeDraggedIndex = -1;
537 for (i = 0; i < mWidgets.size(); ++i)
538 {
539 if (mWidgets[i] == mToBeDragged)
540 {
541 toBeDraggedIndex = i;
542 break;
543 }
544 }
545
546 if (toBeDraggedIndex < 0)
547 {
548 throw GCN_EXCEPTION("Trying to give drag to a none existing widget");
549 }
550
551 mDraggedWidget = mWidgets.at(toBeDraggedIndex);
552 mToBeDragged = NULL;
553 }
554 }
555 }