summaryrefslogtreecommitdiff
path: root/find.go
blob: 6982f2ef6c3d48c85ff9c5ad70c57222753f5a50 (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// Copyright 2017-2025 WIT.COM Inc. All rights reserved.
// Use of this source code is governed by the GPL 3.0

package main

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

/*
	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 rootW.findByXYreal(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 (tk *guiWidget) findByXYreal(w int, h int) []*guiWidget {
	var widgets []*guiWidget

	if !tk.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 (tk.gocuiSize.w0 <= w) && (w <= tk.gocuiSize.w1) &&
			(tk.gocuiSize.h0 <= h) && (h <= tk.gocuiSize.h1) {
			widgets = append(widgets, tk)
			// log.Log(GOCUI, "findByXY() found", widget.node.WidgetType, w, h)
		}
	}
	tk.verifyRect()

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

	return widgets
}

func (tk *guiWidget) setFullSize() rectType {
	r := tk.getFullSize()

	/*
		r.w0 = tk.gocuiSize.w0
		r.w1 = tk.gocuiSize.w1
		r.h0 = tk.gocuiSize.h0
		r.h1 = tk.gocuiSize.h1
	*/
	var changed bool
	if tk.full.w0 != r.w0 {
		tk.full.w0 = r.w0
		changed = true
	}
	if tk.full.w1 != r.w1 {
		tk.full.w1 = r.w1
		changed = true
	}
	if tk.full.h0 != r.h0 {
		tk.full.h0 = r.h0
		changed = true
	}
	if tk.full.h1 != r.h1 {
		tk.full.h1 = r.h1
		changed = true
	}
	if changed {
		if tk.node.WidgetType == widget.Window {
			log.Info("REDRAW WINDOW IN setFullSize()")
			tk.full.w1 = r.w1 + 2
			tk.full.h1 = r.h1 + 1
			tk.Hide()
			tk.drawView()
		}
	}
	return r
}

// 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 (tk *guiWidget) getFullSize() rectType {
	var r rectType

	r.w0 = tk.gocuiSize.w0
	r.w1 = tk.gocuiSize.w1
	r.h0 = tk.gocuiSize.h0
	r.h1 = tk.gocuiSize.h1

	// search through the children widgets in the binary tree
	for _, child := range tk.children {
		cr := child.getFullSize()
		if r.w0 > cr.w0 {
			r.w0 = cr.w0
		}
		if r.h0 > cr.h0 {
			r.h0 = cr.h0
		}
		if r.w1 < cr.w1 {
			r.w1 = cr.w1
		}
		if r.h1 < cr.h1 {
			r.h1 = cr.h1
		}
	}

	return r
}

// returns the "highest priority widget under the mouse
func findUnderMouse() *guiWidget {
	w, h := me.baseGui.MousePosition()

	widgets := findByXY(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.node.WidgetType == widget.Window {
			return w
		}
	}

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

// panics. todo: fix ctrl-mouse click?
// 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
}