zenity/color_windows.go

91 lines
1.8 KiB
Go
Raw Normal View History

2020-01-18 07:40:16 -05:00
package zenity
import (
"image/color"
"sync"
"unsafe"
)
var (
chooseColor = comdlg32.NewProc("ChooseColorW")
2021-04-27 20:27:28 -04:00
savedColors [16]uint32
2020-01-18 07:40:16 -05:00
colorsMutex sync.Mutex
)
func init() {
for i := range savedColors {
savedColors[i] = 0xffffff
}
}
2022-03-28 22:05:46 -04:00
const (
_CC_RGBINIT = 0x00000001
_CC_FULLOPEN = 0x00000002
_CC_PREVENTFULLOPEN = 0x00000004
)
2021-03-04 07:42:30 -05:00
func selectColor(opts options) (color.Color, error) {
2020-01-18 07:40:16 -05:00
// load custom colors
colorsMutex.Lock()
customColors := savedColors
colorsMutex.Unlock()
2021-03-26 09:26:56 -04:00
var args _CHOOSECOLOR
2020-01-18 07:40:16 -05:00
args.StructSize = uint32(unsafe.Sizeof(args))
2022-06-08 15:33:10 -04:00
args.Owner, _ = opts.attach.(uintptr)
2020-01-18 07:40:16 -05:00
args.CustColors = &customColors
if opts.color != nil {
2022-03-28 22:05:46 -04:00
args.Flags |= _CC_RGBINIT
2020-01-21 11:13:45 -05:00
n := color.NRGBAModel.Convert(opts.color).(color.NRGBA)
2021-04-25 19:36:15 -04:00
args.RgbResult = uint32(n.R) | uint32(n.G)<<8 | uint32(n.B)<<16
2020-01-18 07:40:16 -05:00
}
2020-01-24 07:52:45 -05:00
if opts.showPalette {
2022-03-28 22:05:46 -04:00
args.Flags |= _CC_PREVENTFULLOPEN
2020-01-18 07:40:16 -05:00
} else {
2022-03-28 22:05:46 -04:00
args.Flags |= _CC_FULLOPEN
2020-01-18 07:40:16 -05:00
}
2021-03-26 08:40:24 -04:00
defer setup()()
2020-01-18 07:40:16 -05:00
2021-03-03 22:25:45 -05:00
if opts.ctx != nil || opts.title != nil {
2022-05-19 18:39:21 -04:00
unhook, err := hookDialog(opts.ctx, opts.windowIcon, opts.title, nil)
2020-01-30 09:14:42 -05:00
if err != nil {
2020-01-24 08:59:03 -05:00
return nil, err
}
2020-01-30 09:14:42 -05:00
defer unhook()
2020-01-24 08:59:03 -05:00
}
2020-01-29 09:15:21 -05:00
s, _, _ := chooseColor.Call(uintptr(unsafe.Pointer(&args)))
2020-01-30 09:14:42 -05:00
if opts.ctx != nil && opts.ctx.Err() != nil {
return nil, opts.ctx.Err()
}
2020-01-29 09:15:21 -05:00
if s == 0 {
2020-01-27 08:44:47 -05:00
return nil, commDlgError()
}
2020-01-18 07:40:16 -05:00
// save custom colors back
colorsMutex.Lock()
savedColors = customColors
colorsMutex.Unlock()
r := uint8(args.RgbResult >> 0)
g := uint8(args.RgbResult >> 8)
b := uint8(args.RgbResult >> 16)
return color.RGBA{R: r, G: g, B: b, A: 255}, nil
}
2021-03-10 09:49:09 -05:00
// https://docs.microsoft.com/en-us/windows/win32/api/commdlg/ns-commdlg-choosecolorw-r1
2021-03-26 09:26:56 -04:00
type _CHOOSECOLOR struct {
2020-01-18 07:40:16 -05:00
StructSize uint32
Owner uintptr
Instance uintptr
RgbResult uint32
CustColors *[16]uint32
Flags uint32
CustData uintptr
FnHook uintptr
TemplateName *uint16
}