-
Notifications
You must be signed in to change notification settings - Fork 30
/
commondlgs.go
123 lines (100 loc) · 3.5 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
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
/*
* Copyright (C) 2019 The Winc Authors. All Rights Reserved.
* Copyright (C) 2010-2013 Allen Dang. All Rights Reserved.
*/
package winc
import (
"fmt"
"syscall"
"unsafe"
"github.com/tadvi/winc/w32"
)
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 expected '\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
}
// MsgBoxOkCancel basic pop up message. Returns 1 for OK and 2 for CANCEL.
func MsgBoxOkCancel(parent Controller, title, caption string) int {
return MsgBox(parent, title, caption, w32.MB_ICONEXCLAMATION|w32.MB_OKCANCEL)
}
func MsgBoxYesNo(parent Controller, title, caption string) int {
return MsgBox(parent, title, caption, w32.MB_ICONEXCLAMATION|w32.MB_YESNO)
}
func MsgBoxOk(parent Controller, title, caption string) {
MsgBox(parent, title, caption, w32.MB_ICONINFORMATION|w32.MB_OK)
}
// Warningf is generic warning message with OK and Cancel buttons. Returns 1 for OK.
func Warningf(parent Controller, format string, data ...interface{}) int {
caption := fmt.Sprintf(format, data...)
return MsgBox(parent, "Warning", caption, w32.MB_ICONWARNING|w32.MB_OKCANCEL)
}
// Printf is generic info message with OK button.
func Printf(parent Controller, format string, data ...interface{}) {
caption := fmt.Sprintf(format, data...)
MsgBox(parent, "Information", caption, w32.MB_ICONINFORMATION|w32.MB_OK)
}
// Errorf is generic error message with OK button.
func Errorf(parent Controller, format string, data ...interface{}) {
caption := fmt.Sprintf(format, data...)
MsgBox(parent, "Error", caption, w32.MB_ICONERROR|w32.MB_OK)
}
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
}