This repository has been archived by the owner on Jun 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathdlgs_windows.go
610 lines (505 loc) · 16.3 KB
/
dlgs_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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
// +build windows,!linux,!darwin,!js
package dlgs
import (
"strings"
"syscall"
"unicode/utf16"
"unsafe"
)
var (
user32 = syscall.NewLazyDLL("user32.dll")
gdi32 = syscall.NewLazyDLL("gdi32.dll")
comdlg32 = syscall.NewLazyDLL("comdlg32.dll")
shell32 = syscall.NewLazyDLL("shell32.dll")
kernel32 = syscall.NewLazyDLL("kernel32.dll")
messageBoxW = user32.NewProc("MessageBoxW")
createWindowExW = user32.NewProc("CreateWindowExW")
defWindowProcW = user32.NewProc("DefWindowProcW")
destroyWindowW = user32.NewProc("DestroyWindow")
dispatchMessageW = user32.NewProc("DispatchMessageW")
getMessageW = user32.NewProc("GetMessageW")
sendMessageW = user32.NewProc("SendMessageW")
postQuitMessageW = user32.NewProc("PostQuitMessage")
registerClassExW = user32.NewProc("RegisterClassExW")
unregisterClassW = user32.NewProc("UnregisterClassW")
translateMessageW = user32.NewProc("TranslateMessage")
setWindowTextW = user32.NewProc("SetWindowTextW")
setFocus = user32.NewProc("SetFocus")
getWindowTextLengthW = user32.NewProc("GetWindowTextLengthW")
getWindowTextW = user32.NewProc("GetWindowTextW")
getWindowLongW = user32.NewProc("GetWindowLongW")
setWindowLongW = user32.NewProc("SetWindowLongW")
getWindowRectW = user32.NewProc("GetWindowRect")
setWindowPosW = user32.NewProc("SetWindowPos")
showWindowW = user32.NewProc("ShowWindow")
updateWindowW = user32.NewProc("UpdateWindow")
isDialogMessageW = user32.NewProc("IsDialogMessageW")
getSystemMetricsW = user32.NewProc("GetSystemMetrics")
systemParametersInfoW = user32.NewProc("SystemParametersInfoW")
createFontIndirectW = gdi32.NewProc("CreateFontIndirectW")
getOpenFileNameW = comdlg32.NewProc("GetOpenFileNameW")
chooseColorW = comdlg32.NewProc("ChooseColorW")
shBrowseForFolderW = shell32.NewProc("SHBrowseForFolderW")
shGetPathFromIDListW = shell32.NewProc("SHGetPathFromIDListW")
getModuleHandleW = kernel32.NewProc("GetModuleHandleW")
)
const (
mbOk = 0x00000000
mbYesNo = 0x00000004
mbDefaultIcon1 = 0x00000000
mbDefaultIcon2 = 0x00000100
mbIconInfo = 0x00000040
mbIconWarning = 0x00000030
mbIconError = 0x00000010
mbIconQuestion = 0x00000020
idOk = 1
idYes = 6
swShow = 5
swShowNormal = 1
swUseDefault = 0x80000000
swpNoZOrder = 0x0004
swpNoSize = 0x0001
smCxScreen = 0
smCyScreen = 1
wsThickFrame = 0x00040000
wsSysMenu = 0x00080000
wsBorder = 0x00800000
wsCaption = 0x00C00000
wsChild = 0x40000000
wsVisible = 0x10000000
wsMaximizeBox = 0x00010000
wsMinimizeBox = 0x00020000
wsTabStop = 0x00010000
wsGroup = 0x00020000
wsOverlappedWindow = 0x00CF0000
wsExClientEdge = 0x00000200
wmCreate = 0x0001
wmDestroy = 0x0002
wmClose = 0x0010
wmCommand = 0x0111
wmSetFont = 0x0030
wmKeydown = 0x0100
wmInitDialog = 0x0110
ofnAllowMultiSelect = 0x00000200
ofnExplorer = 0x00080000
ofnFileMustExist = 0x00001000
ofnHideReadOnly = 0x00000004
ofnOverwriteprompt = 0x00000002
esPassword = 0x0020
esAutoVScroll = 0x0040
esAutoHScroll = 0x0080
bifEditBox = 0x00000010
bifNewDialogStyle = 0x00000040
ccRgbInit = 0x00000001
ccFullOpen = 0x00000002
lbAddString = 0x0180
lbGetCurSel = 0x0188
lbGetSelCount = 0x0190
lbGetSelItems = 0x0191
lbGetItemData = 0x0199
lbSetItemData = 0x019A
lbSeparator = "LB_SEP"
lbsExtendedsel = 0x0800
dtsUpdown = 0x0001
dtsShowNone = 0x0002
dtsShortDateFormat = 0x0000
dtsLongDateFormat = 0x0004
dtmFirst = 0x1000
dtmGetSystemTime = dtmFirst + 1
dtmSetSystemTime = dtmFirst + 2
gdtError = -1
gdtValid = 0
gdtNone = 1
vkEscape = 0x1B
enUpdate = 0x0400
bsPushButton = 0
colorWindow = 5
spiGetNonClientMetrics = 0x0029
gwlStyle = -16
maxPath = 260
)
// wndClassExW https://msdn.microsoft.com/en-us/library/windows/desktop/ms633577.aspx
type wndClassExW struct {
size uint32
style uint32
wndProc uintptr
clsExtra int32
wndExtra int32
instance syscall.Handle
icon syscall.Handle
cursor syscall.Handle
background syscall.Handle
menuName *uint16
className *uint16
iconSm syscall.Handle
}
// msgW https://msdn.microsoft.com/en-us/library/windows/desktop/ms644958.aspx
type msgW struct {
hwnd syscall.Handle
message uint32
wParam uintptr
lParam uintptr
time uint32
pt pointW
}
// nonClientMetricsW https://msdn.microsoft.com/en-us/library/windows/desktop/ff729175.aspx
type nonClientMetricsW struct {
cbSize uint32
iBorderWidth int32
iScrollWidth int32
iScrollHeight int32
iCaptionWidth int32
iCaptionHeight int32
lfCaptionFont logfontW
iSmCaptionWidth int32
iSmCaptionHeight int32
lfSmCaptionFont logfontW
iMenuWidth int32
iMenuHeight int32
lfMenuFont logfontW
lfStatusFont logfontW
lfMessageFont logfontW
}
// logfontW https://msdn.microsoft.com/en-us/library/windows/desktop/dd145037.aspx
type logfontW struct {
lfHeight int32
lfWidth int32
lfEscapement int32
lfOrientation int32
lfWeight int32
lfItalic byte
lfUnderline byte
lfStrikeOut byte
lfCharSet byte
lfOutPrecision byte
lfClipPrecision byte
lfQuality byte
lfPitchAndFamily byte
lfFaceName [32]uint16
}
type pointW struct {
x, y int32
}
type rectW struct {
left int32
top int32
right int32
bottom int32
}
// openfilenameW https://msdn.microsoft.com/en-us/library/windows/desktop/ms646839.aspx
type openfilenameW struct {
lStructSize uint32
hwndOwner syscall.Handle
hInstance syscall.Handle
lpstrFilter *uint16
lpstrCustomFilter *uint16
nMaxCustFilter uint32
nFilterIndex uint32
lpstrFile *uint16
nMaxFile uint32
lpstrFileTitle *uint16
nMaxFileTitle uint32
lpstrInitialDir *uint16
lpstrTitle *uint16
flags uint32
nFileOffset uint16
nFileExtension uint16
lpstrDefExt *uint16
lCustData uintptr
lpfnHook syscall.Handle
lpTemplateName *uint16
pvReserved unsafe.Pointer
dwReserved uint32
flagsEx uint32
}
// browseinfoW http://msdn.microsoft.com/en-us/library/windows/desktop/bb773205.aspx
type browseinfoW struct {
owner syscall.Handle
root *uint16
displayName *uint16
title *uint16
flags uint32
callbackFunc uintptr
lParam uintptr
image int32
}
// choosecolorW https://msdn.microsoft.com/en-us/library/windows/desktop/ms646830.aspx
type choosecolorW struct {
lStructSize uint32
hwndOwner syscall.Handle
hInstance syscall.Handle
rgbResult uint32
lpCustColors *uint32
flags uint32
lCustData uintptr
lpfnHook uintptr
lpTemplateName *uint16
}
// systemtimeW https://msdn.microsoft.com/en-us/library/windows/desktop/ms724950.aspx
type systemtimeW struct {
wYear uint16
wMonth uint16
wDayOfWeek uint16
wDay uint16
wHour uint16
wMinute uint16
wSecond uint16
wMilliseconds uint16
}
func messageBox(title, text string, flags int) int {
ret, _, _ := messageBoxW.Call(0, uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(text))),
uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(title))), uintptr(uint(flags)))
return int(ret)
}
func getModuleHandle() (syscall.Handle, error) {
ret, _, err := getModuleHandleW.Call(uintptr(0))
if ret == 0 {
return 0, err
}
return syscall.Handle(ret), nil
}
func createWindow(exStyle uint64, className, windowName string, style uint64, x, y, width, height int64,
parent, menu, instance syscall.Handle) (syscall.Handle, error) {
ret, _, err := createWindowExW.Call(uintptr(exStyle), uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(className))),
uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(windowName))), uintptr(style), uintptr(x), uintptr(y),
uintptr(width), uintptr(height), uintptr(parent), uintptr(menu), uintptr(instance), uintptr(0))
if ret == 0 {
return 0, err
}
return syscall.Handle(ret), nil
}
func setWindowFocus(instance syscall.Handle) (syscall.Handle, error) {
ret, _, err := setFocus.Call(uintptr(instance))
if ret == 0 {
return 0, err
}
return syscall.Handle(ret), nil
}
func destroyWindow(hwnd syscall.Handle) error {
ret, _, err := destroyWindowW.Call(uintptr(hwnd))
if ret == 0 {
return err
}
return nil
}
func defWindowProc(hwnd syscall.Handle, msg uint32, wparam, lparam uintptr) uintptr {
ret, _, _ := defWindowProcW.Call(uintptr(hwnd), uintptr(msg), uintptr(wparam), uintptr(lparam))
return uintptr(ret)
}
func registerClassEx(wcx *wndClassExW) (uint16, error) {
ret, _, err := registerClassExW.Call(uintptr(unsafe.Pointer(wcx)))
if ret == 0 {
return 0, err
}
return uint16(ret), nil
}
func unregisterClass(className string, instance syscall.Handle) bool {
ret, _, _ := unregisterClassW.Call(uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(className))), uintptr(instance))
return ret != 0
}
func getMessage(msg *msgW, hwnd syscall.Handle, msgFilterMin, msgFilterMax uint32) (bool, error) {
ret, _, err := getMessageW.Call(uintptr(unsafe.Pointer(msg)), uintptr(hwnd), uintptr(msgFilterMin), uintptr(msgFilterMax))
if int32(ret) == -1 {
return false, err
}
return int32(ret) != 0, nil
}
func sendMessage(hwnd syscall.Handle, msg uint32, wparam, lparam uintptr) uintptr {
ret, _, _ := sendMessageW.Call(uintptr(hwnd), uintptr(msg), wparam, lparam, 0, 0)
return ret
}
func dispatchMessage(msg *msgW) {
dispatchMessageW.Call(uintptr(unsafe.Pointer(msg)))
}
func postQuitMessage(exitCode int32) {
postQuitMessageW.Call(uintptr(exitCode))
}
func translateMessage(msg *msgW) {
translateMessageW.Call(uintptr(unsafe.Pointer(msg)))
}
func setWindowText(hwnd syscall.Handle, text string) {
setWindowTextW.Call(uintptr(hwnd), uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(text))))
}
func getWindowTextLength(hwnd syscall.Handle) int {
ret, _, _ := getWindowTextLengthW.Call(uintptr(hwnd))
return int(ret)
}
func getWindowText(hwnd syscall.Handle) string {
textLen := getWindowTextLength(hwnd) + 1
buf := make([]uint16, textLen)
getWindowTextW.Call(uintptr(hwnd), uintptr(unsafe.Pointer(&buf[0])), uintptr(textLen))
return syscall.UTF16ToString(buf)
}
func systemParametersInfo(uiAction, uiParam uint32, pvParam unsafe.Pointer, fWinIni uint32) bool {
ret, _, _ := systemParametersInfoW.Call(uintptr(uiAction), uintptr(uiParam), uintptr(pvParam), uintptr(fWinIni), 0, 0)
return int32(ret) != 0
}
func createFontIndirect(lplf *logfontW) uintptr {
ret, _, _ := createFontIndirectW.Call(uintptr(unsafe.Pointer(lplf)), 0, 0)
return uintptr(ret)
}
func getWindowLong(hwnd syscall.Handle, index int32) int32 {
ret, _, _ := getWindowLongW.Call(uintptr(hwnd), uintptr(index), 0)
return int32(ret)
}
func setWindowLong(hwnd syscall.Handle, index, value int32) int32 {
ret, _, _ := setWindowLongW.Call(uintptr(hwnd), uintptr(index), uintptr(value))
return int32(ret)
}
func getWindowRect(hwnd syscall.Handle, rect *rectW) bool {
ret, _, _ := getWindowRectW.Call(uintptr(hwnd), uintptr(unsafe.Pointer(rect)), 0)
return ret != 0
}
func setWindowPos(hwnd, hwndInsertAfter syscall.Handle, x, y, width, height int32, flags uint32) bool {
ret, _, _ := setWindowPosW.Call(uintptr(hwnd), uintptr(hwndInsertAfter),
uintptr(x), uintptr(y), uintptr(width), uintptr(height), uintptr(flags), 0, 0)
return ret != 0
}
func showWindow(hwnd syscall.Handle, nCmdShow int32) bool {
ret, _, _ := showWindowW.Call(uintptr(hwnd), uintptr(nCmdShow), 0)
return ret != 0
}
func updateWindow(hwnd syscall.Handle) bool {
ret, _, _ := updateWindowW.Call(uintptr(hwnd), 0, 0)
return ret != 0
}
func isDialogMessage(hwnd syscall.Handle, msg *msgW) bool {
ret, _, _ := isDialogMessageW.Call(uintptr(hwnd), uintptr(unsafe.Pointer(msg)), 0)
return ret != 0
}
func getSystemMetrics(nindex int32) int32 {
ret, _, _ := getSystemMetricsW.Call(uintptr(nindex), 0, 0)
return int32(ret)
}
func getOpenFileName(lpofn *openfilenameW) bool {
ret, _, _ := getOpenFileNameW.Call(uintptr(unsafe.Pointer(lpofn)), 0, 0)
return ret != 0
}
func shBrowseForFolder(lpbi *browseinfoW) uintptr {
ret, _, _ := shBrowseForFolderW.Call(uintptr(unsafe.Pointer(lpbi)), 0, 0)
return ret
}
func shGetPathFromIDList(pidl uintptr, pszPath *uint16) bool {
ret, _, _ := shGetPathFromIDListW.Call(pidl, uintptr(unsafe.Pointer(pszPath)), 0)
return ret != 0
}
func chooseColor(lpcc *choosecolorW) bool {
ret, _, _ := chooseColorW.Call(uintptr(unsafe.Pointer(lpcc)), 0, 0)
return ret != 0
}
func centerWindow(hwnd syscall.Handle) {
var rc rectW
getWindowRect(hwnd, &rc)
xPos := (getSystemMetrics(smCxScreen) - (rc.right - rc.left)) / 2
yPos := (getSystemMetrics(smCyScreen) - (rc.bottom - rc.top)) / 2
setWindowPos(hwnd, 0, xPos, yPos, 0, 0, swpNoZOrder|swpNoSize)
}
func getMessageFont() uintptr {
var metrics nonClientMetricsW
metrics.cbSize = uint32(unsafe.Sizeof(metrics))
systemParametersInfo(spiGetNonClientMetrics, uint32(unsafe.Sizeof(metrics)), unsafe.Pointer(&metrics), 0)
return createFontIndirect(&metrics.lfMessageFont)
}
func registerClass(className string, instance syscall.Handle, fn interface{}) error {
var wcx wndClassExW
wcx.size = uint32(unsafe.Sizeof(wcx))
wcx.wndProc = syscall.NewCallback(fn)
wcx.instance = instance
wcx.background = colorWindow + 1
wcx.className = syscall.StringToUTF16Ptr(className)
_, err := registerClassEx(&wcx)
return err
}
func messageLoop(hwnd syscall.Handle) error {
for {
msg := msgW{}
gotMessage, err := getMessage(&msg, 0, 0, 0)
if err != nil {
return err
}
if gotMessage {
if !isDialogMessage(hwnd, &msg) {
translateMessage(&msg)
dispatchMessage(&msg)
}
} else {
break
}
}
return nil
}
func utf16PtrFromString(s string) *uint16 {
b := utf16.Encode([]rune(s))
return &b[0]
}
func stringFromUtf16Ptr(p *uint16) string {
b := *(*[maxPath]uint16)(unsafe.Pointer(p))
r := utf16.Decode(b[:])
return strings.Trim(string(r), "\x00")
}
// editBox displays textedit/inputbox dialog.
func editBox(title, text, defaultText, className string, password bool) (string, bool, error) {
var out string
notCancledOrClosed := true
var hwndEdit syscall.Handle
instance, err := getModuleHandle()
if err != nil {
return out, false, err
}
fn := func(hwnd syscall.Handle, msg uint32, wparam, lparam uintptr) uintptr {
switch msg {
case wmClose:
notCancledOrClosed = false
destroyWindow(hwnd)
case wmDestroy:
postQuitMessage(0)
case wmKeydown:
if wparam == vkEscape {
notCancledOrClosed = false
destroyWindow(hwnd)
}
case wmCommand:
if wparam == 100 {
out = getWindowText(hwndEdit)
destroyWindow(hwnd)
} else if wparam == 110 {
notCancledOrClosed = false
destroyWindow(hwnd)
}
default:
ret := defWindowProc(hwnd, msg, wparam, lparam)
return ret
}
return 0
}
err = registerClass(className, instance, fn)
if err != nil {
return out, false, err
}
defer unregisterClass(className, instance)
hwnd, _ := createWindow(0, className, title, wsOverlappedWindow, swUseDefault, swUseDefault, 235, 140, 0, 0, instance)
hwndText, _ := createWindow(0, "STATIC", text, wsChild|wsVisible, 10, 10, 200, 16, hwnd, 0, instance)
flags := wsBorder | wsChild | wsVisible | wsGroup | wsTabStop | esAutoHScroll
if password {
flags |= esPassword
}
hwndEdit, _ = createWindow(wsExClientEdge, "EDIT", defaultText, uint64(flags), 10, 30, 200, 24, hwnd, 0, instance)
hwndOK, _ := createWindow(wsExClientEdge, "BUTTON", "OK", wsChild|wsVisible|bsPushButton|wsGroup|wsTabStop, 10, 65, 90, 24, hwnd, 100, instance)
hwndCancel, _ := createWindow(wsExClientEdge, "BUTTON", "Cancel", wsChild|wsVisible|bsPushButton|wsGroup|wsTabStop, 120, 65, 90, 24, hwnd, 110, instance)
setWindowLong(hwnd, gwlStyle, getWindowLong(hwnd, gwlStyle)^wsMinimizeBox)
setWindowLong(hwnd, gwlStyle, getWindowLong(hwnd, gwlStyle)^wsMaximizeBox)
font := getMessageFont()
sendMessage(hwndText, wmSetFont, font, 0)
sendMessage(hwndEdit, wmSetFont, font, 0)
sendMessage(hwndOK, wmSetFont, font, 0)
sendMessage(hwndCancel, wmSetFont, font, 0)
centerWindow(hwnd)
showWindow(hwnd, swShowNormal)
updateWindow(hwnd)
setWindowFocus(hwndEdit)
err = messageLoop(hwnd)
if err != nil {
return out, false, err
}
return out, notCancledOrClosed, nil
}