-
Notifications
You must be signed in to change notification settings - Fork 1
/
intinput_windows.go
340 lines (293 loc) · 8.97 KB
/
intinput_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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
package goey
import (
"strconv"
"syscall"
"unsafe"
"bitbucket.org/rj/goey/base"
win2 "bitbucket.org/rj/goey/internal/syscall"
"github.com/lxn/win"
)
var (
intinput struct {
className []uint16
oldWindowProc uintptr
}
)
func init() {
intinput.className = []uint16{'m', 's', 'c', 't', 'l', 's', '_', 'u', 'p', 'd', 'o', 'w', 'n', '3', '2', 0}
}
func (w *IntInput) mountUpDown(parent base.Control) (win.HWND, error) {
// Range for the updown control is is only int32, not int64.
if !w.useUpDownControl() {
return 0, nil
}
hwnd, _, err := createControlWindow(win.WS_EX_LEFT|win.WS_EX_LTRREADING,
&intinput.className[0],
"",
win.WS_CHILDWINDOW|win.WS_VISIBLE|win.UDS_SETBUDDYINT|win.UDS_ARROWKEYS|win.UDS_HOTTRACK|win.UDS_NOTHOUSANDS,
parent.HWnd)
if err != nil {
return 0, err
}
win.SendMessage(hwnd, win.UDM_SETRANGE32, uintptr(w.Min), uintptr(w.Max))
win.SendMessage(hwnd, win.UDM_SETPOS32, 0, uintptr(w.Value))
return hwnd, nil
}
func (w *IntInput) mount(parent base.Control) (base.Element, error) {
// Create the control
style := uint32(win.WS_CHILD | win.WS_VISIBLE | win.WS_TABSTOP | win.ES_LEFT | win.ES_AUTOHSCROLL | win.ES_NUMBER)
if w.OnEnterKey != nil {
style = style | win.ES_MULTILINE
}
hwnd, _, err := createControlWindow(win.WS_EX_CLIENTEDGE, &edit.className[0], strconv.FormatInt(w.Value, 10), style, parent.HWnd)
if err != nil {
return nil, err
}
// Create the updown control.
hwndUpDown, err := w.mountUpDown(parent)
if err != nil {
return nil, err
}
if w.Disabled {
win.EnableWindow(hwnd, false)
if hwndUpDown != 0 {
win.EnableWindow(hwndUpDown, false)
}
}
// Create placeholder, if required.
if w.Placeholder != "" {
textPlaceholder, err := syscall.UTF16PtrFromString(w.Placeholder)
if err != nil {
win.DestroyWindow(hwnd)
return nil, err
}
win.SendMessage(hwnd, win.EM_SETCUEBANNER, 0, uintptr(unsafe.Pointer(textPlaceholder)))
}
// Create the return value.
retval := &intinputElement{
Control: Control{hwnd},
hwndUpDown: hwndUpDown,
min: w.Min,
max: w.Max,
onChange: w.OnChange,
onFocus: w.OnFocus,
onBlur: w.OnBlur,
onEnterKey: w.OnEnterKey,
}
// Link the control back to Go for event handling
if hwndUpDown != 0 {
win.SendMessage(hwndUpDown, win.UDM_SETBUDDY, uintptr(hwnd), 0)
subclassWindowProcedure(hwnd, &intinput.oldWindowProc, intinputWindowProc)
} else {
subclassWindowProcedure(hwnd, &edit.oldWindowProc, intinputWindowProc)
}
win.SetWindowLongPtr(hwnd, win.GWLP_USERDATA, uintptr(unsafe.Pointer(retval)))
return retval, nil
}
func (w *IntInput) useUpDownControl() bool {
// Range for the updown control is is only int32, not int64.
// Need to make sure that we can properly set the range for the updown
// control in order to include it in the GUI.
return w.Min >= -2147483648 && w.Max <= 2147483647
}
type intinputElement struct {
Control
hwndUpDown win.HWND
min int64
max int64
onChange func(int64)
onFocus func()
onBlur func()
onEnterKey func(int64)
}
func (w *intinputElement) Close() {
if w.hwndUpDown != 0 {
win.DestroyWindow(w.hwndUpDown)
w.hwndUpDown = 0
}
if w.hWnd != 0 {
win.DestroyWindow(w.hWnd)
w.hWnd = 0
}
}
func (w *intinputElement) getClampedValue() (int64, error) {
// Get the text from the control, and convert text to an integer
i, err := strconv.ParseInt(win2.GetWindowText(w.hWnd), 10, 64)
if err != nil {
return 0, err
}
// Clamp the value
if i < w.min {
i = w.min
if w.hwndUpDown != 0 {
win.SendMessage(w.hwndUpDown, win.UDM_SETPOS32, 0, uintptr(i))
win.SendMessage(w.hWnd, win.EM_SETSEL, 0, 0x7fff)
}
} else if i > w.max {
i = w.max
if w.hwndUpDown != 0 {
win.SendMessage(w.hwndUpDown, win.UDM_SETPOS32, 0, uintptr(i))
win.SendMessage(w.hWnd, win.EM_SETSEL, 0, 0x7fff)
}
}
return i, nil
}
func (w *intinputElement) thunkOnChange() {
i, err := w.getClampedValue()
if err != nil {
// This case should not occur, as the control should prevent invalid
// strings from being entered.
// TODO: What reporting should be done here?
return
}
w.onChange(i)
}
func (w *intinputElement) thunkOnEnterKey() {
i, err := w.getClampedValue()
if err != nil {
// This case should not occur, as the control should prevent invalid
// strings from being entered.
// TODO: What reporting should be done here?
return
}
w.onEnterKey(i)
}
func (w *intinputElement) Layout(bc base.Constraints) base.Size {
width := w.MinIntrinsicWidth(0)
height := w.MinIntrinsicHeight(0)
return bc.Constrain(base.Size{width, height})
}
func (w *intinputElement) MinIntrinsicHeight(base.Length) base.Length {
// https://msdn.microsoft.com/en-us/library/windows/desktop/dn742486.aspx#sizingandspacing
return 23 * DIP
}
func (w *intinputElement) MinIntrinsicWidth(base.Length) base.Length {
return 75 * DIP
}
func (w *intinputElement) Props() base.Widget {
value := int64(0)
if w.hwndUpDown != 0 {
value = int64(win.SendMessage(w.hwndUpDown, win.UDM_GETPOS32, 0, 0))
} else {
value, _ = strconv.ParseInt(w.Control.Text(), 10, 64)
}
return &IntInput{
Value: value,
Placeholder: propsPlaceholder(w.hWnd),
Disabled: !win.IsWindowEnabled(w.hWnd),
Min: w.min,
Max: w.max,
OnChange: w.onChange,
OnFocus: w.onFocus,
OnBlur: w.onBlur,
OnEnterKey: w.onEnterKey,
}
}
func (w *intinputElement) SetBounds(bounds base.Rectangle) {
buddyWidth := (23 * DIP) * 2 / 3
if w.hwndUpDown == 0 {
win.MoveWindow(w.hWnd, int32(bounds.Min.X.PixelsX()), int32(bounds.Min.Y.PixelsY()), int32(bounds.Dx().PixelsX()), int32(bounds.Dy().PixelsY()), false)
return
}
if bounds.Dx() >= 4*buddyWidth {
win.MoveWindow(w.hWnd, int32(bounds.Min.X.PixelsX()), int32(bounds.Min.Y.PixelsY()), int32((bounds.Dx() - buddyWidth).PixelsX()), int32(bounds.Dy().PixelsY()), false)
win.MoveWindow(w.hwndUpDown, int32((bounds.Max.X - buddyWidth).PixelsX()), int32(bounds.Min.Y.PixelsY()), int32(buddyWidth.PixelsX()), int32(bounds.Dy().PixelsY()), false)
win.ShowWindow(w.hwndUpDown, win.SW_SHOW)
} else {
win.MoveWindow(w.hWnd, int32(bounds.Min.X.PixelsX()), int32(bounds.Min.Y.PixelsY()), int32(bounds.Dx().PixelsX()), int32(bounds.Dy().PixelsY()), false)
win.ShowWindow(w.hwndUpDown, win.SW_HIDE)
}
}
func (w *intinputElement) SetOrder(previous win.HWND) win.HWND {
if w.hwndUpDown != 0 {
win.SetWindowPos(w.hwndUpDown, previous, 0, 0, 0, 0, win.SWP_NOMOVE|win.SWP_NOSIZE|win.SWP_NOREDRAW|0x400)
previous = w.hwndUpDown
}
win.SetWindowPos(w.hWnd, previous, 0, 0, 0, 0, win.SWP_NOMOVE|win.SWP_NOSIZE|win.SWP_NOREDRAW|0x400)
return w.hWnd
}
func (w *intinputElement) TakeFocus() bool {
ok := w.Control.TakeFocus()
if ok {
win.SendMessage(w.hWnd, win.EM_SETSEL, 0, 0x7fff)
}
return ok
}
func (w *intinputElement) updateProps(data *IntInput) error {
// Remove the updown control is the range is too large.
if w.hwndUpDown != 0 && !data.useUpDownControl() {
win.DestroyWindow(w.hwndUpDown)
w.hwndUpDown = 0
}
text := strconv.FormatInt(data.Value, 10)
if text != w.Text() {
w.SetText(text)
}
if w.hwndUpDown != 0 {
win.SendMessage(w.hwndUpDown, win.UDM_SETRANGE32, uintptr(data.Min), uintptr(data.Max))
win.SendMessage(w.hwndUpDown, win.UDM_SETPOS32, 0, uintptr(data.Value))
}
err := updatePlaceholder(w.hWnd, data.Placeholder)
if err != nil {
return err
}
w.SetDisabled(data.Disabled)
w.onChange = data.OnChange
w.onFocus = data.OnFocus
w.onBlur = data.OnBlur
w.onEnterKey = data.OnEnterKey
return nil
}
func intinputWindowProc(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.
intinputGetPtr(hwnd).hWnd = 0
// Defer to the old window proc
case win.WM_SETFOCUS:
if w := intinputGetPtr(hwnd); w.onFocus != nil {
w.onFocus()
}
// Defer to the old window proc
case win.WM_KILLFOCUS:
if w := intinputGetPtr(hwnd); w.onBlur != nil {
w.onBlur()
}
// Defer to the old window proc
case win.WM_KEYDOWN:
if wParam == win.VK_RETURN {
if w := intinputGetPtr(hwnd); w.onEnterKey != nil {
w.thunkOnEnterKey()
return 0
}
}
// 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 EN_UPDATE, but we will
// still check.
switch notification := win.HIWORD(uint32(wParam)); notification {
case win.EN_UPDATE:
if w := intinputGetPtr(hwnd); w.onChange != nil {
w.thunkOnChange()
}
}
return 0
}
if intinputGetPtr(hwnd).hwndUpDown != 0 {
return win.CallWindowProc(intinput.oldWindowProc, hwnd, msg, wParam, lParam)
}
return win.CallWindowProc(edit.oldWindowProc, hwnd, msg, wParam, lParam)
}
func intinputGetPtr(hwnd win.HWND) *intinputElement {
gwl := win.GetWindowLongPtr(hwnd, win.GWLP_USERDATA)
if gwl == 0 {
panic("Internal error.")
}
ptr := (*intinputElement)(unsafe.Pointer(gwl))
if ptr.hWnd != hwnd && ptr.hWnd != 0 {
panic("Internal error.")
}
return ptr
}