-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrush.go
211 lines (160 loc) · 4.16 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
// 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.
package walk
import . "github.com/lxn/go-winapi"
type HatchStyle int
const (
HatchHorizontal HatchStyle = HS_HORIZONTAL
HatchVertical HatchStyle = HS_VERTICAL
HatchForwardDiagonal HatchStyle = HS_FDIAGONAL
HatchBackwardDiagonal HatchStyle = HS_BDIAGONAL
HatchCross HatchStyle = HS_CROSS
HatchDiagonalCross HatchStyle = HS_DIAGCROSS
)
type Brush interface {
Dispose()
handle() HBRUSH
logbrush() *LOGBRUSH
}
type nullBrush struct {
hBrush HBRUSH
}
func newNullBrush() *nullBrush {
lb := &LOGBRUSH{LbStyle: BS_NULL}
hBrush := CreateBrushIndirect(lb)
if hBrush == 0 {
panic("failed to create null brush")
}
return &nullBrush{hBrush: hBrush}
}
func (b *nullBrush) Dispose() {
if b.hBrush != 0 {
DeleteObject(HGDIOBJ(b.hBrush))
b.hBrush = 0
}
}
func (b *nullBrush) handle() HBRUSH {
return b.hBrush
}
func (b *nullBrush) logbrush() *LOGBRUSH {
return &LOGBRUSH{LbStyle: BS_NULL}
}
var nullBrushSingleton Brush = newNullBrush()
func NullBrush() Brush {
return nullBrushSingleton
}
type SystemColorBrush struct {
hBrush HBRUSH
colorIndex int
}
func NewSystemColorBrush(colorIndex int) (*SystemColorBrush, error) {
hBrush := GetSysColorBrush(colorIndex)
if hBrush == 0 {
return nil, newError("GetSysColorBrush failed")
}
return &SystemColorBrush{hBrush, colorIndex}, nil
}
func (b *SystemColorBrush) ColorIndex() int {
return b.colorIndex
}
func (b *SystemColorBrush) Dispose() {
// nop
}
func (b *SystemColorBrush) handle() HBRUSH {
return b.hBrush
}
func (b *SystemColorBrush) logbrush() *LOGBRUSH {
return &LOGBRUSH{
LbStyle: BS_SOLID,
LbColor: COLORREF(GetSysColor(b.colorIndex)),
}
}
type SolidColorBrush struct {
hBrush HBRUSH
color Color
}
func NewSolidColorBrush(color Color) (*SolidColorBrush, error) {
lb := &LOGBRUSH{LbStyle: BS_SOLID, LbColor: COLORREF(color)}
hBrush := CreateBrushIndirect(lb)
if hBrush == 0 {
return nil, newError("CreateBrushIndirect failed")
}
return &SolidColorBrush{hBrush: hBrush, color: color}, nil
}
func (b *SolidColorBrush) Color() Color {
return b.color
}
func (b *SolidColorBrush) Dispose() {
if b.hBrush != 0 {
DeleteObject(HGDIOBJ(b.hBrush))
b.hBrush = 0
}
}
func (b *SolidColorBrush) handle() HBRUSH {
return b.hBrush
}
func (b *SolidColorBrush) logbrush() *LOGBRUSH {
return &LOGBRUSH{LbStyle: BS_SOLID, LbColor: COLORREF(b.color)}
}
type HatchBrush struct {
hBrush HBRUSH
color Color
style HatchStyle
}
func NewHatchBrush(color Color, style HatchStyle) (*HatchBrush, error) {
lb := &LOGBRUSH{LbStyle: BS_HATCHED, LbColor: COLORREF(color), LbHatch: uintptr(style)}
hBrush := CreateBrushIndirect(lb)
if hBrush == 0 {
return nil, newError("CreateBrushIndirect failed")
}
return &HatchBrush{hBrush: hBrush, color: color, style: style}, nil
}
func (b *HatchBrush) Color() Color {
return b.color
}
func (b *HatchBrush) Dispose() {
if b.hBrush != 0 {
DeleteObject(HGDIOBJ(b.hBrush))
b.hBrush = 0
}
}
func (b *HatchBrush) handle() HBRUSH {
return b.hBrush
}
func (b *HatchBrush) logbrush() *LOGBRUSH {
return &LOGBRUSH{LbStyle: BS_HATCHED, LbColor: COLORREF(b.color), LbHatch: uintptr(b.style)}
}
func (b *HatchBrush) Style() HatchStyle {
return b.style
}
type BitmapBrush struct {
hBrush HBRUSH
bitmap *Bitmap
}
func NewBitmapBrush(bitmap *Bitmap) (*BitmapBrush, error) {
if bitmap == nil {
return nil, newError("bitmap cannot be nil")
}
lb := &LOGBRUSH{LbStyle: BS_DIBPATTERN, LbColor: DIB_RGB_COLORS, LbHatch: uintptr(bitmap.hPackedDIB)}
hBrush := CreateBrushIndirect(lb)
if hBrush == 0 {
return nil, newError("CreateBrushIndirect failed")
}
return &BitmapBrush{hBrush: hBrush, bitmap: bitmap}, nil
}
func (b *BitmapBrush) Dispose() {
if b.hBrush != 0 {
DeleteObject(HGDIOBJ(b.hBrush))
b.hBrush = 0
}
}
func (b *BitmapBrush) handle() HBRUSH {
return b.hBrush
}
func (b *BitmapBrush) logbrush() *LOGBRUSH {
return &LOGBRUSH{LbStyle: BS_DIBPATTERN, LbColor: DIB_RGB_COLORS, LbHatch: uintptr(b.bitmap.hPackedDIB)}
}
func (b *BitmapBrush) Bitmap() *Bitmap {
return b.bitmap
}