ManaPlus
focushandler.cpp
Go to the documentation of this file.
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 #include "gui/focushandler.h"
68 
69 #include "gui/gui.h"
70 
71 #include "gui/widgets/window.h"
72 
74 
75 #include "utils/foreach.h"
76 
77 #include "debug.h"
78 
80  mWidgets(),
81  mFocusedWidget(nullptr),
82  mModalFocusedWidget(nullptr),
83  mModalMouseInputFocusedWidget(nullptr),
84  mDraggedWidget(nullptr),
85  mLastWidgetWithMouse(nullptr),
86  mLastWidgetWithModalFocus(nullptr),
87  mLastWidgetWithModalMouseInputFocus(nullptr),
88  mLastWidgetPressed(nullptr),
89  mModalStack()
90 {
91 }
92 
94 {
95  /* If there is another widget with modal focus, remove its modal focus
96  * and put it on the modal widget stack.
97  */
98  if ((mModalFocusedWidget != nullptr) && mModalFocusedWidget != widget)
99  {
100  mModalStack.push_front(mModalFocusedWidget);
101  mModalFocusedWidget = nullptr;
102  }
103 
104  mModalFocusedWidget = widget;
105  if ((mFocusedWidget != nullptr) && !mFocusedWidget->isModalFocused())
106  focusNone();
107 }
108 
110 {
111  mModalStack.remove(widget);
112 
113  if (mModalFocusedWidget == widget)
114  {
115  mModalFocusedWidget = nullptr;
116 
117  /* Check if there were any previously modal widgets that'd still like
118  * to regain their modal focus.
119  */
120  if (!mModalStack.empty())
121  {
123  mModalStack.pop_front();
124  }
125  }
126 }
127 
128 void FocusHandler::remove(Widget *const widget)
129 {
130  releaseModalFocus(widget);
131 
132  if (isFocused(widget))
133  mFocusedWidget = nullptr;
134 
136  {
137  if ((*iter) == widget)
138  {
139  mWidgets.erase(iter);
140  break;
141  }
142  }
143 
144  if (mDraggedWidget == widget)
145  {
146  mDraggedWidget = nullptr;
147  return;
148  }
149 
150  if (mLastWidgetWithMouse == widget)
151  {
152  mLastWidgetWithMouse = nullptr;
153  return;
154  }
155 
156  if (mLastWidgetWithModalFocus == widget)
157  {
158  mLastWidgetWithModalFocus = nullptr;
159  return;
160  }
161 
163  {
165  return;
166  }
167 
168  if (mLastWidgetPressed == widget)
169  {
170  mLastWidgetPressed = nullptr;
171  return;
172  }
173 }
174 
176 {
177  if (mFocusedWidget != nullptr)
178  {
180  return;
181  }
182 
183  if (mWidgets.empty())
184  {
185  mFocusedWidget = nullptr;
186  return;
187  }
188 
189  int i;
190  int focusedWidget = -1;
191  const int sz = CAST_S32(mWidgets.size());
192  for (i = 0; i < sz; ++ i)
193  {
194  if (mWidgets[i] == mFocusedWidget)
195  focusedWidget = i;
196  }
197  const int focused = focusedWidget;
198  bool done = false;
199 
200  // i is a counter that ensures that the following loop
201  // won't get stuck in an infinite loop
202  i = sz;
203  do
204  {
205  ++ focusedWidget;
206 
207  if (i == 0)
208  {
209  focusedWidget = -1;
210  break;
211  }
212 
213  -- i;
214 
215  if (focusedWidget >= sz)
216  focusedWidget = 0;
217 
218  if (focusedWidget == focused)
219  return;
220 
221  const Widget *const widget = mWidgets.at(focusedWidget);
222  if (widget->isFocusable() && widget->isTabInEnabled() &&
223  ((mModalFocusedWidget == nullptr) || widget->isModalFocused()))
224  {
225  done = true;
226  }
227  }
228  while (!done);
229 
230  if (focusedWidget >= 0)
231  {
232  mFocusedWidget = mWidgets.at(focusedWidget);
233  Event focusEvent(mFocusedWidget);
234  distributeFocusGainedEvent(focusEvent);
235  }
236 
237  if (focused >= 0)
238  {
239  Event focusEvent(mWidgets.at(focused));
240  distributeFocusLostEvent(focusEvent);
241  }
242 
243  checkForWindow();
244 }
245 
247 {
248  if (mFocusedWidget != nullptr)
249  {
251  return;
252  }
253 
254  if (mWidgets.empty())
255  {
256  mFocusedWidget = nullptr;
257  return;
258  }
259 
260  int i;
261  int focusedWidget = -1;
262  const int sz = CAST_S32(mWidgets.size());
263  for (i = 0; i < sz; ++ i)
264  {
265  if (mWidgets[i] == mFocusedWidget)
266  focusedWidget = i;
267  }
268  const int focused = focusedWidget;
269  bool done = false;
270 
271  // i is a counter that ensures that the following loop
272  // won't get stuck in an infinite loop
273  i = sz;
274  do
275  {
276  -- focusedWidget;
277 
278  if (i == 0)
279  {
280  focusedWidget = -1;
281  break;
282  }
283 
284  -- i;
285 
286  if (focusedWidget <= 0)
287  focusedWidget = sz - 1;
288 
289  if (focusedWidget == focused)
290  return;
291 
292  const Widget *const widget = mWidgets.at(focusedWidget);
293  if (widget->isFocusable() && widget->isTabInEnabled() &&
294  ((mModalFocusedWidget == nullptr) || widget->isModalFocused()))
295  {
296  done = true;
297  }
298  }
299  while (!done);
300 
301  if (focusedWidget >= 0)
302  {
303  mFocusedWidget = mWidgets.at(focusedWidget);
304  Event focusEvent(mFocusedWidget);
305  distributeFocusGainedEvent(focusEvent);
306  }
307 
308  if (focused >= 0)
309  {
310  Event focusEvent(mWidgets.at(focused));
311  distributeFocusLostEvent(focusEvent);
312  }
313 
314  checkForWindow();
315 }
316 
318 {
319  if (mFocusedWidget != nullptr)
320  {
321  Widget *widget = mFocusedWidget->getParent();
322 
323  while (widget != nullptr)
324  {
325  Window *const window = dynamic_cast<Window*>(widget);
326 
327  if (window != nullptr)
328  {
329  window->requestMoveToTop();
330  break;
331  }
332 
333  widget = widget->getParent();
334  }
335  }
336 }
337 
339 {
340  if (gui != nullptr)
342 
343  const Widget *const sourceWidget = focusEvent.getSource();
344 
345  if (sourceWidget == nullptr)
346  return;
347  std::list<FocusListener*> focusListeners
348  = sourceWidget->getFocusListeners();
349 
350  // Send the event to all focus listeners of the widget.
351  for (std::list<FocusListener*>::const_iterator
352  it = focusListeners.begin();
353  it != focusListeners.end();
354  ++ it)
355  {
356  (*it)->focusGained(focusEvent);
357  }
358 }
359 
360 void FocusHandler::requestFocus(const Widget *const widget)
361 {
362  if ((widget == nullptr) || widget == mFocusedWidget)
363  return;
364 
365  int toBeFocusedIndex = -1;
366  for (unsigned int i = 0, fsz = CAST_U32(
367  mWidgets.size()); i < fsz; ++i)
368  {
369  if (mWidgets[i] == widget)
370  {
371  toBeFocusedIndex = i;
372  break;
373  }
374  }
375 
376  if (toBeFocusedIndex < 0)
377  return;
378 
379  Widget *const oldFocused = mFocusedWidget;
380 
381  if (oldFocused != widget)
382  {
383  mFocusedWidget = mWidgets.at(toBeFocusedIndex);
384 
385  Event focusEvent(mFocusedWidget);
386  distributeFocusGainedEvent(focusEvent);
387 
388  if (oldFocused != nullptr)
389  {
390  Event oldFocusEvent(oldFocused);
391  distributeFocusLostEvent(oldFocusEvent);
392  }
393  }
394 }
395 
397 {
398  if ((mModalMouseInputFocusedWidget != nullptr)
399  && mModalMouseInputFocusedWidget != widget)
400  {
401  return;
402  }
403 
405 }
406 
408 {
409  if (mModalMouseInputFocusedWidget == widget)
411 }
412 
414 {
415  return mFocusedWidget;
416 }
417 
419 {
420  return mModalFocusedWidget;
421 }
422 
424 {
426 }
427 
429 {
430  int i;
431  int focusedWidget = -1;
432  const int sz = CAST_S32(mWidgets.size());
433  for (i = 0; i < sz; ++i)
434  {
435  if (mWidgets[i] == mFocusedWidget)
436  focusedWidget = i;
437  }
438  const int focused = focusedWidget;
439 
440  // i is a counter that ensures that the following loop
441  // won't get stuck in an infinite loop
442  i = sz;
443  do
444  {
445  ++ focusedWidget;
446 
447  if (i == 0)
448  {
449  focusedWidget = -1;
450  break;
451  }
452 
453  -- i;
454 
455  if (focusedWidget >= sz)
456  focusedWidget = 0;
457 
458  if (focusedWidget == focused)
459  return;
460  }
461  while (!mWidgets.at(focusedWidget)->isFocusable());
462 
463  if (focusedWidget >= 0)
464  {
465  mFocusedWidget = mWidgets.at(focusedWidget);
466 
467  Event focusEvent(mFocusedWidget);
468  distributeFocusGainedEvent(focusEvent);
469  }
470 
471  if (focused >= 0)
472  {
473  Event focusEvent(mWidgets.at(focused));
474  distributeFocusLostEvent(focusEvent);
475  }
476 }
477 
479 {
480  if (mWidgets.empty())
481  {
482  mFocusedWidget = nullptr;
483  return;
484  }
485 
486  int i;
487  int focusedWidget = -1;
488  const int sz = CAST_S32(mWidgets.size());
489  for (i = 0; i < sz; ++ i)
490  {
491  if (mWidgets[i] == mFocusedWidget)
492  focusedWidget = i;
493  }
494  const int focused = focusedWidget;
495 
496  // i is a counter that ensures that the following loop
497  // won't get stuck in an infinite loop
498  i = sz;
499  do
500  {
501  -- focusedWidget;
502 
503  if (i == 0)
504  {
505  focusedWidget = -1;
506  break;
507  }
508 
509  -- i;
510 
511  if (focusedWidget <= 0)
512  focusedWidget = sz - 1;
513 
514  if (focusedWidget == focused)
515  return;
516  }
517  while (!mWidgets.at(focusedWidget)->isFocusable());
518 
519  if (focusedWidget >= 0)
520  {
521  mFocusedWidget = mWidgets.at(focusedWidget);
522  Event focusEvent(mFocusedWidget);
523  distributeFocusGainedEvent(focusEvent);
524  }
525 
526  if (focused >= 0)
527  {
528  Event focusEvent(mWidgets.at(focused));
529  distributeFocusLostEvent(focusEvent);
530  }
531 }
532 
533 bool FocusHandler::isFocused(const Widget *const widget) const
534 {
535  return mFocusedWidget == widget;
536 }
537 
538 void FocusHandler::add(Widget *const widget)
539 {
540  mWidgets.push_back(widget);
541 }
542 
544 {
545  if (mFocusedWidget != nullptr)
546  {
547  Widget *const focused = mFocusedWidget;
548  mFocusedWidget = nullptr;
549 
550  Event focusEvent(focused);
551  distributeFocusLostEvent(focusEvent);
552  }
553 }
554 
556 {
557  const Widget *const sourceWidget = focusEvent.getSource();
558  if (sourceWidget == nullptr)
559  return;
560 
561  std::list<FocusListener*> focusListeners
562  = sourceWidget->getFocusListeners();
563 
564  // Send the event to all focus listeners of the widget.
565  for (std::list<FocusListener*>::const_iterator
566  it = focusListeners.begin();
567  it != focusListeners.end();
568  ++ it)
569  {
570  (*it)->focusLost(focusEvent);
571  }
572 }
573 
575 {
576  return mDraggedWidget;
577 }
578 
579 void FocusHandler::setDraggedWidget(Widget *const draggedWidget)
580 {
581  mDraggedWidget = draggedWidget;
582 }
583 
585 {
586  return mLastWidgetWithMouse;
587 }
588 
589 void FocusHandler::setLastWidgetWithMouse(Widget *const lastWidgetWithMouse)
590 {
591  mLastWidgetWithMouse = lastWidgetWithMouse;
592 }
593 
595 {
597 }
598 
600 {
601  mLastWidgetWithModalFocus = widget;
602 }
603 
605 {
607 }
608 
610 {
612 }
613 
615 {
616  return mLastWidgetPressed;
617 }
618 
619 void FocusHandler::setLastWidgetPressed(Widget *const lastWidgetPressed)
620 {
621  mLastWidgetPressed = lastWidgetPressed;
622 }
#define CAST_S32
Definition: cast.h:30
#define CAST_U32
Definition: cast.h:31
Definition: event.h:79
Widget * getSource() const
Definition: event.h:104
static void distributeFocusGainedEvent(const Event &focusEvent)
void setLastWidgetWithModalFocus(Widget *const widget)
static void distributeFocusLostEvent(const Event &focusEvent)
void add(Widget *const widget)
void checkForWindow() const
void requestModalFocus(Widget *const widget)
Widget * mLastWidgetWithMouse
Definition: focushandler.h:388
void tabPrevious()
Widget * mLastWidgetWithModalFocus
Definition: focushandler.h:393
Widget * getLastWidgetWithModalMouseInputFocus() const
Widget * getModalMouseInputFocused() const
Widget * mFocusedWidget
Definition: focushandler.h:365
Widget * getDraggedWidget() const
Widget * mModalMouseInputFocusedWidget
Definition: focushandler.h:377
void requestModalMouseInputFocus(Widget *const widget)
void requestFocus(const Widget *const widget)
Widget * mModalFocusedWidget
Definition: focushandler.h:371
void setLastWidgetWithMouse(Widget *const lastWidgetWithMouse)
Widget * getFocused() const
void releaseModalMouseInputFocus(const Widget *const widget)
Widget * getLastWidgetPressed() const
Widget * mLastWidgetPressed
Definition: focushandler.h:403
std::list< Widget * > mModalStack
Definition: focushandler.h:408
WidgetVector::iterator WidgetIterator
Definition: focushandler.h:354
void releaseModalFocus(Widget *const widget)
Widget * mDraggedWidget
Definition: focushandler.h:383
Widget * mLastWidgetWithModalMouseInputFocus
Definition: focushandler.h:398
void remove(Widget *const widget)
void focusPrevious()
void setLastWidgetWithModalMouseInputFocus(Widget *const widget)
void setDraggedWidget(Widget *const draggedWidget)
Widget * getLastWidgetWithMouse() const
WidgetVector mWidgets
Definition: focushandler.h:360
Widget * getModalFocused() const
bool isFocused(const Widget *const widget) const
Widget * getLastWidgetWithModalFocus() const
void setLastWidgetPressed(Widget *const lastWidgetPressed)
void distributeGlobalFocusGainedEvent(const Event &focusEvent)
Definition: gui.cpp:1152
Definition: widget.h:99
virtual void requestMoveToTop()
Definition: widget.cpp:213
bool isFocusable() const
Definition: widget.cpp:199
bool isTabInEnabled() const
Definition: widget.h:688
bool isTabOutEnabled() const
Definition: widget.h:712
virtual bool isModalFocused() const
Definition: widget.cpp:408
const std::list< FocusListener * > & getFocusListeners() const A_CONST
Definition: widget.cpp:446
Widget * getParent() const
Definition: widget.h:202
Definition: window.h:102
#define FOR_EACH(type, iter, array)
Definition: foreach.h:25
Gui * gui
Definition: gui.cpp:111
#define nullptr
Definition: localconsts.h:45