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-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
|
|
|
|
2024-01-11 19:32:40 -06:00
|
|
|
n.value = str
|
2023-05-09 19:24:37 -05:00
|
|
|
|
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
|
|
|
|
2024-01-11 19:32:40 -06:00
|
|
|
n.value = str
|
2023-05-09 19:24:37 -05:00
|
|
|
|
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 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) AppendText(str string) {
|
2024-01-11 19:32:40 -06:00
|
|
|
tmp := widget.GetString(n.value) + str
|
|
|
|
n.value = tmp
|
2023-05-09 19:24:37 -05:00
|
|
|
|
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() ?
|
|
|
|
|
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-03 13:37:03 -06:00
|
|
|
// myButton = myGroup.NewButton("hit ball", nil).SetName("HIT")
|
|
|
|
// myButton.GetName() should return "HIT"
|
|
|
|
// n = Find("HIT") should return myButton
|
|
|
|
func (n *Node) GetName() string {
|
2024-01-11 19:32:40 -06:00
|
|
|
if ! n.Ready() { return "" }
|
|
|
|
return n.progname
|
2024-01-03 13:37:03 -06: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) {
|
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
|
|
|
|
2024-01-11 19:32:40 -06: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-11 19:32:40 -06:00
|
|
|
*/
|
|
|
|
|
|
|
|
func (n *Node) ProgName() string {
|
|
|
|
if ! n.Ready() { return "" }
|
|
|
|
return n.progname
|
|
|
|
}
|
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")
|
|
|
|
// TODO: figure out if you can identify the code trace to help find the root cause
|
|
|
|
return false
|
|
|
|
}
|
2024-01-04 12:34:42 -06:00
|
|
|
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
|
|
|
*/
|