GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/focushandler.h Lines: 1 1 100.0 %
Date: 2017-11-29 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-2017  The ManaPlus Developers
6
 *
7
 *  This file is part of The ManaPlus Client.
8
 *
9
 *  This program is free software; you can redistribute it and/or modify
10
 *  it under the terms of the GNU General Public License as published by
11
 *  the Free Software Foundation; either version 2 of the License, or
12
 *  any later version.
13
 *
14
 *  This program is distributed in the hope that it will be useful,
15
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 *  GNU General Public License for more details.
18
 *
19
 *  You should have received a copy of the GNU General Public License
20
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 */
22
23
/*      _______   __   __   __   ______   __   __   _______   __   __
24
 *     / _____/\ / /\ / /\ / /\ / ____/\ / /\ / /\ / ___  /\ /  |\/ /\
25
 *    / /\____\// / // / // / // /\___\// /_// / // /\_/ / // , |/ / /
26
 *   / / /__   / / // / // / // / /    / ___  / // ___  / // /| ' / /
27
 *  / /_// /\ / /_// / // / // /_/_   / / // / // /\_/ / // / |  / /
28
 * /______/ //______/ //_/ //_____/\ /_/ //_/ //_/ //_/ //_/ /|_/ /
29
 * \______\/ \______\/ \_\/ \_____\/ \_\/ \_\/ \_\/ \_\/ \_\/ \_\/
30
 *
31
 * Copyright (c) 2004 - 2008 Olof Naessén and Per Larsson
32
 *
33
 *
34
 * Per Larsson a.k.a finalman
35
 * Olof Naessén a.k.a jansem/yakslem
36
 *
37
 * Visit: http://guichan.sourceforge.net
38
 *
39
 * License: (BSD)
40
 * Redistribution and use in source and binary forms, with or without
41
 * modification, are permitted provided that the following conditions
42
 * are met:
43
 * 1. Redistributions of source code must retain the above copyright
44
 *    notice, this list of conditions and the following disclaimer.
45
 * 2. Redistributions in binary form must reproduce the above copyright
46
 *    notice, this list of conditions and the following disclaimer in
47
 *    the documentation and/or other materials provided with the
48
 *    distribution.
49
 * 3. Neither the name of Guichan nor the names of its contributors may
50
 *    be used to endorse or promote products derived from this software
51
 *    without specific prior written permission.
52
 *
53
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
54
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
55
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
56
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
57
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
58
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
59
 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
60
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
61
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
62
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
63
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
64
 */
65
66
#ifndef GUI_FOCUSHANDLER_H
67
#define GUI_FOCUSHANDLER_H
68
69
#include "utils/vector.h"
70
71
#include <list>
72
73
#include "localconsts.h"
74
75
class Event;
76
class Widget;
77
78
/**
79
 * The focus handler. This focus handler does exactly the same as the Guichan
80
 * focus handler, but keeps a stack of modal widgets to be able to handle
81
 * multiple modal focus requests.
82
 */
83
1488
class FocusHandler final
84
{
85
    public:
86
        FocusHandler();
87
88
        A_DELETE_COPY(FocusHandler)
89
90
        /**
91
          * Requests focus for a widget. Focus will only be granted to a widget
92
          * if it's focusable and if no other widget has modal focus.
93
          * If a widget receives focus a focus event will be sent to the
94
          * focus listeners of the widget.
95
          *
96
          * @param widget The widget to request focus for.
97
          * @see isFocused, Widget::requestFocus
98
          */
99
        void requestFocus(const Widget *const widget);
100
101
        /**
102
          * Requests modal focus for a widget. Focus will only be granted
103
          * to a widget if it's focusable and if no other widget has modal
104
          * focus.
105
          *
106
          * @param widget The widget to request modal focus for.
107
          * @throws Exception when another widget already has modal focus.
108
          * @see releaseModalFocus, Widget::requestModalFocus
109
          */
110
        void requestModalFocus(Widget *const widget);
111
112
        /**
113
          * Requests modal mouse input focus for a widget. Focus will only
114
          * be granted to a widget if it's focusable and if no other widget
115
          * has modal mouse input focus.
116
          *
117
          * Modal mouse input focus means no other widget then the widget with
118
          * modal mouse input focus will receive mouse input. The widget with
119
          * modal mouse input focus will also receive mouse input no matter what
120
          * the mouse input is or where the mouse input occurs.
121
          *
122
          * @param widget The widget to focus for modal mouse input focus.
123
          * @throws Exception when another widget already has modal mouse input
124
          *         focus.
125
          * @see releaseModalMouseInputFocus, Widget::requestModalMouseInputFocus
126
          */
127
        void requestModalMouseInputFocus(Widget *const widget);
128
129
        /**
130
          * Releases modal focus if the widget has modal focus.
131
          * If the widget doesn't have modal focus no relase will occur.
132
          *
133
          * @param widget The widget to release modal focus for.
134
          * @see reuqestModalFocus, Widget::releaseModalFocus
135
          */
136
        void releaseModalFocus(Widget *const widget);
137
138
        /**
139
          * Releases modal mouse input focus if the widget has modal mouse input
140
          * focus. If the widget doesn't have modal mouse input focus no relase
141
          * will occur.
142
          *
143
          * @param widget the widget to release modal mouse input focus for.
144
          * @see requestModalMouseInputFocus, Widget::releaseModalMouseInputFocus
145
          */
146
        void releaseModalMouseInputFocus(const Widget *const widget);
147
148
        /**
149
          * Checks if a widget is focused.
150
          *
151
          * @param widget The widget to check.
152
          * @return True if the widget is focused, false otherwise.
153
          * @see Widget::isFocused
154
          */
155
        bool isFocused(const Widget *const widget) const;
156
157
        /**
158
          * Gets the widget with focus.
159
          *
160
          * @return The widget with focus. NULL if no widget has focus.
161
          */
162
        Widget* getFocused() const A_WARN_UNUSED;
163
164
        /**
165
          * Gets the widget with modal focus.
166
          *
167
          * @return The widget with modal focus. NULL if no widget has
168
          *         modal focus.
169
          */
170
        Widget* getModalFocused() const A_WARN_UNUSED;
171
172
        /**
173
          * Gets the widget with modal mouse input focus.
174
          *
175
          * @return The widget with modal mouse input focus. NULL if
176
          *         no widget has modal mouse input focus.
177
          */
178
        Widget* getModalMouseInputFocused() const A_WARN_UNUSED;
179
180
        /**
181
          * Focuses the next widget added to a conainer.
182
          * If no widget has focus the first widget gets focus. The order
183
          * in which the widgets are focused is determined by the order
184
          * they were added to a container.
185
          *
186
          * @see focusPrevious
187
          */
188
        void focusNext();
189
190
        /**
191
          * Focuses the previous widget added to a contaienr.
192
          * If no widget has focus the first widget gets focus. The order
193
          * in which the widgets are focused is determined by the order
194
          * they were added to a container.
195
          *
196
          * @see focusNext
197
          */
198
        void focusPrevious();
199
200
        /**
201
          * Adds a widget to by handles by the focus handler.
202
          *
203
          * @param widget The widget to add.
204
          * @see remove
205
          */
206
        void add(Widget *const widget);
207
208
        /**
209
          * Removes a widget from the focus handler.
210
          *
211
          * @param widget The widget to remove.
212
          * @see add
213
          */
214
        void remove(Widget *const widget);
215
216
        /**
217
          * Focuses nothing. A focus event will also be sent to the
218
          * focused widget's focus listeners if a widget has focus.
219
          */
220
        void focusNone();
221
222
        /**
223
          * Focuses the next widget which allows tabbing in unless
224
          * the current focused Widget disallows tabbing out.
225
          *
226
          * @see tabPrevious
227
          */
228
        void tabNext();
229
230
        /**
231
          * Focuses the previous widget which allows tabbing in unless
232
          * current focused widget disallows tabbing out.
233
          *
234
          * @see tabNext
235
          */
236
        void tabPrevious();
237
238
        /**
239
          * Gets the widget being dragged. Used by the Gui class to
240
          * keep track of the dragged widget.
241
          *
242
          * @return the widget being dragged.
243
          * @see setDraggedWidget
244
          */
245
        Widget* getDraggedWidget() const A_WARN_UNUSED;
246
247
        /**
248
          * Sets the widget being dragged. Used by the Gui class to
249
          * keep track of the dragged widget.
250
          *
251
          * @param draggedWidget The widget being dragged.
252
          * @see getDraggedWidget
253
          */
254
        void setDraggedWidget(Widget *const draggedWidget);
255
256
        /**
257
          * Gets the last widget with the mouse. Used by the Gui class
258
          * to keep track the last widget with the mouse.
259
          *
260
          * @return The last widget with the mouse.
261
          * @see setLastWidgetWithMouse
262
          */
263
        Widget* getLastWidgetWithMouse() const A_WARN_UNUSED;
264
265
        /**
266
          * Sets the last widget with the mouse. Used by the Gui class
267
          * to keep track the last widget with the mouse.
268
          *
269
          * @param lastWidgetWithMouse The last widget with the mouse.
270
          * @see getLastWidgetWithMouse
271
          */
272
        void setLastWidgetWithMouse(Widget *const lastWidgetWithMouse);
273
274
        /**
275
          * Gets the last widget with modal focus.
276
          *
277
          * @return The last widget with modal focus.
278
          * @see setLastWidgetWithModalFocus
279
          */
280
        Widget* getLastWidgetWithModalFocus() const A_WARN_UNUSED;
281
282
        /**
283
          * Sets the last widget with modal focus.
284
          *
285
          * @param widget The last widget with modal focus.
286
          * @see getLastWidgetWithModalFocus
287
          */
288
        void setLastWidgetWithModalFocus(Widget *const widget);
289
290
        /**
291
          * Gets the last widget with modal mouse input focus.
292
          *
293
          * @return The last widget with modal mouse input focus.
294
          * @see setLastWidgetWithModalMouseInputFocus
295
          */
296
        Widget* getLastWidgetWithModalMouseInputFocus() const A_WARN_UNUSED;
297
298
        /**
299
          * Sets the last widget with modal mouse input focus.
300
          *
301
          * @param widget The last widget with modal mouse input focus.
302
          * @see getLastWidgetWithModalMouseInputFocus
303
          */
304
        void setLastWidgetWithModalMouseInputFocus(Widget *const widget);
305
306
        /**
307
          * Gets the last widget pressed. Used by the Gui class to keep track
308
          * of pressed widgets.
309
          *
310
          * @return The last widget pressed.
311
          * @see setLastWidgetPressed
312
          */
313
        Widget* getLastWidgetPressed() const A_WARN_UNUSED;
314
315
        /**
316
          * Sets the last widget pressed. Used by the Gui class to keep track
317
          * of pressed widgets.
318
          *
319
          * @param lastWidgetPressed The last widget pressed.
320
          * @see getLastWidgetPressed
321
          */
322
        void setLastWidgetPressed(Widget *const lastWidgetPressed);
323
324
    private:
325
        /**
326
         * Checks to see if the widget tabbed to is in a window, and if it is,
327
         * it requests the window be moved to the top.
328
         */
329
        void checkForWindow() const;
330
331
        /**
332
         * Distributes a focus lost event.
333
         *
334
         * @param focusEvent the event to distribute.
335
         */
336
        static void distributeFocusLostEvent(const Event& focusEvent);
337
338
        /**
339
         * Distributes a focus gained event.
340
         *
341
         * @param focusEvent the event to distribute.
342
         */
343
        static void distributeFocusGainedEvent(const Event& focusEvent);
344
345
        /**
346
         * Typedef.
347
         */
348
        typedef STD_VECTOR<Widget*> WidgetVector;
349
350
        /**
351
         * Typedef.
352
         */
353
        typedef WidgetVector::iterator WidgetIterator;
354
355
        /**
356
         * Holds the widgets currently being handled by the
357
         * focus handler.
358
         */
359
        WidgetVector mWidgets;
360
361
        /**
362
         * Holds the focused widget. NULL if no widget has focus.
363
         */
364
        Widget* mFocusedWidget;
365
366
        /**
367
         * Holds the modal focused widget. NULL if no widget has
368
         * modal focused.
369
         */
370
        Widget* mModalFocusedWidget;
371
372
        /**
373
         * Holds the modal mouse input focused widget. NULL if no widget
374
         * is being dragged.
375
         */
376
        Widget* mModalMouseInputFocusedWidget;
377
378
        /**
379
         * Holds the dragged widget. NULL if no widget is
380
         * being dragged.
381
         */
382
        Widget* mDraggedWidget;
383
384
        /**
385
         * Holds the last widget with the mouse.
386
         */
387
        Widget* mLastWidgetWithMouse;
388
389
        /**
390
         * Holds the last widget with modal focus.
391
         */
392
        Widget* mLastWidgetWithModalFocus;
393
394
        /**
395
         * Holds the last widget with modal mouse input focus.
396
         */
397
        Widget* mLastWidgetWithModalMouseInputFocus;
398
399
        /**
400
         * Holds the last widget pressed.
401
         */
402
        Widget* mLastWidgetPressed;
403
404
        /**
405
         * Stack of widgets that have requested modal forcus.
406
         */
407
        std::list<Widget*> mModalStack;
408
};
409
410
#endif  // GUI_FOCUSHANDLER_H