2025-02-01 11:42:31 -06:00
|
|
|
// Copyright 2017-2025 WIT.COM Inc. All rights reserved.
|
|
|
|
// Use of this source code is governed by the GPL 3.0
|
|
|
|
|
2025-01-31 09:49:27 -06:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/awesome-gocui/gocui"
|
|
|
|
"go.wit.com/widget"
|
|
|
|
)
|
|
|
|
|
2025-01-31 10:28:08 -06:00
|
|
|
/*
|
|
|
|
gocui defines the offset like this:
|
|
|
|
|
|
|
|
width -> increases to the right
|
|
|
|
---------------------------------- hieght
|
|
|
|
| H = 1 | increases
|
|
|
|
| | |
|
|
|
|
| W = 1 W = 18 | |
|
|
|
|
| | v
|
|
|
|
| H = 5 | downwards
|
|
|
|
-------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
// change over to this name
|
|
|
|
// returns all the widgets under (X,H) that are visible
|
|
|
|
func findByXY(w int, h int) []*guiWidget {
|
|
|
|
rootW := me.treeRoot.TK.(*guiWidget)
|
|
|
|
|
|
|
|
// this searches the binary tree recursively (function is right below)
|
|
|
|
return findByXYreal(rootW, w, h)
|
|
|
|
}
|
|
|
|
|
|
|
|
// this checks a widget to see if it is under (W,H), then checks the widget's children
|
|
|
|
// anything that matches is passed back as an array of widgets
|
|
|
|
func findByXYreal(widget *guiWidget, w int, h int) []*guiWidget {
|
2025-01-31 09:49:27 -06:00
|
|
|
var widgets []*guiWidget
|
|
|
|
|
|
|
|
if !widget.Visible() {
|
|
|
|
// ignore widgets that are not visible
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// check the location to see if this is under (W,H)
|
|
|
|
// if it is, return this widget
|
|
|
|
if (widget.gocuiSize.w0 <= w) && (w <= widget.gocuiSize.w1) &&
|
|
|
|
(widget.gocuiSize.h0 <= h) && (h <= widget.gocuiSize.h1) {
|
|
|
|
widgets = append(widgets, widget)
|
2025-02-01 15:15:05 -06:00
|
|
|
// log.Log(GOCUI, "findByXY() found", widget.node.WidgetType, w, h)
|
2025-01-31 09:49:27 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// search through the children widgets in the binary tree
|
|
|
|
for _, child := range widget.children {
|
2025-01-31 10:28:08 -06:00
|
|
|
widgets = append(widgets, findByXYreal(child, w, h)...)
|
2025-01-31 09:49:27 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return widgets
|
|
|
|
}
|
|
|
|
|
2025-01-31 22:56:05 -06:00
|
|
|
// returns the "highest priority widget under the mouse
|
2025-01-31 09:49:27 -06:00
|
|
|
func findUnderMouse() *guiWidget {
|
|
|
|
w, h := me.baseGui.MousePosition()
|
|
|
|
|
2025-01-31 10:28:08 -06:00
|
|
|
widgets := findByXY(w, h)
|
2025-01-31 09:49:27 -06:00
|
|
|
|
|
|
|
// search through all the widgets that were below the mouse click
|
|
|
|
var found *guiWidget
|
|
|
|
for _, w := range widgets {
|
|
|
|
// prioritize window buttons. This means if some code covers
|
|
|
|
// up the window widgets, then it will ignore everything else
|
|
|
|
// and allow the user (hopefully) to redraw or switch windows
|
|
|
|
// TODO: display the window widgets on top
|
2025-02-01 15:15:05 -06:00
|
|
|
if w.node.WidgetType == widget.Window {
|
2025-01-31 09:49:27 -06:00
|
|
|
return w
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// return anything else that is interactive
|
|
|
|
for _, w := range widgets {
|
2025-02-01 15:15:05 -06:00
|
|
|
if w.node.WidgetType == widget.Button {
|
2025-01-31 09:49:27 -06:00
|
|
|
return w
|
|
|
|
}
|
2025-02-01 15:15:05 -06:00
|
|
|
if w.node.WidgetType == widget.Combobox {
|
2025-01-31 09:49:27 -06:00
|
|
|
return w
|
|
|
|
}
|
2025-02-01 15:15:05 -06:00
|
|
|
if w.node.WidgetType == widget.Checkbox {
|
2025-01-31 09:49:27 -06:00
|
|
|
return w
|
|
|
|
}
|
2025-02-01 13:43:51 -06:00
|
|
|
w.dumpWidget("findUnderMouse() found something unknown")
|
2025-01-31 09:49:27 -06:00
|
|
|
found = w
|
|
|
|
}
|
|
|
|
// maybe something else was found
|
|
|
|
return found
|
|
|
|
}
|
|
|
|
|
2025-01-31 22:56:05 -06:00
|
|
|
// panics. todo: fix ctrl-mouse click?
|
2025-01-31 09:49:27 -06:00
|
|
|
// find the widget under the mouse click
|
|
|
|
func ctrlDown(g *gocui.Gui, v *gocui.View) error {
|
|
|
|
var found *guiWidget
|
|
|
|
// 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.(*guiWidget)
|
|
|
|
}
|
|
|
|
tk.labelN = found.String()
|
|
|
|
newR := found.realGocuiSize()
|
|
|
|
tk.gocuiSize.w0 = newR.w0
|
|
|
|
tk.gocuiSize.h0 = newR.h0
|
|
|
|
tk.gocuiSize.w1 = newR.w1
|
|
|
|
tk.gocuiSize.h1 = newR.h1
|
|
|
|
return nil
|
|
|
|
}
|