/
rootwidget.cpp
143 lines (116 loc) · 3 KB
/
rootwidget.cpp
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
/** @file rootwidget.cpp Widget for managing the root of the UI.
* @ingroup widget
*
* @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>
*/
#include "de/RootWidget"
#include "de/ConstantRule"
#include "de/RuleRectangle"
#include "de/math.h"
namespace de {
struct RootWidget::Instance
{
RuleRectangle *viewRect;
Widget *focus;
Instance() : focus(0)
{
viewRect = new RuleRectangle;
viewRect->setLeftTop (Const(0), Const(0))
.setRightBottom(Const(0), Const(0));
}
~Instance()
{
delete viewRect;
}
Vector2i viewSize() const
{
return Vector2i(de::floor(viewRect->right().value()),
de::floor(viewRect->bottom().value()));
}
};
RootWidget::RootWidget() : Widget(), d(new Instance)
{}
RootWidget::~RootWidget()
{
delete d;
}
Vector2i RootWidget::viewSize() const
{
return d->viewSize();
}
Rule const &RootWidget::viewLeft() const
{
return d->viewRect->left();
}
Rule const &RootWidget::viewRight() const
{
return d->viewRect->right();
}
Rule const &RootWidget::viewTop() const
{
return d->viewRect->top();
}
Rule const &RootWidget::viewBottom() const
{
return d->viewRect->bottom();
}
Rule const &RootWidget::viewWidth() const
{
return d->viewRect->right();
}
Rule const &RootWidget::viewHeight() const
{
return d->viewRect->bottom();
}
void RootWidget::setViewSize(Vector2i const &size)
{
d->viewRect->setInput(Rule::Right, Const(size.x));
d->viewRect->setInput(Rule::Bottom, Const(size.y));
notifyTree(&Widget::viewResized);
}
void RootWidget::setFocus(Widget *widget)
{
if(d->focus) d->focus->focusLost();
d->focus = widget;
if(d->focus) d->focus->focusGained();
}
Widget *RootWidget::focus() const
{
return d->focus;
}
void RootWidget::initialize()
{
notifyTree(&Widget::initialize);
}
void RootWidget::update()
{
notifyTree(&Widget::update);
}
void RootWidget::draw()
{
notifyTree(&Widget::drawIfVisible);
Rule::markRulesValid(); // All done for this frame.
}
bool RootWidget::processEvent(Event const &event)
{
if(focus() && focus()->handleEvent(event))
{
// The focused widget ate the event.
return true;
}
return dispatchEvent(event, &Widget::handleEvent);
}
} // namespace de