gadgets/basicWindow.go

225 lines
4.1 KiB
Go
Raw Normal View History

/*
A Standard Window
*/
package gadgets
import (
"go.wit.com/gui"
"go.wit.com/log"
)
type BasicWindow struct {
ready bool
hidden bool
vertical bool
title string
// parent *gui.Node
win *gui.Node // window widget
box *gui.Node // box
Custom func()
}
func RawBasicWindow(title string) *BasicWindow {
var w *BasicWindow
w = &BasicWindow{
title: title,
vertical: false,
2024-02-12 15:22:17 -06:00
hidden: true,
}
log.Log(GADGETS, "NewBasicWindow() END")
return w
}
func NewBasicWindow(parent *gui.Node, title string) *BasicWindow {
var w *BasicWindow
w = &BasicWindow{
title: title,
vertical: false,
hidden: true,
}
log.Log(GADGETS, "NewBasicWindow() END")
return w
}
func (w *BasicWindow) Hidden() bool {
return w.hidden
}
func (w *BasicWindow) Show() {
if !w.Ready() {
return
}
w.win.Show()
w.hidden = false
return
}
func (w *BasicWindow) Hide() {
if !w.Ready() {
return
}
log.Log(GADGETS, "BasicWindow.Hide() here")
w.win.Hide()
w.hidden = true
return
}
func (w *BasicWindow) Enable() {
w.box.Enable()
}
func (w *BasicWindow) Disable() {
w.box.Disable()
}
func (w *BasicWindow) Toggle() {
if !w.Ready() {
return
}
if w.hidden {
w.Show()
} else {
w.Hide()
}
return
}
func (w *BasicWindow) SetTitle(title string) {
w.SetLabel(title)
return
}
func (w *BasicWindow) SetLabel(title string) {
if !w.Ready() {
return
}
w.win.SetText(title)
return
}
// sets this window to run os.Exit()
func (w *BasicWindow) StandardClose() {
if !w.Initialized() {
return
}
w.win.Custom = w.win.StandardClose
return
}
// sets this window to run os.Exit()
func (w *BasicWindow) StandardExit() {
if !w.Ready() {
return
}
w.win.Custom = func() {
log.Log(GADGETS, "BasicWindow.Custom() closed. handled properly?", w.title)
2024-02-13 16:17:08 -06:00
w.hidden = true
if w.Custom != nil {
log.Log(GADGETS, "BasicWindow.Custom() HAS CUSTOM")
w.Custom()
return
}
w.win.StandardExit()
}
return
}
// Returns true if initialized
func (w *BasicWindow) Initialized() bool {
if w == nil {
return false
}
return true
}
// Returns true if the status is valid
func (w *BasicWindow) Ready() bool {
if !w.Initialized() {
return false
}
return w.ready
}
func (w *BasicWindow) Box() *gui.Node {
if w.win == nil {
w.Draw()
}
return w.box
}
func (w *BasicWindow) Vertical() {
log.Log(GADGETS, "BasicWindow() Vertical() START w.vertical =", w.vertical, w.title)
if w == nil {
return
}
w.vertical = true
log.Log(GADGETS, "BasicWindow() Vertical() END w.vertical =", w.vertical, w.title)
}
func (w *BasicWindow) Horizontal() {
log.Log(GADGETS, "BasicWindow() Horizontal() START w.vertical =", w.vertical, w.title)
if w == nil {
return
}
w.vertical = false
log.Log(GADGETS, "BasicWindow() Horizontal() END w.vertical =", w.vertical, w.title)
}
2024-02-12 15:22:17 -06:00
func (w *BasicWindow) Make() *BasicWindow {
if w.win != nil {
2024-02-12 15:22:17 -06:00
log.Warn("BasicWindow.Make() window was already created")
return w
}
// w.win = w.parent.RawWindow(w.title)
w.win = gui.RawWindow(w.title)
// if Custom isn't set, set it here. This prevents the application from
// os.Exit() if the window is closed. It destroy's the widgets in the toolkit plugins
// the the window still exists in the binary tree and functions normally
// I like to call this Sierpinski mode
if w.win.Custom == nil {
w.win.Custom = func() {
w.hidden = true
log.Log(GADGETS, "BasicWindow.Custom() closed. handled properly?", w.title)
if w.Custom != nil {
w.Custom()
}
}
}
if w.vertical {
w.box = w.win.NewVerticalBox("BW VBOX")
log.Log(GADGETS, "BasicWindow.Make() made NewVerticalBox", w.title)
} else {
w.box = w.win.NewHorizontalBox("BW HBOX")
log.Log(GADGETS, "BasicWindow.Make() made NewHorizontalBox", w.title)
}
w.win.SetVisable(false)
w.ready = true
2024-02-12 15:22:17 -06:00
return w
}
func (w *BasicWindow) TestDraw() {
if !w.Initialized() {
return
}
if w.win == nil {
log.Log(GADGETS, "BasicWindow.TestDraw() can't draw on window == nil")
w.Make()
}
log.Log(GADGETS, "BasicWindow.TestDraw() RUNNING HERE")
w.win.TestDraw()
}
func (w *BasicWindow) Draw() {
if w.win == nil {
log.Log(GADGETS, "BasicWindow.Draw() needs Make()")
w.Make()
}
w.win.TestDraw()
}