summaryrefslogtreecommitdiff
path: root/dropdown.go
blob: 48dbe7f88405966f3457c839812823bd492af488 (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
// Copyright 2017-2025 WIT.COM Inc. All rights reserved.
// Use of this source code is governed by the GPL 3.0

package main

// simulates a dropdown menu in gocui

import (
	"fmt"
	"strings"

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

// create a new widget in the binary tree
func makeNewFlagWidget(wId int) *guiWidget {
	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.node = n
	if tk.node.Parent == nil {
		tk.node.Parent = me.treeRoot
	}

	// set the name used by gocui to the id
	tk.cuiName = fmt.Sprintf("%d DR", wId)

	tk.setColorInput()

	// add this new widget on the binary tree
	tk.parent = me.treeRoot.TK.(*guiWidget)
	if tk.parent == nil {
		panic("makeNewFlagWidget() didn't get treeRoot guiWidget")
	} else {
		tk.parent.children = append(tk.parent.children, tk)
	}

	n.TK = tk
	return tk
}

func (tk *guiWidget) showDropdown() {
	if me.dropdown.tk == nil {
		// should only happen once
		me.dropdown.tk = makeNewFlagWidget(me.dropdown.wId)
		me.dropdown.tk.dumpWidget("init() dropdown")
	}
	if me.dropdown.tk == nil {
		log.Log(GOCUI, "showDropdown() Is Broken!")
		return
	}

	// todo: fix this after switching to protobuf
	me.dropdown.items = []string{} // zero out whatever was there before
	for i, s := range tk.node.Strings() {
		log.Log(GOCUI, "showDropdown()", tk.String(), i, s)
		me.dropdown.items = append(me.dropdown.items, s)
	}
	log.Log(GOCUI, "new dropdown items should be set to:", me.dropdown.items)

	startW, startH := tk.Position()
	log.Log(GOCUI, "showDropdown() SHOWING AT W,H=", startW, startH)
	me.dropdown.tk.Hide()
	me.dropdown.tk.MoveToOffset(startW+3, startH+2)
	me.dropdown.tk.labelN = strings.Join(me.dropdown.items, "\n")
	me.dropdown.tk.Show()
	me.dropdown.active = true
	me.dropdown.callerTK = tk

	r := me.dropdown.tk.gocuiSize // set the 'full' size so that mouse clicks are sent here
	me.dropdown.tk.full.w0 = r.w0
	me.dropdown.tk.full.w1 = r.w1
	me.dropdown.tk.full.h0 = r.h0
	me.dropdown.tk.full.h1 = r.h1

	me.dropdown.tk.dumpWidget("showDropdown()")
}

// 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()
	me.dropdown.active = false

	// only need height to figure out what line in the dropdown menu the user clicked
	_, startH := w.Position()

	itemNumber := mouseH - startH
	items := me.dropdown.items
	// 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.dropdown.tk != nil {
				// log.Log(GOCUI, "dropdownClicked() send event for", me.dropdownW.cuiName, me.dropdownW.node.WidgetType)
				me.dropdown.callerTK.SetText(items[itemNumber-1])
				me.dropdown.callerTK.node.SetCurrentS(items[itemNumber-1])
				me.myTree.SendUserEvent(me.dropdown.callerTK.node)
			}
		}
		return items[itemNumber-1]
	}
	return ""
}