-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathcallbacks.go
147 lines (134 loc) · 3.19 KB
/
callbacks.go
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
package fltk
/*
#include <stdlib.h>
#include "callbacks.h"
#include "widget.h"
*/
import "C"
import "fmt"
// Parameterless callbacks
type callbackMap struct {
callbackMap map[uintptr]func()
id uintptr
}
func newCallbackMap() *callbackMap {
return &callbackMap{
callbackMap: make(map[uintptr]func()),
}
}
func (m *callbackMap) register(fn func()) uintptr {
m.id++
m.callbackMap[m.id] = fn
return m.id
}
func (m *callbackMap) unregister(id uintptr) {
delete(m.callbackMap, id)
}
func (m *callbackMap) invoke(id uintptr) {
if callback, ok := m.callbackMap[id]; ok && callback != nil {
callback()
}
}
func (m *callbackMap) isEmpty() bool {
return len(m.callbackMap) == 0
}
func (m *callbackMap) size() int {
return len(m.callbackMap)
}
func (m *callbackMap) clear() {
for id := range m.callbackMap {
delete(m.callbackMap, id)
}
}
var globalCallbackMap = newCallbackMap()
//export _go_callbackHandler
func _go_callbackHandler(id uintptr) {
globalCallbackMap.invoke(id)
}
// Event handlers
type eventHandlerMap struct {
eventHandlerMap map[int]func(Event) bool
id int
}
func newEventHandlerMap() *eventHandlerMap {
return &eventHandlerMap{
eventHandlerMap: make(map[int]func(Event) bool),
}
}
func (m *eventHandlerMap) register(fn func(Event) bool) int {
m.id++
m.eventHandlerMap[m.id] = fn
return m.id
}
func (m *eventHandlerMap) unregister(id int) {
delete(m.eventHandlerMap, id)
}
func (m *eventHandlerMap) invoke(id int, event Event) bool {
if handler, ok := m.eventHandlerMap[id]; ok && handler != nil {
return handler(event)
}
return false
}
func (m *eventHandlerMap) isEmpty() bool {
return len(m.eventHandlerMap) == 0
}
func (m *eventHandlerMap) size() int {
return len(m.eventHandlerMap)
}
func (m *eventHandlerMap) clear() {
for id := range m.eventHandlerMap {
delete(m.eventHandlerMap, id)
}
}
var globalEventHandlerMap = newEventHandlerMap()
//export _go_eventHandler
func _go_eventHandler(handlerId C.int, event C.int) C.int {
if globalEventHandlerMap.invoke(int(handlerId), Event(event)) {
return 1
}
return 0
}
// Draw handlers
type drawHandlerMap struct {
drawHandlerMap map[uintptr]func(func())
id uintptr
}
func newDrawHandlerMap() *drawHandlerMap {
return &drawHandlerMap{
drawHandlerMap: make(map[uintptr]func(func())),
}
}
func (m *drawHandlerMap) register(fn func(func())) uintptr {
m.id++
m.drawHandlerMap[m.id] = fn
return m.id
}
func (m *drawHandlerMap) unregister(id uintptr) {
if _, ok := m.drawHandlerMap[id]; !ok {
panic(fmt.Errorf("unknown draw handler id: %d", id))
}
delete(m.drawHandlerMap, id)
}
func (m *drawHandlerMap) invoke(id uintptr, baseDraw func()) {
if handler, ok := m.drawHandlerMap[id]; ok && handler != nil {
handler(baseDraw)
}
}
func (m *drawHandlerMap) isEmpty() bool {
return len(m.drawHandlerMap) == 0
}
func (m *drawHandlerMap) size() int {
return len(m.drawHandlerMap)
}
func (m *drawHandlerMap) clear() {
for id := range m.drawHandlerMap {
delete(m.drawHandlerMap, id)
}
}
var globalDrawHandlerMap = newDrawHandlerMap()
//export _go_drawHandler
func _go_drawHandler(handlerId uintptr, widget *C.Fl_Widget) {
globalDrawHandlerMap.invoke(handlerId, func() {
C.go_fltk_Widget_basedraw(widget)
})
}