gocui/click.go

384 lines
8.7 KiB
Go
Raw Normal View History

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 (w *guiWidget) unsetCurrent() {
w.isCurrent = false
if w.node.WidgetType == widget.Tab {
// n.tk.color = &colorTab
// n.setColor()
}
for _, child := range w.children {
child.unsetCurrent()
}
}
// 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 (w *guiWidget) updateCurrent() {
log.Log(NOW, "updateCurrent()", w.String())
if w.WidgetType == widget.Tab {
if w.IsCurrent() {
// n.tk.color = &colorActiveT
w.setColor(&colorActiveT)
w.hideView()
w.showView()
w.setCurrentTab()
} else {
// n.tk.color = &colorTab
// n.setColor()
}
return
}
if w.WidgetType == widget.Window {
if w.IsCurrent() {
// setCurrentWindow(n)
}
return
}
if w.WidgetType == widget.Root {
return
}
w.parent.updateCurrent()
}
// shows the widgets in a window
func (w *guiWidget) setCurrentWindow() {
if w.IsCurrent() {
return
}
if w.WidgetType != widget.Window {
return
}
var rootTK *guiWidget
rootTK = me.treeRoot.TK.(*guiWidget)
rootTK.unsetCurrent()
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 (w *guiWidget) setCurrentTab() {
var p, rootTK *guiWidget
if w.WidgetType != widget.Tab {
return
}
rootTK = me.treeRoot.TK.(*guiWidget)
rootTK.unsetCurrent()
w.isCurrent = true
p = w.parent
p.isCurrent = true
log.Log(NOW, "setCurrent()", w.String())
}
func (w *guiWidget) doWidgetClick() {
switch w.WidgetType {
case widget.Root:
// THIS IS THE BEGINING OF THE LAYOUT
log.Log(NOW, "doWidgetClick()", w.String())
redoWindows(0, 0)
case widget.Flag:
log.Log(NOW, "doWidgetClick() FLAG widget name =", w.String())
log.Log(NOW, "doWidgetClick() if this is the dropdown menu, handle it here?")
case widget.Window:
log.Log(NOW, "doWidgetClick() START on window", w.String())
if me.currentWindow == w.node {
return
}
if me.currentWindow != nil {
var curw *guiWidget
curw = me.currentWindow.TK.(*guiWidget)
curw.unsetCurrent()
curw.setColor(&colorWindow)
curw.hideWidgets()
}
me.currentWindow = w.node
log.Log(NOW, "doWidgetClick() set currentWindow to", w.String())
w.setColor(&colorActiveW)
w.hideWidgets()
w.placeWidgets(me.RawW, me.RawH)
w.showWidgets()
/*
w.redoTabs(me.TabW, me.TabH)
for _, child := range w.children {
if child.currentTab == true {
log.Log(NOW, "FOUND CURRENT TAB", child.String())
child.setCurrentTab()
child.placeWidgets(me.RawW, me.RawH)
child.showWidgets()
return
}
}
*/
/* FIXME: redo this
if ! n.hasTabs {
}
*/
case widget.Tab:
if w.IsCurrent() {
return // do nothing if you reclick on the already selected tab
}
// find the window and disable the active tab
p := w.parent
if p != nil {
p.hideWidgets()
p.redoTabs(me.TabW, me.TabH)
p.unsetCurrent()
for _, child := range w.children {
if child.WidgetType == widget.Tab {
child.setColor(&colorTab)
child.currentTab = false
}
}
}
w.currentTab = true
w.setColor(&colorActiveT)
w.setCurrentTab()
w.placeWidgets(me.RawW, me.RawH)
w.showWidgets()
case widget.Group:
// n.placeWidgets(p.tk.startH, newH)
w.toggleTree()
case widget.Checkbox:
if widget.GetBool(w.value) {
w.setCheckbox(false)
} else {
w.setCheckbox(true)
}
// n.doUserEvent()
me.myTree.SendUserEvent(me.treeRoot)
case widget.Grid:
newR := w.realGocuiSize()
// w,h := n.logicalSize()
// w := newR.w1 - newR.w0
// h := newR.h1 - newR.h0
w.placeGrid(newR.w0, newR.h0)
w.showWidgets()
case widget.Box:
// w.showWidgetPlacement(logNow, "drawTree()")
if w.direction == widget.Horizontal {
log.Log(NOW, "BOX IS HORIZONTAL", w.String())
} else {
log.Log(NOW, "BOX IS VERTICAL", w.String())
}
w.placeWidgets(me.RawW, me.RawH)
w.toggleTree()
case widget.Button:
// doUserEvent(n)
me.myTree.SendUserEvent(w.node)
case widget.Dropdown:
log.Log(NOW, "do the dropdown here")
if me.ddview == nil {
me.ddview = addDropdown()
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")
tk.SetVisible(true)
return
}
var tk *guiWidget
tk = me.ddview.TK.(*guiWidget)
log.Log(NOW, "doWidgetClick() visible =", tk.Visible())
if tk.Visible() {
tk.SetVisible(false)
me.baseGui.DeleteView("ddview")
tk.v = nil
} else {
var dnsList string
for i, s := range tk.vals {
log.Log(NOW, "AddText()", w.String(), i, s)
dnsList += s + "\n"
}
me.ddNode = w.node
log.Log(NOW, "new dns list should be set to:", dnsList)
tk.labelN = dnsList
tk.SetText(dnsList)
tk.SetVisible(true)
}
for i, s := range tk.vals {
log.Log(NOW, "AddText()", tk.String(), i, s)
}
default:
}
}
var toggle bool = true
func (w *guiWidget) toggleTree() {
if toggle {
w.drawTree(toggle)
toggle = false
} else {
w.hideWidgets()
toggle = true
}
}
// display the widgets in the binary tree
func (w *guiWidget) drawTree(draw bool) {
if w == nil {
return
}
w.showWidgetPlacement(true, "drawTree()")
if draw {
// w.textResize()
w.showView()
} else {
w.deleteView()
}
for _, child := range w.children {
child.drawTree(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 w != nil {
log.Log(NOW, "click() Found widget =", w.node.WidgetId, w.String(), ",", w.labelN)
if w.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
}
w.doWidgetClick()
} 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 widget.Visible() {
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 {
tk := me.ddview.TK.(*guiWidget)
log.Log(NOW, "findUnderMouse() found ddview")
if tk.Visible() {
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)
widget := n.TK.(*guiWidget)
widget.showWidgetPlacement(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.labelN = 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.labelN = found.String()
foundtk := found.TK.(*guiWidget)
newR := foundtk.realGocuiSize()
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
}