-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
widget.h
150 lines (122 loc) · 4.2 KB
/
widget.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/** @file widget.h Base class for widgets.
*
* @defgroup widgets Widget Framework
*
* @authors Copyright © 2013 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details. You should have received a copy of the GNU
* General Public License along with this program; if not, see:
* http://www.gnu.org/licenses</small>
*/
#ifndef LIBDENG2_WIDGET_H
#define LIBDENG2_WIDGET_H
#include "../String"
#include "../Event"
#include "../Id"
namespace de {
class Widget;
class RootWidget;
typedef QList<Widget *> WidgetList;
/**
* Base class for widgets.
* @ingroup widgets
*/
class DENG2_PUBLIC Widget
{
public:
/// Widget that was expected to exist was not found. @ingroup errors
DENG2_ERROR(NotFoundError);
enum Behavior
{
/// Widget is invisible: not drawn. Hidden widgets also receive no events.
Hidden = 0x1,
/// Widget is disabled. The meaning of this is Widget-specific. The
/// Widget will not be given events.
Disabled = 0x2,
/// Widget will only receive events if it has focus.
HandleEventsOnlyWhenFocused = 0x4,
DefaultBehavior = 0
};
Q_DECLARE_FLAGS(Behaviors, Behavior)
typedef WidgetList Children;
public:
Widget(String const &name = "");
virtual ~Widget();
/**
* Returns the automatically generated, unique identifier of the widget.
*/
Id id() const;
String name() const;
void setName(String const &name);
bool hasRoot() const;
RootWidget &root() const;
bool hasFocus() const;
void show(bool doShow = true);
inline void hide() { show(false); }
void enable(bool yes = true) { setBehavior(Disabled, yes? UnsetFlags : SetFlags); }
void disable(bool yes = true) { setBehavior(Disabled, yes? SetFlags : UnsetFlags); }
bool isHidden() const;
inline bool isEnabled() const { return !behavior().testFlag(Disabled); }
inline bool isDisabled() const { return behavior().testFlag(Disabled); }
/**
* Sets or clears one or more behavior flags.
*
* @param behavior Flags.
* @param set @c true to set, @c false to clear.
*/
void setBehavior(Behaviors behavior, FlagOp operation = SetFlags);
Behaviors behavior() const;
/**
* Sets the identifier of the widget that will receive focus when
* a forward focus navigation is requested.
*
* @param name Name of a widget for forwards navigation.
*/
void setFocusNext(String const &name);
/**
* Sets the identifier of the widget that will receive focus when
* a backwards focus navigation is requested.
*
* @param name Name of a widget for backwards navigation.
*/
void setFocusPrev(String const &name);
String focusNext() const;
String focusPrev() const;
// Tree organization.
void clear();
Widget &add(Widget *child);
Widget *remove(Widget &child);
Widget *find(String const &name);
Widget const *find(String const &name) const;
Children children() const;
Widget *parent() const;
// Utilities.
String uniqueName(String const &name) const;
void notifyTree(void (Widget::*notifyFunc)());
void notifyTreeReversed(void (Widget::*notifyFunc)());
bool dispatchEvent(Event const &event, bool (Widget::*memberFunc)(Event const &));
// Events.
virtual void initialize();
virtual void viewResized();
virtual void focusGained();
virtual void focusLost();
virtual void update();
virtual void drawIfVisible();
virtual void draw();
virtual bool handleEvent(Event const &event);
public:
static void setFocusCycle(WidgetList const &order);
private:
DENG2_PRIVATE(d)
};
} // namespace de
#endif // LIBDENG2_WIDGET_H