summaryrefslogtreecommitdiff
path: root/action.go
blob: 8cb749e9f0304cb871d0ba198577db084d0896b8 (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
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
package main

import (
	"go.wit.com/dev/andlabs/ui"
	"go.wit.com/log"
	"go.wit.com/toolkits/tree"
	"go.wit.com/widget"
)

// this will check to make sure that the node
// is valid for making a New TK andlabs widget
// Basically, it makes sure there is a parent ID
// and that there already a widget created
func notNew(n *tree.Node) bool {
	if n == nil {
		log.Log(ERROR, "notNew() n = nil")
		return true
	}
	if n.TK != nil {
		log.Log(ERROR, "notNew() n.TK = nil", n.WidgetId, n.GetProgName())
		return true
	}
	if n.Parent == nil {
		log.Log(ERROR, "notNew() n.Parent = nil", n.WidgetId, n.GetProgName())
		return true
	}
	if n.Parent.TK == nil {
		if n.Parent.WidgetId == 0 {
			// this is normal if the widget type is a window
			if n.WidgetType == widget.Window {
				return true
			}
		}
		log.Log(ERROR, "notNew() n.Parent.TK = nil", n.WidgetId, n.GetProgName())
		log.Log(ERROR, "notNew() n.Parent.TK = nil", n.Parent.WidgetId, n.Parent.GetProgName())
		return true
	}
	// this means you can add a new widgets
	return false
}

func tkbad(n *tree.Node) bool {
	if n == nil {
		log.Log(ERROR, "tkbad() n = nil")
		return true
	}
	if n.TK == nil {
		log.Log(ERROR, "tkbad() n.TK = nil", n.WidgetId, n.GetProgName())
		return true
	}
	return false
}

// this makes sure widget and it's parent exists
func ready(n *tree.Node) bool {
	if n == nil {
		log.Log(ERROR, "ready() n = nil")
		return false
	}
	if n.TK == nil {
		log.Log(ERROR, "ready() n.TK = nil", n.WidgetId, n.GetProgName())
		return false
	}
	if n.Parent == nil {
		log.Log(ERROR, "ready() n.Parent = nil", n.WidgetId, n.GetProgName())
		return false
	}
	if n.Parent.TK == nil {
		if n.Parent.WidgetId == 0 {
			// this is normal if the widget type is a window
			if n.WidgetType == widget.Window {
				return false
			}
		}
		log.Log(ERROR, "ready() n.Parent.TK = nil", n.WidgetId, n.GetProgName())
		log.Log(ERROR, "ready() n.Parent.TK = nil", n.Parent.WidgetId, n.Parent.GetProgName())
		return false
	}
	return true
}

func hide(n *tree.Node) {
	show(n, false)
	n.State.Hidden = true
}

func show(n *tree.Node, b bool) {
	if tkbad(n) {
		return
	}
	var tk *guiWidget
	tk = n.TK.(*guiWidget)
	// tk = getTK(n)

	if tk == nil {
		return
	}
	if tk.uiControl == nil {
		return
	}
	if b {
		tk.uiControl.Show()
	} else {
		tk.uiControl.Hide()
	}
}

func enable(n *tree.Node, b bool) {
	if !ready(n) {
		return
	}
	var tk *guiWidget
	tk = n.TK.(*guiWidget)
	if tk.uiControl == nil {
		return
	}
	if b {
		tk.uiControl.Enable()
	} else {
		tk.uiControl.Disable()
	}
}

func pad(n *tree.Node, b bool) {
	if tkbad(n) {
		return
	}
	log.Log(ANDLABS, "pad() on WidgetId =", n.WidgetId)

	t := n.TK.(*guiWidget)
	if t == nil {
		log.Log(ERROR, "pad() toolkit struct == nil. for", n.WidgetId)
		return
	}

	switch n.WidgetType {
	case widget.Window:
		t.uiWindow.SetMargined(b)
		t.uiWindow.SetBorderless(b)
	case widget.Tab:
		tabSetMargined(t.uiTab, b)
	case widget.Group:
		t.uiGroup.SetMargined(b)
	case widget.Grid:
		t.uiGrid.SetPadded(b)
	case widget.Box:
		t.uiBox.SetPadded(b)
	default:
		log.Log(ERROR, "TODO: implement pad() for", n.WidgetType, n.GetProgName())
	}
}

func widgetDelete(n *tree.Node) {
	log.Log(ANDLABS, "widgetDelete()", n.WidgetId, n.WidgetType)
	var tk *guiWidget
	tk = n.TK.(*guiWidget)

	if n.WidgetType == widget.Window {
		log.Log(ANDLABS, "DESTROY uiWindow here")
		log.Log(ANDLABS, "DESTROY NEED TO REMOVE n from parent.Children")
		if tk.uiWindow != nil {
			tk.uiWindow.Destroy()
			tk.uiWindow = nil
		}
		n.DeleteNode()
	} else {
		log.Log(ANDLABS, "DESTROY can't destroy TODO:", n.WidgetId, n.WidgetType)
	}
}

/*
func processAction(a *widget.Action) {
	log.Log(ANDLABS, "processAction() START a.ActionType =", a.ActionType, "a.Value", a.Value)

	if a.ActionType == widget.ToolkitInit {
		Init()
		return
	}
	switch a.WidgetType {
	case widget.Root:
		if me.treeRoot == nil {
			log.Log(INFO, "processAction() found the treeRoot")
			me.treeRoot = me.myTree.AddNode(a)
		} else {
			log.Log(ERROR, "processAction() Something terrible has happened")
			log.Log(ERROR, "processAction() treeNode was sent an action", a.ActionType, a)
		}
		return
	}

	log.Log(ANDLABS, "andlabs processAction() START a.WidgetId =", a.WidgetId, "a.ParentId =", a.ParentId, a.ActionType)
	switch a.WidgetType {
	case widget.Flag:
		log.Log(ERROR, "processAction() RE-IMPLEMENT LOG FLAGS")
		return
	}

	if me.treeRoot == nil {
		panic("me.treeRoot == nil")
	}

	if a.ActionType == widget.Add {
		n := add(a)
		// show(n, !a.State.Hidden)
		if a.State.Hidden {
			hide(n)
		} else {
			if a.State.Enable {
				// nothing to do
			} else {
				enable(n, false)
			}
		}
		// pad(n, n.State.Pad)
		// expand(n, a.State.Expand)
		return
	}

	n := me.treeRoot.FindWidgetId(a.WidgetId)
	if n == nil {
		if a.ActionType == widget.Delete {
			// this is normal. the widget is aleady deleted
			return
		}
		if a.WidgetType == widget.Window {
			// this could happen maybe someday
			log.Log(ANDLABS, "processAction() trying on nonexistant window", a.WidgetId, a.ActionType)
			return
		}
		str := a.ActionType.String() + " id " + widget.GetString(a.WidgetId) + " " + a.WidgetType.String()
		err := errors.New("andlabs processAction() findWidgetId got nil " + str)
		log.Error(err, a.ActionType, a.WidgetType)
		log.Log(WARN, "processAction() ERROR findWidgetId found nil for id =", a.WidgetId)
		log.Log(WARN, "processAction() ERROR findWidgetId found nil", a.ActionType, a.WidgetType)
		log.Log(WARN, "processAction() ERROR findWidgetId found nil for id =", a.WidgetId)
		if WARN.Bool() {
			me.treeRoot.ListWidgets()
		}
		return
		panic("findWidgetId found nil for id = " + string(a.WidgetId))
	}

	if a.ActionType == widget.Dump {
		log.Log(NOW, "processAction() Dump =", a.ActionType, a.WidgetType, n.State.ProgName)
		return
	}

	switch a.ActionType {
	case widget.Delete:
		widgetDelete(n)
	case widget.Show:
		show(n, true)
	case widget.Hide:
		hide(n)
	case widget.Enable:
		enable(n, true)
	case widget.Disable:
		log.Log(ANDLABS, "andlabs got disable for", n.WidgetId, n.State.ProgName)
		enable(n, false)
	case widget.Checked:
		setChecked(n, a.State.Checked)
	case widget.Get:
		setText(n, a)
	case widget.GetText:
		switch a.WidgetType {
		case widget.Textbox:
			a.Value = n.State.Value
		}
	case widget.Set:
		setText(n, a)
	case widget.SetText:
		log.Log(ANDLABS, "andlabs SetText wid =", n.WidgetId, n.State.Value, a.State.Value)
		setText(n, a)
	case widget.AddText:
		addText(n, a)
	default:
		log.Log(ERROR, "processAction() Unknown =", a.ActionType, a.WidgetType)
	}
	log.Log(INFO, "processAction() END =", a.ActionType, a.WidgetType)
}
*/

func SetTitle(n *tree.Node, s string) {
	SetText(n, s)
}

func SetLabel(n *tree.Node, s string) {
	SetText(n, s)
}

func SetText(n *tree.Node, s string) {
	if n == nil {
		log.Warn("Tree Error: Add() sent n == nil")
		return
	}
	if n.TK == nil {
		log.Warn("Tree sent an action on a widget we didn't seem to have.")
		return
	}
	setText(n, s)
	log.Info("SetText() (new)", n.WidgetType, n.String(), s)
}

func AddText(n *tree.Node, s string) {
	if n == nil {
		log.Warn("Tree Error: Add() sent n == nil")
		return
	}
	if n.TK == nil {
		log.Warn("Tree sent an action on a widget we didn't seem to have.")
		return
	}
	log.Info("AddText()", n.WidgetType, n.String())
	// w := n.TK.(*guiWidget)
	// w.AddText(s)
	addText(n, s)
}

func newAction(n *tree.Node, atype widget.ActionType) {
	log.Log(INFO, "newaction() START", atype)
	if n == nil {
		log.Warn("Tree Error: Add() sent n == nil")
		return
	}
	if n.TK == nil {
		log.Warn("Tree sent an action on a widget we didn't seem to have.")
		// do this init here again? Probably something
		// went wrong and we should reset the our while gocui.View tree
		n.TK = initWidget(n)
	}
	// w := n.TK.(*guiWidget)
	switch atype {
	case widget.Show:
		log.Log(NOW, "Show() HERE. a.Hidden() was =", n.Hidden())
		show(n, true)
	case widget.Hide:
		log.Log(NOW, "Hide() HERE. a.State.Hidden was =", n.Hidden())
		hide(n)
	case widget.Move:
		log.Log(NOW, "attempt to move() =", atype, n.WidgetType, n.ProgName())
	case widget.ToolkitClose:
		log.Log(NOW, "attempting to Quit andlabs.ui")
		// standardClose()
		ui.Quit()
	case widget.Enable:
		enable(n, true)
	case widget.Disable:
		enable(n, false)
	case widget.Delete:
		widgetDelete(n)
	default:
		log.Log(ERROR, "newaction() UNHANDLED Action Type =", atype, "WidgetType =", n.WidgetType, "Name =", n.ProgName())
	}
	log.Log(INFO, "newaction() END", atype, n.String())
}