summaryrefslogtreecommitdiff
path: root/toolkit/andlabs/common.go
blob: 630c6a99380dd012f283d61ba8d0f440e2c123d5 (plain)
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
package main

import (
	"git.wit.org/wit/gui/toolkit"
)

// searches the binary tree for a WidgetId
func (n *node) findWidgetId(id int) *node {
	if (n == nil) {
		return nil
	}

	if n.WidgetId == id {
		return n
	}

	for _, child := range n.children {
		newN := child.findWidgetId(id)
		if (newN != nil) {
			return newN
		}
	}
	return nil
}

func addWidget(a *toolkit.Action, tk *andlabsT) *node {
	n := new(node)
	n.WidgetType = a.WidgetType
	n.WidgetId = a.WidgetId
	n.ParentId = a.ParentId

	// copy the data from the action message
	n.Name = a.Name
	n.Text = a.Text
	n.I = a.I
	n.S = a.S
	n.B = a.B
	n.X = a.X
	n.Y = a.Y

	// store the internal toolkit information
	n.tk = tk

	if (a.WidgetType == toolkit.Root) {
		log(logInfo, "addWidget() Root")
		return n
	}

	if (rootNode.findWidgetId(a.WidgetId) != nil) {
		log(logError, "addWidget() WidgetId already exists", a.WidgetId)
		return rootNode.findWidgetId(a.WidgetId)
	}

	// add this new widget on the binary tree
	n.parent = rootNode.findWidgetId(a.ParentId)
	if n.parent != nil {
		n.parent.children = append(n.parent.children, n)
	}

	// deprecate this when this toolkit uses the binary tree instead
	if (andlabs[a.WidgetId] == nil) {
		andlabs[a.WidgetId] = tk
	}

	return n
}

func (t *andlabsT) doUserEvent() {
	if (callback == nil) {
		log(debugError, "doUserEvent() callback == nil", t.wId)
		return
	}
	var a toolkit.Action
	a.WidgetId = t.wId
	a.Name = t.Name
	a.S = t.s
	a.I = t.i
	a.B = t.b
	a.ActionType = toolkit.User
	log(logInfo, "doUserEvent() START: send a user event to the callback channel")
	callback <- a
	log(logInfo, "doUserEvent() END:   sent a user event to the callback channel")
	return
}