zenity/msg_windows.go

118 lines
3 KiB
Go
Raw Permalink Normal View History

2020-01-04 22:21:39 -05:00
package zenity
import (
"runtime"
2020-01-04 22:21:39 -05:00
"syscall"
"unsafe"
)
var (
messageBox = user32.NewProc("MessageBoxW")
)
2020-01-23 06:44:28 -05:00
func message(kind messageKind, text string, options []Option) (bool, error) {
2020-01-24 07:52:45 -05:00
opts := applyOptions(options)
2020-01-04 22:21:39 -05:00
var flags, caption uintptr
switch {
2020-01-24 07:52:45 -05:00
case kind == questionKind && opts.extraButton != "":
2020-01-04 22:21:39 -05:00
flags |= 0x3 // MB_YESNOCANCEL
2020-01-24 07:52:45 -05:00
case kind == questionKind || opts.extraButton != "":
2020-01-04 22:21:39 -05:00
flags |= 0x1 // MB_OKCANCEL
}
switch opts.icon {
case ErrorIcon:
flags |= 0x10 // MB_ICONERROR
case QuestionIcon:
flags |= 0x20 // MB_ICONQUESTION
case WarningIcon:
flags |= 0x30 // MB_ICONWARNING
case InfoIcon:
flags |= 0x40 // MB_ICONINFORMATION
}
2020-01-24 07:52:45 -05:00
if kind == questionKind && opts.defaultCancel {
if opts.extraButton == "" {
flags |= 0x100 // MB_DEFBUTTON2
} else {
flags |= 0x200 // MB_DEFBUTTON3
}
}
2020-01-04 22:21:39 -05:00
if opts.title != "" {
caption = uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(opts.title)))
}
2020-01-24 07:52:45 -05:00
if opts.okLabel != "" || opts.cancelLabel != "" || opts.extraButton != "" {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
2020-01-23 06:44:28 -05:00
hook, err := hookMessageLabels(kind, opts)
if hook == 0 {
return false, err
}
defer unhookWindowsHookEx.Call(hook)
}
2020-01-04 22:21:39 -05:00
n, _, err := messageBox.Call(0,
uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(text))),
caption, flags)
if n == 0 {
return false, err
}
2020-01-23 06:44:28 -05:00
if n == 7 || n == 2 && kind != questionKind { // IDNO
return false, ErrExtraButton
}
if n == 1 || n == 6 { // IDOK, IDYES
return true, nil
}
return false, nil
}
2020-01-23 06:44:28 -05:00
func hookMessageLabels(kind messageKind, opts options) (hook uintptr, err error) {
tid, _, _ := getCurrentThreadId.Call()
hook, _, err = setWindowsHookEx.Call(12, // WH_CALLWNDPROCRET
2020-01-08 19:54:34 -05:00
syscall.NewCallback(func(code int, wparam uintptr, lparam *_CWPRETSTRUCT) uintptr {
if lparam.Message == 0x0110 { // WM_INITDIALOG
2020-01-17 07:28:44 -05:00
name := [7]uint16{}
getClassName.Call(lparam.Wnd, uintptr(unsafe.Pointer(&name)), uintptr(len(name)))
if syscall.UTF16ToString(name[:]) == "#32770" { // The class for a dialog box
enumChildWindows.Call(lparam.Wnd,
syscall.NewCallback(func(wnd, lparam uintptr) uintptr {
name := [7]uint16{}
getClassName.Call(wnd, uintptr(unsafe.Pointer(&name)), uintptr(len(name)))
if syscall.UTF16ToString(name[:]) == "Button" {
ctl, _, _ := getDlgCtrlID.Call(wnd)
var text string
switch ctl {
case 1, 6: // IDOK, IDYES
2020-01-24 07:52:45 -05:00
text = opts.okLabel
case 2: // IDCANCEL
2020-01-23 06:44:28 -05:00
if kind == questionKind {
2020-01-24 07:52:45 -05:00
text = opts.cancelLabel
} else if opts.extraButton != "" {
text = opts.extraButton
} else {
2020-01-24 07:52:45 -05:00
text = opts.okLabel
}
case 7: // IDNO
2020-01-24 07:52:45 -05:00
text = opts.extraButton
}
if text != "" {
ptr := syscall.StringToUTF16Ptr(text)
2020-01-17 07:28:44 -05:00
setWindowText.Call(wnd, uintptr(unsafe.Pointer(ptr)))
}
}
return 1
}), 0)
}
}
2020-01-08 19:54:34 -05:00
next, _, _ := callNextHookEx.Call(hook, uintptr(code), wparam, uintptr(unsafe.Pointer(lparam)))
return next
}), 0, tid)
return
2020-01-04 22:21:39 -05:00
}