zenity/color_windows.go

88 lines
1.7 KiB
Go
Raw Normal View History

2020-01-18 07:40:16 -05:00
package zenity
import (
"image/color"
"runtime"
"sync"
"unsafe"
)
var (
chooseColor = comdlg32.NewProc("ChooseColorW")
savedColors = [16]uint32{}
colorsMutex sync.Mutex
)
func init() {
for i := range savedColors {
savedColors[i] = 0xffffff
}
}
2020-01-26 11:04:49 -05:00
func selectColor(options []Option) (color.Color, error) {
2020-01-24 07:52:45 -05:00
opts := applyOptions(options)
2020-01-18 07:40:16 -05:00
// load custom colors
colorsMutex.Lock()
customColors := savedColors
colorsMutex.Unlock()
var args _CHOOSECOLORW
args.StructSize = uint32(unsafe.Sizeof(args))
args.CustColors = &customColors
if opts.color != nil {
args.Flags |= 0x1 // CC_RGBINIT
2020-01-21 11:13:45 -05:00
n := color.NRGBAModel.Convert(opts.color).(color.NRGBA)
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 {
2020-01-27 08:44:47 -05:00
args.Flags |= 0x4 // CC_PREVENTFULLOPEN
2020-01-18 07:40:16 -05:00
} else {
2020-01-27 08:44:47 -05:00
args.Flags |= 0x2 // CC_FULLOPEN
2020-01-18 07:40:16 -05:00
}
runtime.LockOSThread()
defer runtime.UnlockOSThread()
2020-01-30 09:14:42 -05:00
if opts.ctx != nil || opts.title != "" {
unhook, err := hookDialogTitle(opts.ctx, opts.title)
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
}
2021-01-05 10:20:42 -05:00
activate()
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
}
type _CHOOSECOLORW struct {
StructSize uint32
Owner uintptr
Instance uintptr
RgbResult uint32
CustColors *[16]uint32
Flags uint32
CustData uintptr
FnHook uintptr
TemplateName *uint16
}