List DisallowEmpty (windows).

This commit is contained in:
Nuno Cruces 2022-12-05 13:24:58 +00:00
parent 589e1b5b01
commit 5533b77ca0
4 changed files with 33 additions and 14 deletions

View file

@ -70,10 +70,10 @@ var (
username bool username bool
// List options // List options
columns int columns int
checklist bool checklist bool
radiolist bool radiolist bool
allowEmpty bool disallowEmpty bool
// Calendar options // Calendar options
year uint year uint
@ -242,7 +242,7 @@ func parseFlags() []string {
fset.Bool("hide-header", true, "Hide the column headers") fset.Bool("hide-header", true, "Hide the column headers")
fset.BoolVar(&checklist, "checklist", false, "Use check boxes for the first column (Unix only)") fset.BoolVar(&checklist, "checklist", false, "Use check boxes for the first column (Unix only)")
fset.BoolVar(&radiolist, "radiolist", false, "Use radio buttons for the first column (Unix only)") fset.BoolVar(&radiolist, "radiolist", false, "Use radio buttons for the first column (Unix only)")
fset.BoolVar(&allowEmpty, "allow-empty", true, "Allow empty selection (macOS only)") fset.BoolVar(&disallowEmpty, "disallow-empty", false, "Disallow empty selection (Windows and macOS only)")
// Calendar options // Calendar options
fset.UintVar(&year, "year", 0, "Set the calendar `year`") fset.UintVar(&year, "year", 0, "Set the calendar `year`")
@ -538,7 +538,7 @@ func loadFlags() []zenity.Option {
if radiolist { if radiolist {
opts = append(opts, zenity.RadioList()) opts = append(opts, zenity.RadioList())
} }
if !allowEmpty { if disallowEmpty {
opts = append(opts, zenity.DisallowEmpty()) opts = append(opts, zenity.DisallowEmpty())
} }

View file

@ -142,6 +142,7 @@ const (
ES_AUTOHSCROLL = 0x0080 ES_AUTOHSCROLL = 0x0080
// List box control styles // List box control styles
LBS_NOTIFY = 0x0001
LBS_EXTENDEDSEL = 0x0800 LBS_EXTENDEDSEL = 0x0800
// Month calendar control styles // Month calendar control styles

View file

@ -57,7 +57,7 @@ func DefaultItems(items ...string) Option {
return funcOption(func(o *options) { o.defaultItems = items }) return funcOption(func(o *options) { o.defaultItems = items })
} }
// DisallowEmpty returns an Option to not allow zero items to be selected (macOS only). // DisallowEmpty returns an Option to not allow zero items to be selected (Windows and macOS only).
func DisallowEmpty() Option { func DisallowEmpty() Option {
return funcOption(func(o *options) { o.disallowEmpty = true }) return funcOption(func(o *options) { o.disallowEmpty = true })
} }

View file

@ -31,17 +31,19 @@ func listDlg(text string, items []string, multiple bool, opts options) ([]string
} }
dlg := &listDialog{ dlg := &listDialog{
items: items, items: items,
multiple: multiple, multiple: multiple,
disallowEmpty: opts.disallowEmpty,
} }
return dlg.setup(text, opts) return dlg.setup(text, opts)
} }
type listDialog struct { type listDialog struct {
items []string items []string
multiple bool multiple bool
out []string disallowEmpty bool
err error out []string
err error
wnd win.HWND wnd win.HWND
textCtl win.HWND textCtl win.HWND
@ -84,7 +86,7 @@ func (dlg *listDialog) setup(text string, opts options) ([]string, error) {
strptr("STATIC"), strptr(text), _WS_ZEN_LABEL, strptr("STATIC"), strptr(text), _WS_ZEN_LABEL,
12, 10, 241, 16, dlg.wnd, 0, instance, nil) 12, 10, 241, 16, dlg.wnd, 0, instance, nil)
var flags uint32 = _WS_ZEN_CONTROL | win.WS_VSCROLL var flags uint32 = _WS_ZEN_CONTROL | win.WS_VSCROLL | win.LBS_NOTIFY
if dlg.multiple { if dlg.multiple {
flags |= win.LBS_EXTENDEDSEL flags |= win.LBS_EXTENDEDSEL
} }
@ -120,6 +122,7 @@ func (dlg *listDialog) setup(text string, opts options) ([]string, error) {
} }
} }
dlg.update()
dlg.layout(getDPI(dlg.wnd)) dlg.layout(getDPI(dlg.wnd))
centerWindow(dlg.wnd) centerWindow(dlg.wnd)
win.SetFocus(dlg.listCtl) win.SetFocus(dlg.listCtl)
@ -166,6 +169,20 @@ func (dlg *listDialog) layout(dpi dpi) {
} }
} }
func (dlg *listDialog) update() {
if dlg.disallowEmpty {
var enable bool
if dlg.multiple {
len := win.SendMessage(dlg.listCtl, win.LB_GETSELCOUNT, 0, 0)
enable = int32(len) > 0
} else {
idx := win.SendMessage(dlg.listCtl, win.LB_GETCURSEL, 0, 0)
enable = int32(idx) >= 0
}
win.EnableWindow(dlg.okBtn, enable)
}
}
func listProc(wnd win.HWND, msg uint32, wparam uintptr, lparam *unsafe.Pointer) uintptr { func listProc(wnd win.HWND, msg uint32, wparam uintptr, lparam *unsafe.Pointer) uintptr {
var dlg *listDialog var dlg *listDialog
switch msg { switch msg {
@ -189,6 +206,7 @@ func listProc(wnd win.HWND, msg uint32, wparam uintptr, lparam *unsafe.Pointer)
case win.WM_COMMAND: case win.WM_COMMAND:
switch wparam { switch wparam {
default: default:
dlg.update()
return 1 return 1
case win.IDOK, win.IDYES: case win.IDOK, win.IDYES:
if dlg.multiple { if dlg.multiple {