forked from gonutz/wui
/
panel.go
171 lines (142 loc) · 3.23 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
//+build windows
package wui
import (
"syscall"
"github.com/gonutz/w32"
)
func NewPanel() *Panel {
return &Panel{}
}
type Panel struct {
control
border panelBorder
controls []Control
font *Font
}
func (*Panel) isContainer() {}
type panelBorder int
const (
borderNone panelBorder = iota
borderSingleLine
borderSunken
borderSunkenThick
borderRaised
)
func borderStyleEx(b panelBorder) uint {
if b == borderSunken {
return w32.WS_EX_STATICEDGE
}
if b == borderSunkenThick {
return w32.WS_EX_CLIENTEDGE
}
return 0
}
func borderStyle(b panelBorder) uint {
if b == borderSingleLine {
return w32.WS_BORDER
}
if b == borderRaised {
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 i, c := range p.controls {
c.create(id + i + 1)
p.parent.registerControl(c)
}
}
func (p *Panel) getHandle() w32.HWND {
return p.handle
}
func (p *Panel) getInstance() w32.HINSTANCE {
return p.parent.getInstance()
}
func (p *Panel) setBorder(b panelBorder) {
p.border = b
if p.handle != 0 {
style := uint(w32.GetWindowLongPtr(p.handle, w32.GWL_STYLE))
style = style &^ w32.WS_BORDER &^ w32.WS_DLGFRAME
style |= borderStyle(b)
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(b)
w32.SetWindowLongPtr(p.handle, w32.GWL_EXSTYLE, uintptr(exStyle))
w32.InvalidateRect(p.parent.getHandle(), nil, true)
}
}
func (p *Panel) SetNoBorder() {
p.setBorder(borderNone)
}
func (p *Panel) SetSingleLineBorder() {
p.setBorder(borderSingleLine)
}
func (p *Panel) SetSunkenBorder() {
p.setBorder(borderSunken)
}
func (p *Panel) SetSunkenThickBorder() {
p.setBorder(borderSunkenThick)
}
func (p *Panel) SetRaisedBorder() {
p.setBorder(borderRaised)
}
// TODO there is a bug in this when Adding control while parent is not set
func (p *Panel) Add(c Control) {
c.setParent(p)
if p.handle != 0 {
c.create(p.parent.controlCount() + controlIDOffset)
}
p.registerControl(c)
}
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) controlCount() int {
return p.parent.controlCount()
}
func (p *Panel) registerControl(c Control) {
if p.parent != nil {
p.parent.registerControl(c)
}
}
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.controls {
c.parentFontChanged()
}
}