forked from lxn/walk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrush.go
256 lines (201 loc) · 6.66 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
// 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 (
"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
}
type nullBrush struct {
hBrush win.HBRUSH
}
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{hBrush: hBrush}
}
func (b *nullBrush) Dispose() {
if b.hBrush != 0 {
win.DeleteObject(win.HGDIOBJ(b.hBrush))
b.hBrush = 0
}
}
func (b *nullBrush) handle() win.HBRUSH {
return b.hBrush
}
func (b *nullBrush) logbrush() *win.LOGBRUSH {
return &win.LOGBRUSH{LbStyle: win.BS_NULL}
}
var nullBrushSingleton Brush = newNullBrush()
func NullBrush() Brush {
return nullBrushSingleton
}
type SystemColorBrush struct {
hBrush win.HBRUSH
sysColor SystemColor
}
var sysColorBtnFaceBrush, _ = NewSystemColorBrush(SysColorBtnFace)
func NewSystemColorBrush(sysColor SystemColor) (*SystemColorBrush, error) {
hBrush := win.GetSysColorBrush(int(sysColor))
if hBrush == 0 {
return nil, newError("GetSysColorBrush failed")
}
return &SystemColorBrush{hBrush, sysColor}, nil
}
func (b *SystemColorBrush) Color() Color {
return Color(win.GetSysColor(int(b.sysColor)))
}
func (b *SystemColorBrush) SystemColor() SystemColor {
return b.sysColor
}
func (b *SystemColorBrush) Dispose() {
// nop
}
func (b *SystemColorBrush) handle() win.HBRUSH {
return b.hBrush
}
func (b *SystemColorBrush) logbrush() *win.LOGBRUSH {
return &win.LOGBRUSH{
LbStyle: win.BS_SOLID,
LbColor: win.COLORREF(win.GetSysColor(int(b.sysColor))),
}
}
type SolidColorBrush struct {
hBrush win.HBRUSH
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{hBrush: hBrush, color: color}, nil
}
func (b *SolidColorBrush) Color() Color {
return b.color
}
func (b *SolidColorBrush) Dispose() {
if b.hBrush != 0 {
win.DeleteObject(win.HGDIOBJ(b.hBrush))
b.hBrush = 0
}
}
func (b *SolidColorBrush) handle() win.HBRUSH {
return b.hBrush
}
func (b *SolidColorBrush) logbrush() *win.LOGBRUSH {
return &win.LOGBRUSH{LbStyle: win.BS_SOLID, LbColor: win.COLORREF(b.color)}
}
type HatchBrush struct {
hBrush win.HBRUSH
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{hBrush: hBrush, color: color, style: style}, nil
}
func (b *HatchBrush) Color() Color {
return b.color
}
func (b *HatchBrush) Dispose() {
if b.hBrush != 0 {
win.DeleteObject(win.HGDIOBJ(b.hBrush))
b.hBrush = 0
}
}
func (b *HatchBrush) handle() win.HBRUSH {
return b.hBrush
}
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
}
type BitmapBrush struct {
hBrush win.HBRUSH
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{hBrush: hBrush, bitmap: bitmap}, nil
}
func (b *BitmapBrush) Dispose() {
if b.hBrush != 0 {
win.DeleteObject(win.HGDIOBJ(b.hBrush))
b.hBrush = 0
}
}
func (b *BitmapBrush) handle() win.HBRUSH {
return b.hBrush
}
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
}