WIP: progress (windows).

This commit is contained in:
Nuno Cruces 2021-04-27 14:05:04 +01:00
parent 10c3d63ca5
commit de7c119f33
4 changed files with 215 additions and 37 deletions

View file

@ -5,9 +5,8 @@ import (
) )
func entry(text string, opts options) (out string, ok bool, err error) { func entry(text string, opts options) (out string, ok bool, err error) {
var title string if opts.title == nil {
if opts.title != nil { opts.title = stringPtr("")
title = *opts.title
} }
if opts.okLabel == nil { if opts.okLabel == nil {
opts.okLabel = stringPtr("OK") opts.okLabel = stringPtr("OK")
@ -15,10 +14,7 @@ func entry(text string, opts options) (out string, ok bool, err error) {
if opts.cancelLabel == nil { if opts.cancelLabel == nil {
opts.cancelLabel = stringPtr("Cancel") opts.cancelLabel = stringPtr("Cancel")
} }
return entryDlg(title, text, opts)
}
func entryDlg(title, text string, opts options) (out string, ok bool, err error) {
defer setup()() defer setup()()
font := getFont() font := getFont()
defer font.Delete() defer font.Delete()
@ -95,16 +91,16 @@ func entryDlg(title, text string, opts options) (out string, ok bool, err error)
defer unregisterClass.Call(cls, instance) defer unregisterClass.Call(cls, instance)
wnd, _, _ = createWindowEx.Call(0x10101, // WS_EX_CONTROLPARENT|WS_EX_WINDOWEDGE|WS_EX_DLGMODALFRAME wnd, _, _ = createWindowEx.Call(0x10101, // WS_EX_CONTROLPARENT|WS_EX_WINDOWEDGE|WS_EX_DLGMODALFRAME
cls, strptr(title), cls, strptr(*opts.title),
0x84c80000, // WS_POPUPWINDOW|WS_CLIPSIBLINGS|WS_DLGFRAME 0x84c80000, // WS_POPUPWINDOW|WS_CLIPSIBLINGS|WS_DLGFRAME
0x80000000, // CW_USEDEFAULT 0x80000000, // CW_USEDEFAULT
0x80000000, // CW_USEDEFAULT 0x80000000, // CW_USEDEFAULT
281, 141, 0, 0, instance) 281, 141, 0, 0, instance, 0)
textCtl, _, _ = createWindowEx.Call(0, textCtl, _, _ = createWindowEx.Call(0,
strptr("STATIC"), strptr(text), strptr("STATIC"), strptr(text),
0x5002e080, // WS_CHILD|WS_VISIBLE|WS_GROUP|SS_WORDELLIPSIS|SS_EDITCONTROL|SS_NOPREFIX 0x5002e080, // WS_CHILD|WS_VISIBLE|WS_GROUP|SS_WORDELLIPSIS|SS_EDITCONTROL|SS_NOPREFIX
12, 10, 241, 16, wnd, 0, instance) 12, 10, 241, 16, wnd, 0, instance, 0)
var flags uintptr = 0x50030080 // WS_CHILD|WS_VISIBLE|WS_GROUP|WS_TABSTOP|ES_AUTOHSCROLL var flags uintptr = 0x50030080 // WS_CHILD|WS_VISIBLE|WS_GROUP|WS_TABSTOP|ES_AUTOHSCROLL
if opts.hideText { if opts.hideText {
@ -113,21 +109,21 @@ func entryDlg(title, text string, opts options) (out string, ok bool, err error)
editCtl, _, _ = createWindowEx.Call(0x200, // WS_EX_CLIENTEDGE editCtl, _, _ = createWindowEx.Call(0x200, // WS_EX_CLIENTEDGE
strptr("EDIT"), strptr(opts.entryText), strptr("EDIT"), strptr(opts.entryText),
flags, flags,
12, 30, 241, 24, wnd, 0, instance) 12, 30, 241, 24, wnd, 0, instance, 0)
okBtn, _, _ = createWindowEx.Call(0, okBtn, _, _ = createWindowEx.Call(0,
strptr("BUTTON"), strptr(*opts.okLabel), strptr("BUTTON"), strptr(*opts.okLabel),
0x50030001, // WS_CHILD|WS_VISIBLE|WS_GROUP|WS_TABSTOP|BS_DEFPUSHBUTTON 0x50030001, // WS_CHILD|WS_VISIBLE|WS_GROUP|WS_TABSTOP|BS_DEFPUSHBUTTON
12, 66, 75, 24, wnd, 1 /* IDOK */, instance) 12, 66, 75, 24, wnd, 1 /* IDOK */, instance, 0)
cancelBtn, _, _ = createWindowEx.Call(0, cancelBtn, _, _ = createWindowEx.Call(0,
strptr("BUTTON"), strptr(*opts.cancelLabel), strptr("BUTTON"), strptr(*opts.cancelLabel),
0x50010000, // WS_CHILD|WS_VISIBLE|WS_GROUP|WS_TABSTOP 0x50010000, // WS_CHILD|WS_VISIBLE|WS_GROUP|WS_TABSTOP
12, 66, 75, 24, wnd, 2 /* IDCANCEL */, instance) 12, 66, 75, 24, wnd, 2 /* IDCANCEL */, instance, 0)
if opts.extraButton != nil { if opts.extraButton != nil {
extraBtn, _, _ = createWindowEx.Call(0, extraBtn, _, _ = createWindowEx.Call(0,
strptr("BUTTON"), strptr(*opts.extraButton), strptr("BUTTON"), strptr(*opts.extraButton),
0x50010000, // WS_CHILD|WS_VISIBLE|WS_GROUP|WS_TABSTOP 0x50010000, // WS_CHILD|WS_VISIBLE|WS_GROUP|WS_TABSTOP
12, 66, 75, 24, wnd, 7 /* IDNO */, instance) 12, 66, 75, 24, wnd, 7 /* IDNO */, instance, 0)
} }
layout(getDPI(wnd)) layout(getDPI(wnd))

View file

@ -6,17 +6,7 @@ import (
) )
func list(text string, items []string, opts options) (string, bool, error) { func list(text string, items []string, opts options) (string, bool, error) {
var title string items, err := listDlg(text, items, false, opts)
if opts.title != nil {
title = *opts.title
}
if opts.okLabel == nil {
opts.okLabel = stringPtr("OK")
}
if opts.cancelLabel == nil {
opts.cancelLabel = stringPtr("Cancel")
}
items, err := listDlg(title, text, items, false, opts)
if len(items) == 1 { if len(items) == 1 {
return items[0], true, err return items[0], true, err
} }
@ -24,9 +14,12 @@ func list(text string, items []string, opts options) (string, bool, error) {
} }
func listMultiple(text string, items []string, opts options) ([]string, error) { func listMultiple(text string, items []string, opts options) ([]string, error) {
var title string return listDlg(text, items, true, opts)
if opts.title != nil { }
title = *opts.title
func listDlg(text string, items []string, multiple bool, opts options) (out []string, err error) {
if opts.title == nil {
opts.title = stringPtr("")
} }
if opts.okLabel == nil { if opts.okLabel == nil {
opts.okLabel = stringPtr("OK") opts.okLabel = stringPtr("OK")
@ -34,10 +27,7 @@ func listMultiple(text string, items []string, opts options) ([]string, error) {
if opts.cancelLabel == nil { if opts.cancelLabel == nil {
opts.cancelLabel = stringPtr("Cancel") opts.cancelLabel = stringPtr("Cancel")
} }
return listDlg(title, text, items, true, opts)
}
func listDlg(title, text string, items []string, multiple bool, opts options) (out []string, err error) {
defer setup()() defer setup()()
font := getFont() font := getFont()
defer font.Delete() defer font.Delete()
@ -130,16 +120,16 @@ func listDlg(title, text string, items []string, multiple bool, opts options) (o
defer unregisterClass.Call(cls, instance) defer unregisterClass.Call(cls, instance)
wnd, _, _ = createWindowEx.Call(0x10101, // WS_EX_CONTROLPARENT|WS_EX_WINDOWEDGE|WS_EX_DLGMODALFRAME wnd, _, _ = createWindowEx.Call(0x10101, // WS_EX_CONTROLPARENT|WS_EX_WINDOWEDGE|WS_EX_DLGMODALFRAME
cls, strptr(title), cls, strptr(*opts.title),
0x84c80000, // WS_POPUPWINDOW|WS_CLIPSIBLINGS|WS_DLGFRAME 0x84c80000, // WS_POPUPWINDOW|WS_CLIPSIBLINGS|WS_DLGFRAME
0x80000000, // CW_USEDEFAULT 0x80000000, // CW_USEDEFAULT
0x80000000, // CW_USEDEFAULT 0x80000000, // CW_USEDEFAULT
281, 281, 0, 0, instance) 281, 281, 0, 0, instance, 0)
textCtl, _, _ = createWindowEx.Call(0, textCtl, _, _ = createWindowEx.Call(0,
strptr("STATIC"), strptr(text), strptr("STATIC"), strptr(text),
0x5002e080, // WS_CHILD|WS_VISIBLE|WS_GROUP|SS_WORDELLIPSIS|SS_EDITCONTROL|SS_NOPREFIX 0x5002e080, // WS_CHILD|WS_VISIBLE|WS_GROUP|SS_WORDELLIPSIS|SS_EDITCONTROL|SS_NOPREFIX
12, 10, 241, 16, wnd, 0, instance) 12, 10, 241, 16, wnd, 0, instance, 0)
var flags uintptr = 0x50320000 // WS_CHILD|WS_VISIBLE|WS_VSCROLL|WS_GROUP|WS_TABSTOP var flags uintptr = 0x50320000 // WS_CHILD|WS_VISIBLE|WS_VSCROLL|WS_GROUP|WS_TABSTOP
if multiple { if multiple {
@ -148,21 +138,21 @@ func listDlg(title, text string, items []string, multiple bool, opts options) (o
listCtl, _, _ = createWindowEx.Call(0x200, // WS_EX_CLIENTEDGE listCtl, _, _ = createWindowEx.Call(0x200, // WS_EX_CLIENTEDGE
strptr("LISTBOX"), strptr(opts.entryText), strptr("LISTBOX"), strptr(opts.entryText),
flags, flags,
12, 30, 241, 164, wnd, 0, instance) 12, 30, 241, 164, wnd, 0, instance, 0)
okBtn, _, _ = createWindowEx.Call(0, okBtn, _, _ = createWindowEx.Call(0,
strptr("BUTTON"), strptr(*opts.okLabel), strptr("BUTTON"), strptr(*opts.okLabel),
0x50030001, // WS_CHILD|WS_VISIBLE|WS_GROUP|WS_TABSTOP|BS_DEFPUSHBUTTON 0x50030001, // WS_CHILD|WS_VISIBLE|WS_GROUP|WS_TABSTOP|BS_DEFPUSHBUTTON
12, 206, 75, 24, wnd, 1 /* IDOK */, instance) 12, 206, 75, 24, wnd, 1 /* IDOK */, instance, 0)
cancelBtn, _, _ = createWindowEx.Call(0, cancelBtn, _, _ = createWindowEx.Call(0,
strptr("BUTTON"), strptr(*opts.cancelLabel), strptr("BUTTON"), strptr(*opts.cancelLabel),
0x50010000, // WS_CHILD|WS_VISIBLE|WS_GROUP|WS_TABSTOP 0x50010000, // WS_CHILD|WS_VISIBLE|WS_GROUP|WS_TABSTOP
12, 206, 75, 24, wnd, 2 /* IDCANCEL */, instance) 12, 206, 75, 24, wnd, 2 /* IDCANCEL */, instance, 0)
if opts.extraButton != nil { if opts.extraButton != nil {
extraBtn, _, _ = createWindowEx.Call(0, extraBtn, _, _ = createWindowEx.Call(0,
strptr("BUTTON"), strptr(*opts.extraButton), strptr("BUTTON"), strptr(*opts.extraButton),
0x50010000, // WS_CHILD|WS_VISIBLE|WS_GROUP|WS_TABSTOP 0x50010000, // WS_CHILD|WS_VISIBLE|WS_GROUP|WS_TABSTOP
12, 206, 75, 24, wnd, 7 /* IDNO */, instance) 12, 206, 75, 24, wnd, 7 /* IDNO */, instance, 0)
} }
for _, item := range items { for _, item := range items {

160
progress_windows.go Normal file
View file

@ -0,0 +1,160 @@
package zenity
import (
"syscall"
)
func progress(opts options) (ProgressDialog, error) {
if opts.title == nil {
opts.title = stringPtr("")
}
if opts.okLabel == nil {
opts.okLabel = stringPtr("OK")
}
if opts.cancelLabel == nil {
opts.cancelLabel = stringPtr("Cancel")
}
if opts.maxValue == 0 {
opts.maxValue = 100
}
defer setup()()
font := getFont()
defer font.Delete()
defWindowProc := defWindowProc.Addr()
var wnd, textCtl, progCtl uintptr
var okBtn, cancelBtn, extraBtn uintptr
layout := func(dpi dpi) {
hfont := font.ForDPI(dpi)
sendMessage.Call(textCtl, 0x0030 /* WM_SETFONT */, hfont, 1)
sendMessage.Call(okBtn, 0x0030 /* WM_SETFONT */, hfont, 1)
sendMessage.Call(cancelBtn, 0x0030 /* WM_SETFONT */, hfont, 1)
setWindowPos.Call(wnd, 0, 0, 0, dpi.Scale(281), dpi.Scale(141), 0x6) // SWP_NOZORDER|SWP_NOMOVE
setWindowPos.Call(textCtl, 0, dpi.Scale(12), dpi.Scale(10), dpi.Scale(241), dpi.Scale(16), 0x4) // SWP_NOZORDER
setWindowPos.Call(progCtl, 0, dpi.Scale(12), dpi.Scale(30), dpi.Scale(241), dpi.Scale(24), 0x4) // SWP_NOZORDER
if extraBtn == 0 {
setWindowPos.Call(okBtn, 0, dpi.Scale(95), dpi.Scale(66), dpi.Scale(75), dpi.Scale(24), 0x4) // SWP_NOZORDER
setWindowPos.Call(cancelBtn, 0, dpi.Scale(178), dpi.Scale(66), dpi.Scale(75), dpi.Scale(24), 0x4) // SWP_NOZORDER
} else {
sendMessage.Call(extraBtn, 0x0030 /* WM_SETFONT */, hfont, 1)
setWindowPos.Call(okBtn, 0, dpi.Scale(12), dpi.Scale(66), dpi.Scale(75), dpi.Scale(24), 0x4) // SWP_NOZORDER
setWindowPos.Call(extraBtn, 0, dpi.Scale(95), dpi.Scale(66), dpi.Scale(75), dpi.Scale(24), 0x4) // SWP_NOZORDER
setWindowPos.Call(cancelBtn, 0, dpi.Scale(178), dpi.Scale(66), dpi.Scale(75), dpi.Scale(24), 0x4) // SWP_NOZORDER
}
}
proc := func(wnd uintptr, msg uint32, wparam, lparam uintptr) uintptr {
switch msg {
case 0x0002: // WM_DESTROY
postQuitMessage.Call(0)
case 0x0010: // WM_CLOSE
destroyWindow.Call(wnd)
case 0x0111: // WM_COMMAND
switch wparam {
default:
return 1
case 1, 6: // IDOK, IDYES
case 2: // IDCANCEL
case 7: // IDNO
}
destroyWindow.Call(wnd)
case 0x02e0: // WM_DPICHANGED
layout(dpi(uint32(wparam) >> 16))
default:
ret, _, _ := syscall.Syscall6(defWindowProc, 4, wnd, uintptr(msg), wparam, lparam, 0, 0)
return ret
}
return 0
}
if opts.ctx != nil && opts.ctx.Err() != nil {
return nil, opts.ctx.Err()
}
instance, _, err := getModuleHandle.Call(0)
if instance == 0 {
return nil, err
}
cls, err := registerClass(instance, syscall.NewCallback(proc))
if cls == 0 {
return nil, err
}
defer unregisterClass.Call(cls, instance)
wnd, _, _ = createWindowEx.Call(0x10101, // WS_EX_CONTROLPARENT|WS_EX_WINDOWEDGE|WS_EX_DLGMODALFRAME
cls, strptr(*opts.title),
0x84c80000, // WS_POPUPWINDOW|WS_CLIPSIBLINGS|WS_DLGFRAME
0x80000000, // CW_USEDEFAULT
0x80000000, // CW_USEDEFAULT
281, 141, 0, 0, instance, 0)
textCtl, _, _ = createWindowEx.Call(0,
strptr("STATIC"), 0,
0x5002e080, // WS_CHILD|WS_VISIBLE|WS_GROUP|SS_WORDELLIPSIS|SS_EDITCONTROL|SS_NOPREFIX
12, 10, 241, 16, wnd, 0, instance, 0)
var flags uintptr = 0x50000001 // WS_CHILD|WS_VISIBLE|PBS_SMOOTH
if opts.maxValue < 0 {
flags |= 0x8 // PBS_MARQUEE
}
progCtl, _, _ = createWindowEx.Call(0,
strptr("msctls_progress32"), // PROGRESS_CLASS
0, flags,
12, 30, 241, 24, wnd, 0, instance, 0)
okBtn, _, _ = createWindowEx.Call(0,
strptr("BUTTON"), strptr(*opts.okLabel),
0x50030001, // WS_CHILD|WS_VISIBLE|WS_GROUP|WS_TABSTOP|BS_DEFPUSHBUTTON
12, 66, 75, 24, wnd, 1 /* IDOK */, instance, 0)
cancelBtn, _, _ = createWindowEx.Call(0,
strptr("BUTTON"), strptr(*opts.cancelLabel),
0x50010000, // WS_CHILD|WS_VISIBLE|WS_GROUP|WS_TABSTOP
12, 66, 75, 24, wnd, 2 /* IDCANCEL */, instance, 0)
if opts.extraButton != nil {
extraBtn, _, _ = createWindowEx.Call(0,
strptr("BUTTON"), strptr(*opts.extraButton),
0x50010000, // WS_CHILD|WS_VISIBLE|WS_GROUP|WS_TABSTOP
12, 66, 75, 24, wnd, 7 /* IDNO */, instance, 0)
}
layout(getDPI(wnd))
centerWindow(wnd)
showWindow.Call(wnd, 1 /* SW_SHOWNORMAL */, 0)
if opts.maxValue < 0 {
sendMessage.Call(progCtl, 0x410 /* PBM_SETMARQUEE */, 1, 0)
} else {
sendMessage.Call(progCtl, 0x402 /* PBM_SETPOS */, 33, 0)
sendMessage.Call(progCtl, 0x406 /* PBM_SETRANGE32 */, 0, uintptr(opts.maxValue))
}
if opts.ctx != nil {
wait := make(chan struct{})
defer close(wait)
go func() {
select {
case <-opts.ctx.Done():
sendMessage.Call(wnd, 0x0112 /* WM_SYSCOMMAND */, 0xf060 /* SC_CLOSE */, 0)
case <-wait:
}
}()
}
// set default values
// out, ok, err = "", false, nil
if err := messageLoop(wnd); err != nil {
return nil, err
}
if opts.ctx != nil && opts.ctx.Err() != nil {
return nil, opts.ctx.Err()
}
return nil, err
}

View file

@ -13,6 +13,7 @@ import (
) )
var ( var (
comctl32 = syscall.NewLazyDLL("comctl32.dll")
comdlg32 = syscall.NewLazyDLL("comdlg32.dll") comdlg32 = syscall.NewLazyDLL("comdlg32.dll")
gdi32 = syscall.NewLazyDLL("gdi32.dll") gdi32 = syscall.NewLazyDLL("gdi32.dll")
kernel32 = syscall.NewLazyDLL("kernel32.dll") kernel32 = syscall.NewLazyDLL("kernel32.dll")
@ -21,6 +22,7 @@ var (
user32 = syscall.NewLazyDLL("user32.dll") user32 = syscall.NewLazyDLL("user32.dll")
wtsapi32 = syscall.NewLazyDLL("wtsapi32.dll") wtsapi32 = syscall.NewLazyDLL("wtsapi32.dll")
initCommonControlsEx = comctl32.NewProc("InitCommonControlsEx")
commDlgExtendedError = comdlg32.NewProc("CommDlgExtendedError") commDlgExtendedError = comdlg32.NewProc("CommDlgExtendedError")
deleteObject = gdi32.NewProc("DeleteObject") deleteObject = gdi32.NewProc("DeleteObject")
@ -111,6 +113,10 @@ func setup() context.CancelFunc {
} }
} }
var icc _INITCOMMONCONTROLSEX
icc.Size = uint32(unsafe.Sizeof(icc))
icc.ICC = 0x00004020 // ICC_STANDARD_CLASSES|ICC_PROGRESS_CLASS
return func() { return func() {
if old != 0 { if old != 0 {
setThreadDpiAwarenessContext.Call(old) setThreadDpiAwarenessContext.Call(old)
@ -284,6 +290,26 @@ func registerClass(instance, proc uintptr) (uintptr, error) {
return ret, err return ret, err
} }
// https://stackoverflow.com/questions/4308503/how-to-enable-visual-styles-without-a-manifest
// ULONG_PTR EnableVisualStyles(VOID)
// {
// TCHAR dir[MAX_PATH];
// ULONG_PTR ulpActivationCookie = FALSE;
// ACTCTX actCtx =
// {
// sizeof(actCtx),
// ACTCTX_FLAG_RESOURCE_NAME_VALID
// | ACTCTX_FLAG_SET_PROCESS_DEFAULT
// | ACTCTX_FLAG_ASSEMBLY_DIRECTORY_VALID,
// TEXT("shell32.dll"), 0, 0, dir, (LPCTSTR)124
// };
// UINT cch = GetSystemDirectory(dir, sizeof(dir) / sizeof(*dir));
// if (cch >= sizeof(dir) / sizeof(*dir)) { return FALSE; /*shouldn't happen*/ }
// dir[cch] = TEXT('\0');
// ActivateActCtx(CreateActCtx(&actCtx), &ulpActivationCookie);
// return ulpActivationCookie;
// }
// https://docs.microsoft.com/en-us/windows/win32/winmsg/using-messages-and-message-queues // https://docs.microsoft.com/en-us/windows/win32/winmsg/using-messages-and-message-queues
func messageLoop(wnd uintptr) error { func messageLoop(wnd uintptr) error {
getMessage := getMessage.Addr() getMessage := getMessage.Addr()
@ -394,6 +420,12 @@ type _WNDCLASSEX struct {
IconSm uintptr IconSm uintptr
} }
// https://docs.microsoft.com/en-us/windows/win32/api/commctrl/ns-commctrl-initcommoncontrolsex
type _INITCOMMONCONTROLSEX struct {
Size uint32
ICC uint32
}
// https://github.com/wine-mirror/wine/blob/master/include/unknwn.idl // https://github.com/wine-mirror/wine/blob/master/include/unknwn.idl
type _IUnknownVtbl struct { type _IUnknownVtbl struct {