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
|
2023-03-29 23:03:04 -05:00
|
|
|
a.ActionType = toolkit.Show
|
2023-03-23 12:35:12 -05:00
|
|
|
newaction(&a, n, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Node) Hide() {
|
|
|
|
var a toolkit.Action
|
2023-03-29 23:03:04 -05:00
|
|
|
a.ActionType = toolkit.Hide
|
2023-03-23 12:35:12 -05:00
|
|
|
newaction(&a, n, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Node) Enable() {
|
|
|
|
var a toolkit.Action
|
2023-03-29 23:03:04 -05:00
|
|
|
a.ActionType = toolkit.Enable
|
2023-03-23 12:35:12 -05:00
|
|
|
newaction(&a, n, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Node) Disable() {
|
|
|
|
var a toolkit.Action
|
2023-03-29 23:03:04 -05:00
|
|
|
a.ActionType = toolkit.Disable
|
2023-03-23 12:35:12 -05:00
|
|
|
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
|
2023-03-29 23:03:04 -05:00
|
|
|
a.ActionType = toolkit.Add
|
2023-03-23 12:35:12 -05:00
|
|
|
a.S = str
|
|
|
|
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)
|
|
|
|
|
2023-03-30 08:51:33 -05:00
|
|
|
n.Text = str
|
2023-03-23 12:35:12 -05:00
|
|
|
var a toolkit.Action
|
2023-03-29 23:03:04 -05:00
|
|
|
a.ActionType = toolkit.AddText
|
2023-03-23 12:35:12 -05:00
|
|
|
a.S = str
|
|
|
|
newaction(&a, n, nil)
|
|
|
|
}
|
|
|
|
|
2023-04-08 09:17:17 -05:00
|
|
|
func (n *Node) SetText(text string) *Node{
|
2023-03-30 08:51:33 -05:00
|
|
|
log(debugChange, "SetText() value =", text)
|
2023-03-23 12:35:12 -05:00
|
|
|
|
2023-03-30 08:51:33 -05:00
|
|
|
n.Text = text
|
2023-03-23 12:35:12 -05:00
|
|
|
var a toolkit.Action
|
2023-03-29 23:03:04 -05:00
|
|
|
a.ActionType = toolkit.SetText
|
2023-03-30 08:51:33 -05:00
|
|
|
a.S = text
|
2023-03-23 12:35:12 -05:00
|
|
|
newaction(&a, n, nil)
|
2023-04-08 09:17:17 -05:00
|
|
|
return n
|
2023-03-23 12:35:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2023-03-29 23:03:04 -05:00
|
|
|
a.ActionType = toolkit.Set
|
2023-03-23 12:35:12 -05:00
|
|
|
|
|
|
|
switch v := val.(type) {
|
2023-03-03 14:41:38 -06:00
|
|
|
case bool:
|
2023-03-30 08:51:33 -05:00
|
|
|
n.B = val.(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-30 08:51:33 -05:00
|
|
|
n.Text = val.(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-30 08:51:33 -05:00
|
|
|
n.I = val.(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
|
|
|
|
|
|
|
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
|
2023-03-29 23:03:04 -05:00
|
|
|
a.ActionType = 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
|
2023-03-30 08:51:33 -05:00
|
|
|
n.Text = tmp
|
2023-03-23 12:35:12 -05:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2023-03-29 23:03:04 -05:00
|
|
|
|
2023-04-08 09:17:17 -05:00
|
|
|
func (n *Node) Margin() *Node {
|
2023-03-29 23:03:04 -05:00
|
|
|
var a toolkit.Action
|
|
|
|
a.ActionType = toolkit.Margin
|
|
|
|
newaction(&a, n, nil)
|
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 {
|
2023-03-29 23:03:04 -05:00
|
|
|
var a toolkit.Action
|
|
|
|
a.ActionType = toolkit.Unmargin
|
|
|
|
newaction(&a, n, nil)
|
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 {
|
2023-03-29 23:03:04 -05:00
|
|
|
var a toolkit.Action
|
|
|
|
a.ActionType = toolkit.Pad
|
|
|
|
newaction(&a, n, nil)
|
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 {
|
2023-03-29 23:03:04 -05:00
|
|
|
var a toolkit.Action
|
|
|
|
a.ActionType = toolkit.Unpad
|
|
|
|
newaction(&a, n, nil)
|
2023-04-08 09:17:17 -05:00
|
|
|
return n
|
2023-03-29 23:03:04 -05:00
|
|
|
}
|
|
|
|
|
2023-03-30 08:51:33 -05:00
|
|
|
// is this better?
|
2023-04-08 09:17:17 -05:00
|
|
|
// yes, this is better. it allows Internationalization very easily
|
2023-03-30 08:51:33 -05:00
|
|
|
// me.window = myGui.New2().Window("DNS and IPv6 Control Panel").Standard()
|
2023-04-08 09:17:17 -05:00
|
|
|
// myFunnyWindow = myGui.New().Window("Hello").Standard().SetText("Hola")
|
2023-03-30 08:51:33 -05:00
|
|
|
|
2023-03-29 23:03:04 -05:00
|
|
|
func (n *Node) New2() *Node {
|
2023-04-08 09:17:17 -05:00
|
|
|
log(debugNow, "New2() Start")
|
|
|
|
return n.NewWindow2("New2")
|
2023-03-29 23:03:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Node) Window(title string) *Node {
|
|
|
|
log(debugError, "Window()", n)
|
2023-04-08 09:17:17 -05:00
|
|
|
return n.NewWindow2(title)
|
2023-03-29 23:03:04 -05:00
|
|
|
}
|
|
|
|
|
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-03-29 23:03:04 -05:00
|
|
|
func (n *Node) Standard() *Node {
|
2023-04-08 09:17:17 -05:00
|
|
|
log(debugError, "Standard() not implemented yet")
|
2023-03-29 23:03:04 -05:00
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Node) DoMargin() *Node {
|
2023-04-08 09:17:17 -05:00
|
|
|
log(debugError, "DoMargin() not implemented yet")
|
2023-03-29 23:03:04 -05:00
|
|
|
return n
|
|
|
|
}
|