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-12-20 14:15:00 -06:00
|
|
|
"go.wit.com/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-04-08 11:06:50 -05:00
|
|
|
func (n *Node) Show() *Node {
|
2023-05-09 19:24:37 -05:00
|
|
|
a := newAction(n, toolkit.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 {
|
2023-05-09 19:24:37 -05:00
|
|
|
a := newAction(n, toolkit.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 {
|
2023-05-09 19:24:37 -05:00
|
|
|
a := newAction(n, toolkit.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 {
|
2023-05-09 19:24:37 -05:00
|
|
|
a := newAction(n, toolkit.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) {
|
|
|
|
log(debugGui, "gui.Add() value =", str)
|
2023-03-23 12:35:12 -05:00
|
|
|
|
2023-05-09 19:24:37 -05:00
|
|
|
n.S = str
|
|
|
|
|
|
|
|
a := newAction(n, toolkit.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) {
|
|
|
|
log(debugChange, "AddText() value =", str)
|
|
|
|
|
2023-03-30 08:51:33 -05:00
|
|
|
n.Text = str
|
2023-05-09 19:24:37 -05:00
|
|
|
n.S = str
|
|
|
|
|
|
|
|
a := newAction(n, toolkit.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 {
|
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-05-09 19:24:37 -05:00
|
|
|
n.S = text
|
|
|
|
|
|
|
|
a := newAction(n, toolkit.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
|
|
|
|
log(debugNow, "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) {
|
|
|
|
log(debugChange, "Set() value =", val)
|
|
|
|
|
|
|
|
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:
|
2023-05-09 19:04:39 -05:00
|
|
|
log(debugError, "Set() unknown type =", v)
|
2023-03-03 14:41:38 -06:00
|
|
|
}
|
2023-03-23 12:35:12 -05:00
|
|
|
|
2023-05-09 19:04:39 -05:00
|
|
|
a := newAction(n, toolkit.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
|
|
|
|
|
|
|
|
a := newAction(n, toolkit.SetText)
|
|
|
|
sendAction(a)
|
2022-10-21 11:40:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Node) GetText() string {
|
2023-04-23 07:37:24 -05:00
|
|
|
return n.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-05-09 19:24:37 -05:00
|
|
|
a := newAction(n, toolkit.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 {
|
2023-05-09 19:24:37 -05:00
|
|
|
a := newAction(n, toolkit.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 {
|
2023-05-09 19:24:37 -05:00
|
|
|
a := newAction(n, toolkit.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 {
|
2023-05-09 19:24:37 -05:00
|
|
|
a := newAction(n, toolkit.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 {
|
|
|
|
a := newAction(n, toolkit.Pad)
|
|
|
|
a.Expand = true
|
|
|
|
sendAction(a)
|
|
|
|
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 {
|
|
|
|
log(debugError, "Window()", n)
|
2023-04-08 15:34:36 -05:00
|
|
|
return n.NewWindow(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-12-20 05:58:33 -06:00
|
|
|
/*
|
2023-03-29 23:03:04 -05:00
|
|
|
func (n *Node) Standard() *Node {
|
2023-12-20 05:58:33 -06:00
|
|
|
log(debugInfo, "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 {
|
2023-04-08 09:17:17 -05:00
|
|
|
log(debugError, "DoMargin() not implemented yet")
|
2023-03-29 23:03:04 -05:00
|
|
|
return n
|
|
|
|
}
|
2023-12-20 05:58:33 -06:00
|
|
|
*/
|