package gui // Common actions for widgets like 'Enable' or 'Hide' import ( "regexp" "go.wit.com/log" "go.wit.com/gui/widget" ) // functions for handling text related GUI elements func (n *Node) Show() *Node { if ! n.Ready() { return n } if ! n.Hidden() { return n } n.hidden = false n.changed = true if n.WidgetType == widget.Window { log.Warn("Show on a hidden window", n.progname) log.Warn("this needs to do TestDraw()") n.TestDraw() return n } // inform the toolkits sendAction(n, widget.Show) return n } func (n *Node) Hide() *Node { if ! n.Ready() { return n } if n.Hidden() { return n } if n.WidgetType == widget.Window { log.Warn("Hide on a window", n.progname) log.Warn("this needs to do TestDestroy() ?") n.Destroy() n.hidden = true n.changed = true return nil } n.hidden = true n.changed = true // inform the toolkits sendAction(n, widget.Hide) return n } // enables a widget so the user can see it and work/click/etc on it // by default, widgets are enabled when they are created func (n *Node) Enable() *Node { if ! n.Ready() { return n } // if n.enabled { return n } n.enabled = true n.changed = true // inform the toolkits sendAction(n, widget.Enable) return n } // disables a widget so the user can see it, but can not // interact or change it. func (n *Node) Disable() *Node { if ! n.Ready() { return n } // if ! n.enabled { return n } n.enabled = false n.changed = true // inform the toolkits sendAction(n, widget.Disable) return n } func (n *Node) Destroy() { if ! n.Ready() { return } // if ! n.enabled { return } n.enabled = false n.changed = true // inform the toolkits sendAction(n, widget.Delete) return } // add a new text string to widgets that support // multiple string values // These must be unique. return false if the string already exists func (n *Node) AddText(str string) bool { if ! n.Ready() { return false } log.Log(CHANGE, "AddText() value =", str) n.value = str // TODO: make sure these are unique n.strings = append(n.strings, str) // inform the toolkits sendAction(n, widget.AddText) return true } // appends text to the existing text // TODO: this is an experiement func (n *Node) AppendText(str string) { if ! n.Ready() { return } tmp := widget.GetString(n.value) + str n.value = tmp n.changed = true // inform the toolkits sendAction(n, widget.SetText) } // THESE TWO FUNCTIONS ARE TERRIBLY NAMED AND NEED TO BE FIXED // 5 seconds worth of ideas: // Value() ? // Progname() Reference() ? // 2024/01/13 the names are starting to grow on me and make it clearer to code against // get a string from the widget func (n *Node) GetText() string { if ! n.Ready() { return "" } return widget.GetString(n.value) } // 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 func (n *Node) SetProgName(s string) *Node { if ! n.Ready() { return n } if n.progname == s { // don't do anything since nothing changed return n } n.changed = true n.progname = s return n } /* TODO: ensure these are unique and make a way to look them up myButton = myGroup.NewButton("hit ball", nil).SetName("HIT") myButton.GetName() should return "HIT" n = Find("HIT") should return myButton */ func (n *Node) GetProgName() string { if ! n.Ready() { return "" } return n.progname } /* func commonCallback(n *Node) { // TODO: make all of this common code to all the widgets // This might be common everywhere finally (2023/03/01) if (n.Custom == nil) { log.Log(CHANGE, "Not Running n.Custom(n) == nil") } else { log.Log(CHANGE, "Running n.Custom(n)") n.Custom() } } */ func (n *Node) Margin() *Node { if ! n.Ready() { return n } if n.margin { return n } n.margin = true n.changed = true log.Warn("Margin()", n.WidgetType, n.progname) // inform the toolkits sendAction(n, widget.Margin) return n } func (n *Node) Unmargin() *Node { if ! n.Ready() { return n } if ! n.margin { return n } n.margin = false n.changed = true // inform the toolkits sendAction(n, widget.Unmargin) return n } func (n *Node) Pad() *Node { if ! n.Ready() { return n } if n.pad == true { return n } // nothing changed n.pad = true n.changed = true log.Warn("Pad()", n.WidgetType, n.progname) // inform the toolkits sendAction(n, widget.Pad) return n } func (n *Node) Unpad() *Node { if ! n.Ready() { return n } if n.pad == false { return n } // nothing changed n.pad = false n.changed = true // inform the toolkits sendAction(n, widget.Unpad) return n } func (n *Node) Expand() *Node { if ! n.Ready() { return n } if n.expand == true { return n } // nothing changed n.expand = true n.changed = true // inform the toolkits sendAction(n, widget.SetExpand) return n } func (n *Node) SetExpand(b bool) *Node { if ! n.Ready() { return n } if n.expand == b { return n } // nothing changed n.expand = b n.changed = true // inform the toolkits sendAction(n, widget.SetExpand) return n } // is the widget currently viewable? func (n *Node) Hidden() bool { if ! n.Ready() { return false } return n.hidden } func (n *Node) Ready() bool { 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 } return true } // // // DEPRECATE / REDO / SORT OUT THIS STUFF // // // This should not really do anything. as per the docs, the "Standard()" way // should be the default way /* func (n *Node) Standard() *Node { log.Warn("Standard() not implemented yet") return n } func (n *Node) SetMargin() *Node { log.Warn("DoMargin() not implemented yet") return n } */ /* func (n *Node) Window(title string) *Node { log.Warn("Window()", n) return n.NewWindow(title) } */ /* func (n *Node) Add(str string) { log.Log(GUI, "gui.Add() value =", str) n.value = str // inform the toolkits sendAction(n, widget.Add) } */ /* func (n *Node) SetNext(w int, h int) { n.NextW = w n.NextH = h log.Info("SetNext() w,h =", n.NextW, n.NextH) } */ /* // 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) { log.Log(GUI, "%q is not valid\n", username) } } 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 { log.Log(GUI, "normalizeInt() regexp.Compile() ERROR =", err) return s } clean := reg.ReplaceAllString(s, "") log.Log(GUI, "normalizeInt() s =", clean) return clean }