forked from pwiecz/go-fltk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
event_handler.h
90 lines (74 loc) · 2.05 KB
/
event_handler.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
#pragma once
#include "_cgo_export.h"
#include <vector>
class WidgetWithEventHandler {
public:
virtual void set_event_handler(int handlerId) = 0;
};
class WidgetWithResizeHandler {
public:
virtual void set_resize_handler(uintptr_t handlerId) = 0;
};
class WidgetWithDrawHandler {
public:
virtual void set_draw_handler(uintptr_t handlerId) = 0;
virtual void basedraw() = 0;
};
class WidgetWithDeletionHandler {
public:
virtual void add_deletion_handler(uintptr_t handlerId) = 0;
};
template<class BaseWidget>
class EventHandler : public BaseWidget, public WidgetWithEventHandler, public WidgetWithDrawHandler, public WidgetWithResizeHandler, public WidgetWithDeletionHandler {
public:
template<class... Arg>
EventHandler(Arg... args)
: BaseWidget(args...) {}
virtual ~EventHandler() {
for (uintptr_t deletionHandlerId : m_deletionHandlerIds) {
_go_callbackHandler(deletionHandlerId);
}
}
int handle(int event) final {
if (m_eventHandlerId >= 0) {
const int ret = _go_eventHandler(m_eventHandlerId, event);
if (ret != 0) {
return ret;
}
}
return BaseWidget::handle(event);
}
void draw() override {
if (m_drawHandlerId != 0) {
_go_drawHandler(m_drawHandlerId, this);
} else {
BaseWidget::draw();
}
}
void basedraw() final {
BaseWidget::draw();
}
void resize(int x, int y, int w, int h) final {
BaseWidget::resize(x, y, w, h);
if (m_resizeHandlerId != 0) {
_go_callbackHandler(m_resizeHandlerId);
}
}
void set_event_handler(int handlerId) final {
m_eventHandlerId = handlerId;
}
void set_draw_handler(uintptr_t handlerId) final {
m_drawHandlerId = handlerId;
}
void set_resize_handler(uintptr_t handlerId) final {
m_resizeHandlerId = handlerId;
}
void add_deletion_handler(uintptr_t handlerId) final {
m_deletionHandlerIds.push_back(handlerId);
}
protected:
int m_eventHandlerId = -1;
uintptr_t m_drawHandlerId = 0;
uintptr_t m_resizeHandlerId = 0;
std::vector<uintptr_t> m_deletionHandlerIds;
};