andlabs/action.go

247 lines
5.5 KiB
Go

package main
import (
"errors"
"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.Warn("ready() n = nil")
return true
}
if n.TK != nil {
log.Warn("ready() n.TK = nil", n.WidgetId, n.GetProgName())
return true
}
if n.Parent == nil {
log.Warn("ready() n.Parent = nil", n.WidgetId, n.GetProgName())
return true
}
if n.Parent.TK == nil {
log.Warn("ready() n.Parent.TK = nil", n.WidgetId, n.GetProgName())
log.Warn("ready() 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.Warn("ready() n = nil")
return true
}
if n.TK == nil {
log.Warn("ready() 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.Warn("ready() n = nil")
return false
}
if n.TK == nil {
log.Warn("ready() n.TK = nil", n.WidgetId, n.GetProgName())
return false
}
if n.Parent == nil {
log.Warn("ready() n.Parent = nil", n.WidgetId, n.GetProgName())
return false
}
if n.Parent.TK == nil {
log.Warn("ready() n.Parent.TK = nil", n.WidgetId, n.GetProgName())
log.Warn("ready() n.Parent.TK = nil", n.Parent.WidgetId, n.Parent.GetProgName())
return false
}
return 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) {
var tk *guiWidget
tk = n.TK.(*guiWidget)
if n == nil {
panic("WHAT? enable was passed nil. How does this even happen?")
}
if tk == nil {
return
}
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.Warn("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(NOW, "widgetDelete()", n.WidgetId)
var tk *guiWidget
tk = n.TK.(*guiWidget)
if n.WidgetType == widget.Window {
log.Warn("DESTROY uiWindow here")
log.Warn("NEED TO REMOVE n from parent.Children")
if tk.uiWindow != nil {
tk.uiWindow.Destroy()
tk.uiWindow = nil
}
n.DeleteNode()
}
}
func processAction(a *widget.Action) {
log.Warn("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.Warn("processAction() Something terrible has happened")
log.Warn("processAction() treeNode was sent an action", a.ActionType, a)
}
return
}
log.Warn("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, n.State.Visable)
// 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
}
log.Error(errors.New("andlabs processAction() ERROR findWidgetId found nil"), a.ActionType, a.WidgetType)
log.Log(NOW, "processAction() ERROR findWidgetId found nil for id =", a.WidgetId)
log.Log(NOW, "processAction() ERROR findWidgetId found nil", a.ActionType, a.WidgetType)
log.Log(NOW, "processAction() ERROR findWidgetId found nil for id =", a.WidgetId)
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:
show(n, false)
case widget.Enable:
enable(n, true)
case widget.Disable:
log.Warn("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.Warn("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)
}