-
Notifications
You must be signed in to change notification settings - Fork 1
/
commondialogs.go
228 lines (188 loc) · 5.45 KB
/
commondialogs.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
// Copyright 2010 The Walk Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build windows
package winapi
import (
"fmt"
"path/filepath"
"syscall"
"unsafe"
"github.com/Gipcomp/win32/comdlg32"
"github.com/Gipcomp/win32/handle"
"github.com/Gipcomp/win32/kernel32"
"github.com/Gipcomp/win32/ole32"
"github.com/Gipcomp/win32/shell32"
"github.com/Gipcomp/win32/user32"
"github.com/Gipcomp/win32/win"
"github.com/Gipcomp/winapi/errs"
)
type FileDialog struct {
Title string
FilePath string
FilePaths []string
InitialDirPath string
Filter string
FilterIndex int
Flags uint32
ShowReadOnlyCB bool
}
func (dlg *FileDialog) show(owner Form, fun func(ofn *comdlg32.OPENFILENAME) bool, flags uint32) (accepted bool, err error) {
ofn := new(comdlg32.OPENFILENAME)
ofn.LStructSize = uint32(unsafe.Sizeof(*ofn))
if owner != nil {
ofn.HwndOwner = owner.Handle()
}
filter := make([]uint16, len(dlg.Filter)+2)
strUtf, err := syscall.UTF16FromString(dlg.Filter)
if err != nil {
errs.NewError(err.Error())
}
copy(filter, strUtf)
// Replace '|' with the expected '\0'.
for i, c := range filter {
if byte(c) == '|' {
filter[i] = uint16(0)
}
}
ofn.LpstrFilter = &filter[0]
ofn.NFilterIndex = uint32(dlg.FilterIndex)
ofn.LpstrInitialDir, err = syscall.UTF16PtrFromString(dlg.InitialDirPath)
if err != nil {
errs.NewError(err.Error())
}
ofn.LpstrTitle, err = syscall.UTF16PtrFromString(dlg.Title)
if err != nil {
errs.NewError(err.Error())
}
ofn.Flags = comdlg32.OFN_FILEMUSTEXIST | flags | dlg.Flags
if !dlg.ShowReadOnlyCB {
ofn.Flags |= comdlg32.OFN_HIDEREADONLY
}
var fileBuf []uint16
if flags&comdlg32.OFN_ALLOWMULTISELECT > 0 {
fileBuf = make([]uint16, 65536)
} else {
fileBuf = make([]uint16, 1024)
strUtf, err := syscall.UTF16FromString(dlg.FilePath)
if err != nil {
errs.NewError(err.Error())
}
copy(fileBuf, strUtf)
}
ofn.LpstrFile = &fileBuf[0]
ofn.NMaxFile = uint32(len(fileBuf))
if !fun(ofn) {
errno := comdlg32.CommDlgExtendedError()
if errno != 0 {
err = errs.NewError(fmt.Sprintf("Error %d", errno))
}
return
}
dlg.FilterIndex = int(ofn.NFilterIndex)
if flags&comdlg32.OFN_ALLOWMULTISELECT > 0 {
split := func() [][]uint16 {
var parts [][]uint16
from := 0
for i, c := range fileBuf {
if c == 0 {
if i == from {
return parts
}
parts = append(parts, fileBuf[from:i])
from = i + 1
}
}
return parts
}
parts := split()
if len(parts) == 1 {
dlg.FilePaths = []string{syscall.UTF16ToString(parts[0])}
} else {
dirPath := syscall.UTF16ToString(parts[0])
dlg.FilePaths = make([]string, len(parts)-1)
for i, fp := range parts[1:] {
dlg.FilePaths[i] = filepath.Join(dirPath, syscall.UTF16ToString(fp))
}
}
} else {
dlg.FilePath = syscall.UTF16ToString(fileBuf)
}
accepted = true
return
}
func (dlg *FileDialog) ShowOpen(owner Form) (accepted bool, err error) {
return dlg.show(owner, comdlg32.GetOpenFileName, comdlg32.OFN_NOCHANGEDIR)
}
func (dlg *FileDialog) ShowOpenMultiple(owner Form) (accepted bool, err error) {
return dlg.show(owner, comdlg32.GetOpenFileName, comdlg32.OFN_ALLOWMULTISELECT|comdlg32.OFN_EXPLORER|comdlg32.OFN_NOCHANGEDIR)
}
func (dlg *FileDialog) ShowSave(owner Form) (accepted bool, err error) {
return dlg.show(owner, comdlg32.GetSaveFileName, comdlg32.OFN_NOCHANGEDIR)
}
func pathFromPIDL(pidl uintptr) (string, error) {
var path [kernel32.MAX_PATH]uint16
if !shell32.SHGetPathFromIDList(pidl, &path[0]) {
return "", errs.NewError("SHGetPathFromIDList failed")
}
return syscall.UTF16ToString(path[:]), nil
}
// We use this callback to disable the OK button in case of "invalid" selections.
func browseFolderCallback(hwnd handle.HWND, msg uint32, lp, wp uintptr) uintptr {
const BFFM_SELCHANGED = 2
if msg == BFFM_SELCHANGED {
_, err := pathFromPIDL(lp)
var enabled uintptr
if err == nil {
enabled = 1
}
const BFFM_ENABLEOK = user32.WM_USER + 101
user32.SendMessage(hwnd, BFFM_ENABLEOK, 0, enabled)
}
return 0
}
var browseFolderCallbackPtr uintptr
func init() {
AppendToWalkInit(func() {
browseFolderCallbackPtr = syscall.NewCallback(browseFolderCallback)
})
}
func (dlg *FileDialog) ShowBrowseFolder(owner Form) (accepted bool, err error) {
// Calling OleInitialize (or similar) is required for BIF_NEWDIALOGSTYLE.
if hr := ole32.OleInitialize(); hr != win.S_OK && hr != win.S_FALSE {
return false, errs.NewError(fmt.Sprint("OleInitialize Error: ", hr))
}
defer ole32.OleUninitialize()
var ownerHwnd handle.HWND
if owner != nil {
ownerHwnd = owner.Handle()
}
// We need to put the initial path into a buffer of at least MAX_LENGTH
// length, or we may get random crashes.
var buf [kernel32.MAX_PATH]uint16
strUtf, err := syscall.UTF16FromString(dlg.InitialDirPath)
if err != nil {
errs.NewError(err.Error())
}
copy(buf[:], strUtf)
const BIF_NEWDIALOGSTYLE = 0x00000040
strPtr, err := syscall.UTF16PtrFromString(dlg.Title)
if err != nil {
errs.NewError(err.Error())
}
bi := shell32.BROWSEINFO{
HwndOwner: ownerHwnd,
LpszTitle: strPtr,
UlFlags: BIF_NEWDIALOGSTYLE,
Lpfn: browseFolderCallbackPtr,
}
shell32.SHParseDisplayName(&buf[0], 0, &bi.PidlRoot, 0, nil)
pidl := shell32.SHBrowseForFolder(&bi)
if pidl == 0 {
return false, nil
}
defer ole32.CoTaskMemFree(pidl)
dlg.FilePath, err = pathFromPIDL(pidl)
accepted = dlg.FilePath != ""
return
}