2024-02-01 20:12:26 -06:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2024-02-02 14:49:17 -06:00
|
|
|
"strings"
|
2024-02-01 20:12:26 -06:00
|
|
|
|
|
|
|
"github.com/awesome-gocui/gocui"
|
|
|
|
log "go.wit.com/log"
|
|
|
|
"go.wit.com/toolkits/tree"
|
|
|
|
"go.wit.com/widget"
|
|
|
|
)
|
|
|
|
|
2024-02-02 11:07:56 -06:00
|
|
|
func makeDropdownView(ddItems string) *guiWidget {
|
2024-02-01 20:12:26 -06:00
|
|
|
newNode := addDropdown()
|
|
|
|
tk := newNode.TK.(*guiWidget)
|
|
|
|
tk.labelN = ddItems
|
|
|
|
tk.SetText(ddItems)
|
|
|
|
tk.gocuiSize.w0 = 20
|
|
|
|
tk.gocuiSize.w1 = 40
|
|
|
|
tk.gocuiSize.h0 = 10
|
|
|
|
tk.gocuiSize.h1 = 25
|
|
|
|
tk.v, _ = me.baseGui.SetView("ddview",
|
|
|
|
tk.gocuiSize.w0,
|
|
|
|
tk.gocuiSize.h0,
|
|
|
|
tk.gocuiSize.w1,
|
|
|
|
tk.gocuiSize.h1, 0)
|
|
|
|
if tk.v == nil {
|
2024-02-02 11:07:56 -06:00
|
|
|
return tk
|
2024-02-01 20:12:26 -06:00
|
|
|
}
|
|
|
|
tk.v.Wrap = true
|
|
|
|
tk.v.Frame = true
|
|
|
|
tk.v.Clear()
|
|
|
|
fmt.Fprint(tk.v, ddItems)
|
|
|
|
tk.SetVisible(true)
|
2024-02-02 11:07:56 -06:00
|
|
|
return tk
|
2024-02-01 20:12:26 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func addDropdown() *tree.Node {
|
|
|
|
n := new(tree.Node)
|
|
|
|
n.WidgetType = widget.Flag
|
|
|
|
n.WidgetId = 2222
|
|
|
|
n.ParentId = 0
|
|
|
|
|
|
|
|
// store the internal toolkit information
|
|
|
|
tk := new(guiWidget)
|
|
|
|
tk.frame = true
|
|
|
|
tk.labelN = "DropBox text"
|
|
|
|
|
|
|
|
tk.node = n
|
|
|
|
// copy the data from the action message
|
|
|
|
tk.node.State.Label = "DropBox"
|
|
|
|
|
|
|
|
// set the name used by gocui to the id
|
|
|
|
tk.cuiName = "-1 dropbox"
|
|
|
|
|
|
|
|
tk.color = &colorFlag
|
|
|
|
|
|
|
|
// add this new widget on the binary tree
|
|
|
|
tk.parent = me.treeRoot.TK.(*guiWidget)
|
|
|
|
if tk.parent == nil {
|
|
|
|
panic("addDropdown() didn't get treeRoot guiWidget")
|
|
|
|
} else {
|
|
|
|
tk.parent.children = append(tk.parent.children, tk)
|
|
|
|
}
|
|
|
|
|
|
|
|
n.TK = tk
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
2024-02-02 11:07:56 -06:00
|
|
|
func (tk *guiWidget) showDropdown() {
|
|
|
|
var ddItems string
|
2024-02-02 14:49:17 -06:00
|
|
|
// todo: fix this after switching to protobuf
|
|
|
|
// var items []string
|
|
|
|
// items = tk.node.State.Strings
|
|
|
|
//for i, s := range items {
|
2024-02-02 11:07:56 -06:00
|
|
|
for i, s := range tk.vals {
|
|
|
|
log.Log(NOW, "showDropdown()", tk.String(), i, s)
|
|
|
|
ddItems += s + "\n"
|
2024-02-01 20:12:26 -06:00
|
|
|
}
|
2024-02-02 11:07:56 -06:00
|
|
|
|
2024-02-02 14:49:17 -06:00
|
|
|
log.Log(NOW, "new dropdown items should be set to:", ddItems)
|
|
|
|
sizeW, sizeH := tk.Size()
|
|
|
|
log.Log(NOW, "showDropdown() size W,H=", sizeW, sizeH)
|
|
|
|
startW, startH := tk.Position()
|
|
|
|
log.Log(NOW, "showDropdown() location W,H=", startW, startH)
|
|
|
|
me.dropdownV.MoveToOffset(startW+3, startH+2)
|
|
|
|
me.dropdownV.labelN = ddItems
|
|
|
|
me.dropdownV.Show()
|
2024-02-01 20:12:26 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func hideDDview() error {
|
|
|
|
w, h := me.baseGui.MousePosition()
|
|
|
|
log.Log(NOW, "hide dropdown menu() view msgMouseDown (w,h) =", w, h)
|
2024-02-02 11:07:56 -06:00
|
|
|
if me.dropdownV == nil {
|
2024-02-01 20:12:26 -06:00
|
|
|
return gocui.ErrUnknownView
|
|
|
|
}
|
2024-02-02 11:07:56 -06:00
|
|
|
if me.dropdownV.v == nil {
|
2024-02-01 20:12:26 -06:00
|
|
|
return gocui.ErrUnknownView
|
|
|
|
}
|
2024-02-02 11:07:56 -06:00
|
|
|
me.dropdownV.SetVisible(false)
|
2024-02-01 20:12:26 -06:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func showDDview() error {
|
|
|
|
w, h := me.baseGui.MousePosition()
|
|
|
|
log.Log(NOW, "show dropdown menu() view msgMouseDown (w,h) =", w, h)
|
2024-02-02 11:07:56 -06:00
|
|
|
if me.dropdownV == nil {
|
2024-02-01 20:12:26 -06:00
|
|
|
return gocui.ErrUnknownView
|
|
|
|
}
|
2024-02-02 11:07:56 -06:00
|
|
|
if me.dropdownV.v == nil {
|
2024-02-01 20:12:26 -06:00
|
|
|
return gocui.ErrUnknownView
|
|
|
|
}
|
2024-02-02 11:07:56 -06:00
|
|
|
me.dropdownV.SetVisible(true)
|
2024-02-01 20:12:26 -06:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-02-01 21:47:31 -06:00
|
|
|
// if there is a drop down view active, treat it like a dialog box and close it
|
2024-02-02 14:49:17 -06:00
|
|
|
func (w *guiWidget) dropdownClicked(mouseW, mouseH int) string {
|
|
|
|
w.Hide()
|
2024-02-02 11:47:32 -06:00
|
|
|
|
2024-02-02 14:49:17 -06:00
|
|
|
startW, startH := w.Position()
|
|
|
|
log.Log(NOW, "dropdownClicked() start (w,h) =", startW, startH)
|
|
|
|
log.Log(NOW, "dropdownClicked() at (w,h) =", mouseW, mouseH)
|
|
|
|
|
|
|
|
itemNumber := mouseH - startH
|
|
|
|
log.Log(NOW, "dropdownClicked() look for item", itemNumber)
|
|
|
|
if itemNumber < 1 {
|
|
|
|
return ""
|
2024-02-02 11:47:32 -06:00
|
|
|
}
|
2024-02-01 20:12:26 -06:00
|
|
|
|
2024-02-02 14:49:17 -06:00
|
|
|
items := strings.Split(w.labelN, "\n")
|
|
|
|
if len(items) >= itemNumber-1 {
|
|
|
|
log.Log(NOW, "dropdownClicked() found", items[itemNumber-1])
|
2024-02-02 15:12:25 -06:00
|
|
|
if items[itemNumber-1] != "" {
|
|
|
|
if me.dropdownW != nil {
|
|
|
|
log.Log(NOW, "dropdownClicked() send event for", me.dropdownW.cuiName, me.dropdownW.WidgetType)
|
|
|
|
me.dropdownW.SetText(items[itemNumber-1])
|
|
|
|
me.myTree.SendUserEvent(me.dropdownW.node)
|
|
|
|
}
|
|
|
|
}
|
2024-02-02 14:49:17 -06:00
|
|
|
return items[itemNumber-1]
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
2024-02-02 11:47:32 -06:00
|
|
|
|
2024-02-01 20:12:26 -06:00
|
|
|
func dropdownUnclicked(mouseX, mouseH int) {
|
2024-02-02 11:07:56 -06:00
|
|
|
tk := me.dropdownV
|
2024-02-01 20:12:26 -06:00
|
|
|
log.Log(NOW, "mouseUp() view msgMouseDown (check here for dropdown menu click) (w,h) =", mouseX, mouseH)
|
2024-02-02 11:07:56 -06:00
|
|
|
log.Log(NOW, "mouseUp() ddview is the thing that was clicked", mouseX, mouseH)
|
|
|
|
log.Log(NOW, "mouseUp() find out what the string is here", mouseX, mouseH, tk.gocuiSize.h1)
|
2024-02-01 20:12:26 -06:00
|
|
|
}
|