summaryrefslogtreecommitdiff
path: root/toolkit/gocui/common.go
blob: 94b63b1fa780c1a09ac0f346fab85114477ce8be (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
package main

import (
	"strconv"
	"git.wit.org/wit/gui/toolkit"
//	"github.com/awesome-gocui/gocui"
)

func makeWidget(a *toolkit.Action) *cuiWidget {
	var w *cuiWidget
	w = new(cuiWidget)

	w.name = a.Name
	w.text = a.Text
	w.b = a.B
	w.i = a.I
	w.s = a.S
	w.X = a.X
	w.Y = a.Y


	t := len(w.text)
	w.gocuiSize.w1 = w.gocuiSize.w0 + t + me.PadW
	w.gocuiSize.h1 = w.gocuiSize.h0 + me.DefaultHeight + me.PadH

	w.realWidth = w.gocuiSize.Width()
	w.realHeight = w.gocuiSize.Height()

	// set the gocui view.Frame = true by default
	w.frame = true
	if (w.frame) {
		w.realHeight += me.FramePadH
		w.gocuiSize.height += me.FramePadH
	}

	w.widgetType = a.WidgetType
	w.id = a.WidgetId
	// set the name used by gocui to the id
	w.cuiName = strconv.Itoa(w.id)

	if w.widgetType == toolkit.Root {
		log(logInfo, "setupWidget() FOUND ROOT w.id =", w.id, "w.parent", w.parent, "ParentId =", a.ParentId)
		w.id = 0
		me.rootNode = w
		return w
	}

	w.parent = findWidget(a.ParentId, me.rootNode)
	log(logInfo, "setupWidget() w.id =", w.id, "w.parent", w.parent, "ParentId =", a.ParentId)
	if (w.parent == nil) {
		log(logError, "setupWidget() ERROR: PARENT = NIL w.id =", w.id, "w.parent", w.parent, "ParentId =", a.ParentId)
		// just use the rootNode (hopefully it's not nil)
		w.parent = me.rootNode
		// return w
	}

	// add this widget as a child for the parent
	w.parent.Append(w)

	if (a.WidgetType == toolkit.Box) {
		if (a.B) {
			w.horizontal = true
		} else {
			w.horizontal = false
		}
	}
	if (a.WidgetType == toolkit.Grid) {
		w.widths = make(map[int]int) // how tall each row in the grid is
		w.heights = make(map[int]int) // how wide each column in the grid is
	}
	return w
}

func setupCtrlDownWidget() {
	var w *cuiWidget
	w = new(cuiWidget)

	w.name = "ctrlDown"

	w.widgetType = toolkit.Flag
	w.id = -1
	me.ctrlDown = w
	// me.rootNode.Append(w)
}

func (w *cuiWidget) deleteView() {
	if (w.v != nil) {
		me.baseGui.DeleteView(w.cuiName)
	}
	w.v = nil
}

func (n *cuiWidget) Append(child *cuiWidget) {
	n.children = append(n.children, child)
	// child.parent = n
}

// find widget by number
func findWidget(i int, w *cuiWidget) (*cuiWidget) {
	if (w == nil) {
		log(logVerbose, "findWidget() Trying to find i =", i, "currently checking against w.id = nil")
		return nil
	}
	log(logVerbose, "findWidget() Trying to find i =", i, "currently checking against w.id =", w.id)

	if (w.id == i) {
		log(logInfo, "findWidget() FOUND w.id ==", i, w.widgetType, w.name)
		return w
	}

	for _, child := range w.children {
		newW := findWidget(i, child)
		log(logVerbose, "findWidget() Trying to find i =", i, "currently checking against child.id =", child.id)
		if (newW != nil) {
			return newW
		}
	}
	return nil
}