zenity/internal/win/shell32.go

345 lines
10 KiB
Go
Raw Normal View History

2022-06-17 22:45:49 -04:00
//go:build windows
package win
2022-06-21 19:58:14 -04:00
import (
"syscall"
"unsafe"
2022-11-02 06:21:31 -04:00
"golang.org/x/sys/windows"
2022-06-21 19:58:14 -04:00
)
2022-06-17 22:45:49 -04:00
const (
2022-06-19 21:14:08 -04:00
BIF_RETURNONLYFSDIRS = 0x00000001
BFFM_INITIALIZED = 1
BFFM_SETSELECTION = WM_USER + 103
2022-06-22 10:14:52 -04:00
// ShellNotifyIcon messages
NIM_ADD = 0
NIM_MODIFY = 1
NIM_DELETE = 2
NIM_SETFOCUS = 3
NIM_SETVERSION = 4
// NOTIFYICONDATA flags
NIF_MESSAGE = 0x01
NIF_ICON = 0x02
NIF_TIP = 0x04
NIF_STATE = 0x08
NIF_INFO = 0x10
NIF_GUID = 0x20
NIF_REALTIME = 0x40
NIF_SHOWTIP = 0x80
// NOTIFYICONDATA info flags
NIIF_NONE = 0x00
NIIF_INFO = 0x01
NIIF_WARNING = 0x02
NIIF_ERROR = 0x03
NIIF_USER = 0x04
NIIF_NOSOUND = 0x10
NIIF_LARGE_ICON = 0x20
NIIF_RESPECT_QUIET_TIME = 0x80
NIIF_ICON_MASK = 0x0F
// NOTIFYICONDATA state
NIS_HIDDEN = 0x1
NIS_SHAREDICON = 0x2
2024-02-07 13:58:08 -05:00
)
// https://docs.microsoft.com/en-us/windows/win32/api/shobjidl_core/ne-shobjidl_core-_fileopendialogoptions
type _FILEOPENDIALOGOPTIONS int
const (
FOS_OVERWRITEPROMPT _FILEOPENDIALOGOPTIONS = 0x2
FOS_STRICTFILETYPES _FILEOPENDIALOGOPTIONS = 0x4
FOS_NOCHANGEDIR _FILEOPENDIALOGOPTIONS = 0x8
FOS_PICKFOLDERS _FILEOPENDIALOGOPTIONS = 0x20
FOS_FORCEFILESYSTEM _FILEOPENDIALOGOPTIONS = 0x40
FOS_ALLNONSTORAGEITEMS _FILEOPENDIALOGOPTIONS = 0x80
FOS_NOVALIDATE _FILEOPENDIALOGOPTIONS = 0x100
FOS_ALLOWMULTISELECT _FILEOPENDIALOGOPTIONS = 0x200
FOS_PATHMUSTEXIST _FILEOPENDIALOGOPTIONS = 0x800
FOS_FILEMUSTEXIST _FILEOPENDIALOGOPTIONS = 0x1000
FOS_CREATEPROMPT _FILEOPENDIALOGOPTIONS = 0x2000
FOS_SHAREAWARE _FILEOPENDIALOGOPTIONS = 0x4000
FOS_NOREADONLYRETURN _FILEOPENDIALOGOPTIONS = 0x8000
FOS_NOTESTFILECREATE _FILEOPENDIALOGOPTIONS = 0x10000
FOS_HIDEMRUPLACES _FILEOPENDIALOGOPTIONS = 0x20000
FOS_HIDEPINNEDPLACES _FILEOPENDIALOGOPTIONS = 0x40000
FOS_NODEREFERENCELINKS _FILEOPENDIALOGOPTIONS = 0x100000
FOS_OKBUTTONNEEDSINTERACTION _FILEOPENDIALOGOPTIONS = 0x200000
FOS_DONTADDTORECENT _FILEOPENDIALOGOPTIONS = 0x2000000
FOS_FORCESHOWHIDDEN _FILEOPENDIALOGOPTIONS = 0x10000000
FOS_DEFAULTNOMINIMODE _FILEOPENDIALOGOPTIONS = 0x20000000
FOS_FORCEPREVIEWPANEON _FILEOPENDIALOGOPTIONS = 0x40000000
FOS_SUPPORTSTREAMABLEITEMS _FILEOPENDIALOGOPTIONS = 0x80000000
)
// https://docs.microsoft.com/en-us/windows/win32/api/shobjidl_core/ne-shobjidl_core-sigdn
type SIGDN int
2022-06-22 10:14:52 -04:00
2024-02-07 13:58:08 -05:00
const (
SIGDN_NORMALDISPLAY SIGDN = 0x00000000
SIGDN_PARENTRELATIVEPARSING SIGDN = ^(^0x18001 + 0x80000000)
SIGDN_DESKTOPABSOLUTEPARSING SIGDN = ^(^0x28000 + 0x80000000)
SIGDN_PARENTRELATIVEEDITING SIGDN = ^(^0x31001 + 0x80000000)
SIGDN_DESKTOPABSOLUTEEDITING SIGDN = ^(^0x4c000 + 0x80000000)
SIGDN_FILESYSPATH SIGDN = ^(^0x58000 + 0x80000000)
SIGDN_URL SIGDN = ^(^0x68000 + 0x80000000)
SIGDN_PARENTRELATIVEFORADDRESSBAR SIGDN = ^(^0x7c001 + 0x80000000)
SIGDN_PARENTRELATIVE SIGDN = ^(^0x80001 + 0x80000000)
SIGDN_PARENTRELATIVEFORUI SIGDN = ^(^0x94001 + 0x80000000)
2022-06-17 22:45:49 -04:00
)
// https://docs.microsoft.com/en-us/windows/win32/api/shlobj_core/ns-shlobj_core-browseinfow
type BROWSEINFO struct {
2022-06-18 07:37:39 -04:00
Owner HWND
2022-06-19 21:14:08 -04:00
Root Pointer
2022-06-17 22:45:49 -04:00
DisplayName *uint16
Title *uint16
Flags uint32
CallbackFunc uintptr
LParam *uint16
Image int32
}
// https://docs.microsoft.com/en-us/windows/win32/api/shellapi/ns-shellapi-notifyicondataw
type NOTIFYICONDATA struct {
StructSize uint32
2022-06-18 07:37:39 -04:00
Wnd HWND
2022-06-17 22:45:49 -04:00
ID uint32
Flags uint32
CallbackMessage uint32
2022-06-18 07:37:39 -04:00
Icon Handle
2022-06-17 22:45:49 -04:00
Tip [128]uint16 // NOTIFYICONDATAA_V1_SIZE
State uint32
StateMask uint32
Info [256]uint16
Version uint32
InfoTitle [64]uint16
InfoFlags uint32
2022-06-18 07:37:39 -04:00
// GuidItem [16]byte // NOTIFYICONDATAA_V2_SIZE
// BalloonIcon Handle // NOTIFYICONDATAA_V3_SIZE
2022-06-17 22:45:49 -04:00
}
2024-02-07 13:58:08 -05:00
// https://docs.microsoft.com/en-us/windows/win32/api/shtypes/ns-shtypes-comdlg_filterspec
type COMDLG_FILTERSPEC struct {
Name *uint16
Spec *uint16
}
// https://docs.microsoft.com/en-us/windows/win32/api/shtypes/ns-shtypes-itemidlist
type ITEMIDLIST struct{}
2022-06-22 10:14:52 -04:00
2022-06-21 19:58:14 -04:00
// https://github.com/wine-mirror/wine/blob/master/include/shobjidl.idl
var (
2022-11-02 06:21:31 -04:00
IID_IShellItem = guid("\x1e\x6d\x82\x43\x18\xe7\xee\x42\xbc\x55\xa1\xe2\x61\xc3\x7b\xfe")
IID_IFileOpenDialog = guid("\x88\x72\x7c\xd5\xad\xd4\x68\x47\xbe\x02\x9d\x96\x95\x32\xd9\x60")
2024-02-07 08:00:08 -05:00
IID_IFileSaveDialog = guid("\x23\xcd\xbc\x84\xde\x5f\xdb\x4c\xae\xa4\xaf\x64\xb8\x3d\x78\xab")
2022-11-02 06:21:31 -04:00
CLSID_FileOpenDialog = guid("\x9c\x5a\x1c\xdc\x8a\xe8\xde\x4d\xa5\xa1\x60\xf8\x2a\x20\xae\xf7")
2024-02-07 08:00:08 -05:00
CLSID_FileSaveDialog = guid("\xf3\xe2\xb4\xc0\x21\xba\x73\x47\x8d\xba\x33\x5e\xc9\x46\xeb\x8b")
2022-06-21 19:58:14 -04:00
)
2022-06-22 10:14:52 -04:00
type IFileOpenDialog struct{ IFileDialog }
type iFileOpenDialogVtbl struct {
iFileDialogVtbl
2022-06-21 19:58:14 -04:00
GetResults uintptr
GetSelectedItems uintptr
}
2022-06-22 10:14:52 -04:00
func (u *IFileOpenDialog) GetResults() (res *IShellItemArray, err error) {
vtbl := *(**iFileOpenDialogVtbl)(unsafe.Pointer(u))
hr, _, _ := u.call(vtbl.GetResults, uintptr(unsafe.Pointer(&res)))
2022-06-21 19:58:14 -04:00
if hr != 0 {
err = syscall.Errno(hr)
}
return
}
2024-02-07 08:00:08 -05:00
type IFileSaveDialog struct{ IFileDialog }
type IFileSaveDialogVtbl struct {
iFileDialogVtbl
SetSaveAsItem uintptr
SetProperties uintptr
SetCollectedProperties uintptr
GetProperties uintptr
ApplyProperties uintptr
}
2022-06-22 10:14:52 -04:00
type IFileDialog struct{ IModalWindow }
type iFileDialogVtbl struct {
iModalWindowVtbl
2022-06-21 19:58:14 -04:00
SetFileTypes uintptr
SetFileTypeIndex uintptr
GetFileTypeIndex uintptr
Advise uintptr
Unadvise uintptr
SetOptions uintptr
GetOptions uintptr
SetDefaultFolder uintptr
SetFolder uintptr
GetFolder uintptr
GetCurrentSelection uintptr
SetFileName uintptr
GetFileName uintptr
SetTitle uintptr
SetOkButtonLabel uintptr
SetFileNameLabel uintptr
GetResult uintptr
AddPlace uintptr
SetDefaultExtension uintptr
Close uintptr
SetClientGuid uintptr
ClearClientData uintptr
SetFilter uintptr
}
2024-02-07 13:58:08 -05:00
func (u *IFileDialog) SetFileTypes(cFileTypes int, rgFilterSpec *COMDLG_FILTERSPEC) (err error) {
vtbl := *(**iFileDialogVtbl)(unsafe.Pointer(u))
hr, _, _ := u.call(vtbl.SetFileTypes, uintptr(cFileTypes), uintptr(unsafe.Pointer(rgFilterSpec)))
if hr != 0 {
err = syscall.Errno(hr)
}
return
}
func (u *IFileDialog) SetOptions(fos _FILEOPENDIALOGOPTIONS) (err error) {
2022-06-22 10:14:52 -04:00
vtbl := *(**iFileDialogVtbl)(unsafe.Pointer(u))
hr, _, _ := u.call(vtbl.SetOptions, uintptr(fos))
2022-06-21 19:58:14 -04:00
if hr != 0 {
err = syscall.Errno(hr)
}
return
}
2024-02-07 13:58:08 -05:00
func (u *IFileDialog) GetOptions() (fos _FILEOPENDIALOGOPTIONS, err error) {
2022-06-22 10:14:52 -04:00
vtbl := *(**iFileDialogVtbl)(unsafe.Pointer(u))
hr, _, _ := u.call(vtbl.GetOptions, uintptr(unsafe.Pointer(&fos)))
2022-06-21 19:58:14 -04:00
if hr != 0 {
err = syscall.Errno(hr)
}
return
}
2022-06-22 10:14:52 -04:00
func (u *IFileDialog) SetFolder(item *IShellItem) (err error) {
vtbl := *(**iFileDialogVtbl)(unsafe.Pointer(u))
hr, _, _ := u.call(vtbl.SetFolder, uintptr(unsafe.Pointer(item)))
2022-06-21 19:58:14 -04:00
if hr != 0 {
err = syscall.Errno(hr)
}
return
}
2024-02-07 05:50:58 -05:00
func (u *IFileDialog) SetFileName(name *uint16) (err error) {
vtbl := *(**iFileDialogVtbl)(unsafe.Pointer(u))
hr, _, _ := u.call(vtbl.SetFileName, uintptr(unsafe.Pointer(name)))
if hr != 0 {
err = syscall.Errno(hr)
}
return
}
2022-06-22 10:14:52 -04:00
func (u *IFileDialog) SetTitle(title *uint16) (err error) {
vtbl := *(**iFileDialogVtbl)(unsafe.Pointer(u))
hr, _, _ := u.call(vtbl.SetTitle, uintptr(unsafe.Pointer(title)))
2022-06-21 19:58:14 -04:00
if hr != 0 {
err = syscall.Errno(hr)
}
return
}
2024-02-07 05:50:58 -05:00
func (u *IFileDialog) SetDefaultExtension(extension *uint16) (err error) {
vtbl := *(**iFileDialogVtbl)(unsafe.Pointer(u))
hr, _, _ := u.call(vtbl.SetDefaultExtension, uintptr(unsafe.Pointer(extension)))
if hr != 0 {
err = syscall.Errno(hr)
}
return
}
2022-06-22 10:14:52 -04:00
func (u *IFileDialog) GetResult() (item *IShellItem, err error) {
vtbl := *(**iFileDialogVtbl)(unsafe.Pointer(u))
hr, _, _ := u.call(vtbl.GetResult, uintptr(unsafe.Pointer(&item)))
2022-06-21 19:58:14 -04:00
if hr != 0 {
err = syscall.Errno(hr)
}
return
}
2022-06-22 10:14:52 -04:00
type IModalWindow struct{ IUnknown }
type iModalWindowVtbl struct {
iUnknownVtbl
2022-06-21 19:58:14 -04:00
Show uintptr
}
2022-06-22 10:14:52 -04:00
func (u *IModalWindow) Show(wnd HWND) (err error) {
vtbl := *(**iModalWindowVtbl)(unsafe.Pointer(u))
hr, _, _ := u.call(vtbl.Show, uintptr(wnd))
2022-06-21 19:58:14 -04:00
if hr != 0 {
err = syscall.Errno(hr)
}
return
}
2022-06-22 10:14:52 -04:00
type IShellItem struct{ IUnknown }
type iShellItemVtbl struct {
iUnknownVtbl
2022-06-17 22:45:49 -04:00
BindToHandler uintptr
GetParent uintptr
GetDisplayName uintptr
GetAttributes uintptr
Compare uintptr
}
2024-02-07 13:58:08 -05:00
func (u *IShellItem) GetDisplayName(name SIGDN) (res string, err error) {
2022-11-02 06:21:31 -04:00
var ptr *uint16
2022-06-22 10:14:52 -04:00
vtbl := *(**iShellItemVtbl)(unsafe.Pointer(u))
hr, _, _ := u.call(vtbl.GetDisplayName, uintptr(name), uintptr(unsafe.Pointer(&ptr)))
2022-06-21 19:58:14 -04:00
if hr != 0 {
err = syscall.Errno(hr)
} else {
2022-11-02 06:21:31 -04:00
res = windows.UTF16PtrToString(ptr)
CoTaskMemFree(unsafe.Pointer(ptr))
2022-06-21 19:58:14 -04:00
}
return
}
2022-06-22 10:14:52 -04:00
type IShellItemArray struct{ IUnknown }
type iShellItemArrayVtbl struct {
iUnknownVtbl
2022-06-21 19:58:14 -04:00
BindToHandler uintptr
GetPropertyStore uintptr
GetPropertyDescriptionList uintptr
GetAttributes uintptr
GetCount uintptr
GetItemAt uintptr
EnumItems uintptr
}
2022-06-22 10:14:52 -04:00
func (u *IShellItemArray) GetCount() (numItems uint32, err error) {
vtbl := *(**iShellItemArrayVtbl)(unsafe.Pointer(u))
hr, _, _ := u.call(vtbl.GetCount, uintptr(unsafe.Pointer(&numItems)))
2022-06-21 19:58:14 -04:00
if hr != 0 {
err = syscall.Errno(hr)
}
return
}
2022-06-22 10:14:52 -04:00
func (u *IShellItemArray) GetItemAt(index uint32) (item *IShellItem, err error) {
vtbl := *(**iShellItemArrayVtbl)(unsafe.Pointer(u))
hr, _, _ := u.call(vtbl.GetItemAt, uintptr(index), uintptr(unsafe.Pointer(&item)))
2022-06-21 19:58:14 -04:00
if hr != 0 {
err = syscall.Errno(hr)
}
return
}
2022-07-26 11:56:43 -04:00
//sys ExtractAssociatedIcon(instance Handle, path *uint16, icon *uint16) (ret Handle, err error) = shell32.ExtractAssociatedIconW
2024-02-07 13:58:08 -05:00
//sys SHBrowseForFolder(bi *BROWSEINFO) (ret *ITEMIDLIST) = shell32.SHBrowseForFolder
2022-11-02 06:21:31 -04:00
//sys SHCreateItemFromParsingName(path *uint16, bc *IBindCtx, iid *GUID, item **IShellItem) (res error) = shell32.SHCreateItemFromParsingName
2022-07-11 19:09:50 -04:00
//sys ShellNotifyIcon(message uint32, data *NOTIFYICONDATA) (ok bool) = shell32.Shell_NotifyIconW
2024-02-07 13:58:08 -05:00
//sys SHGetPathFromIDListEx(ptr *ITEMIDLIST, path *uint16, pathLen int, opts int) (ok bool) = shell32.SHGetPathFromIDListEx