-
Notifications
You must be signed in to change notification settings - Fork 24
/
file_save_dialog.go
151 lines (135 loc) · 3.25 KB
/
file_save_dialog.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
//+build windows
package wui
import (
"os"
"strings"
"syscall"
"github.com/gonutz/w32/v2"
)
type FileSaveDialog struct {
appendExt bool
filters []uint16
filterCount int
filterIndex int
initPath string
title string
exts []string
}
func NewFileSaveDialog() *FileSaveDialog {
return &FileSaveDialog{appendExt: true}
}
func (dlg *FileSaveDialog) SetAppendExt(ext bool) {
dlg.appendExt = ext
}
func (dlg *FileSaveDialog) SetTitle(title string) {
dlg.title = title
}
func (dlg *FileSaveDialog) SetInitialPath(path string) {
dlg.initPath = path
}
func (dlg *FileSaveDialog) AddFilter(text, ext1 string, exts ...string) {
text16, err := syscall.UTF16FromString(text)
if err != nil {
return
}
validateMask := func(ext string) string {
ext = strings.TrimSpace(ext)
if ext == "" {
return "*.*"
}
if !strings.HasPrefix(ext, ".") {
ext = "*." + ext
} else if !strings.HasPrefix(ext, "*") {
ext = "*" + ext
}
return ext
}
mask := validateMask(ext1)
for _, ext := range exts {
mask += ";" + validateMask(ext)
}
mask16, err := syscall.UTF16FromString(mask)
if err != nil {
return
}
dlg.filters = append(dlg.filters, text16...)
dlg.filters = append(dlg.filters, mask16...)
dlg.filterCount++
if ext1 == "" {
dlg.exts = append(dlg.exts, "")
} else {
dlg.exts = append(
dlg.exts,
"."+strings.TrimPrefix(strings.ToLower(ext1), "."),
)
}
}
// SetFilterIndex sets the active filter, 0-indexed.
func (dlg *FileSaveDialog) SetFilterIndex(i int) {
dlg.filterIndex = i
}
func (dlg *FileSaveDialog) Execute(parent *Window) (bool, string) {
ok, buf, filterIndex := dlg.getSaveFileName(parent, w32.MAX_PATH+2)
if ok {
path := syscall.UTF16ToString(buf)
if dlg.appendExt && 0 <= filterIndex && filterIndex < len(dlg.exts) {
ext := dlg.exts[filterIndex]
if !strings.HasSuffix(path, ext) {
path += ext
}
}
return true, path
}
return false, ""
}
func (dlg *FileSaveDialog) getSaveFileName(parent *Window, bufLen int) (bool, []uint16, int) {
var owner w32.HWND
if parent != nil {
owner = parent.handle
}
dlg.filters = append(dlg.filters, 0)
if dlg.filterIndex < 0 {
dlg.filterIndex = 0
}
if dlg.filterIndex >= dlg.filterCount {
dlg.filterIndex = dlg.filterCount - 1
}
var initDir *uint16
var initDir16 []uint16
filenameBuf := make([]uint16, bufLen)
if dlg.initPath != "" {
if info, err := os.Stat(dlg.initPath); err == nil && info.IsDir() {
initDir16, err = syscall.UTF16FromString(dlg.initPath)
if err == nil {
initDir = &initDir16[0]
}
} else {
path, err := syscall.UTF16FromString(dlg.initPath)
if err == nil {
copy(filenameBuf, path)
}
}
}
var title16 []uint16
var title *uint16
if dlg.title != "" {
var err error
title16, err = syscall.UTF16FromString(dlg.title)
if err == nil {
title = &title16[0]
}
}
ofn := &w32.OPENFILENAME{
Owner: owner,
Filter: &dlg.filters[0],
FilterIndex: uint32(dlg.filterIndex + 1), // NOTE one-indexed
File: &filenameBuf[0],
MaxFile: uint32(len(filenameBuf)),
InitialDir: initDir,
Title: title,
Flags: w32.OFN_ENABLESIZING | w32.OFN_EXPLORER | w32.OFN_LONGNAMES |
w32.OFN_OVERWRITEPROMPT,
}
ok := w32.GetSaveFileName(ofn)
return ok, filenameBuf, int(ofn.FilterIndex) - 1
}