zenity/msg_windows.go

126 lines
2.7 KiB
Go
Raw Permalink Normal View History

2020-01-04 22:21:39 -05:00
package zenity
import (
2020-01-30 09:14:42 -05:00
"context"
2022-06-18 07:37:39 -04:00
2024-07-10 00:06:01 -04:00
"git.bigun.dev/evan/zenity/internal/win"
2024-07-09 23:47:11 -04:00
"golang.org/x/sys/windows"
2020-01-04 22:21:39 -05:00
)
2021-04-29 11:05:28 -04:00
func message(kind messageKind, text string, opts options) error {
2024-07-04 19:20:01 -04:00
var flags uint32 = win.MB_SETFOREGROUND
2020-01-04 22:21:39 -05:00
switch {
2021-03-03 22:25:45 -05:00
case kind == questionKind && opts.extraButton != nil:
2022-06-18 07:37:39 -04:00
flags |= win.MB_YESNOCANCEL
2021-03-03 22:32:55 -05:00
case kind == questionKind:
2022-06-18 07:37:39 -04:00
flags |= win.MB_OKCANCEL
2021-03-03 22:32:55 -05:00
case opts.extraButton != nil:
2022-06-18 07:37:39 -04:00
flags |= win.MB_YESNO
2022-06-18 11:12:03 -04:00
default:
opts.cancelLabel = opts.okLabel
2020-01-04 22:21:39 -05:00
}
switch opts.icon {
case ErrorIcon:
2022-06-18 07:37:39 -04:00
flags |= win.MB_ICONERROR
2020-01-04 22:21:39 -05:00
case QuestionIcon:
2022-06-18 07:37:39 -04:00
flags |= win.MB_ICONQUESTION
2020-01-04 22:21:39 -05:00
case WarningIcon:
2022-06-18 07:37:39 -04:00
flags |= win.MB_ICONWARNING
2020-01-04 22:21:39 -05:00
case InfoIcon:
2022-06-18 07:37:39 -04:00
flags |= win.MB_ICONINFORMATION
2022-05-18 09:48:09 -04:00
case NoIcon:
//
default:
2022-03-25 22:56:06 -04:00
switch kind {
case errorKind:
2022-06-18 07:37:39 -04:00
flags |= win.MB_ICONERROR
2022-03-25 22:56:06 -04:00
case questionKind:
2022-06-18 07:37:39 -04:00
flags |= win.MB_ICONQUESTION
2022-03-25 22:56:06 -04:00
case warningKind:
2022-06-18 07:37:39 -04:00
flags |= win.MB_ICONWARNING
2022-03-25 22:56:06 -04:00
case infoKind:
2022-06-18 07:37:39 -04:00
flags |= win.MB_ICONINFORMATION
2022-03-25 22:56:06 -04:00
}
2020-01-04 22:21:39 -05:00
}
2020-01-24 07:52:45 -05:00
if kind == questionKind && opts.defaultCancel {
2021-03-03 22:25:45 -05:00
if opts.extraButton == nil {
2022-06-18 07:37:39 -04:00
flags |= win.MB_DEFBUTTON2
} else {
2022-06-18 07:37:39 -04:00
flags |= win.MB_DEFBUTTON3
}
}
2024-07-09 23:47:11 -04:00
if opts.alwaysOnTop {
flags |= windows.MB_SYSTEMMODAL
}
2022-06-28 09:52:02 -04:00
owner, _ := opts.attach.(win.HWND)
defer setup(owner)()
2022-06-23 09:49:08 -04:00
unhook, err := hookMessageDialog(opts)
if err != nil {
return err
}
2022-06-23 09:49:08 -04:00
defer unhook()
2021-09-15 08:54:35 -04:00
var title *uint16
2021-03-03 22:25:45 -05:00
if opts.title != nil {
2022-06-20 11:05:11 -04:00
title = strptr(*opts.title)
2021-03-03 22:25:45 -05:00
}
2022-06-20 11:05:11 -04:00
s, err := win.MessageBox(owner, strptr(text), title, flags)
2020-01-04 22:21:39 -05:00
2020-01-30 09:14:42 -05:00
if opts.ctx != nil && opts.ctx.Err() != nil {
2021-04-29 11:05:28 -04:00
return opts.ctx.Err()
2020-01-30 09:14:42 -05:00
}
2021-03-03 22:32:55 -05:00
switch s {
2022-06-18 07:37:39 -04:00
case win.IDOK, win.IDYES:
2021-04-29 11:05:28 -04:00
return nil
2022-06-18 07:37:39 -04:00
case win.IDCANCEL:
2021-04-29 11:05:28 -04:00
return ErrCanceled
2022-06-18 07:37:39 -04:00
case win.IDNO:
2021-04-29 11:05:28 -04:00
return ErrExtraButton
2021-03-03 22:32:55 -05:00
default:
2021-04-29 11:05:28 -04:00
return err
}
}
2022-07-26 09:49:49 -04:00
func hookMessageDialog(opts options) (context.CancelFunc, error) {
2022-06-23 09:49:08 -04:00
var init func(wnd win.HWND)
2022-07-26 09:49:49 -04:00
var icon icon
2022-06-23 09:49:08 -04:00
if opts.okLabel != nil || opts.cancelLabel != nil || opts.extraButton != nil || opts.icon != nil {
init = func(wnd win.HWND) {
if opts.okLabel != nil {
2022-12-07 09:35:37 -05:00
win.SetDlgItemText(wnd, win.IDOK, strptr(quoteAccelerators(*opts.okLabel)))
win.SetDlgItemText(wnd, win.IDYES, strptr(quoteAccelerators(*opts.okLabel)))
2022-06-23 09:49:08 -04:00
}
if opts.cancelLabel != nil {
2022-12-07 09:35:37 -05:00
win.SetDlgItemText(wnd, win.IDCANCEL, strptr(quoteAccelerators(*opts.cancelLabel)))
2022-06-23 09:49:08 -04:00
}
if opts.extraButton != nil {
2022-12-07 09:35:37 -05:00
win.SetDlgItemText(wnd, win.IDNO, strptr(quoteAccelerators(*opts.extraButton)))
2022-06-23 09:49:08 -04:00
}
2022-05-09 10:49:32 -04:00
2022-06-23 09:49:08 -04:00
if icon.handle != 0 {
ctl, _ := win.GetDlgItem(wnd, win.IDC_STATIC_OK)
win.SendMessage(ctl, win.STM_SETICON, uintptr(icon.handle), 0)
}
}
}
2022-07-26 11:56:43 -04:00
icon, err := getIcon(opts.icon)
if err != nil {
return nil, err
}
2022-07-26 09:49:49 -04:00
unhook, err := hookDialog(opts.ctx, opts.windowIcon, nil, init)
2022-07-26 11:56:43 -04:00
if err != nil {
icon.delete()
return nil, err
2022-07-26 09:49:49 -04:00
}
return func() {
icon.delete()
unhook()
}, nil
}