-
Notifications
You must be signed in to change notification settings - Fork 1
/
selectinput_windows.go
208 lines (179 loc) · 5.32 KB
/
selectinput_windows.go
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
package goey
import (
"bitbucket.org/rj/goey/base"
"github.com/lxn/win"
"syscall"
"unsafe"
)
var (
comboboxClassName = []uint16{'C', 'O', 'M', 'B', 'O', 'B', 'O', 'X', 0}
oldComboboxWindowProc uintptr
)
func (w *SelectInput) mount(parent base.Control) (base.Element, error) {
const STYLE = win.WS_CHILD | win.WS_VISIBLE | win.WS_TABSTOP | win.CBS_DROPDOWNLIST
hwnd, _, err := createControlWindow(win.WS_EX_CLIENTEDGE, &comboboxClassName[0], "", STYLE, parent.HWnd)
if err != nil {
return nil, err
}
// Set the font for the window
if hMessageFont != 0 {
win.SendMessage(hwnd, win.WM_SETFONT, uintptr(hMessageFont), 0)
}
if w.Disabled {
win.EnableWindow(hwnd, false)
}
// Add items to the control
longestString, err := selectinputAddItems(hwnd, w.Items)
if err != nil {
win.DestroyWindow(hwnd)
return nil, err
}
if !w.Unset {
win.SendMessage(hwnd, win.CB_SETCURSEL, uintptr(w.Value), 0)
}
// Subclass the window procedure
subclassWindowProcedure(hwnd, &oldComboboxWindowProc, comboboxWindowProc)
retval := &selectinputElement{
Control: Control{hwnd},
onChange: w.OnChange,
onFocus: w.OnFocus,
onBlur: w.OnBlur,
longestString: longestString,
}
win.SetWindowLongPtr(hwnd, win.GWLP_USERDATA, uintptr(unsafe.Pointer(retval)))
return retval, nil
}
func selectinputAddItems(hwnd win.HWND, items []string) (string, error) {
longestString := ""
for _, v := range items {
text, err := syscall.UTF16PtrFromString(v)
if err != nil {
return "", err
}
win.SendMessage(hwnd, win.CB_ADDSTRING, 0, uintptr(unsafe.Pointer(text)))
if len(v) > len(longestString) {
longestString = v
}
}
return longestString, nil
}
type selectinputElement struct {
Control
onChange func(value int)
onFocus func()
onBlur func()
longestString string
preferredWidth base.Length
}
func (w *selectinputElement) Layout(bc base.Constraints) base.Size {
width := w.MinIntrinsicWidth(0)
height := w.MinIntrinsicHeight(0)
return bc.Constrain(base.Size{width, height})
}
func (w *selectinputElement) MinIntrinsicHeight(width base.Length) base.Length {
// https://msdn.microsoft.com/en-us/library/windows/desktop/dn742486.aspx#sizingandspacing
return 23 * DIP
}
func (w *selectinputElement) MinIntrinsicWidth(height base.Length) base.Length {
if w.preferredWidth == 0 {
text, err := syscall.UTF16FromString(w.longestString)
if err != nil {
w.preferredWidth = 75 * DIP
} else {
width, _ := w.CalcRect(text)
w.preferredWidth = base.FromPixelsX(int(width)).Scale(13, 10)
}
}
return w.preferredWidth
}
func (w *selectinputElement) Props() base.Widget {
length := win.SendMessage(w.hWnd, win.CB_GETCOUNT, 0, 0)
items := make([]string, int(length))
for i := range items {
buffer := [80]uint16{}
length := win.SendMessage(w.hWnd, win.CB_GETLBTEXTLEN, uintptr(i), 0)
if length > 79 {
panic("not enough room")
}
win.SendMessage(w.hWnd, win.CB_GETLBTEXT, uintptr(i),
uintptr(unsafe.Pointer(&buffer)))
items[i] = syscall.UTF16ToString(buffer[:length])
}
value := win.SendMessage(w.hWnd, win.CB_GETCURSEL, 0, 0)
unset := false
if value == 0xFFFFFFFF /*win.CB_ERR, but bug with extension*/ {
value, unset = 0, true
}
return &SelectInput{
Items: items,
Value: int(value),
Unset: unset,
Disabled: !win.IsWindowEnabled(w.hWnd),
OnChange: w.onChange,
OnFocus: w.onFocus,
OnBlur: w.onBlur,
}
}
func (w *selectinputElement) updateProps(data *SelectInput) error {
// This is a brute force approach. The list of items is probably unchanged
// most of the time.
win.SendMessage(w.hWnd, win.CB_RESETCONTENT, 0, 0)
longestString, err := selectinputAddItems(w.hWnd, data.Items)
if err != nil {
return err
}
if !data.Unset {
win.SendMessage(w.hWnd, win.CB_SETCURSEL, uintptr(data.Value), 0)
}
w.SetDisabled(data.Disabled)
w.onChange = data.OnChange
w.onFocus = data.OnFocus
w.onBlur = data.OnBlur
w.longestString = longestString
// Clear cache
w.preferredWidth = 0
return nil
}
func comboboxWindowProc(hwnd win.HWND, msg uint32, wParam uintptr, lParam uintptr) (result uintptr) {
switch msg {
case win.WM_DESTROY:
// Make sure that the data structure on the Go-side does not point to a non-existent
// window.
selectinputGetPtr(hwnd).hWnd = 0
// Defer to the old window proc
case win.WM_SETFOCUS:
if w := selectinputGetPtr(hwnd); w.onFocus != nil {
w.onFocus()
}
// Defer to the old window proc
case win.WM_KILLFOCUS:
if w := selectinputGetPtr(hwnd); w.onBlur != nil {
w.onBlur()
}
// Defer to the old window proc
case win.WM_COMMAND:
// WM_COMMAND is sent to the parent, which will only forward certain
// message. This code should only ever see CBN_SELCHANGE, but we will
// still check.
switch notification := win.HIWORD(uint32(wParam)); notification {
case win.CBN_SELCHANGE:
if w := selectinputGetPtr(hwnd); w.onChange != nil {
cursel := win.SendMessage(hwnd, win.CB_GETCURSEL, 0, 0)
w.onChange(int(cursel))
}
}
// defer to old window proc
}
return win.CallWindowProc(oldComboboxWindowProc, hwnd, msg, wParam, lParam)
}
func selectinputGetPtr(hwnd win.HWND) *selectinputElement {
gwl := win.GetWindowLongPtr(hwnd, win.GWLP_USERDATA)
if gwl == 0 {
panic("Internal error.")
}
ptr := (*selectinputElement)(unsafe.Pointer(gwl))
if ptr.hWnd != hwnd && ptr.hWnd != 0 {
panic("Internal error.")
}
return ptr
}