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

import (
	"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
}

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