-
Notifications
You must be signed in to change notification settings - Fork 25
/
panel.go
331 lines (278 loc) · 6.87 KB
/
panel.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
//+build windows
package wui
import (
"syscall"
"github.com/gonutz/w32/v2"
)
func NewPanel() *Panel {
return &Panel{}
}
type Panel struct {
control
children []Control
border PanelBorderStyle
font *Font
}
var _ Control = (*Panel)(nil)
var _ Container = (*Panel)(nil)
func (p *Panel) closing() {
for _, c := range p.children {
c.closing()
}
}
func (p *Panel) destroy() {
if p.handle != 0 {
for _, c := range p.children {
c.destroy()
}
p.control.destroy()
}
}
func (*Panel) canFocus() bool {
return false
}
func (*Panel) eatsTabs() bool {
return false
}
type PanelBorderStyle int
const (
PanelBorderNone PanelBorderStyle = iota
PanelBorderSingleLine
PanelBorderSunken
PanelBorderSunkenThick
PanelBorderRaised
)
func (s PanelBorderStyle) String() string {
// NOTE that these strings are used in the designer to get their
// representations as Go code so they must always correspond to their
// constant names and be prefixed with the package name.
switch s {
case PanelBorderNone:
return "wui.PanelBorderNone"
case PanelBorderSingleLine:
return "wui.PanelBorderSingleLine"
case PanelBorderSunken:
return "wui.PanelBorderSunken"
case PanelBorderSunkenThick:
return "wui.PanelBorderSunkenThick"
case PanelBorderRaised:
return "wui.PanelBorderRaised"
default:
return "unknown PanelBorderStyle"
}
}
func borderStyleEx(b PanelBorderStyle) uint {
if b == PanelBorderSunken {
return w32.WS_EX_STATICEDGE
}
if b == PanelBorderSunkenThick {
return w32.WS_EX_CLIENTEDGE
}
return 0
}
func borderStyle(b PanelBorderStyle) uint {
if b == PanelBorderSingleLine {
return w32.WS_BORDER
}
if b == PanelBorderRaised {
return w32.WS_DLGFRAME
}
return 0
}
func (p *Panel) create(id int) {
p.control.create(id, borderStyleEx(p.border), "STATIC", borderStyle(p.border))
w32.SetWindowSubclass(p.handle, syscall.NewCallback(func(
window w32.HWND,
msg uint32,
wParam, lParam uintptr,
subclassID uintptr,
refData uintptr,
) uintptr {
switch msg {
case w32.WM_COMMAND:
p.onWM_COMMAND(wParam, lParam)
return 0
case w32.WM_DRAWITEM:
p.onWM_DRAWITEM(wParam, lParam)
return 0
case w32.WM_NOTIFY:
p.onWM_NOTIFY(wParam, lParam)
return 0
default:
return w32.DefSubclassProc(window, msg, wParam, lParam)
}
}), 0, 0)
for _, c := range p.children {
c.create(p.getIDFor(c))
}
}
func (p *Panel) Add(c Control) {
p.children = append(p.children, c)
c.setParent(p)
if p.handle != 0 {
c.create(p.getIDFor(c))
}
}
func (p *Panel) Remove(c Control) {
for i, child := range p.children {
if child == c {
child.setParent(nil)
child.destroy()
p.children = append(p.children[:i], p.children[i+1:]...)
return
}
}
}
func (p *Panel) getHandle() w32.HWND {
return p.handle
}
func (p *Panel) getInstance() w32.HINSTANCE {
return p.parent.getInstance()
}
func (p *Panel) SetBorderStyle(s PanelBorderStyle) {
p.border = s
if p.handle != 0 {
style := uint(w32.GetWindowLongPtr(p.handle, w32.GWL_STYLE))
style = style &^ w32.WS_BORDER &^ w32.WS_DLGFRAME
style |= borderStyle(s)
w32.SetWindowLongPtr(p.handle, w32.GWL_STYLE, uintptr(style))
exStyle := uint(w32.GetWindowLongPtr(p.handle, w32.GWL_EXSTYLE))
exStyle = exStyle &^ w32.WS_EX_STATICEDGE &^ w32.WS_EX_CLIENTEDGE
exStyle |= borderStyleEx(s)
w32.SetWindowLongPtr(p.handle, w32.GWL_EXSTYLE, uintptr(exStyle))
w32.InvalidateRect(p.parent.getHandle(), nil, true)
}
}
func (p *Panel) BorderStyle() PanelBorderStyle {
return p.border
}
func (p *Panel) getIDFor(c Control) int {
if p.parent == nil {
return -1
}
return p.parent.getIDFor(c)
}
func (p *Panel) Children() []Control {
return p.children
}
func (p *Panel) onWM_COMMAND(w, l uintptr) {
p.parent.onWM_COMMAND(w, l)
}
func (p *Panel) onWM_DRAWITEM(w, l uintptr) {
p.parent.onWM_DRAWITEM(w, l)
}
func (p *Panel) onWM_NOTIFY(w, l uintptr) {
p.parent.onWM_NOTIFY(w, l)
}
func (p *Panel) Font() *Font {
if p.font == nil && p.parent != nil {
return p.parent.Font()
}
return p.font
}
func (p *Panel) SetFont(f *Font) {
p.font = f
for _, c := range p.children {
c.parentFontChanged()
}
}
func (p *Panel) InnerX() int {
x, _, _, _ := p.InnerBounds()
return x
}
func (p *Panel) InnerY() int {
_, y, _, _ := p.InnerBounds()
return y
}
func (p *Panel) InnerPosition() (x, y int) {
x, y, _, _ = p.InnerBounds()
return
}
func (p *Panel) InnerWidth() int {
_, _, width, _ := p.InnerBounds()
return width
}
func (p *Panel) InnerHeight() int {
_, _, _, height := p.InnerBounds()
return height
}
func (p *Panel) InnerSize() (width, height int) {
_, _, width, height = p.InnerBounds()
return
}
func (p *Panel) InnerBounds() (x, y, width, height int) {
x, y, width, height = p.Bounds()
var r w32.RECT
w32.AdjustWindowRectEx(&r, borderStyle(p.border), false, borderStyleEx(p.border))
x -= int(r.Left)
y -= int(r.Top)
width -= int(r.Width())
height -= int(r.Height())
return
}
func (p *Panel) SetInnerX(x int) {
_, y, width, height := p.InnerBounds()
p.SetInnerBounds(x, y, width, height)
}
func (p *Panel) SetInnerY(y int) {
x, _, width, height := p.InnerBounds()
p.SetInnerBounds(x, y, width, height)
}
func (p *Panel) SetInnerPosition(x, y int) {
_, _, width, height := p.InnerBounds()
p.SetInnerBounds(x, y, width, height)
}
func (p *Panel) SetInnerWidth(width int) {
x, y, _, height := p.InnerBounds()
p.SetInnerBounds(x, y, width, height)
}
func (p *Panel) SetInnerHeight(height int) {
x, y, width, _ := p.InnerBounds()
p.SetInnerBounds(x, y, width, height)
}
func (p *Panel) SetInnerSize(width, height int) {
x, y, _, _ := p.InnerBounds()
p.SetInnerBounds(x, y, width, height)
}
func (p *Panel) SetInnerBounds(x, y, width, height int) {
var r w32.RECT
w32.AdjustWindowRectEx(&r, borderStyle(p.border), false, borderStyleEx(p.border))
x += int(r.Left)
y += int(r.Top)
width += int(r.Width())
height += int(r.Height())
p.SetBounds(x, y, width, height)
}
func (p *Panel) SetBounds(x, y, width, height int) {
_, _, oldW, oldH := p.InnerBounds()
p.control.SetBounds(x, y, width, height)
_, _, newW, newH := p.InnerBounds()
repositionChidrenByAnchors(p, oldW, oldH, newW, newH)
}
// NOTE that we need to re-write all the Set... functions here to make them go
// throught Panel's SetBounds. control's Set... functions go through control's
// SetBounds which does not do what we want.
func (p *Panel) SetX(x int) {
_, y, width, height := p.Bounds()
p.SetBounds(x, y, width, height)
}
func (p *Panel) SetY(y int) {
x, _, width, height := p.Bounds()
p.SetBounds(x, y, width, height)
}
func (p *Panel) SetPosition(x, y int) {
_, _, width, height := p.Bounds()
p.SetBounds(x, y, width, height)
}
func (p *Panel) SetWidth(width int) {
x, y, _, height := p.Bounds()
p.SetBounds(x, y, width, height)
}
func (p *Panel) SetHeight(height int) {
x, y, width, _ := p.Bounds()
p.SetBounds(x, y, width, height)
}
func (p *Panel) SetSize(width, height int) {
x, y, _, _ := p.Bounds()
p.SetBounds(x, y, width, height)
}