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
|
|
|
"errors"
|
|
|
|
"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-06 13:53:15 -06:00
|
|
|
if ! n.hidden {
|
|
|
|
a := newAction(n, widget.Show)
|
|
|
|
sendAction(a)
|
|
|
|
}
|
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-06 13:53:15 -06:00
|
|
|
if ! n.hidden {
|
|
|
|
a := newAction(n, widget.Hide)
|
|
|
|
sendAction(a)
|
|
|
|
}
|
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) Enable() *Node {
|
2024-01-06 13:53:15 -06:00
|
|
|
if ! n.hidden {
|
|
|
|
a := newAction(n, widget.Enable)
|
|
|
|
sendAction(a)
|
|
|
|
}
|
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) Disable() *Node {
|
2024-01-06 13:53:15 -06:00
|
|
|
if ! n.hidden {
|
|
|
|
a := newAction(n, widget.Disable)
|
|
|
|
sendAction(a)
|
|
|
|
}
|
2023-04-08 11:06:50 -05:00
|
|
|
return n
|
2023-03-23 12:35:12 -05:00
|
|
|
}
|
|
|
|
|
2023-03-03 14:41:38 -06:00
|
|
|
func (n *Node) Add(str string) {
|
2024-01-03 18:54:08 -06:00
|
|
|
log.Log(GUI, "gui.Add() value =", str)
|
2023-03-23 12:35:12 -05:00
|
|
|
|
2023-05-09 19:24:37 -05:00
|
|
|
n.S = str
|
|
|
|
|
2024-01-06 13:53:15 -06:00
|
|
|
if ! n.hidden {
|
|
|
|
a := newAction(n, widget.Add)
|
|
|
|
sendAction(a)
|
|
|
|
}
|
2023-03-03 14:41:38 -06:00
|
|
|
}
|
|
|
|
|
2023-03-23 12:35:12 -05:00
|
|
|
func (n *Node) AddText(str string) {
|
2024-01-03 18:54:08 -06:00
|
|
|
log.Log(CHANGE, "AddText() value =", str)
|
2023-03-23 12:35:12 -05:00
|
|
|
|
2023-03-30 08:51:33 -05:00
|
|
|
n.Text = str
|
2023-05-09 19:24:37 -05:00
|
|
|
n.S = str
|
|
|
|
|
2024-01-06 13:53:15 -06:00
|
|
|
if ! n.hidden {
|
|
|
|
a := newAction(n, widget.AddText)
|
|
|
|
sendAction(a)
|
|
|
|
}
|
2023-03-23 12:35:12 -05:00
|
|
|
}
|
|
|
|
|
2023-05-09 19:24:37 -05:00
|
|
|
func (n *Node) SetText(text string) *Node {
|
2024-01-03 18:54:08 -06:00
|
|
|
log.Log(CHANGE, "SetText() value =", text)
|
2023-03-23 12:35:12 -05:00
|
|
|
|
2023-03-30 08:51:33 -05:00
|
|
|
n.Text = text
|
2023-05-09 19:24:37 -05:00
|
|
|
n.S = text
|
|
|
|
|
2024-01-06 13:53:15 -06:00
|
|
|
if ! n.hidden {
|
|
|
|
a := newAction(n, widget.SetText)
|
|
|
|
sendAction(a)
|
|
|
|
}
|
2023-04-08 09:17:17 -05:00
|
|
|
return n
|
2023-03-23 12:35:12 -05:00
|
|
|
}
|
|
|
|
|
2023-05-09 17:48:21 -05:00
|
|
|
func (n *Node) SetNext(w int, h int) {
|
|
|
|
n.NextW = w
|
|
|
|
n.NextH = h
|
2024-01-03 18:54:08 -06:00
|
|
|
log.Info("SetNext() w,h =", n.NextW, n.NextH)
|
2023-03-01 11:35:36 -06:00
|
|
|
}
|
|
|
|
|
2023-03-23 12:35:12 -05:00
|
|
|
func (n *Node) Set(val any) {
|
2024-01-03 18:54:08 -06:00
|
|
|
log.Log(CHANGE, "Set() value =", val)
|
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-03 14:41:38 -06:00
|
|
|
case string:
|
2023-03-30 08:51:33 -05:00
|
|
|
n.Text = val.(string)
|
2023-05-09 19:04:39 -05:00
|
|
|
n.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-03 14:41:38 -06:00
|
|
|
default:
|
2024-01-03 18:54:08 -06:00
|
|
|
log.Error(errors.New("Set() unknown type"), "v =", v)
|
2023-03-03 14:41:38 -06:00
|
|
|
}
|
2023-03-23 12:35:12 -05:00
|
|
|
|
2024-01-06 13:53:15 -06:00
|
|
|
if ! n.hidden {
|
|
|
|
a := newAction(n, widget.Set)
|
|
|
|
sendAction(a)
|
|
|
|
}
|
2023-03-03 14:41:38 -06:00
|
|
|
}
|
|
|
|
|
2023-03-23 12:35:12 -05:00
|
|
|
func (n *Node) AppendText(str string) {
|
2023-04-23 07:37:24 -05:00
|
|
|
tmp := n.S + str
|
2023-03-30 08:51:33 -05:00
|
|
|
n.Text = tmp
|
2023-05-09 19:24:37 -05:00
|
|
|
n.S = tmp
|
|
|
|
|
2024-01-06 13:53:15 -06:00
|
|
|
if ! n.hidden {
|
|
|
|
a := newAction(n, widget.SetText)
|
|
|
|
sendAction(a)
|
|
|
|
}
|
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() ?
|
|
|
|
|
|
|
|
// should get the value of the node
|
2022-10-21 11:40:08 -05:00
|
|
|
func (n *Node) GetText() string {
|
2024-01-10 17:03:13 -06:00
|
|
|
if n.value != nil {
|
|
|
|
return n.value.(string)
|
|
|
|
}
|
2023-12-31 07:40:53 -06:00
|
|
|
if (n.S != n.Text) {
|
2024-01-10 17:03:13 -06:00
|
|
|
log.Warn("GetText() is screwed up. TODO: fix this dumb crap. n.S =", n.S, "and n.Text =", n.Text)
|
2023-12-31 07:40:53 -06:00
|
|
|
}
|
|
|
|
if (n.S != "") {
|
|
|
|
return n.S
|
|
|
|
}
|
|
|
|
return n.Text
|
2022-10-19 13:23:22 -05:00
|
|
|
}
|
2022-10-20 06:55:42 -05:00
|
|
|
|
2024-01-03 13:37:03 -06:00
|
|
|
// should get the value of the node
|
|
|
|
// myButton = myGroup.NewButton("hit ball", nil).SetName("HIT")
|
|
|
|
// myButton.GetName() should return "HIT"
|
|
|
|
// n = Find("HIT") should return myButton
|
|
|
|
func (n *Node) GetName() string {
|
|
|
|
return n.Name
|
|
|
|
}
|
|
|
|
|
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) {
|
2024-01-03 18:54:08 -06:00
|
|
|
log.Log(GUI, "%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 {
|
2024-01-03 18:54:08 -06:00
|
|
|
log.Log(GUI, "normalizeInt() regexp.Compile() ERROR =", err)
|
2022-10-20 06:55:42 -05:00
|
|
|
return s
|
|
|
|
}
|
|
|
|
clean := reg.ReplaceAllString(s, "")
|
2024-01-03 18:54:08 -06:00
|
|
|
log.Log(GUI, "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) {
|
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
|
|
|
}
|
|
|
|
}
|
2023-03-29 23:03:04 -05:00
|
|
|
|
2023-04-08 09:17:17 -05:00
|
|
|
func (n *Node) Margin() *Node {
|
2024-01-06 13:53:15 -06:00
|
|
|
n.margin = true
|
|
|
|
if ! n.hidden {
|
|
|
|
a := newAction(n, widget.Margin)
|
|
|
|
sendAction(a)
|
|
|
|
}
|
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-06 13:53:15 -06:00
|
|
|
n.margin = false
|
|
|
|
if ! n.hidden {
|
|
|
|
a := newAction(n, widget.Unmargin)
|
|
|
|
sendAction(a)
|
|
|
|
}
|
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-06 13:53:15 -06:00
|
|
|
n.pad = true
|
|
|
|
if ! n.hidden {
|
|
|
|
a := newAction(n, widget.Pad)
|
|
|
|
sendAction(a)
|
|
|
|
}
|
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-06 13:53:15 -06:00
|
|
|
n.pad = false
|
|
|
|
if ! n.hidden {
|
|
|
|
a := newAction(n, widget.Unpad)
|
|
|
|
sendAction(a)
|
|
|
|
}
|
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-06 13:53:15 -06:00
|
|
|
n.expand = true
|
|
|
|
if ! n.hidden {
|
|
|
|
a := newAction(n, widget.Pad)
|
|
|
|
a.Expand = true
|
|
|
|
sendAction(a)
|
|
|
|
}
|
2023-12-20 05:58:33 -06:00
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
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 11:06:50 -05:00
|
|
|
// myFunnyWindow = myGui.NewWindow("Hello").Standard().SetText("Hola")
|
2023-03-30 08:51:33 -05:00
|
|
|
|
2023-03-29 23:03:04 -05:00
|
|
|
func (n *Node) Window(title string) *Node {
|
2024-01-03 18:54:08 -06:00
|
|
|
log.Warn("Window()", n)
|
2023-04-08 15:34:36 -05:00
|
|
|
return n.NewWindow(title)
|
2023-03-29 23:03:04 -05:00
|
|
|
}
|
|
|
|
|
2024-01-04 12:34:42 -06:00
|
|
|
func (n *Node) Ready() bool {
|
|
|
|
if n == nil {return false}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
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
|
|
|
*/
|