debian/andlabs/dropdown.go

88 lines
1.8 KiB
Go
Raw Normal View History

2024-01-01 16:11:54 -06:00
package main
import (
"github.com/andlabs/ui"
_ "github.com/andlabs/ui/winmanifest"
"go.wit.com/log"
"go.wit.com/gui/widget"
2024-01-01 16:11:54 -06:00
)
func (p *node) newDropdown(n *node) {
newt := new(guiWidget)
log.Log(INFO, "gui.Toolbox.newDropdown() START", n.progname)
2024-01-01 16:11:54 -06:00
cb := ui.NewCombobox()
newt.uiCombobox = cb
newt.uiControl = cb
// initialize the index
newt.c = 0
newt.val = make(map[int]string)
cb.OnSelected(func(spin *ui.Combobox) {
i := spin.Selected()
if (newt.val == nil) {
log.Log(ERROR, "make map didn't work")
n.value = "map did not work. ui.Combobox error"
2024-01-01 16:11:54 -06:00
} else {
n.value = newt.val[i]
2024-01-01 16:11:54 -06:00
}
n.doUserEvent()
})
2024-01-01 16:11:54 -06:00
n.tk = newt
p.place(n)
// add the initial dropdown entries
for i, s := range n.strings {
log.Warn("add dropdown: add entries on create", n.progname, i, s)
n.addDropdownName(s)
2024-01-01 16:11:54 -06:00
}
cur := widget.GetString(n.value)
log.Warn("add dropdown: set default value on create", n.progname, cur)
n.setDropdownName(cur)
2024-01-01 16:11:54 -06:00
}
func (n *node) SetDropdownInt(i int) {
if ! n.ready() { return }
n.tk.uiCombobox.SetSelected(i)
2024-01-01 16:11:54 -06:00
}
func (n *node) addDropdownName(s string) {
if ! n.ready() { return }
log.Log(INFO, "addDropdownName()", n.WidgetId, "add:", s)
2024-01-01 16:11:54 -06:00
n.tk.uiCombobox.Append(s)
if (n.tk.val == nil) {
log.Log(INFO, "make map didn't work")
2024-01-01 16:11:54 -06:00
return
}
n.tk.val[n.tk.c] = s
// If this is the first menu added, set the dropdown to it
if (n.tk.c == 0) {
log.Log(INFO, "THIS IS THE FIRST Dropdown", s)
n.tk.uiCombobox.SetSelected(0)
}
n.tk.c = n.tk.c + 1
2024-01-01 16:11:54 -06:00
}
func (n *node) setDropdownName(s string) bool {
if ! n.ready() { return false}
log.Log(INFO, "SetDropdownName()", n.WidgetId, ",", s)
2024-01-01 16:11:54 -06:00
for i, tmp := range n.tk.val {
if s == tmp {
n.value = s
n.SetDropdownInt(i)
log.Warn("SetDropdownInt() worked", tmp, i)
return true
}
2024-01-01 16:11:54 -06:00
}
log.Warn("SetDropdownName() failed", s)
return false
2024-01-01 16:11:54 -06:00
}