andlabs/action.go

244 lines
5.3 KiB
Go
Raw Normal View History

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