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
209
210
211
212
213
214
215
|
// 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"
"strings"
log "go.wit.com/log"
"go.wit.com/toolkits/tree"
"go.wit.com/widget"
)
// dropdowns don't exist so this is an attempt to pretend they exist
// by having a fake window view in gocui that appears with the dropdown
// items. by detecting where the user clicks, we should be
// able to set the text of the button to that value (and
// rezise it.
// the thing is this is so damned complicated because it
// is very hard to debug this. thanks to the floating on
// screen debugging output I might be able to figure it
// it out now. maybe. notsure.
func makeDropdownView(ddItems string) *guiWidget {
newNode := addDropdown()
tk := newNode.TK.(*guiWidget)
tk.labelN = ddItems
tk.SetText(ddItems)
tk.gocuiSize.w0 = 100
tk.gocuiSize.w1 = 120
tk.gocuiSize.h0 = 15
tk.gocuiSize.h1 = 18
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
}
tk.v.Wrap = true
tk.v.Frame = true
tk.v.Clear()
fmt.Fprint(tk.v, ddItems)
tk.Show()
return tk
}
func addDropdownTK(wId int) *guiWidget {
n := addDropdownNew(wId)
return n.TK.(*guiWidget)
}
func addDropdownNew(wId int) *tree.Node {
n := new(tree.Node)
n.WidgetType = widget.Flag
n.WidgetId = wId
n.ParentId = 0
// store the internal toolkit information
tk := new(guiWidget)
tk.frame = true
tk.labelN = "DropBox text"
tk.node = n
if tk.node.Parent == nil {
tk.node.Parent = me.treeRoot
}
// copy the data from the action message
tk.node.State.Label = "DropBox"
// set the name used by gocui to the id
tk.cuiName = fmt.Sprintf("%d DR", wId)
tk.color = &colorFlag
// add this new widget on the binary tree
tk.parent = me.treeRoot.TK.(*guiWidget)
if tk.parent == nil {
panic("addDropdown() didn't get treeRoot guiWidget")
} else {
tk.parent.children = append(tk.parent.children, tk)
}
n.TK = tk
return n
}
func (tk *guiWidget) showDropdown() {
// todo: fix this after switching to protobuf
// var items []string
// items = tk.node.State.Strings
//for i, s := range items {
/*
var ddItems string
for i, s := range tk.node.Strings() {
// log.Log(GOCUI, "showDropdown()", tk.String(), i, s)
ddItems += s + "\n"
}
*/
// log.Log(GOCUI, "new dropdown items should be set to:", ddItems)
// sizeW, sizeH := tk.Size()
// log.Log(GOCUI, "showDropdown() size W,H=", sizeW, sizeH)
startW, startH := tk.Position()
log.Log(GOCUI, "showDropdown() IS BROKEN W,H=", startW, startH)
// me.dropdownV.MoveToOffset(startW+3, startH+2)
// me.dropdownV.labelN = ddItems
// me.dropdownV.Show()
}
/*
func hideDDview() error {
w, h := me.baseGui.MousePosition()
log.Log(GOCUI, "hide dropdown menu() view (w,h) =", w, h)
if me.dropdownV == nil {
return gocui.ErrUnknownView
}
if me.dropdownV.v == nil {
return gocui.ErrUnknownView
}
me.dropdownV.SetVisible(false)
return nil
}
func showDDview() error {
w, h := me.baseGui.MousePosition()
log.Log(GOCUI, "show dropdown menu() view (w,h) =", w, h)
if me.dropdownV == nil {
return gocui.ErrUnknownView
}
if me.dropdownV.v == nil {
return gocui.ErrUnknownView
}
me.dropdownV.SetVisible(true)
return nil
}
*/
// if there is a drop down view active, treat it like a dialog box and close it
func (w *guiWidget) dropdownClicked(mouseW, mouseH int) string {
w.Hide()
// only need height to figure out what line in the dropdown menu the user clicked
_, startH := w.Position()
// startW, startH := w.Position()
// log.Log(GOCUI, "dropdownClicked() start (w,h) =", startW, startH)
// log.Log(GOCUI, "dropdownClicked() at (w,h) =", mouseW, mouseH)
itemNumber := mouseH - startH
items := strings.Split(w.labelN, "\n")
// log.Log(GOCUI, "dropdownClicked() look for item", itemNumber, "len(items) =", len(items))
if itemNumber < 1 {
return ""
}
if len(items) >= itemNumber {
// log.Log(GOCUI, "dropdownClicked() found", items[itemNumber-1])
if items[itemNumber-1] != "" {
if me.dropdownW != nil {
// log.Log(GOCUI, "dropdownClicked() send event for", me.dropdownW.cuiName, me.dropdownW.node.WidgetType)
me.dropdownW.SetText(items[itemNumber-1])
me.dropdownW.node.SetCurrentS(items[itemNumber-1])
me.myTree.SendUserEvent(me.dropdownW.node)
}
}
return items[itemNumber-1]
}
return ""
}
var dtoggle bool = false // temporarily tracking show & hide
var doffset int = 5 // how many spaces over the dropdown menu should be from the mouse
func dropdownUnclicked(w, h int) {
var d *guiWidget
/*
if me.dropdownV == nil {
log.Log(GOCUI, "mouseUp() dropdownV = nil")
return
}
*/
// examine everything under X & Y on the screen)
for _, tk := range findByXY(w, h) {
// tk.dumpWidget("dropdownUnclicked()")
if tk.node.WidgetType == widget.Dropdown {
d = tk
}
}
if d == nil {
// log.Log(GOCUI, fmt.Sprintf("dropdownUnclicked() there was no dropdown widget at (w=%d h=%d)", mouseW, mouseH))
return
}
// log.Log(GOCUI, "dropdownUnclicked()", d.node.Strings(), "end. now try to enable me.dropdownV")
/*
dtext := strings.Join(d.node.Strings(), "\n")
tk := me.dropdownV
if dtoggle {
// log.Log(GOCUI, "dropdownUnclicked() set visible=false")
tk.Hide()
dtoggle = false
tk.SetText("goodbye")
} else {
// log.Log(GOCUI, "dropdownUnclicked() set visible=true")
tk.MoveToOffset(w+doffset, h)
tk.SetText(dtext)
tk.Show()
dtoggle = true
}
*/
}
|