gocui/click.go

398 lines
8.9 KiB
Go

package main
import (
"fmt"
"github.com/awesome-gocui/gocui"
"go.wit.com/log"
"go.wit.com/toolkits/tree"
"go.wit.com/widget"
)
// set isCurrent = false everywhere
func unsetCurrent(w *guiWidget) {
w.isCurrent = false
if w.node.WidgetType == widget.Tab {
// n.tk.color = &colorTab
// n.setColor()
}
for _, child := range w.children {
unsetCurrent(child)
}
}
// when adding a new widget, this will update the display
// of the current widgets if that widget is supposed
// to be in current display
func updateCurrent(n *tree.Node) {
var w *guiWidget
w = n.TK.(*guiWidget)
log.Log(NOW, "updateCurrent()", w.String())
if n.WidgetType == widget.Tab {
if w.IsCurrent() {
// n.tk.color = &colorActiveT
setColor(n, &colorActiveT)
w.hideView()
w.showView()
setCurrentTab(n)
} else {
// n.tk.color = &colorTab
// n.setColor()
}
return
}
if n.WidgetType == widget.Window {
if w.IsCurrent() {
// setCurrentWindow(n)
}
return
}
if n.WidgetType == widget.Root {
return
}
updateCurrent(n.Parent)
}
// shows the widgets in a window
func setCurrentWindow(n *tree.Node) {
var w *guiWidget
w = n.TK.(*guiWidget)
if w.IsCurrent() {
return
}
if n.WidgetType != widget.Window {
return
}
var rootTK *guiWidget
rootTK = me.treeRoot.TK.(*guiWidget)
unsetCurrent(rootTK)
if w.hasTabs {
// set isCurrent = true on the first tab
for _, child := range w.children {
child.isCurrent = true
break
}
} else {
w.isCurrent = true
}
}
// shows the widgets in a tab
func setCurrentTab(n *tree.Node) {
var w, p, rootTK *guiWidget
w = n.TK.(*guiWidget)
if n.WidgetType != widget.Tab {
return
}
rootTK = me.treeRoot.TK.(*guiWidget)
unsetCurrent(rootTK)
w.isCurrent = true
p = n.Parent.TK.(*guiWidget)
p.isCurrent = true
log.Log(NOW, "setCurrent()", n.String())
}
func doWidgetClick(n *tree.Node) {
switch n.WidgetType {
case widget.Root:
// THIS IS THE BEGINING OF THE LAYOUT
log.Log(NOW, "doWidgetClick()", n.String())
redoWindows(0, 0)
case widget.Flag:
log.Log(NOW, "doWidgetClick() FLAG widget name =", n.String())
log.Log(NOW, "doWidgetClick() if this is the dropdown menu, handle it here?")
case widget.Window:
if me.currentWindow == n {
return
}
if me.currentWindow != nil {
var w *guiWidget
w = me.currentWindow.TK.(*guiWidget)
unsetCurrent(w)
setColor(me.currentWindow, &colorWindow)
w.hideWidgets()
}
me.currentWindow = n
// setCurrentWindow(n) // probably delete this
setColor(n, &colorActiveW)
var w *guiWidget
w = n.TK.(*guiWidget)
w.hideWidgets()
w.redoTabs(me.TabW, me.TabH)
for _, child := range w.children {
if child.currentTab == true {
log.Log(NOW, "FOUND CURRENT TAB", child.String())
setCurrentTab(child.node)
placeWidgets(child.node, me.RawW, me.RawH)
child.showWidgets()
return
}
}
/* FIXME: redo this
if ! n.hasTabs {
}
*/
case widget.Tab:
var w *guiWidget
w = n.TK.(*guiWidget)
if w.IsCurrent() {
return // do nothing if you reclick on the already selected tab
}
// find the window and disable the active tab
p := n.Parent
if p != nil {
var w *guiWidget
w = p.TK.(*guiWidget)
w.hideWidgets()
w.redoTabs(me.TabW, me.TabH)
unsetCurrent(w)
for _, child := range w.children {
if child.node.WidgetType == widget.Tab {
setColor(child.node, &colorTab)
child.currentTab = false
}
}
}
w.currentTab = true
setColor(n, &colorActiveT)
setCurrentTab(n)
placeWidgets(n, me.RawW, me.RawH)
w.showWidgets()
case widget.Group:
// n.placeWidgets(p.tk.startH, newH)
toggleTree(n)
case widget.Checkbox:
var w *guiWidget
w = n.TK.(*guiWidget)
if widget.GetBool(w.value) {
w.setCheckbox(false)
} else {
w.setCheckbox(true)
}
// n.doUserEvent()
me.myTree.SendUserEvent(me.treeRoot)
case widget.Grid:
newR := realGocuiSize(n)
// w,h := n.logicalSize()
// w := newR.w1 - newR.w0
// h := newR.h1 - newR.h0
placeGrid(n, newR.w0, newR.h0)
var w *guiWidget
w = n.TK.(*guiWidget)
w.showWidgets()
case widget.Box:
var w *guiWidget
w = n.TK.(*guiWidget)
// w.showWidgetPlacement(logNow, "drawTree()")
if w.direction == widget.Horizontal {
log.Log(NOW, "BOX IS HORIZONTAL", n.String())
} else {
log.Log(NOW, "BOX IS VERTICAL", n.String())
}
placeWidgets(n, me.RawW, me.RawH)
toggleTree(n)
case widget.Button:
// doUserEvent(n)
me.myTree.SendUserEvent(n)
case widget.Dropdown:
log.Log(NOW, "do the dropdown here")
if me.ddview == nil {
me.ddview = addDropdown()
// n.TK = initWidget(n)
var tk *guiWidget
tk = me.ddview.TK.(*guiWidget)
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 {
return
}
tk.v.Wrap = true
tk.v.Frame = true
tk.v.Clear()
fmt.Fprint(tk.v, "example.com\nwit.com")
SetVisible(me.ddview, true)
return
}
log.Log(NOW, "doWidgetClick() visible =", Visible(me.ddview))
var tk *guiWidget
tk = me.ddview.TK.(*guiWidget)
if Visible(me.ddview) {
SetVisible(me.ddview, false)
me.baseGui.DeleteView("ddview")
tk.v = nil
} else {
var dnsList string
for i, s := range tk.vals {
log.Log(NOW, "AddText()", n.String(), i, s)
dnsList += s + "\n"
}
me.ddNode = n
log.Log(NOW, "new dns list should be set to:", dnsList)
tk.label = dnsList
tk.SetText(dnsList)
SetVisible(me.ddview, true)
}
for i, s := range tk.vals {
log.Log(NOW, "AddText()", tk.String(), i, s)
}
default:
}
}
var toggle bool = true
func toggleTree(n *tree.Node) {
var w *guiWidget
w = n.TK.(*guiWidget)
if toggle {
drawTree(n, toggle)
toggle = false
} else {
w.hideWidgets()
toggle = true
}
}
// display the widgets in the binary tree
func drawTree(n *tree.Node, draw bool) {
var w *guiWidget
w = n.TK.(*guiWidget)
if w == nil {
return
}
showWidgetPlacement(n, true, "drawTree()")
if draw {
// w.textResize()
w.showView()
} else {
w.deleteView()
}
for _, child := range w.children {
drawTree(child.node, draw)
}
}
func click(g *gocui.Gui, v *gocui.View) error {
// var l string
// var err error
log.Log(INFO, "click() START", v.Name())
// n := me.rootNode.findWidgetName(v.Name())
n := findUnderMouse()
var w *guiWidget
w = n.TK.(*guiWidget)
if n != nil {
log.Log(NOW, "click() Found widget =", n.WidgetId, n.String(), ",", w.label)
if n.String() == "DropBox" {
log.Log(NOW, "click() this is the dropdown menu. set a flag here what did I click? where is the mouse?")
log.Log(NOW, "click() set a global dropdown clicked flag=true here")
me.ddClicked = true
}
doWidgetClick(n)
} else {
log.Log(NOW, "click() could not find node name =", v.Name())
}
if _, err := g.SetCurrentView(v.Name()); err != nil {
log.Log(NOW, "click() END err =", err)
return err
}
log.Log(NOW, "click() END")
return nil
}
func findUnderMouse() *tree.Node {
var found *tree.Node
var widgets []*tree.Node
var f func(n *tree.Node)
w, h := me.baseGui.MousePosition()
// find buttons that are below where the mouse button click
f = func(n *tree.Node) {
var widget *guiWidget
widget = n.TK.(*guiWidget)
// ignore widgets that are not visible
if Visible(n) {
if (widget.gocuiSize.w0 <= w) && (w <= widget.gocuiSize.w1) &&
(widget.gocuiSize.h0 <= h) && (h <= widget.gocuiSize.h1) {
widgets = append(widgets, n)
found = n
}
}
if n == me.ddview {
log.Log(NOW, "findUnderMouse() found ddview")
if Visible(n) {
log.Log(NOW, "findUnderMouse() and ddview is visable. hide it here. TODO: find highlighted row")
found = n
// find the actual value here and set the dropdown widget
me.baseGui.DeleteView("ddview")
} else {
log.Log(NOW, "findUnderMouse() I was lying, actually it's not found")
}
}
for _, child := range widget.children {
f(child.node)
}
}
f(me.treeRoot)
// widgets has everything that matches
// TODO: pop up menu with a list of them
for _, n := range widgets {
//log(logNow, "ctrlDown() FOUND widget", widget.id, widget.name)
showWidgetPlacement(n, true, "findUnderMouse() FOUND")
}
return found
}
// find the widget under the mouse click
func ctrlDown(g *gocui.Gui, v *gocui.View) error {
var found *tree.Node
// var widgets []*node
// var f func (n *node)
found = findUnderMouse()
if me.ctrlDown == nil {
setupCtrlDownWidget()
var tk *guiWidget
tk = me.ctrlDown.TK.(*guiWidget)
tk.label = found.String()
tk.cuiName = "ctrlDown"
// me.ctrlDown.parent = me.rootNode
}
var tk *guiWidget
tk = me.ctrlDown.TK.(*guiWidget)
if found == nil {
found = me.treeRoot
}
tk.label = found.String()
newR := realGocuiSize(found)
tk.gocuiSize.w0 = newR.w0
tk.gocuiSize.h0 = newR.h0
tk.gocuiSize.w1 = newR.w1
tk.gocuiSize.h1 = newR.h1
if tk.Visible() {
// me.ctrlDown.hideView()
} else {
// me.ctrlDown.showView()
}
// me.ctrlDown.showWidgetPlacement(true, "ctrlDown:")
return nil
}