-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimagelist.go
221 lines (173 loc) · 4.92 KB
/
imagelist.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// Copyright 2010 The Walk Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build windows
package walk
import (
"syscall"
"unsafe"
"github.com/lxn/win"
)
type ImageList struct {
hIml win.HIMAGELIST
maskColor Color
imageSize96dpi Size
colorMaskedBitmap2Index map[*Bitmap]int
bitmapMaskedBitmap2Index map[bitmapMaskedBitmap]int
}
type bitmapMaskedBitmap struct {
bitmap *Bitmap
mask *Bitmap
}
func NewImageList(imageSize Size, maskColor Color) (*ImageList, error) {
hDC := win.GetDC(0)
defer win.ReleaseDC(0, hDC)
dpi := int(win.GetDeviceCaps(hDC, win.LOGPIXELSX))
return newImageList(imageSize, maskColor, dpi)
}
func newImageList(imageSize Size, maskColor Color, dpi int) (*ImageList, error) {
scale := float64(dpi) / 96.0
width := int32(float64(imageSize.Width) * scale)
height := int32(float64(imageSize.Height) * scale)
hIml := win.ImageList_Create(
width,
height,
win.ILC_MASK|win.ILC_COLOR32,
8,
8)
if hIml == 0 {
return nil, newError("ImageList_Create failed")
}
return &ImageList{
hIml: hIml,
maskColor: maskColor,
imageSize96dpi: imageSize,
colorMaskedBitmap2Index: make(map[*Bitmap]int),
bitmapMaskedBitmap2Index: make(map[bitmapMaskedBitmap]int),
}, nil
}
func (il *ImageList) Handle() win.HIMAGELIST {
return il.hIml
}
func (il *ImageList) Add(bitmap, maskBitmap *Bitmap) (int, error) {
if bitmap == nil {
return 0, newError("bitmap cannot be nil")
}
key := bitmapMaskedBitmap{bitmap: bitmap, mask: maskBitmap}
if index, ok := il.bitmapMaskedBitmap2Index[key]; ok {
return index, nil
}
var maskHandle win.HBITMAP
if maskBitmap != nil {
maskHandle = maskBitmap.handle()
}
index := int(win.ImageList_Add(il.hIml, bitmap.handle(), maskHandle))
if index == -1 {
return 0, newError("ImageList_Add failed")
}
il.bitmapMaskedBitmap2Index[key] = index
return index, nil
}
func (il *ImageList) AddMasked(bitmap *Bitmap) (int32, error) {
if bitmap == nil {
return 0, newError("bitmap cannot be nil")
}
if index, ok := il.colorMaskedBitmap2Index[bitmap]; ok {
return int32(index), nil
}
index := win.ImageList_AddMasked(
il.hIml,
bitmap.handle(),
win.COLORREF(il.maskColor))
if index == -1 {
return 0, newError("ImageList_AddMasked failed")
}
il.colorMaskedBitmap2Index[bitmap] = int(index)
return index, nil
}
func (il *ImageList) Dispose() {
if il.hIml != 0 {
win.ImageList_Destroy(il.hIml)
il.hIml = 0
}
}
func (il *ImageList) MaskColor() Color {
return il.maskColor
}
func imageListForImage(image interface{}, dpi int) (hIml win.HIMAGELIST, isSysIml bool, err error) {
if name, ok := image.(string); ok {
if img, err := Resources.Image(name); err == nil {
image = img
}
}
if filePath, ok := image.(string); ok {
_, hIml = iconIndexAndHImlForFilePath(filePath)
isSysIml = hIml != 0
} else {
scale := float64(dpi) / 96.0
w := int32(float64(win.GetSystemMetrics(win.SM_CXSMICON)) * scale)
h := int32(float64(win.GetSystemMetrics(win.SM_CYSMICON)) * scale)
hIml = win.ImageList_Create(w, h, win.ILC_MASK|win.ILC_COLOR32, 8, 8)
if hIml == 0 {
return 0, false, newError("ImageList_Create failed")
}
}
return
}
func iconIndexAndHImlForFilePath(filePath string) (int32, win.HIMAGELIST) {
var shfi win.SHFILEINFO
if hIml := win.HIMAGELIST(win.SHGetFileInfo(
syscall.StringToUTF16Ptr(filePath),
0,
&shfi,
uint32(unsafe.Sizeof(shfi)),
win.SHGFI_SYSICONINDEX|win.SHGFI_SMALLICON)); hIml != 0 {
return shfi.IIcon, hIml
}
return -1, 0
}
func imageIndexMaybeAdd(image interface{}, hIml win.HIMAGELIST, isSysIml bool, imageUintptr2Index map[uintptr]int32, filePath2IconIndex map[string]int32, dpi int) int32 {
if !isSysIml {
return imageIndexAddIfNotExists(image, hIml, imageUintptr2Index, dpi)
} 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 win.HIMAGELIST, imageUintptr2Index map[uintptr]int32, dpi int) int32 {
imageIndex := int32(-1)
if image != nil {
if name, ok := image.(string); ok {
image, _ = Resources.Image(name)
}
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 = win.ImageList_AddMasked(hIml, img.hBmp, 0)
case *Icon:
imageIndex = win.ImageList_ReplaceIcon(hIml, -1, img.handleForDPI(dpi))
}
if imageIndex > -1 {
imageUintptr2Index[ptr] = imageIndex
}
}
return imageIndex
}