summaryrefslogtreecommitdiff
path: root/toolkit/nocui/common.go
blob: 797f86a8073723ddb089d620ec4c49959e8b8abe (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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package main

/*
	These code should be common to all gui plugins

	There are some helper functions that are probably going to be
	the same everywhere. Mostly due to handling the binary tree structure
	and the channel communication

	For now, it's just a symlink to the 'master' version in
	./toolkit/nocui/common.go
*/

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

// this is the channel that sends the events from the user clicking or typing
// back to the program using this golang package
var callback chan toolkit.Action

type node struct {
	parent	*node
	children []*node

	WidgetId	int	// widget ID
	WidgetType	toolkit.WidgetType
	ParentId	int	// parent ID

	Name   string
	Text   string

	// This is how the values are passed back and forth
	// values from things like checkboxes & dropdown's
	B	bool
	I	int
	S	string

	A	any // switch to this or deprecate this? pros/cons?

	// This is used for things like a slider(0,100)
	X      int
	Y      int

	// This is for the grid size & widget position
	W      int
	H      int
	AtW    int
	AtH    int

	vals []string // dropdown menu items

	// horizontal=true  means layout widgets like books on a bookshelf
	// horizontal=false means layout widgets like books in a stack
	horizontal bool `default:false`

	hasTabs bool // does the window have tabs?
	currentTab bool // the visible tab

	// the internal plugin toolkit structure
	// in the gtk plugin, it has gtk things like margin & border settings
	// in the text console one, it has text console things like colors for menus & buttons
	tk *guiWidget
}

// 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) *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

	n.W = a.W
	n.H = a.H
	n.AtW = a.AtW
	n.AtH = a.AtH

	// store the internal toolkit information
	n.tk = new(guiWidget)

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

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

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

func (n *node) doUserEvent() {
	if (callback == nil) {
		log(logError, "doUserEvent() callback == nil", n.WidgetId)
		return
	}
	var a toolkit.Action
	a.WidgetId = n.WidgetId
	a.Name = n.Name
	a.Text = n.Text
	a.S = n.S
	a.I = n.I
	a.B = n.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
}

func addNode(a *toolkit.Action) *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

	n.W = a.W
	n.H = a.H
	n.AtW = a.AtW
	n.AtH = a.AtH

	// store the internal toolkit information
	n.tk = initWidget(n)
	// n.tk = new(guiWidget)

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

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

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