-
Notifications
You must be signed in to change notification settings - Fork 45
/
commondlgs.go
88 lines (70 loc) · 2.16 KB
/
commondlgs.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
package gform
import (
"github.com/AllenDang/w32"
"syscall"
"unsafe"
)
func genOFN(parent Controller, title, filter string, filterIndex uint, initialDir string, buf []uint16) *w32.OPENFILENAME {
var ofn w32.OPENFILENAME
ofn.StructSize = uint32(unsafe.Sizeof(ofn))
ofn.Owner = parent.Handle()
if filter != "" {
filterBuf := make([]uint16, len(filter)+1)
copy(filterBuf, syscall.StringToUTF16(filter))
// Replace '|' with the expcted '\0'
for i, c := range filterBuf {
if byte(c) == '|' {
filterBuf[i] = uint16(0)
}
}
ofn.Filter = &filterBuf[0]
ofn.FilterIndex = uint32(filterIndex)
}
ofn.File = &buf[0]
ofn.MaxFile = uint32(len(buf))
if initialDir != "" {
ofn.InitialDir = syscall.StringToUTF16Ptr(initialDir)
}
if title != "" {
ofn.Title = syscall.StringToUTF16Ptr(title)
}
ofn.Flags = w32.OFN_FILEMUSTEXIST
return &ofn
}
func ShowOpenFileDlg(parent Controller, title, filter string, filterIndex uint, initialDir string) (filePath string, accepted bool) {
buf := make([]uint16, 1024)
ofn := genOFN(parent, title, filter, filterIndex, initialDir, buf)
if accepted = w32.GetOpenFileName(ofn); accepted {
filePath = syscall.UTF16ToString(buf)
}
return
}
func ShowSaveFileDlg(parent Controller, title, filter string, filterIndex uint, initialDir string) (filePath string, accepted bool) {
buf := make([]uint16, 1024)
ofn := genOFN(parent, title, filter, filterIndex, initialDir, buf)
if accepted = w32.GetSaveFileName(ofn); accepted {
filePath = syscall.UTF16ToString(buf)
}
return
}
func ShowBrowseFolderDlg(parent Controller, title string) (folder string, accepted bool) {
var bi w32.BROWSEINFO
bi.Owner = parent.Handle()
bi.Title = syscall.StringToUTF16Ptr(title)
bi.Flags = w32.BIF_RETURNONLYFSDIRS | w32.BIF_NEWDIALOGSTYLE
w32.CoInitialize()
ret := w32.SHBrowseForFolder(&bi)
w32.CoUninitialize()
folder = w32.SHGetPathFromIDList(ret)
accepted = folder != ""
return
}
func MsgBox(parent Controller, title, caption string, flags uint) int {
var result int
if parent != nil {
result = w32.MessageBox(parent.Handle(), caption, title, flags)
} else {
result = w32.MessageBox(0, caption, title, flags)
}
return result
}