responder.cpp
2.46 KB
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
#include <escher/responder.h>
#include <escher/app.h>
#include <escher/toolbox.h>
#include <escher/metric.h>
#include <assert.h>
Responder::Responder(Responder * parentResponder) :
m_parentResponder(parentResponder)
{
}
Responder * Responder::parentResponder() const {
return m_parentResponder;
}
void Responder::setParentResponder(Responder * responder) {
m_parentResponder = responder;
}
bool Responder::handleEvent(Ion::Events::Event event) {
if (event == Ion::Events::Toolbox && toolbox() != nullptr) {
toolbox()->setSender(this);
app()->displayModalViewController(toolbox(), 0.f, 0.f, Metric::PopUpTopMargin, Metric::PopUpLeftMargin, 0, Metric::PopUpRightMargin);
return true;
}
return false;
}
void Responder::didBecomeFirstResponder() {
}
void Responder::willResignFirstResponder() {
}
void Responder::didEnterResponderChain(Responder * previousFirstResponder) {
}
void Responder::willExitResponderChain(Responder * nextFirstResponder) {
}
Responder * Responder::commonAncestorWith(Responder * responder) {
if (responder == nullptr) {
return nullptr;
}
if (this == responder) {
return this;
}
Responder * rootResponder = this;
while (rootResponder->parentResponder() != responder && rootResponder->parentResponder() != nullptr) {
rootResponder = rootResponder->parentResponder();
}
if (rootResponder->parentResponder() == responder) {
return responder;
}
rootResponder = responder;
while (rootResponder->parentResponder() != this && rootResponder->parentResponder() != nullptr) {
rootResponder = rootResponder->parentResponder();
}
if (rootResponder->parentResponder() == this) {
return this;
}
Responder * r = nullptr;
if (parentResponder()) {
r = parentResponder()->commonAncestorWith(responder);
}
Responder * s = nullptr;
if (responder->parentResponder()) {
s = commonAncestorWith(responder->parentResponder());
}
if (r) {
return r;
}
return s;
}
/* We assume the app is the root parent. */
App * Responder::app() {
Responder * rootResponder = this;
while (rootResponder->parentResponder() != nullptr) {
rootResponder = rootResponder->parentResponder();
}
/* If we used RTTI we could use a dynamic_cast, which would be a lot more
* safe, as such:
* return dynamic_cast<App *>(rootResponder); */
App * result = (App *)rootResponder;
assert(result->m_magic == App::Magic); // Poor man's RTTI
return result;
}
Toolbox * Responder::toolbox() {
return nullptr;
}