-
Notifications
You must be signed in to change notification settings - Fork 0
/
brush.go
534 lines (419 loc) · 12.6 KB
/
brush.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
// 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 (
"unsafe"
"github.com/lxn/win"
)
type HatchStyle int
const (
HatchHorizontal HatchStyle = win.HS_HORIZONTAL
HatchVertical HatchStyle = win.HS_VERTICAL
HatchForwardDiagonal HatchStyle = win.HS_FDIAGONAL
HatchBackwardDiagonal HatchStyle = win.HS_BDIAGONAL
HatchCross HatchStyle = win.HS_CROSS
HatchDiagonalCross HatchStyle = win.HS_DIAGCROSS
)
type SystemColor int
const (
SysColor3DDkShadow SystemColor = win.COLOR_3DDKSHADOW
SysColor3DFace SystemColor = win.COLOR_3DFACE
SysColor3DHighlight SystemColor = win.COLOR_3DHIGHLIGHT
SysColor3DLight SystemColor = win.COLOR_3DLIGHT
SysColor3DShadow SystemColor = win.COLOR_3DSHADOW
SysColorActiveBorder SystemColor = win.COLOR_ACTIVEBORDER
SysColorActiveCaption SystemColor = win.COLOR_ACTIVECAPTION
SysColorAppWorkspace SystemColor = win.COLOR_APPWORKSPACE
SysColorBackground SystemColor = win.COLOR_BACKGROUND
SysColorDesktop SystemColor = win.COLOR_DESKTOP
SysColorBtnFace SystemColor = win.COLOR_BTNFACE
SysColorBtnHighlight SystemColor = win.COLOR_BTNHIGHLIGHT
SysColorBtnShadow SystemColor = win.COLOR_BTNSHADOW
SysColorBtnText SystemColor = win.COLOR_BTNTEXT
SysColorCaptionText SystemColor = win.COLOR_CAPTIONTEXT
SysColorGrayText SystemColor = win.COLOR_GRAYTEXT
SysColorHighlight SystemColor = win.COLOR_HIGHLIGHT
SysColorHighlightText SystemColor = win.COLOR_HIGHLIGHTTEXT
SysColorInactiveBorder SystemColor = win.COLOR_INACTIVEBORDER
SysColorInactiveCaption SystemColor = win.COLOR_INACTIVECAPTION
SysColorInactiveCaptionText SystemColor = win.COLOR_INACTIVECAPTIONTEXT
SysColorInfoBk SystemColor = win.COLOR_INFOBK
SysColorInfoText SystemColor = win.COLOR_INFOTEXT
SysColorMenu SystemColor = win.COLOR_MENU
SysColorMenuText SystemColor = win.COLOR_MENUTEXT
SysColorScrollBar SystemColor = win.COLOR_SCROLLBAR
SysColorWindow SystemColor = win.COLOR_WINDOW
SysColorWindowFrame SystemColor = win.COLOR_WINDOWFRAME
SysColorWindowText SystemColor = win.COLOR_WINDOWTEXT
SysColorHotLight SystemColor = win.COLOR_HOTLIGHT
SysColorGradientActiveCaption SystemColor = win.COLOR_GRADIENTACTIVECAPTION
SysColorGradientInactiveCaption SystemColor = win.COLOR_GRADIENTINACTIVECAPTION
)
type Brush interface {
Dispose()
handle() win.HBRUSH
logbrush() *win.LOGBRUSH
attachWindow(wb *WindowBase)
detachWindow(wb *WindowBase)
simple() bool
}
type perWindowBrush interface {
Brush
delegateForWindow(wb *WindowBase) Brush
}
type windowBrushInfo struct {
SizeChangedHandle int
Delegate *BitmapBrush
}
type brushBase struct {
hBrush win.HBRUSH
wb2info map[*WindowBase]*windowBrushInfo
}
func (bb *brushBase) Dispose() {
if bb.hBrush != 0 {
win.DeleteObject(win.HGDIOBJ(bb.hBrush))
bb.hBrush = 0
}
}
func (bb *brushBase) handle() win.HBRUSH {
return bb.hBrush
}
func (bb *brushBase) attachWindow(wb *WindowBase) {
if wb == nil {
return
}
if bb.wb2info == nil {
bb.wb2info = make(map[*WindowBase]*windowBrushInfo)
}
bb.wb2info[wb] = nil
}
func (bb *brushBase) detachWindow(wb *WindowBase) {
if bb.wb2info == nil || wb == nil {
return
}
delete(bb.wb2info, wb)
if len(bb.wb2info) == 0 {
bb.Dispose()
}
}
type nullBrush struct {
brushBase
}
func newNullBrush() *nullBrush {
lb := &win.LOGBRUSH{LbStyle: win.BS_NULL}
hBrush := win.CreateBrushIndirect(lb)
if hBrush == 0 {
panic("failed to create null brush")
}
return &nullBrush{brushBase: brushBase{hBrush: hBrush}}
}
func (b *nullBrush) Dispose() {
if b == nullBrushSingleton {
return
}
b.brushBase.Dispose()
}
func (*nullBrush) logbrush() *win.LOGBRUSH {
return &win.LOGBRUSH{LbStyle: win.BS_NULL}
}
func (*nullBrush) simple() bool {
return true
}
var (
nullBrushSingleton Brush
sysColorBtnFaceBrush *SystemColorBrush
)
func NullBrush() Brush {
return nullBrushSingleton
}
type SystemColorBrush struct {
brushBase
sysColor SystemColor
}
func init() {
AppendToWalkInit(func() {
nullBrushSingleton = newNullBrush()
sysColorBtnFaceBrush, _ = NewSystemColorBrush(SysColorBtnFace)
})
}
func NewSystemColorBrush(sysColor SystemColor) (*SystemColorBrush, error) {
hBrush := win.GetSysColorBrush(int(sysColor))
if hBrush == 0 {
return nil, newError("GetSysColorBrush failed")
}
return &SystemColorBrush{brushBase: brushBase{hBrush: hBrush}, sysColor: sysColor}, nil
}
func (b *SystemColorBrush) Color() Color {
return Color(win.GetSysColor(int(b.sysColor)))
}
func (b *SystemColorBrush) SystemColor() SystemColor {
return b.sysColor
}
func (*SystemColorBrush) Dispose() {
// nop
}
func (b *SystemColorBrush) logbrush() *win.LOGBRUSH {
return &win.LOGBRUSH{
LbStyle: win.BS_SOLID,
LbColor: win.COLORREF(win.GetSysColor(int(b.sysColor))),
}
}
func (*SystemColorBrush) simple() bool {
return true
}
type SolidColorBrush struct {
brushBase
color Color
}
func NewSolidColorBrush(color Color) (*SolidColorBrush, error) {
lb := &win.LOGBRUSH{LbStyle: win.BS_SOLID, LbColor: win.COLORREF(color)}
hBrush := win.CreateBrushIndirect(lb)
if hBrush == 0 {
return nil, newError("CreateBrushIndirect failed")
}
return &SolidColorBrush{brushBase: brushBase{hBrush: hBrush}, color: color}, nil
}
func (b *SolidColorBrush) Color() Color {
return b.color
}
func (b *SolidColorBrush) logbrush() *win.LOGBRUSH {
return &win.LOGBRUSH{LbStyle: win.BS_SOLID, LbColor: win.COLORREF(b.color)}
}
func (*SolidColorBrush) simple() bool {
return true
}
type HatchBrush struct {
brushBase
color Color
style HatchStyle
}
func NewHatchBrush(color Color, style HatchStyle) (*HatchBrush, error) {
lb := &win.LOGBRUSH{LbStyle: win.BS_HATCHED, LbColor: win.COLORREF(color), LbHatch: uintptr(style)}
hBrush := win.CreateBrushIndirect(lb)
if hBrush == 0 {
return nil, newError("CreateBrushIndirect failed")
}
return &HatchBrush{brushBase: brushBase{hBrush: hBrush}, color: color, style: style}, nil
}
func (b *HatchBrush) Color() Color {
return b.color
}
func (b *HatchBrush) logbrush() *win.LOGBRUSH {
return &win.LOGBRUSH{LbStyle: win.BS_HATCHED, LbColor: win.COLORREF(b.color), LbHatch: uintptr(b.style)}
}
func (b *HatchBrush) Style() HatchStyle {
return b.style
}
func (b *HatchBrush) simple() bool {
return false
}
type BitmapBrush struct {
brushBase
bitmap *Bitmap
}
func NewBitmapBrush(bitmap *Bitmap) (*BitmapBrush, error) {
if bitmap == nil {
return nil, newError("bitmap cannot be nil")
}
hBrush := win.CreatePatternBrush(bitmap.hBmp)
if hBrush == 0 {
return nil, newError("CreatePatternBrush failed")
}
return &BitmapBrush{brushBase: brushBase{hBrush: hBrush}, bitmap: bitmap}, nil
}
func (b *BitmapBrush) logbrush() *win.LOGBRUSH {
return &win.LOGBRUSH{LbStyle: win.BS_DIBPATTERN, LbColor: win.DIB_RGB_COLORS, LbHatch: uintptr(b.bitmap.hPackedDIB)}
}
func (b *BitmapBrush) Bitmap() *Bitmap {
return b.bitmap
}
func (b *BitmapBrush) simple() bool {
return false
}
type GradientStop struct {
Offset float64
Color Color
}
type GradientVertex struct {
X float64
Y float64
Color Color
}
type GradientTriangle struct {
Vertex1 int
Vertex2 int
Vertex3 int
}
type GradientBrush struct {
brushBase
mainDelegate *BitmapBrush
vertexes []GradientVertex
triangles []GradientTriangle
orientation gradientOrientation
absolute bool
}
type gradientOrientation int
const (
gradientOrientationNone gradientOrientation = iota
gradientOrientationHorizontal
gradientOrientationVertical
)
func NewHorizontalGradientBrush(stops []GradientStop) (*GradientBrush, error) {
return newGradientBrushWithOrientation(stops, gradientOrientationHorizontal)
}
func NewVerticalGradientBrush(stops []GradientStop) (*GradientBrush, error) {
return newGradientBrushWithOrientation(stops, gradientOrientationVertical)
}
func newGradientBrushWithOrientation(stops []GradientStop, orientation gradientOrientation) (*GradientBrush, error) {
if len(stops) < 2 {
return nil, newError("at least 2 stops are required")
}
var vertexes []GradientVertex
var triangles []GradientTriangle
for i, stop := range stops {
var x0, y0, x1, y1 float64
if orientation == gradientOrientationHorizontal {
x0 = stop.Offset
x1 = stop.Offset
y1 = 1.0
} else {
y0 = stop.Offset
x1 = 1.0
y1 = stop.Offset
}
vertexes = append(vertexes, GradientVertex{X: x0, Y: y0, Color: stop.Color})
vertexes = append(vertexes, GradientVertex{X: x1, Y: y1, Color: stop.Color})
if i > 0 {
triangles = append(triangles, GradientTriangle{Vertex1: i*2 - 2, Vertex2: i*2 + 1, Vertex3: i*2 - 1})
triangles = append(triangles, GradientTriangle{Vertex1: i*2 - 2, Vertex2: i * 2, Vertex3: i*2 + 1})
}
}
return newGradientBrush(vertexes, triangles, orientation)
}
func NewGradientBrush(vertexes []GradientVertex, triangles []GradientTriangle) (*GradientBrush, error) {
if len(vertexes) < 3 {
return nil, newError("at least 3 vertexes are required")
}
if len(triangles) < 1 {
return nil, newError("at least 1 triangle is required")
}
return newGradientBrush(vertexes, triangles, gradientOrientationNone)
}
func newGradientBrush(vertexes []GradientVertex, triangles []GradientTriangle, orientation gradientOrientation) (*GradientBrush, error) {
var size Size
for _, v := range vertexes {
size = maxSize(size, Size{int(v.X), int(v.Y)})
}
gb := &GradientBrush{vertexes: vertexes, triangles: triangles, orientation: orientation, absolute: size.Width > 1 || size.Height > 1}
if gb.absolute {
bb, err := gb.create(size)
if err != nil {
return nil, err
}
gb.mainDelegate = bb
gb.hBrush = bb.hBrush
}
return gb, nil
}
func (b *GradientBrush) logbrush() *win.LOGBRUSH {
if b.mainDelegate == nil {
return nil
}
return b.mainDelegate.logbrush()
}
func (*GradientBrush) simple() bool {
return false
}
// create creates a gradient brush at given size in native pixels.
func (b *GradientBrush) create(size Size) (*BitmapBrush, error) {
var disposables Disposables
defer disposables.Treat()
switch b.orientation {
case gradientOrientationHorizontal:
size.Height = 1
case gradientOrientationVertical:
size.Width = 1
}
bitmap, err := NewBitmapForDPI(size, 96) // Size is in native pixels and bitmap is used for brush only => DPI is not used anywhere.
if err != nil {
return nil, err
}
disposables.Add(bitmap)
canvas, err := NewCanvasFromImage(bitmap)
if err != nil {
return nil, err
}
defer canvas.Dispose()
var scaleX, scaleY float64
if b.absolute {
scaleX, scaleY = 1, 1
} else {
scaleX, scaleY = float64(size.Width), float64(size.Height)
}
vertexes := make([]win.TRIVERTEX, len(b.vertexes))
for i, src := range b.vertexes {
dst := &vertexes[i]
dst.X = int32(src.X * scaleX)
dst.Y = int32(src.Y * scaleY)
dst.Red = uint16(src.Color.R()) * 256
dst.Green = uint16(src.Color.G()) * 256
dst.Blue = uint16(src.Color.B()) * 256
}
triangles := make([]win.GRADIENT_TRIANGLE, len(b.triangles))
for i, src := range b.triangles {
dst := &triangles[i]
dst.Vertex1 = uint32(src.Vertex1)
dst.Vertex2 = uint32(src.Vertex2)
dst.Vertex3 = uint32(src.Vertex3)
}
if !win.GradientFill(canvas.hdc, &vertexes[0], uint32(len(vertexes)), unsafe.Pointer(&triangles[0]), uint32(len(triangles)), win.GRADIENT_FILL_TRIANGLE) {
return nil, newError("GradientFill failed")
}
disposables.Spare()
return NewBitmapBrush(bitmap)
}
func (b *GradientBrush) attachWindow(wb *WindowBase) {
b.brushBase.attachWindow(wb)
if b.absolute {
return
}
var info *windowBrushInfo
update := func() {
if bb, err := b.create(wb.window.ClientBoundsPixels().Size()); err == nil {
if info.Delegate != nil {
info.Delegate.bitmap.Dispose()
info.Delegate.Dispose()
}
info.Delegate = bb
wb.Invalidate()
}
}
info = &windowBrushInfo{
SizeChangedHandle: wb.SizeChanged().Attach(update),
}
update()
b.wb2info[wb] = info
}
func (b *GradientBrush) detachWindow(wb *WindowBase) {
if !b.absolute {
if info, ok := b.wb2info[wb]; ok {
if info.Delegate != nil {
info.Delegate.bitmap.Dispose()
info.Delegate.Dispose()
}
wb.SizeChanged().Detach(info.SizeChangedHandle)
}
}
b.brushBase.detachWindow(wb)
}
func (b *GradientBrush) delegateForWindow(wb *WindowBase) Brush {
if b.absolute {
return b.mainDelegate
}
if info, ok := b.wb2info[wb]; ok && info.Delegate != nil {
return info.Delegate
}
return nil
}