gocui/dropdown.go

157 lines
3.8 KiB
Go
Raw Normal View History

package main
import (
"fmt"
"strings"
"github.com/awesome-gocui/gocui"
log "go.wit.com/log"
"go.wit.com/toolkits/tree"
"go.wit.com/widget"
)
func makeDropdownView(ddItems string) *guiWidget {
newNode := addDropdown()
tk := newNode.TK.(*guiWidget)
tk.labelN = ddItems
tk.SetText(ddItems)
tk.gocuiSize.w0 = 100
tk.gocuiSize.w1 = 120
tk.gocuiSize.h0 = 15
tk.gocuiSize.h1 = 18
tk.v, _ = me.baseGui.SetView("ddview",
tk.gocuiSize.w0,
tk.gocuiSize.h0,
tk.gocuiSize.w1,
tk.gocuiSize.h1, 0)
if tk.v == nil {
return tk
}
tk.v.Wrap = true
tk.v.Frame = true
tk.v.Clear()
fmt.Fprint(tk.v, ddItems)
tk.Show()
return tk
}
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
}
func (tk *guiWidget) showDropdown() {
var ddItems string
// todo: fix this after switching to protobuf
// var items []string
// items = tk.node.State.Strings
//for i, s := range items {
for i, s := range tk.vals {
log.Log(NOW, "showDropdown()", tk.String(), i, s)
ddItems += s + "\n"
}
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()
}
func hideDDview() error {
w, h := me.baseGui.MousePosition()
log.Log(NOW, "hide dropdown menu() view msgMouseDown (w,h) =", w, h)
if me.dropdownV == nil {
return gocui.ErrUnknownView
}
if me.dropdownV.v == nil {
return gocui.ErrUnknownView
}
me.dropdownV.SetVisible(false)
return nil
}
func showDDview() error {
w, h := me.baseGui.MousePosition()
log.Log(NOW, "show dropdown menu() view msgMouseDown (w,h) =", w, h)
if me.dropdownV == nil {
return gocui.ErrUnknownView
}
if me.dropdownV.v == nil {
return gocui.ErrUnknownView
}
me.dropdownV.SetVisible(true)
return nil
}
// if there is a drop down view active, treat it like a dialog box and close it
func (w *guiWidget) dropdownClicked(mouseW, mouseH int) string {
w.Hide()
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 ""
}
items := strings.Split(w.labelN, "\n")
if len(items) >= itemNumber-1 {
log.Log(NOW, "dropdownClicked() found", items[itemNumber-1])
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.dropdownW.node.SetValue(items[itemNumber-1])
me.myTree.SendUserEvent(me.dropdownW.node)
}
}
return items[itemNumber-1]
}
return ""
}
func dropdownUnclicked(mouseX, mouseH int) {
if me.dropdownV == nil {
log.Log(NOW, "mouseUp() dropdownV = nil", mouseX, mouseH)
return
}
tk := me.dropdownV
log.Log(NOW, "mouseUp() view msgMouseDown (check here for dropdown menu click) (w,h) =", mouseX, mouseH)
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)
}