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"
|
2023-03-23 12:35:12 -05:00
|
|
|
"git.wit.org/wit/gui/toolkit"
|
2023-03-01 11:35:36 -06:00
|
|
|
)
|
2022-10-19 13:23:22 -05:00
|
|
|
|
|
|
|
// functions for handling text related GUI elements
|
|
|
|
|
2023-03-23 12:35:12 -05:00
|
|
|
func (n *Node) Show() {
|
|
|
|
var a toolkit.Action
|
|
|
|
a.Type = toolkit.Show
|
|
|
|
newaction(&a, n, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Node) Hide() {
|
|
|
|
var a toolkit.Action
|
|
|
|
a.Type = toolkit.Hide
|
|
|
|
newaction(&a, n, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Node) Enable() {
|
|
|
|
var a toolkit.Action
|
|
|
|
a.Type = toolkit.Enable
|
|
|
|
newaction(&a, n, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Node) Disable() {
|
|
|
|
var a toolkit.Action
|
|
|
|
a.Type = toolkit.Disable
|
|
|
|
newaction(&a, n, nil)
|
|
|
|
}
|
|
|
|
|
2023-03-03 14:41:38 -06:00
|
|
|
func (n *Node) Add(str string) {
|
|
|
|
log(debugGui, "gui.Add() value =", str)
|
2023-03-23 12:35:12 -05:00
|
|
|
|
|
|
|
var a toolkit.Action
|
|
|
|
a.Type = toolkit.Add
|
|
|
|
a.S = str
|
|
|
|
// a.Widget = &n.widget
|
|
|
|
// action(&a)
|
|
|
|
newaction(&a, n, nil)
|
2023-03-03 14:41:38 -06:00
|
|
|
}
|
|
|
|
|
2023-03-23 12:35:12 -05:00
|
|
|
func (n *Node) AddText(str string) {
|
|
|
|
log(debugChange, "AddText() value =", str)
|
|
|
|
|
|
|
|
var a toolkit.Action
|
|
|
|
a.Type = toolkit.AddText
|
|
|
|
a.S = str
|
|
|
|
// a.Widget = &n.widget
|
|
|
|
// action(&a)
|
|
|
|
newaction(&a, n, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Node) SetText(str string) {
|
|
|
|
log(debugChange, "SetText() value =", str)
|
|
|
|
|
|
|
|
var a toolkit.Action
|
|
|
|
a.Type = toolkit.SetText
|
|
|
|
a.S = str
|
|
|
|
// a.Widget = &n.widget
|
|
|
|
// action(&a)
|
|
|
|
newaction(&a, n, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Node) SetNext(x int, y int) {
|
|
|
|
n.NextX = x
|
|
|
|
n.NextY = y
|
|
|
|
log(debugError, "SetNext() x,y =", n.NextX, n.NextY)
|
|
|
|
log(debugError, "SetNext() x,y =", n.NextX, n.NextY)
|
|
|
|
log(debugError, "SetNext() x,y =", n.NextX, n.NextY)
|
|
|
|
log(debugError, "SetNext() x,y =", n.NextX, n.NextY)
|
|
|
|
log(debugError, "SetNext() x,y =", n.NextX, n.NextY)
|
|
|
|
log(debugError, "SetNext() x,y =", n.NextX, n.NextY)
|
2023-03-01 11:35:36 -06:00
|
|
|
}
|
|
|
|
|
2023-03-23 12:35:12 -05:00
|
|
|
func (n *Node) Set(val any) {
|
|
|
|
log(debugChange, "Set() value =", val)
|
|
|
|
var a toolkit.Action
|
|
|
|
a.Type = toolkit.Set
|
|
|
|
|
|
|
|
switch v := val.(type) {
|
2023-03-03 14:41:38 -06:00
|
|
|
case bool:
|
2023-03-23 12:35:12 -05:00
|
|
|
a.B = val.(bool)
|
2023-03-03 14:41:38 -06:00
|
|
|
case string:
|
2023-03-23 12:35:12 -05:00
|
|
|
a.S = val.(string)
|
2023-03-03 14:41:38 -06:00
|
|
|
case int:
|
2023-03-23 12:35:12 -05:00
|
|
|
a.I = val.(int)
|
2023-03-03 14:41:38 -06:00
|
|
|
default:
|
2023-03-23 12:35:12 -05:00
|
|
|
log(debugError, "Set() unknown type =", v, "a =", a)
|
2023-03-03 14:41:38 -06:00
|
|
|
}
|
2023-03-23 12:35:12 -05:00
|
|
|
|
|
|
|
// a.Widget = &n.widget
|
|
|
|
// action(&a)
|
|
|
|
newaction(&a, n, nil)
|
2023-03-03 14:41:38 -06:00
|
|
|
}
|
|
|
|
|
2023-03-23 12:35:12 -05:00
|
|
|
func (n *Node) AppendText(str string) {
|
|
|
|
var a toolkit.Action
|
|
|
|
a.Type = toolkit.SetText
|
2023-03-01 11:35:36 -06:00
|
|
|
tmp := n.widget.S + str
|
2023-03-23 12:35:12 -05:00
|
|
|
log(debugChange, "AppendText() value =", tmp)
|
|
|
|
a.S = tmp
|
|
|
|
// a.Widget = &n.widget
|
|
|
|
// action(&a)
|
|
|
|
newaction(&a, n, nil)
|
2022-10-21 11:40:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Node) GetText() string {
|
2023-03-01 11:35:36 -06:00
|
|
|
return n.widget.S
|
2022-10-19 13:23:22 -05:00
|
|
|
}
|
2022-10-20 06:55:42 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
// 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) {
|
2023-02-25 14:05:25 -06:00
|
|
|
log(debugGui, "%q is not valid\n", username)
|
2022-10-20 06:55:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2023-02-25 14:05:25 -06:00
|
|
|
log(debugGui, "normalizeInt() regexp.Compile() ERROR =", err)
|
2022-10-20 06:55:42 -05:00
|
|
|
return s
|
|
|
|
}
|
|
|
|
clean := reg.ReplaceAllString(s, "")
|
2023-02-25 14:05:25 -06:00
|
|
|
log(debugGui, "normalizeInt() s =", clean)
|
2022-10-20 06:55:42 -05:00
|
|
|
return clean
|
|
|
|
}
|
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) {
|
|
|
|
log(debugChange, "Not Running n.Custom(n) == nil")
|
2022-11-13 08:53:03 -06:00
|
|
|
} else {
|
2023-03-01 11:35:36 -06:00
|
|
|
log(debugChange, "Running n.Custom(n)")
|
|
|
|
n.Custom()
|
2022-11-13 08:53:03 -06:00
|
|
|
}
|
|
|
|
}
|