summaryrefslogtreecommitdiff
path: root/node.go
blob: 95bff4fe55fe08e787c22dda41d505ffd6f54955 (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
package gui

import (
	"go.wit.com/lib/protobuf/guipb"
	"go.wit.com/log"
	"go.wit.com/widget"
)

/*
generic function to create a new node on the binary tree

this is called each time you want a new widget
and it initializes basic default values

there isn't much to see here.
*/
func (parent *Node) newNode(s string, t widget.WidgetType) *Node {
	if parent == nil {
		log.Log(WARN, "newNode() ERROR got parent == nil")
		// this is an error internal to this gui package
		return parent
	}
	newN := rawNode(s, t)
	parent.Append(newN)
	return newN
}

// I've been using 'raw' in the code here and there
// to indicate a widget that hasn't be placed in the binary tree
// there is probably a more approriate name
func rawNode(s string, t widget.WidgetType) *Node {
	var newN *Node

	newN = addNode()
	newN.progname = s
	newN.label = s
	newN.defaultS = s
	newN.newString = s
	newN.currentS = s
	newN.WidgetType = t

	newN.strings = make(map[string]int)

	// set these defaults
	newN.expand = false
	newN.pad = true
	newN.borderless = false // usually this is just used for Window widgets
	newN.enabled = true
	newN.changed = true
	newN.hidden = false // by default, always draw a widget
	return newN
}

/*
raw create function for a new node struct and increments the counter
*/
func addNode() *Node {
	n := new(Node)
	n.id = me.counter
	log.Log(NODE, "addNode = widget setid =", n.id)

	me.counter += 1
	return n
}

func (parent *Node) Append(n *Node) {
	if n.parent != nil {
		log.Log(WARN, "Widget already has a parent already assigned")
		return
	}
	// track the parent and this widget as a child
	parent.children = append(parent.children, n)
	n.parent = parent

	if n.IsMirror() {
		m := n.isMirror
		text := m.label
		n.label = text
		n.progname = text
	}

	// if the parent is a grid, add it to the next cell
	if parent.WidgetType == widget.Grid {
		parent.gridIncrement()
	}
	n.AtW = parent.NextW
	n.AtH = parent.NextH

	// honor the visable settings of the parent
	n.visable = parent.visable

	// deprecate everything above and switch to protobuf below
	if n.widget == nil {
		n.widget = new(guipb.Widget)
	}
	n.widget.Id = int64(n.id)
	// n.widget.Type = n.TypePB()
}

func (n *Node) TypePB() guipb.WidgetType {
	switch n.WidgetType {
	case widget.Window:
		return guipb.WidgetType_Window
	case widget.Group:
		return guipb.WidgetType_Group
	case widget.Grid:
		return guipb.WidgetType_Grid
	case widget.Box:
		return guipb.WidgetType_Box
	case widget.Label:
		return guipb.WidgetType_Label
	case widget.Button:
		return guipb.WidgetType_Button
	case widget.Checkbox:
		return guipb.WidgetType_Checkbox
	case widget.Dropdown:
		return guipb.WidgetType_Dropdown
	default:
		return guipb.WidgetType_Label
	}
}

// returns the parent widget
func (n *Node) Parent() *Node {
	if !n.Ready() {
		return n
	}
	return n.parent
}

func (n *Node) Delete(d *Node) {
	if !n.Ready() {
		return
	}

	for i, child := range n.children {
		log.Log(NODE, "\t", i, child.id, child.progname)
		if child.id == d.id {
			log.Log(NODE, "\t\t Deleting this")
			n.children = append(n.children[:i], n.children[i+1:]...)
			return
		}
	}
	log.Log(WARN, "did not find node to delete", d.id, d.progname)
}