GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/focushandler.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_FOCUSHANDLER_H
68
#define GUI_FOCUSHANDLER_H
69
70
#include "utils/vector.h"
71
72
#include <list>
73
74
#include "localconsts.h"
75
76
class Event;
77
class Widget;
78
79
/**
80
 * The focus handler. This focus handler does exactly the same as the Guichan
81
 * focus handler, but keeps a stack of modal widgets to be able to handle
82
 * multiple modal focus requests.
83
 */
84
762
class FocusHandler final
85
{
86
    public:
87
        FocusHandler();
88
89
        A_DELETE_COPY(FocusHandler)
90
91
        /**
92
          * Requests focus for a widget. Focus will only be granted to a widget
93
          * if it's focusable and if no other widget has modal focus.
94
          * If a widget receives focus a focus event will be sent to the
95
          * focus listeners of the widget.
96
          *
97
          * @param widget The widget to request focus for.
98
          * @see isFocused, Widget::requestFocus
99
          */
100
        void requestFocus(const Widget *const widget);
101
102
        /**
103
          * Requests modal focus for a widget. Focus will only be granted
104
          * to a widget if it's focusable and if no other widget has modal
105
          * focus.
106
          *
107
          * @param widget The widget to request modal focus for.
108
          * @throws Exception when another widget already has modal focus.
109
          * @see releaseModalFocus, Widget::requestModalFocus
110
          */
111
        void requestModalFocus(Widget *const widget);
112
113
        /**
114
          * Requests modal mouse input focus for a widget. Focus will only
115
          * be granted to a widget if it's focusable and if no other widget
116
          * has modal mouse input focus.
117
          *
118
          * Modal mouse input focus means no other widget then the widget with
119
          * modal mouse input focus will receive mouse input. The widget with
120
          * modal mouse input focus will also receive mouse input no matter what
121
          * the mouse input is or where the mouse input occurs.
122
          *
123
          * @param widget The widget to focus for modal mouse input focus.
124
          * @throws Exception when another widget already has modal mouse input
125
          *         focus.
126
          * @see releaseModalMouseInputFocus, Widget::requestModalMouseInputFocus
127
          */
128
        void requestModalMouseInputFocus(Widget *const widget);
129
130
        /**
131
          * Releases modal focus if the widget has modal focus.
132
          * If the widget doesn't have modal focus no relase will occur.
133
          *
134
          * @param widget The widget to release modal focus for.
135
          * @see reuqestModalFocus, Widget::releaseModalFocus
136
          */
137
        void releaseModalFocus(Widget *const widget);
138
139
        /**
140
          * Releases modal mouse input focus if the widget has modal mouse input
141
          * focus. If the widget doesn't have modal mouse input focus no relase
142
          * will occur.
143
          *
144
          * @param widget the widget to release modal mouse input focus for.
145
          * @see requestModalMouseInputFocus, Widget::releaseModalMouseInputFocus
146
          */
147
        void releaseModalMouseInputFocus(const Widget *const widget);
148
149
        /**
150
          * Checks if a widget is focused.
151
          *
152
          * @param widget The widget to check.
153
          * @return True if the widget is focused, false otherwise.
154
          * @see Widget::isFocused
155
          */
156
        bool isFocused(const Widget *const widget) const;
157
158
        /**
159
          * Gets the widget with focus.
160
          *
161
          * @return The widget with focus. NULL if no widget has focus.
162
          */
163
        Widget* getFocused() const A_WARN_UNUSED;
164
165
        /**
166
          * Gets the widget with modal focus.
167
          *
168
          * @return The widget with modal focus. NULL if no widget has
169
          *         modal focus.
170
          */
171
        Widget* getModalFocused() const A_WARN_UNUSED;
172
173
        /**
174
          * Gets the widget with modal mouse input focus.
175
          *
176
          * @return The widget with modal mouse input focus. NULL if
177
          *         no widget has modal mouse input focus.
178
          */
179
        Widget* getModalMouseInputFocused() const A_WARN_UNUSED;
180
181
        /**
182
          * Focuses the next widget added to a conainer.
183
          * If no widget has focus the first widget gets focus. The order
184
          * in which the widgets are focused is determined by the order
185
          * they were added to a container.
186
          *
187
          * @see focusPrevious
188
          */
189
        void focusNext();
190
191
        /**
192
          * Focuses the previous widget added to a contaienr.
193
          * If no widget has focus the first widget gets focus. The order
194
          * in which the widgets are focused is determined by the order
195
          * they were added to a container.
196
          *
197
          * @see focusNext
198
          */
199
        void focusPrevious();
200
201
        /**
202
          * Adds a widget to by handles by the focus handler.
203
          *
204
          * @param widget The widget to add.
205
          * @see remove
206
          */
207
        void add(Widget *const widget);
208
209
        /**
210
          * Removes a widget from the focus handler.
211
          *
212
          * @param widget The widget to remove.
213
          * @see add
214
          */
215
        void remove(Widget *const widget);
216
217
        /**
218
          * Focuses nothing. A focus event will also be sent to the
219
          * focused widget's focus listeners if a widget has focus.
220
          */
221
        void focusNone();
222
223
        /**
224
          * Focuses the next widget which allows tabbing in unless
225
          * the current focused Widget disallows tabbing out.
226
          *
227
          * @see tabPrevious
228
          */
229
        void tabNext();
230
231
        /**
232
          * Focuses the previous widget which allows tabbing in unless
233
          * current focused widget disallows tabbing out.
234
          *
235
          * @see tabNext
236
          */
237
        void tabPrevious();
238
239
        /**
240
          * Gets the widget being dragged. Used by the Gui class to
241
          * keep track of the dragged widget.
242
          *
243
          * @return the widget being dragged.
244
          * @see setDraggedWidget
245
          */
246
        Widget* getDraggedWidget() const A_WARN_UNUSED;
247
248
        /**
249
          * Sets the widget being dragged. Used by the Gui class to
250
          * keep track of the dragged widget.
251
          *
252
          * @param draggedWidget The widget being dragged.
253
          * @see getDraggedWidget
254
          */
255
        void setDraggedWidget(Widget *const draggedWidget);
256
257
        /**
258
          * Gets the last widget with the mouse. Used by the Gui class
259
          * to keep track the last widget with the mouse.
260
          *
261
          * @return The last widget with the mouse.
262
          * @see setLastWidgetWithMouse
263
          */
264
        Widget* getLastWidgetWithMouse() const A_WARN_UNUSED;
265
266
        /**
267
          * Sets the last widget with the mouse. Used by the Gui class
268
          * to keep track the last widget with the mouse.
269
          *
270
          * @param lastWidgetWithMouse The last widget with the mouse.
271
          * @see getLastWidgetWithMouse
272
          */
273
        void setLastWidgetWithMouse(Widget *const lastWidgetWithMouse);
274
275
        /**
276
          * Gets the last widget with modal focus.
277
          *
278
          * @return The last widget with modal focus.
279
          * @see setLastWidgetWithModalFocus
280
          */
281
        Widget* getLastWidgetWithModalFocus() const A_WARN_UNUSED;
282
283
        /**
284
          * Sets the last widget with modal focus.
285
          *
286
          * @param widget The last widget with modal focus.
287
          * @see getLastWidgetWithModalFocus
288
          */
289
        void setLastWidgetWithModalFocus(Widget *const widget);
290
291
        /**
292
          * Gets the last widget with modal mouse input focus.
293
          *
294
          * @return The last widget with modal mouse input focus.
295
          * @see setLastWidgetWithModalMouseInputFocus
296
          */
297
        Widget* getLastWidgetWithModalMouseInputFocus() const A_WARN_UNUSED;
298
299
        /**
300
          * Sets the last widget with modal mouse input focus.
301
          *
302
          * @param widget The last widget with modal mouse input focus.
303
          * @see getLastWidgetWithModalMouseInputFocus
304
          */
305
        void setLastWidgetWithModalMouseInputFocus(Widget *const widget);
306
307
        /**
308
          * Gets the last widget pressed. Used by the Gui class to keep track
309
          * of pressed widgets.
310
          *
311
          * @return The last widget pressed.
312
          * @see setLastWidgetPressed
313
          */
314
        Widget* getLastWidgetPressed() const A_WARN_UNUSED;
315
316
        /**
317
          * Sets the last widget pressed. Used by the Gui class to keep track
318
          * of pressed widgets.
319
          *
320
          * @param lastWidgetPressed The last widget pressed.
321
          * @see getLastWidgetPressed
322
          */
323
        void setLastWidgetPressed(Widget *const lastWidgetPressed);
324
325
    private:
326
        /**
327
         * Checks to see if the widget tabbed to is in a window, and if it is,
328
         * it requests the window be moved to the top.
329
         */
330
        void checkForWindow() const;
331
332
        /**
333
         * Distributes a focus lost event.
334
         *
335
         * @param focusEvent the event to distribute.
336
         */
337
        static void distributeFocusLostEvent(const Event& focusEvent);
338
339
        /**
340
         * Distributes a focus gained event.
341
         *
342
         * @param focusEvent the event to distribute.
343
         */
344
        static void distributeFocusGainedEvent(const Event& focusEvent);
345
346
        /**
347
         * Typedef.
348
         */
349
        typedef STD_VECTOR<Widget*> WidgetVector;
350
351
        /**
352
         * Typedef.
353
         */
354
        typedef WidgetVector::iterator WidgetIterator;
355
356
        /**
357
         * Holds the widgets currently being handled by the
358
         * focus handler.
359
         */
360
        WidgetVector mWidgets;
361
362
        /**
363
         * Holds the focused widget. NULL if no widget has focus.
364
         */
365
        Widget* mFocusedWidget;
366
367
        /**
368
         * Holds the modal focused widget. NULL if no widget has
369
         * modal focused.
370
         */
371
        Widget* mModalFocusedWidget;
372
373
        /**
374
         * Holds the modal mouse input focused widget. NULL if no widget
375
         * is being dragged.
376
         */
377
        Widget* mModalMouseInputFocusedWidget;
378
379
        /**
380
         * Holds the dragged widget. NULL if no widget is
381
         * being dragged.
382
         */
383
        Widget* mDraggedWidget;
384
385
        /**
386
         * Holds the last widget with the mouse.
387
         */
388
        Widget* mLastWidgetWithMouse;
389
390
        /**
391
         * Holds the last widget with modal focus.
392
         */
393
        Widget* mLastWidgetWithModalFocus;
394
395
        /**
396
         * Holds the last widget with modal mouse input focus.
397
         */
398
        Widget* mLastWidgetWithModalMouseInputFocus;
399
400
        /**
401
         * Holds the last widget pressed.
402
         */
403
        Widget* mLastWidgetPressed;
404
405
        /**
406
         * Stack of widgets that have requested modal forcus.
407
         */
408
        std::list<Widget*> mModalStack;
409
};
410
411
#endif  // GUI_FOCUSHANDLER_H