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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
|
package gui
// Common actions for widgets like 'Enable' or 'Hide'
import (
"go.wit.com/log"
"go.wit.com/widget"
)
// functions for handling text related GUI elements
func (n *Node) Show() *Node {
if !n.Ready() {
return n
}
if !n.Hidden() {
return n
}
n.visable = true
n.hidden = false
n.changed = true
if n.WidgetType == widget.Window {
log.Log(CHANGE, "Show() on a hidden window", n.progname)
log.Log(NOW, "Show() TO FIX: this is doing TestDraw()")
n.TestDraw()
return n
}
// inform the toolkits
sendAction(n, widget.Show)
return n
}
func (n *Node) Hide() *Node {
if !n.Ready() {
return n
}
/*
if n.Hidden() {
return n
}
*/
if n.WidgetType == widget.Window {
log.Log(CHANGE, "Hide() on a window", n.progname)
log.Log(CHANGE, "Hide() this needs to do TestDestroy() ?")
n.Destroy()
n.hidden = true
// n.visable = false
n.changed = true
return nil
}
n.hidden = true
n.changed = true
// inform the toolkits
sendAction(n, widget.Hide)
return n
}
// enables a widget so the user can see it and work/click/etc on it
// by default, widgets are enabled when they are created
func (n *Node) Enable() *Node {
if !n.Ready() {
return n
}
// if n.enabled { return n }
n.enabled = true
n.changed = true
// inform the toolkits
sendAction(n, widget.Enable)
return n
}
// disables a widget so the user can see it, but can not
// interact or change it.
func (n *Node) Disable() *Node {
if !n.Ready() {
return n
}
// if ! n.enabled { return n }
n.enabled = false
n.changed = true
// inform the toolkits
sendAction(n, widget.Disable)
return n
}
func (n *Node) Bool() bool {
if !n.Ready() {
return false
}
switch n.WidgetType {
case widget.Checkbox:
return n.checked
default:
}
return widget.GetBool(n.value)
}
func (n *Node) Int() int {
if !n.Ready() {
return -1
}
return widget.GetInt(n.value)
}
func (n *Node) SetBool(b bool) {
switch n.WidgetType {
case widget.Checkbox:
n.checked = b
n.value = b
default:
log.Warn("WidgetType not bool", n.WidgetType, n.id)
}
}
func (n *Node) String() string {
if !n.Ready() {
return ""
}
switch n.WidgetType {
case widget.Window:
return n.label
case widget.Button:
return n.label
case widget.Group:
return n.label
case widget.Label:
return n.label
default:
}
return widget.GetString(n.value)
}
func (n *Node) Strings() []string {
if !n.Ready() {
return nil
}
var tmp []string
for s, _ := range n.strings {
tmp = append(tmp, s)
}
return tmp
}
func (n *Node) Destroy() {
if !n.Ready() {
return
}
n.enabled = false
n.changed = true
log.Log(CHANGE, "Destroy()", n.WidgetType, n.id, n.GetProgName())
// inform the toolkits
sendAction(n, widget.Delete)
return
}
// appends text to the existing text
// TODO: this is an experiement
func (n *Node) AppendText(str string) {
if !n.Ready() {
return
}
tmp := widget.GetString(n.value) + str
n.value = tmp
n.changed = true
// inform the toolkits
sendAction(n, widget.SetText)
}
// should get the reference name used for programming and debugging
/*
TODO: ensure these are unique and make a way to look them up
myButton = myGroup.NewButton("hit ball", nil).SetName("HIT")
myButton.GetName() should return "HIT"
n = Find("HIT") should return myButton
*/
func (n *Node) SetProgName(s string) *Node {
if !n.Ready() {
return n
}
if n.progname == s {
// don't do anything since nothing changed
return n
}
n.changed = true
n.progname = s
return n
}
func (n *Node) GetProgName() string {
if !n.Ready() {
return ""
}
return n.progname
}
func (n *Node) Margin() *Node {
if !n.Ready() {
return n
}
if n.margin {
return n
}
n.margin = true
n.changed = true
log.Log(INFO, "Margin()", n.WidgetType, n.progname)
// inform the toolkits
sendAction(n, widget.Margin)
return n
}
func (n *Node) Unmargin() *Node {
if !n.Ready() {
return n
}
if !n.margin {
return n
}
n.margin = false
n.changed = true
// inform the toolkits
sendAction(n, widget.Unmargin)
return n
}
func (n *Node) Pad() *Node {
if !n.Ready() {
return n
}
if n.pad == true {
return n
} // nothing changed
n.pad = true
n.changed = true
log.Log(INFO, "Pad()", n.WidgetType, n.progname)
// inform the toolkits
sendAction(n, widget.Pad)
return n
}
func (n *Node) Unpad() *Node {
if !n.Ready() {
return n
}
if n.pad == false {
return n
} // nothing changed
n.pad = false
n.changed = true
// inform the toolkits
sendAction(n, widget.Unpad)
return n
}
func (n *Node) Expand() *Node {
if !n.Ready() {
return n
}
if n.expand == true {
return n
} // nothing changed
n.expand = true
n.changed = true
// inform the toolkits
sendAction(n, widget.SetExpand)
return n
}
func (n *Node) SetExpand(b bool) *Node {
if !n.Ready() {
return n
}
if n.expand == b {
return n
} // nothing changed
n.expand = b
n.changed = true
// inform the toolkits
sendAction(n, widget.SetExpand)
return n
}
// is the widget currently viewable?
func (n *Node) Hidden() bool {
if !n.Ready() {
return false
}
return n.hidden
}
func (n *Node) Ready() bool {
if n == nil {
log.Log(NOW, "Ready() got node == nil. TODO: find code trace to here")
panic("ready got nil")
// TODO: figure out if you can identify the code trace
// to help find the root cause
return false
}
return true
}
// returns the root of the binary tree
// change the names to 'Tree' as I think the name is better
func TreeRoot() *Node {
return me.rootNode
}
//
//
// DEPRECATE / REDO / SORT OUT THIS STUFF
//
//
/*
// string handling examples that might be helpful for normalizeInt()
isAlpha := regexp.MustCompile(`^[A-Za-z]+$`).MatchString
for _, username := range []string{"userone", "user2", "user-three"} {
if !isAlpha(username) {
log.Log(GUI, "%q is not valid\n", username)
}
}
const alpha = "abcdefghijklmnopqrstuvwxyz"
func alphaOnly(s string) bool {
for _, char := range s {
if !strings.Contains(alpha, strings.ToLower(string(char))) {
return false
}
}
return true
}
func normalizeInt(s string) string {
// reg, err := regexp.Compile("[^a-zA-Z0-9]+")
reg, err := regexp.Compile("[^0-9]+")
if err != nil {
log.Log(GUI, "normalizeInt() regexp.Compile() ERROR =", err)
return s
}
clean := reg.ReplaceAllString(s, "")
log.Log(GUI, "normalizeInt() s =", clean)
return clean
}
*/
|