2022-10-19 13:23:22 -05:00
|
|
|
package gui
|
|
|
|
|
2023-03-23 12:35:12 -05:00
|
|
|
// Common actions for widgets like 'Enable' or 'Hide'
|
|
|
|
|
2023-03-01 11:35:36 -06:00
|
|
|
import (
|
|
|
|
"regexp"
|
2024-01-03 18:54:08 -06:00
|
|
|
"go.wit.com/log"
|
2024-01-05 13:18:44 -06:00
|
|
|
"go.wit.com/gui/widget"
|
2023-03-01 11:35:36 -06:00
|
|
|
)
|
2022-10-19 13:23:22 -05:00
|
|
|
|
|
|
|
// functions for handling text related GUI elements
|
|
|
|
|
2023-04-08 11:06:50 -05:00
|
|
|
func (n *Node) Show() *Node {
|
2024-01-13 22:02:12 -06:00
|
|
|
if ! n.Ready() { return n }
|
|
|
|
if ! n.Hidden() { return n }
|
|
|
|
|
|
|
|
n.hidden = false
|
|
|
|
n.changed = true
|
|
|
|
|
2024-01-14 10:37:43 -06:00
|
|
|
if n.WidgetType == widget.Window {
|
|
|
|
log.Warn("Show on a hidden window", n.progname)
|
|
|
|
log.Warn("this needs to do TestDraw()")
|
|
|
|
n.TestDraw()
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
2024-01-13 22:02:12 -06:00
|
|
|
// inform the toolkits
|
|
|
|
sendAction(n, widget.Show)
|
2023-04-08 11:06:50 -05:00
|
|
|
return n
|
2023-03-23 12:35:12 -05:00
|
|
|
}
|
|
|
|
|
2023-04-08 11:06:50 -05:00
|
|
|
func (n *Node) Hide() *Node {
|
2024-01-13 22:02:12 -06:00
|
|
|
if ! n.Ready() { return n }
|
|
|
|
if n.Hidden() { return n }
|
|
|
|
|
2024-01-15 16:14:48 -06:00
|
|
|
|
2024-01-14 10:37:43 -06:00
|
|
|
if n.WidgetType == widget.Window {
|
|
|
|
log.Warn("Hide on a window", n.progname)
|
|
|
|
log.Warn("this needs to do TestDestroy() ?")
|
2024-01-15 16:14:48 -06:00
|
|
|
n.Destroy()
|
|
|
|
n.hidden = true
|
|
|
|
n.changed = true
|
|
|
|
return nil
|
2024-01-14 10:37:43 -06:00
|
|
|
}
|
|
|
|
|
2024-01-13 22:02:12 -06:00
|
|
|
n.hidden = true
|
|
|
|
n.changed = true
|
2024-01-15 16:14:48 -06:00
|
|
|
|
2024-01-13 22:02:12 -06:00
|
|
|
// inform the toolkits
|
|
|
|
sendAction(n, widget.Hide)
|
2023-04-08 11:06:50 -05:00
|
|
|
return n
|
2023-03-23 12:35:12 -05:00
|
|
|
}
|
|
|
|
|
2024-01-13 22:02:12 -06:00
|
|
|
// enables a widget so the user can see it and work/click/etc on it
|
|
|
|
// by default, widgets are enabled when they are created
|
2023-04-08 11:06:50 -05:00
|
|
|
func (n *Node) Enable() *Node {
|
2024-01-13 22:02:12 -06:00
|
|
|
if ! n.Ready() { return n }
|
|
|
|
// if n.enabled { return n }
|
2023-03-23 12:35:12 -05:00
|
|
|
|
2024-01-13 22:02:12 -06:00
|
|
|
n.enabled = true
|
|
|
|
n.changed = true
|
|
|
|
|
|
|
|
// inform the toolkits
|
|
|
|
sendAction(n, widget.Enable)
|
2023-04-08 11:06:50 -05:00
|
|
|
return n
|
2023-03-23 12:35:12 -05:00
|
|
|
}
|
|
|
|
|
2024-01-13 22:02:12 -06:00
|
|
|
// 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 }
|
2023-03-23 12:35:12 -05:00
|
|
|
|
2024-01-13 22:02:12 -06:00
|
|
|
n.enabled = false
|
|
|
|
n.changed = true
|
2023-05-09 19:24:37 -05:00
|
|
|
|
2024-01-13 22:02:12 -06:00
|
|
|
// inform the toolkits
|
|
|
|
sendAction(n, widget.Disable)
|
|
|
|
return n
|
2023-03-03 14:41:38 -06:00
|
|
|
}
|
|
|
|
|
2024-01-15 16:14:48 -06:00
|
|
|
func (n *Node) Destroy() {
|
|
|
|
if ! n.Ready() { return }
|
|
|
|
// if ! n.enabled { return }
|
|
|
|
|
|
|
|
n.enabled = false
|
|
|
|
n.changed = true
|
|
|
|
|
|
|
|
// inform the toolkits
|
|
|
|
sendAction(n, widget.Delete)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-13 22:02:12 -06:00
|
|
|
|
|
|
|
// add a new text string to widgets that support
|
|
|
|
// multiple string values
|
|
|
|
// These must be unique. return false if the string already exists
|
|
|
|
func (n *Node) AddText(str string) bool {
|
|
|
|
if ! n.Ready() { return false }
|
2024-01-03 18:54:08 -06:00
|
|
|
log.Log(CHANGE, "AddText() value =", str)
|
2023-03-23 12:35:12 -05:00
|
|
|
|
2024-01-11 19:32:40 -06:00
|
|
|
n.value = str
|
2024-01-13 22:02:12 -06:00
|
|
|
// TODO: make sure these are unique
|
|
|
|
n.strings = append(n.strings, str)
|
2023-05-09 19:24:37 -05:00
|
|
|
|
2024-01-13 22:02:12 -06:00
|
|
|
// inform the toolkits
|
|
|
|
sendAction(n, widget.AddText)
|
|
|
|
return true
|
2023-03-23 12:35:12 -05:00
|
|
|
}
|
|
|
|
|
2023-03-01 11:35:36 -06:00
|
|
|
|
2024-01-13 22:02:12 -06:00
|
|
|
// appends text to the existing text
|
|
|
|
// TODO: this is an experiement
|
2023-03-23 12:35:12 -05:00
|
|
|
func (n *Node) AppendText(str string) {
|
2024-01-13 22:02:12 -06:00
|
|
|
if ! n.Ready() { return }
|
2024-01-11 19:32:40 -06:00
|
|
|
tmp := widget.GetString(n.value) + str
|
|
|
|
n.value = tmp
|
2024-01-13 22:02:12 -06:00
|
|
|
n.changed = true
|
2023-05-09 19:24:37 -05:00
|
|
|
|
2024-01-13 22:02:12 -06:00
|
|
|
// inform the toolkits
|
|
|
|
sendAction(n, widget.SetText)
|
2022-10-21 11:40:08 -05:00
|
|
|
}
|
|
|
|
|
2024-01-03 13:37:03 -06:00
|
|
|
// THESE TWO FUNCTIONS ARE TERRIBLY NAMED AND NEED TO BE FIXED
|
|
|
|
// 5 seconds worth of ideas:
|
|
|
|
// Value() ?
|
|
|
|
// Progname() Reference() ?
|
2024-01-13 22:02:12 -06:00
|
|
|
// 2024/01/13 the names are starting to grow on me and make it clearer to code against
|
2024-01-03 13:37:03 -06:00
|
|
|
|
2024-01-11 19:32:40 -06:00
|
|
|
// get a string from the widget
|
2022-10-21 11:40:08 -05:00
|
|
|
func (n *Node) GetText() string {
|
2024-01-11 19:32:40 -06:00
|
|
|
if ! n.Ready() { return "" }
|
|
|
|
return widget.GetString(n.value)
|
2022-10-19 13:23:22 -05:00
|
|
|
}
|
2022-10-20 06:55:42 -05:00
|
|
|
|
2024-01-11 19:32:40 -06:00
|
|
|
// get a string from the widget
|
|
|
|
func (n *Node) GetInt() int {
|
|
|
|
if ! n.Ready() { return -1 }
|
|
|
|
return widget.GetInt(n.value)
|
|
|
|
}
|
|
|
|
|
|
|
|
// get a string from the widget
|
|
|
|
func (n *Node) GetBool() bool {
|
|
|
|
if ! n.Ready() { return false}
|
|
|
|
return widget.GetBool(n.value)
|
|
|
|
}
|
|
|
|
|
|
|
|
// should get the reference name used for programming and debugging
|
2024-01-15 16:14:48 -06:00
|
|
|
func (n *Node) SetProgName(s string) *Node {
|
|
|
|
if ! n.Ready() { return n }
|
2024-01-03 13:37:03 -06:00
|
|
|
|
2024-01-13 22:02:12 -06:00
|
|
|
if n.progname == s {
|
|
|
|
// don't do anything since nothing changed
|
2024-01-15 16:14:48 -06:00
|
|
|
return n
|
2024-01-13 22:02:12 -06:00
|
|
|
}
|
2022-10-20 06:55:42 -05:00
|
|
|
|
2024-01-13 22:02:12 -06:00
|
|
|
n.changed = true
|
|
|
|
n.progname = s
|
2024-01-15 16:14:48 -06:00
|
|
|
return n
|
2022-10-20 06:55:42 -05:00
|
|
|
}
|
|
|
|
|
2024-01-13 22:02:12 -06:00
|
|
|
/*
|
|
|
|
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
|
2022-10-20 06:55:42 -05:00
|
|
|
*/
|
2024-01-13 22:02:12 -06:00
|
|
|
func (n *Node) GetProgName() string {
|
|
|
|
if ! n.Ready() { return "" }
|
|
|
|
return n.progname
|
2022-10-20 06:55:42 -05:00
|
|
|
}
|
2022-11-13 08:53:03 -06:00
|
|
|
|
2024-01-13 22:02:12 -06:00
|
|
|
/*
|
2022-11-13 08:53:03 -06:00
|
|
|
func commonCallback(n *Node) {
|
|
|
|
// TODO: make all of this common code to all the widgets
|
2023-03-01 11:35:36 -06:00
|
|
|
// This might be common everywhere finally (2023/03/01)
|
|
|
|
if (n.Custom == nil) {
|
2024-01-03 18:54:08 -06:00
|
|
|
log.Log(CHANGE, "Not Running n.Custom(n) == nil")
|
2022-11-13 08:53:03 -06:00
|
|
|
} else {
|
2024-01-03 18:54:08 -06:00
|
|
|
log.Log(CHANGE, "Running n.Custom(n)")
|
2023-03-01 11:35:36 -06:00
|
|
|
n.Custom()
|
2022-11-13 08:53:03 -06:00
|
|
|
}
|
|
|
|
}
|
2024-01-13 22:02:12 -06:00
|
|
|
*/
|
2023-03-29 23:03:04 -05:00
|
|
|
|
2023-04-08 09:17:17 -05:00
|
|
|
func (n *Node) Margin() *Node {
|
2024-01-13 22:02:12 -06:00
|
|
|
if ! n.Ready() { return n }
|
|
|
|
if n.margin { return n }
|
|
|
|
|
2024-01-06 13:53:15 -06:00
|
|
|
n.margin = true
|
2024-01-13 22:02:12 -06:00
|
|
|
n.changed = true
|
|
|
|
|
2024-01-14 10:37:43 -06:00
|
|
|
log.Warn("Margin()", n.WidgetType, n.progname)
|
2024-01-13 22:02:12 -06:00
|
|
|
// inform the toolkits
|
|
|
|
sendAction(n, widget.Margin)
|
2023-04-08 09:17:17 -05:00
|
|
|
return n
|
2023-03-29 23:03:04 -05:00
|
|
|
}
|
|
|
|
|
2023-04-08 09:17:17 -05:00
|
|
|
func (n *Node) Unmargin() *Node {
|
2024-01-13 22:02:12 -06:00
|
|
|
if ! n.Ready() { return n }
|
|
|
|
if ! n.margin { return n }
|
|
|
|
|
2024-01-06 13:53:15 -06:00
|
|
|
n.margin = false
|
2024-01-13 22:02:12 -06:00
|
|
|
n.changed = true
|
|
|
|
|
|
|
|
// inform the toolkits
|
|
|
|
sendAction(n, widget.Unmargin)
|
2023-04-08 09:17:17 -05:00
|
|
|
return n
|
2023-03-29 23:03:04 -05:00
|
|
|
}
|
|
|
|
|
2023-04-08 09:17:17 -05:00
|
|
|
func (n *Node) Pad() *Node {
|
2024-01-13 22:02:12 -06:00
|
|
|
if ! n.Ready() { return n }
|
|
|
|
if n.pad == true { return n } // nothing changed
|
|
|
|
|
2024-01-06 13:53:15 -06:00
|
|
|
n.pad = true
|
2024-01-13 22:02:12 -06:00
|
|
|
n.changed = true
|
|
|
|
|
2024-01-14 10:37:43 -06:00
|
|
|
log.Warn("Pad()", n.WidgetType, n.progname)
|
2024-01-13 22:02:12 -06:00
|
|
|
// inform the toolkits
|
|
|
|
sendAction(n, widget.Pad)
|
2023-04-08 09:17:17 -05:00
|
|
|
return n
|
2023-03-29 23:03:04 -05:00
|
|
|
}
|
|
|
|
|
2023-04-08 09:17:17 -05:00
|
|
|
func (n *Node) Unpad() *Node {
|
2024-01-13 22:02:12 -06:00
|
|
|
if ! n.Ready() { return n }
|
|
|
|
if n.pad == false { return n } // nothing changed
|
|
|
|
|
2024-01-06 13:53:15 -06:00
|
|
|
n.pad = false
|
2024-01-13 22:02:12 -06:00
|
|
|
n.changed = true
|
|
|
|
|
|
|
|
// inform the toolkits
|
|
|
|
sendAction(n, widget.Unpad)
|
2023-04-08 09:17:17 -05:00
|
|
|
return n
|
2023-03-29 23:03:04 -05:00
|
|
|
}
|
|
|
|
|
2023-12-20 05:58:33 -06:00
|
|
|
func (n *Node) Expand() *Node {
|
2024-01-13 22:02:12 -06:00
|
|
|
if ! n.Ready() { return n }
|
|
|
|
if n.expand == true { return n } // nothing changed
|
|
|
|
|
2024-01-06 13:53:15 -06:00
|
|
|
n.expand = true
|
2024-01-13 22:02:12 -06:00
|
|
|
n.changed = true
|
|
|
|
|
|
|
|
// inform the toolkits
|
|
|
|
sendAction(n, widget.SetExpand)
|
2023-12-20 05:58:33 -06:00
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
2024-01-13 22:02:12 -06:00
|
|
|
func (n *Node) SetExpand(b bool) *Node {
|
|
|
|
if ! n.Ready() { return n }
|
|
|
|
if n.expand == b { return n } // nothing changed
|
2023-03-30 08:51:33 -05:00
|
|
|
|
2024-01-13 22:02:12 -06:00
|
|
|
n.expand = b
|
|
|
|
n.changed = true
|
|
|
|
|
|
|
|
// inform the toolkits
|
|
|
|
sendAction(n, widget.SetExpand)
|
|
|
|
return n
|
2023-03-29 23:03:04 -05:00
|
|
|
}
|
2024-01-11 19:32:40 -06:00
|
|
|
|
2024-01-13 22:02:12 -06:00
|
|
|
// is the widget currently viewable?
|
|
|
|
func (n *Node) Hidden() bool {
|
|
|
|
if ! n.Ready() { return false }
|
|
|
|
return n.hidden
|
2024-01-11 19:32:40 -06:00
|
|
|
}
|
2023-03-29 23:03:04 -05:00
|
|
|
|
2024-01-04 12:34:42 -06:00
|
|
|
func (n *Node) Ready() bool {
|
2024-01-11 00:51:37 -06:00
|
|
|
if n == nil {
|
|
|
|
log.Warn("Ready() got node == nil")
|
2024-01-13 22:02:12 -06:00
|
|
|
// TODO: figure out if you can identify the code trace
|
|
|
|
// to help find the root cause
|
2024-01-11 00:51:37 -06:00
|
|
|
return false
|
|
|
|
}
|
2024-01-04 12:34:42 -06:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2024-01-13 22:02:12 -06:00
|
|
|
//
|
|
|
|
//
|
|
|
|
// DEPRECATE / REDO / SORT OUT THIS STUFF
|
|
|
|
//
|
|
|
|
//
|
|
|
|
|
2023-04-08 09:17:17 -05:00
|
|
|
// This should not really do anything. as per the docs, the "Standard()" way
|
|
|
|
// should be the default way
|
2023-12-20 05:58:33 -06:00
|
|
|
/*
|
2023-03-29 23:03:04 -05:00
|
|
|
func (n *Node) Standard() *Node {
|
2024-01-03 18:54:08 -06:00
|
|
|
log.Warn("Standard() not implemented yet")
|
2023-03-29 23:03:04 -05:00
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
2023-12-03 16:08:39 -06:00
|
|
|
func (n *Node) SetMargin() *Node {
|
2024-01-03 18:54:08 -06:00
|
|
|
log.Warn("DoMargin() not implemented yet")
|
2023-03-29 23:03:04 -05:00
|
|
|
return n
|
|
|
|
}
|
2023-12-20 05:58:33 -06:00
|
|
|
*/
|
2024-01-13 22:02:12 -06:00
|
|
|
|
|
|
|
/*
|
|
|
|
func (n *Node) Window(title string) *Node {
|
|
|
|
log.Warn("Window()", n)
|
|
|
|
return n.NewWindow(title)
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
func (n *Node) Add(str string) {
|
|
|
|
log.Log(GUI, "gui.Add() value =", str)
|
|
|
|
|
|
|
|
n.value = str
|
|
|
|
|
|
|
|
// inform the toolkits
|
|
|
|
sendAction(n, widget.Add)
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
func (n *Node) SetNext(w int, h int) {
|
|
|
|
n.NextW = w
|
|
|
|
n.NextH = h
|
|
|
|
log.Info("SetNext() w,h =", n.NextW, n.NextH)
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
// 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
|
|
|
|
}
|