summaryrefslogtreecommitdiff
path: root/toolkit/nocui/common.go
diff options
context:
space:
mode:
authorJeff Carr <[email protected]>2023-04-27 21:11:00 -0500
committerJeff Carr <[email protected]>2023-04-27 21:11:00 -0500
commit87b62c98a6ebd9d0e48850d1710de7f39aba41c8 (patch)
treeb5961b9d4841b20ff41ae95acac4d82459ee9d3f /toolkit/nocui/common.go
parent9e285c7affa3257a46e85acde6dc64a9c781b728 (diff)
nocui: a template for porting new toolkits
Signed-off-by: Jeff Carr <[email protected]>
Diffstat (limited to 'toolkit/nocui/common.go')
-rw-r--r--toolkit/nocui/common.go79
1 files changed, 79 insertions, 0 deletions
diff --git a/toolkit/nocui/common.go b/toolkit/nocui/common.go
new file mode 100644
index 0000000..2e0ffdc
--- /dev/null
+++ b/toolkit/nocui/common.go
@@ -0,0 +1,79 @@
+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) *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 = new(nocuiT)
+
+ 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)
+ }
+ 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
+}