-
-
Notifications
You must be signed in to change notification settings - Fork 37
/
file_windows.go
531 lines (475 loc) · 11.9 KB
/
file_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
package zenity
import (
"context"
"fmt"
"path/filepath"
"runtime"
"syscall"
"unicode/utf16"
"unsafe"
"github.com/ncruces/zenity/internal/win"
)
func selectFile(opts options) (string, error) {
name, _, shown, err := fileOpenDialog(opts, false)
if shown || opts.ctx != nil && opts.ctx.Err() != nil {
return name, err
}
if opts.directory {
return browseForFolder(opts)
}
var args win.OPENFILENAME
args.StructSize = uint32(unsafe.Sizeof(args))
args.Owner, _ = opts.attach.(win.HWND)
args.Flags = win.OFN_NOCHANGEDIR | win.OFN_FILEMUSTEXIST | win.OFN_EXPLORER
if opts.title != nil {
args.Title = strptr(*opts.title)
}
if opts.showHidden {
args.Flags |= win.OFN_FORCESHOWHIDDEN
}
if opts.fileFilters != nil {
args.Filter = initFilters(opts.fileFilters)
}
var res [32768]uint16
args.File = &res[0]
args.MaxFile = uint32(len(res))
args.InitialDir, args.DefExt = initDirNameExt(opts.filename, res[:])
defer setup(args.Owner)()
unhook, err := hookDialog(opts.ctx, opts.windowIcon, nil, nil)
if err != nil {
return "", err
}
defer unhook()
ok := win.GetOpenFileName(&args)
if opts.ctx != nil && opts.ctx.Err() != nil {
return "", opts.ctx.Err()
}
if !ok {
return "", win.CommDlgError()
}
return syscall.UTF16ToString(res[:]), nil
}
func selectFileMultiple(opts options) ([]string, error) {
_, list, shown, err := fileOpenDialog(opts, true)
if shown || opts.ctx != nil && opts.ctx.Err() != nil {
return list, err
}
if opts.directory {
return nil, fmt.Errorf("%w: multiple directory", ErrUnsupported)
}
var args win.OPENFILENAME
args.StructSize = uint32(unsafe.Sizeof(args))
args.Owner, _ = opts.attach.(win.HWND)
args.Flags = win.OFN_NOCHANGEDIR | win.OFN_ALLOWMULTISELECT | win.OFN_FILEMUSTEXIST | win.OFN_EXPLORER
if opts.title != nil {
args.Title = strptr(*opts.title)
}
if opts.showHidden {
args.Flags |= win.OFN_FORCESHOWHIDDEN
}
if opts.fileFilters != nil {
args.Filter = initFilters(opts.fileFilters)
}
var res [32768 + 1024*256]uint16
args.File = &res[0]
args.MaxFile = uint32(len(res))
args.InitialDir, args.DefExt = initDirNameExt(opts.filename, res[:])
defer setup(args.Owner)()
unhook, err := hookDialog(opts.ctx, opts.windowIcon, nil, nil)
if err != nil {
return nil, err
}
defer unhook()
ok := win.GetOpenFileName(&args)
if opts.ctx != nil && opts.ctx.Err() != nil {
return nil, opts.ctx.Err()
}
if !ok {
return nil, win.CommDlgError()
}
var i int
var nul bool
var split []string
for j, p := range res {
if p == 0 {
if nul {
break
}
if i < j {
split = append(split, string(utf16.Decode(res[i:j])))
}
i = j + 1
nul = true
} else {
nul = false
}
}
if len := len(split) - 1; len > 0 {
base := split[0]
for i := 0; i < len; i++ {
split[i] = filepath.Join(base, string(split[i+1]))
}
split = split[:len]
}
return split, nil
}
func selectFileSave(opts options) (string, error) {
if opts.directory {
return selectFile(opts)
}
name, shown, err := fileSaveDialog(opts)
if shown || opts.ctx != nil && opts.ctx.Err() != nil {
return name, err
}
var args win.OPENFILENAME
args.StructSize = uint32(unsafe.Sizeof(args))
args.Owner, _ = opts.attach.(win.HWND)
args.Flags = win.OFN_NOCHANGEDIR | win.OFN_PATHMUSTEXIST | win.OFN_NOREADONLYRETURN | win.OFN_EXPLORER
if opts.title != nil {
args.Title = strptr(*opts.title)
}
if opts.confirmOverwrite {
args.Flags |= win.OFN_OVERWRITEPROMPT
}
if opts.confirmCreate {
args.Flags |= win.OFN_CREATEPROMPT
}
if opts.showHidden {
args.Flags |= win.OFN_FORCESHOWHIDDEN
}
if opts.fileFilters != nil {
args.Filter = initFilters(opts.fileFilters)
}
var res [32768]uint16
args.File = &res[0]
args.MaxFile = uint32(len(res))
args.InitialDir, args.DefExt = initDirNameExt(opts.filename, res[:])
defer setup(args.Owner)()
unhook, err := hookDialog(opts.ctx, opts.windowIcon, nil, nil)
if err != nil {
return "", err
}
defer unhook()
ok := win.GetSaveFileName(&args)
if opts.ctx != nil && opts.ctx.Err() != nil {
return "", opts.ctx.Err()
}
if !ok {
return "", win.CommDlgError()
}
return syscall.UTF16ToString(res[:]), nil
}
func fileOpenDialog(opts options, multi bool) (string, []string, bool, error) {
uninit, err := coInitialize()
if err != nil {
return "", nil, false, err
}
defer uninit()
owner, _ := opts.attach.(win.HWND)
defer setup(owner)()
var dialog *win.IFileOpenDialog
err = win.CoCreateInstance(
win.CLSID_FileOpenDialog, nil, win.CLSCTX_ALL,
win.IID_IFileOpenDialog, unsafe.Pointer(&dialog))
if err != nil {
return "", nil, false, err
}
defer dialog.Release()
flgs, err := dialog.GetOptions()
if err != nil {
return "", nil, false, err
}
flgs |= win.FOS_NOCHANGEDIR | win.FOS_FILEMUSTEXIST | win.FOS_FORCEFILESYSTEM
if multi {
flgs |= win.FOS_ALLOWMULTISELECT
}
if opts.directory {
flgs |= win.FOS_PICKFOLDERS
}
if opts.showHidden {
flgs |= win.FOS_FORCESHOWHIDDEN
}
err = dialog.SetOptions(flgs)
if err != nil {
return "", nil, false, err
}
if opts.title != nil {
dialog.SetTitle(strptr(*opts.title))
}
if opts.fileFilters != nil {
dialog.SetFileTypes(initFileTypes(opts.fileFilters))
}
if opts.filename != "" {
var item *win.IShellItem
dir, name, _ := splitDirAndName(opts.filename)
dialog.SetFileName(strptr(name))
if ext := filepath.Ext(name); len(ext) > 1 {
dialog.SetDefaultExtension(strptr(ext[1:]))
}
win.SHCreateItemFromParsingName(strptr(dir), nil, win.IID_IShellItem, &item)
if item != nil {
defer item.Release()
dialog.SetFolder(item)
}
}
unhook, err := hookDialog(opts.ctx, opts.windowIcon, nil, nil)
if err != nil {
return "", nil, false, err
}
defer unhook()
if opts.ctx != nil && opts.ctx.Done() != nil {
wait := make(chan struct{})
defer close(wait)
go func() {
select {
case <-opts.ctx.Done():
dialog.Close(win.E_TIMEOUT)
case <-wait:
}
}()
}
err = dialog.Show(owner)
if opts.ctx != nil && opts.ctx.Err() != nil {
return "", nil, true, opts.ctx.Err()
}
if err == win.E_CANCELED {
return "", nil, true, ErrCanceled
}
if err != nil {
return "", nil, true, err
}
if multi {
items, err := dialog.GetResults()
if err != nil {
return "", nil, true, err
}
defer items.Release()
count, err := items.GetCount()
if err != nil {
return "", nil, true, err
}
var lst []string
for i := uint32(0); i < count; i++ {
str, err := shellItemPath(items.GetItemAt(i))
if err != nil {
return "", nil, true, err
}
lst = append(lst, str)
}
return "", lst, true, nil
} else {
str, err := shellItemPath(dialog.GetResult())
if err != nil {
return "", nil, true, err
}
return str, nil, true, nil
}
}
func fileSaveDialog(opts options) (string, bool, error) {
uninit, err := coInitialize()
if err != nil {
return "", false, err
}
defer uninit()
owner, _ := opts.attach.(win.HWND)
defer setup(owner)()
var dialog *win.IFileSaveDialog
err = win.CoCreateInstance(
win.CLSID_FileSaveDialog, nil, win.CLSCTX_ALL,
win.IID_IFileSaveDialog, unsafe.Pointer(&dialog))
if err != nil {
return "", false, err
}
defer dialog.Release()
flgs, err := dialog.GetOptions()
if err != nil {
return "", false, err
}
flgs |= win.FOS_NOCHANGEDIR | win.FOS_PATHMUSTEXIST | win.FOS_NOREADONLYRETURN | win.FOS_FORCEFILESYSTEM
if opts.confirmOverwrite {
flgs |= win.FOS_OVERWRITEPROMPT
}
if opts.confirmCreate {
flgs |= win.FOS_CREATEPROMPT
}
if opts.showHidden {
flgs |= win.FOS_FORCESHOWHIDDEN
}
err = dialog.SetOptions(flgs)
if err != nil {
return "", false, err
}
if opts.title != nil {
dialog.SetTitle(strptr(*opts.title))
}
if opts.fileFilters != nil {
dialog.SetFileTypes(initFileTypes(opts.fileFilters))
}
if opts.filename != "" {
var item *win.IShellItem
dir, name, _ := splitDirAndName(opts.filename)
dialog.SetFileName(strptr(name))
if ext := filepath.Ext(name); len(ext) > 1 {
dialog.SetDefaultExtension(strptr(ext[1:]))
}
win.SHCreateItemFromParsingName(strptr(dir), nil, win.IID_IShellItem, &item)
if item != nil {
defer item.Release()
dialog.SetFolder(item)
}
}
unhook, err := hookDialog(opts.ctx, opts.windowIcon, nil, nil)
if err != nil {
return "", false, err
}
defer unhook()
if opts.ctx != nil && opts.ctx.Done() != nil {
wait := make(chan struct{})
defer close(wait)
go func() {
select {
case <-opts.ctx.Done():
dialog.Close(win.E_TIMEOUT)
case <-wait:
}
}()
}
err = dialog.Show(owner)
if opts.ctx != nil && opts.ctx.Err() != nil {
return "", true, opts.ctx.Err()
}
if err == win.E_CANCELED {
return "", true, ErrCanceled
}
if err != nil {
return "", true, err
}
str, err := shellItemPath(dialog.GetResult())
if err != nil {
return "", true, err
}
return str, true, nil
}
func shellItemPath(item *win.IShellItem, err error) (string, error) {
if err != nil {
return "", err
}
defer item.Release()
return item.GetDisplayName(win.SIGDN_FILESYSPATH)
}
func browseForFolder(opts options) (string, error) {
uninit, err := coInitialize()
if err != nil {
return "", err
}
defer uninit()
var args win.BROWSEINFO
args.Owner, _ = opts.attach.(win.HWND)
args.Flags = win.BIF_RETURNONLYFSDIRS
if opts.title != nil {
args.Title = strptr(*opts.title)
}
if opts.filename != "" {
args.LParam = strptr(opts.filename)
args.CallbackFunc = syscall.NewCallback(browseForFolderCallback)
}
unhook, err := hookDialog(opts.ctx, opts.windowIcon, nil, nil)
if err != nil {
return "", err
}
defer unhook()
ptr := win.SHBrowseForFolder(&args)
if opts.ctx != nil && opts.ctx.Err() != nil {
return "", opts.ctx.Err()
}
if ptr == nil {
return "", ErrCanceled
}
defer win.CoTaskMemFree(unsafe.Pointer(ptr))
var res [32768]uint16
win.SHGetPathFromIDListEx(ptr, &res[0], len(res), 0)
str := syscall.UTF16ToString(res[:])
return str, nil
}
func browseForFolderCallback(wnd win.HWND, msg uint32, lparam, data uintptr) uintptr {
if msg == win.BFFM_INITIALIZED {
win.SendMessage(wnd, win.BFFM_SETSELECTION, 1, data)
}
return 0
}
func coInitialize() (context.CancelFunc, error) {
runtime.LockOSThread()
// .NET uses MTA for all background threads, so do the same.
// If someone needs STA because they're doing UI,
// they should initialize COM themselves before.
err := win.CoInitializeEx(0, win.COINIT_MULTITHREADED|win.COINIT_DISABLE_OLE1DDE)
if err == win.S_FALSE {
// COM was already initialized, we simply increased the ref count.
// Make this a no-op by decreasing our ref count.
win.CoUninitialize()
return runtime.UnlockOSThread, nil
}
// Don't uninitialize COM; this is against the docs, but it's what .NET does.
// Eventually all threads will have COM initialized.
if err == nil || err == win.RPC_E_CHANGED_MODE {
return runtime.UnlockOSThread, nil
}
runtime.UnlockOSThread()
return nil, err
}
func initDirNameExt(filename string, name []uint16) (dir *uint16, ext *uint16) {
d, n, _ := splitDirAndName(filename)
e := filepath.Ext(n)
if n != "" {
copy(name, syscall.StringToUTF16(n))
}
if d != "" {
dir = syscall.StringToUTF16Ptr(d)
}
if len(e) > 1 {
ext = syscall.StringToUTF16Ptr(e[1:])
}
return
}
func initFilters(filters FileFilters) *uint16 {
filters.simplify()
filters.name()
var res []uint16
for _, f := range filters {
if len(f.Patterns) == 0 {
continue
}
res = append(res, syscall.StringToUTF16(f.Name)...)
for _, p := range f.Patterns {
res = append(res, syscall.StringToUTF16(p)...)
res[len(res)-1] = ';'
}
res[len(res)-1] = 0
}
if res != nil {
res = append(res, 0)
return &res[0]
}
return nil
}
func initFileTypes(filters FileFilters) (int, *win.COMDLG_FILTERSPEC) {
filters.simplify()
filters.name()
var res []win.COMDLG_FILTERSPEC
for _, f := range filters {
if len(f.Patterns) == 0 {
continue
}
var spec []uint16
for _, p := range f.Patterns {
spec = append(spec, syscall.StringToUTF16(p)...)
spec[len(spec)-1] = ';'
}
spec[len(spec)-1] = 0
res = append(res, win.COMDLG_FILTERSPEC{
Name: syscall.StringToUTF16Ptr(f.Name),
Spec: &spec[0],
})
}
return len(res), &res[0]
}