diff options
author | Alexander Neumann <an2048@gmail.com> | 2012-10-26 14:26:25 +0200 |
---|---|---|
committer | Alexander Neumann <an2048@gmail.com> | 2012-10-26 14:26:25 +0200 |
commit | 3189ccc87713346d41eef10bce0b6ac5db70d774 (patch) | |
tree | dd4accccd5304874eaf00bbc09e306eb830ceedc /imagelist.go | |
parent | Remove ExpandedImager interface (diff) | |
download | wireguard-windows-3189ccc87713346d41eef10bce0b6ac5db70d774.tar.xz wireguard-windows-3189ccc87713346d41eef10bce0b6ac5db70d774.zip |
Add functions for managing image to image list index mapping and caching
Diffstat (limited to 'imagelist.go')
-rw-r--r-- | imagelist.go | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/imagelist.go b/imagelist.go index 4ad0612d..c8ec6cf7 100644 --- a/imagelist.go +++ b/imagelist.go @@ -4,6 +4,11 @@ package walk +import ( + "syscall" + "unsafe" +) + import . "github.com/lxn/go-winapi" type ImageList struct { @@ -69,3 +74,89 @@ func (il *ImageList) Dispose() { func (il *ImageList) MaskColor() Color { return il.maskColor } + +func imageListForImage(image interface{}) (hIml HIMAGELIST, isSysIml bool, err error) { + if filePath, ok := image.(string); ok { + _, hIml = iconIndexAndHImlForFilePath(filePath) + isSysIml = hIml != 0 + } else { + w, h := GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON) + + hIml = ImageList_Create(w, h, ILC_MASK|ILC_COLOR24, 8, 8) + if hIml == 0 { + return 0, false, newError("ImageList_Create failed") + } + } + + return +} + +func iconIndexAndHImlForFilePath(filePath string) (int32, HIMAGELIST) { + var shfi SHFILEINFO + + if hIml := HIMAGELIST(SHGetFileInfo( + syscall.StringToUTF16Ptr(filePath), + 0, + &shfi, + uint32(unsafe.Sizeof(shfi)), + SHGFI_SYSICONINDEX|SHGFI_SMALLICON)); hIml != 0 { + + return shfi.IIcon, hIml + } + + return -1, 0 +} + +func imageIndexMaybeAdd(image interface{}, hIml HIMAGELIST, isSysIml bool, imageUintptr2Index map[uintptr]int32, filePath2IconIndex map[string]int32) int32 { + if !isSysIml { + return imageIndexAddIfNotExists(image, hIml, imageUintptr2Index) + } else if filePath, ok := image.(string); ok { + if iIcon, ok := filePath2IconIndex[filePath]; ok { + return iIcon + } + + if iIcon, _ := iconIndexAndHImlForFilePath(filePath); iIcon != -1 { + filePath2IconIndex[filePath] = iIcon + return iIcon + } + } + + return -1 +} + +func imageIndexAddIfNotExists(image interface{}, hIml HIMAGELIST, imageUintptr2Index map[uintptr]int32) int32 { + imageIndex := int32(-1) + + if image != nil { + var ptr uintptr + switch img := image.(type) { + case *Bitmap: + ptr = uintptr(unsafe.Pointer(img)) + + case *Icon: + ptr = uintptr(unsafe.Pointer(img)) + } + + if ptr == 0 { + return -1 + } + + if imageIndex, ok := imageUintptr2Index[ptr]; ok { + return imageIndex + } + + switch img := image.(type) { + case *Bitmap: + imageIndex = ImageList_AddMasked(hIml, img.hBmp, 0) + + case *Icon: + imageIndex = ImageList_ReplaceIcon(hIml, -1, img.hIcon) + } + + if imageIndex > -1 { + imageUintptr2Index[ptr] = imageIndex + } + } + + return imageIndex +} |