summaryrefslogtreecommitdiff
path: root/treeWidget.go
blob: 4d92abd13e14024139e1a42aa3332869b4d0c07d (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
// Copyright 2017-2025 WIT.COM Inc. All rights reserved.
// Use of this source code is governed by the GPL 3.0

package main

import (
	"strconv"
	"strings"

	"github.com/awesome-gocui/gocui"
	"go.wit.com/log"
	"go.wit.com/toolkits/tree"
	"go.wit.com/widget"
)

func initWidget(n *tree.Node) *guiWidget {
	var w *guiWidget
	w = new(guiWidget)

	w.node = n
	w.cuiName = strconv.Itoa(w.node.WidgetId) + " TK"
	// w.node.WidgetType = n.WidgetType
	w.labelN = n.State.Label
	if w.labelN == "" {
		// remove this debugging hack once things are stable and fixed
		w.labelN = n.GetProgName()
	}
	w.frame = true
	w.enable = n.State.Enable

	if n.WidgetType == widget.Root {
		log.Log(INFO, "setupWidget() FOUND ROOT w.id =", n.WidgetId)
	}

	if n.WidgetType == widget.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
	}

	p := n.Parent
	if p == nil {
		log.Log(ERROR, "parent == nil", w.String(), n.WidgetId, w.node.WidgetType)
		return w
	}
	if p.TK == nil {
		log.Log(ERROR, "parent.TK == nil", w.String(), n.WidgetId, w.node.WidgetType)
		return w
	}

	// set the parent and append to parent children
	var ptk *guiWidget
	ptk = p.TK.(*guiWidget)
	w.parent = ptk
	ptk.children = append(ptk.children, w)
	return w
}

func setupCtrlDownWidget() {
	a := new(widget.Action)
	a.ProgName = "ctrlDown"
	a.WidgetType = widget.Dialog
	a.WidgetId = -1
	a.ParentId = 0
	// n := addNode(a)
	n := me.myTree.AddNode(a)

	me.ctrlDown = n
}

func (w *guiWidget) deleteView() {
	// make sure the view isn't really there
	// log.Log(GOCUI, "deleteView()", w.cuiName, w.node.WidgetType, w.node.WidgetId)
	me.baseGui.DeleteView(w.cuiName)
	w.v = nil
}

func (tk *guiWidget) String() string {
	// deprecate this?
	curval := strings.TrimSpace(tk.labelN)
	if curval != "" {
		return curval
	}
	curval = strings.TrimSpace(tk.node.GetLabel())
	if curval != "" {
		return curval
	}
	curval = tk.GetText()
	if curval != "" {
		return curval
	}
	curval = tk.node.String()
	if curval != "" {
		return curval
	}
	curval = strings.TrimSpace(tk.node.ProgName())
	if curval != "" {
		return curval
	}
	return ""
}

func (tk *guiWidget) Visible() bool {
	if tk == nil {
		return false
	}
	if tk.v == nil {
		return false
	}
	tk.v.Visible = true
	return true
}

func (tk *guiWidget) Hide() {
	tk.deleteView()
}

func (tk *guiWidget) SetVisible(b bool) {
	if b {
		tk.Show()
	} else {
		tk.Hide()
	}
}

func (tk *guiWidget) findWidgetByName(name string) *guiWidget {
	if tk.cuiName == name {
		return tk
	}
	for _, child := range tk.children {
		found := child.findWidgetByName(name)
		if found != nil {
			return found
		}
	}
	return nil
}

func (tk *guiWidget) findWidgetByView(v *gocui.View) *guiWidget {
	if tk.v == v {
		return tk
	}
	if tk.cuiName == v.Name() {
		log.Log(NOW, "findWidget() error. names are mismatched or out of sync", tk.cuiName)
		log.Log(NOW, "findWidget() or maybe the view has been deleted")
		// return tk
	}
	for _, child := range tk.children {
		found := child.findWidgetByView(v)
		if found != nil {
			return found
		}
	}
	return nil
}