summaryrefslogtreecommitdiff
path: root/eventMouseClick.go
blob: 67c877aca5f89da4f856b2fa41879a34974038c0 (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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// Copyright 2017-2025 WIT.COM Inc. All rights reserved.
// Use of this source code is governed by the GPL 3.0

package main

import (
	"fmt"

	"go.wit.com/log"
	"go.wit.com/widget"
)

/*
// this didn't work. panic()
func (tk *guiWidget) DeleteNode() {
	p := tk.parent
	for i, child := range p.children {
		log.Log(GOCUI, "parent has child:", i, child.node.WidgetId, child.node.GetProgName())
		if tk == child {
			log.Log(GOCUI, "Found child ==", i, child.node.WidgetId, child.node.GetProgName())
			log.Log(GOCUI, "Found n ==", i, tk.node.WidgetId, tk.node.GetProgName())
			p.children = append(p.children[:i], p.children[i+1:]...)
		}
	}
		for i, child := range p.children {
			log.Log(TREE, "parent now has child:", i, child.WidgetId, child.GetProgName())
		}
}
*/

func (tk *guiWidget) doWindowClick() {
	w, h := me.baseGui.MousePosition()
	tk.dumpWidget(fmt.Sprintf("doWindowClick(%d,%d)", w, h))

	// compare the mouse location to the 'X' indicator to close the window
	if tk.gocuiSize.inRect(w, h) {
		offset := w - tk.gocuiSize.w1
		if (offset < 2) && (offset > -2) {
			// close enough // close the window here
			tk.dumpWidget(fmt.Sprintf("Close Window(%d,%d) (off=%d)", w, h, offset))
			tk.hideWindow()
			n := tk.node
			tk.deleteNode()
			me.myTree.SendWindowCloseEvent(n)
			/*
				n.DeleteNode()

				tk.DeleteNode()
			*/
			return
		}
		if tk.window.collapsed {
			tk.dumpWidget(fmt.Sprintf("collapse = false"))
			tk.window.collapsed = false
		} else {
			tk.dumpWidget(fmt.Sprintf("collapse = true"))
			tk.window.collapsed = true
		}
	} else {
		tk.window.collapsed = false
	}
	// if there is a current window, hide it
	if me.currentWindow != nil {
		me.currentWindow.setColor(&colorWindow)
	}

	// now set this window as the current window
	me.currentWindow = tk

	tk.redrawWindow(w, h)
	setThingsOnTop() // sets help, Stdout, etc on the top after windows have been redrawn
}

// this whole things was impossible to make but it got me where I am now
// the debugging is way way better now with it being visible in the Stdout window
// so now it's possible to redo all this and make it better
func (tk *guiWidget) doWidgetClick(w int, h int) {
	tk.dumpWidget(fmt.Sprintf("doWidgetClick(%d,%d)", w, h))
	switch tk.node.WidgetType {
	case widget.Window:
		tk.doWindowClick()
		return
	case widget.Checkbox:
		if tk.node.State.Checked {
			log.Log(WARN, "checkbox is being set to false")
			tk.node.State.Checked = false
			tk.setCheckbox()
		} else {
			log.Log(WARN, "checkbox is being set to true")
			tk.node.State.Checked = true
			tk.setCheckbox()
		}
		me.myTree.SendUserEvent(tk.node)
	case widget.Button:
		me.myTree.SendFromUser(tk.node)
	case widget.Combobox:
		tk.showDropdown()
	case widget.Dropdown:
		tk.showDropdown()
	case widget.Textbox:
		tk.showTextbox()
	case widget.Flag:
		tk.dropdownClicked(w, h)
	case widget.Stdout:
		tk.dumpWidget("stdout click()")
	default:
		tk.dumpWidget("undef click()")
	}
}

// handles a mouse click
func doMouseClick(w int, h int) {
	// Flag widgets (dropdown menus, etc) are the highest priority. ALWAYS SEND MOUSE CLICKS THERE FIRST
	// handle an open dropdown menu or text entry window first
	if me.dropdown.active || me.textbox.active {
		// can't drag or do anything when dropdown or textbox are visible
		log.Info("can't do anything. dropdown or textbox is active")
		for _, tk := range findByXY(w, h) {
			if tk.node.WidgetId == me.dropdown.wId {
				log.Info("got dropdwon click", w, h, tk.cuiName)
				tk.dropdownClicked(w, h)
			}
			if tk.node.WidgetId == me.textbox.wId {
				log.Info("got textbox click", w, h, tk.cuiName)
				tk.textboxClosed()
			}
		}
		return
	}

	// priority widgets. send the click here first
	for _, tk := range findByXY(w, h) {
		switch tk.node.WidgetType {
		case widget.Checkbox:
			if tk.node.State.Checked {
				log.Log(WARN, "checkbox is being set to false")
				tk.node.State.Checked = false
				tk.setCheckbox()
			} else {
				log.Log(WARN, "checkbox is being set to true")
				tk.node.State.Checked = true
				tk.setCheckbox()
			}
			me.myTree.SendUserEvent(tk.node)
		case widget.Button:
			tk.dumpWidget("click()") // enable this to debug widget clicks
			me.myTree.SendFromUser(tk.node)
			return
		case widget.Combobox:
			tk.showDropdown()
			return
		case widget.Dropdown:
			tk.showDropdown()
			return
		case widget.Textbox:
			tk.showTextbox()
			return
		default:
			// TODO: enable the GUI debugger in gocui
			// tk.dumpWidget("undef click()") // enable this to debug widget clicks
		}
	}

	var found bool

	for _, tk := range findByXY(w, h) {
		// will show you everything found on a mouse click. great for debugging!
		// tk.dumpWidget("click()")
		if tk.node.WidgetType == widget.Stdout {
			// don't send clicks to the stdout debugging window
			// continue
		}
		found = true
		// tk.doWidgetClick(w, h)
		return
	}
	if found {
		return
	}

	log.Log(GOCUI, "click() nothing was at:", w, h)
	return
}

func doMouseDoubleClick(w int, h int) {
	me.mouse.double = false
	// log.Printf("actually a double click (%d,%d)", w, h)

	for _, tk := range findByXY(w, h) {
		if tk.node.WidgetType == widget.Window {
			tk.redrawWindow(tk.gocuiSize.w0, tk.gocuiSize.h0)
			me.stdout.outputOnTop = false
			setThingsOnTop()
			return
		}

		if tk.node.WidgetType == widget.Stdout {
			if me.stdout.outputOnTop {
				me.stdout.outputOnTop = false
				setThingsOnTop()
			} else {
				me.stdout.outputOnTop = true
				setThingsOnTop()
			}
			return
		}
	}
}