summaryrefslogtreecommitdiff
path: root/find.go
blob: 508967e4df5fdbe162685bb0b0839032eab6e87d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package main

import (
	"github.com/awesome-gocui/gocui"
	"go.wit.com/widget"
)

// returns the widget under the location on the screen
func findByXY(widget *guiWidget, w int, h int) []*guiWidget {
	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)
			// log.Log(GOCUI, "findByXY() found", widget.WidgetType, w, h)
		}
	}

	// search through the children widgets in the binary tree
	for _, child := range widget.children {
		widgets = append(widgets, findByXY(child, w, h)...)
	}

	return widgets
}

func findUnderMouse() *guiWidget {
	w, h := me.baseGui.MousePosition()

	rootW := me.treeRoot.TK.(*guiWidget)
	widgets := findByXY(rootW, w, h)

	// 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
		if w.WidgetType == widget.Window {
			return w
		}
	}

	// return anything else that is interactive
	for _, w := range widgets {
		if w.WidgetType == widget.Button {
			return w
		}
		if w.WidgetType == widget.Combobox {
			return w
		}
		if w.WidgetType == widget.Checkbox {
			return w
		}
		w.showWidgetPlacement("findUnderMouse() found something unknown")
		found = w
	}
	// maybe something else was found
	return found
}

// 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
}