-
Notifications
You must be signed in to change notification settings - Fork 1
/
tabs_windows.go
402 lines (348 loc) · 10.2 KB
/
tabs_windows.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
package goey
import (
"image/color"
"syscall"
"unsafe"
"bitbucket.org/rj/goey/base"
"github.com/lxn/win"
)
var (
tabs struct {
className []uint16
oldWindowProc uintptr
hbrush win.HBRUSH
hbrushFlag bool
}
)
func init() {
tabs.className = []uint16{'S', 'y', 's', 'T', 'a', 'b', 'C', 'o', 'n', 't', 'r', 'o', 'l', '3', '2', 0}
}
func (w *Tabs) mount(parent base.Control) (base.Element, error) {
// Create the control
const STYLE = win.WS_CHILD | win.WS_VISIBLE
hwnd, _, err := createControlWindow(win.WS_EX_CONTROLPARENT, &tabs.className[0], "", STYLE, parent.HWnd)
if err != nil {
return nil, err
}
for i, v := range w.Children {
text, err := syscall.UTF16PtrFromString(v.Caption)
if err != nil {
win.DestroyWindow(hwnd)
return nil, err
}
item := win.TCITEM{
Mask: win.TCIF_TEXT,
PszText: text,
LParam: uintptr(i),
}
win.SendMessage(hwnd, win.TCM_INSERTITEM, uintptr(i), uintptr(unsafe.Pointer(&item)))
}
if w.Value > 0 {
win.SendMessage(hwnd, win.TCM_SETCURSEL, uintptr(w.Value), 0)
}
child := base.Element(nil)
if len(w.Children) > 0 {
err := error(nil)
if w.Value >= 0 {
child, err = base.Mount(base.Control{hwnd}, w.Children[w.Value].Child)
} else {
child, err = base.Mount(base.Control{hwnd}, w.Children[0].Child)
}
if err != nil {
win.DestroyWindow(hwnd)
return nil, err
}
}
retval := &tabsElement{
Control: Control{hwnd},
child: child,
parent: parent,
value: w.Value,
insets: w.Insets,
widgets: w.Children,
onChange: w.OnChange,
}
// Subclass the window procedure
win.SetWindowLongPtr(hwnd, win.GWLP_USERDATA, uintptr(unsafe.Pointer(retval)))
subclassWindowProcedure(hwnd, &tabs.oldWindowProc, tabsWindowProc)
return retval, nil
}
type tabsElement struct {
Control
child base.Element
parent base.Control
value int
insets Insets
widgets []TabItem
onChange func(int)
cachedInsets base.Point
cachedBounds base.Rectangle
hbrush win.HBRUSH
}
func (w *tabsElement) controlInsets() base.Point {
if w.cachedInsets.Y == 0 {
rect := win.RECT{}
win.SendMessage(w.hWnd, win.TCM_ADJUSTRECT, win.TRUE, uintptr(unsafe.Pointer(&rect)))
w.cachedInsets = base.Point{
X: base.FromPixelsX(int(rect.Right - rect.Left)),
Y: base.FromPixelsY(int(rect.Bottom - rect.Top)),
}
}
return w.cachedInsets
}
func (w *tabsElement) controlTabsMinWidth() base.Length {
// No API to get this information has been found.
return 75 * DIP
}
func (w *tabsElement) Props() base.Widget {
count := win.SendMessage(w.hWnd, win.TCM_GETITEMCOUNT, 0, 0)
children := make([]TabItem, count)
for i := uintptr(0); i < count; i++ {
text := [128]uint16{}
item := win.TCITEM{
Mask: win.TCIF_TEXT,
PszText: &text[0],
CchTextMax: 128,
}
win.SendMessage(w.hWnd, win.TCM_GETITEM, i, uintptr(unsafe.Pointer(&item)))
children[i].Caption = syscall.UTF16ToString(text[:])
children[i].Child = w.widgets[i].Child
}
return &Tabs{
Value: int(win.SendMessage(w.hWnd, win.TCM_GETCURSEL, 0, 0)),
Children: children,
OnChange: w.onChange,
}
}
func (w *tabsElement) SetOrder(previous win.HWND) win.HWND {
previous = w.Control.SetOrder(previous)
if w.child != nil {
previous = w.child.SetOrder(previous)
}
return previous
}
func (w *tabsElement) SetBounds(bounds base.Rectangle) {
w.Control.SetBounds(bounds)
if w.hbrush != 0 {
win.DeleteObject(win.HGDIOBJ(w.hbrush))
w.hbrush = 0
}
if w.child != nil {
// Determine the bounds for the child widget
rect := win.RECT{}
win.SendMessage(w.hWnd, win.TCM_ADJUSTRECT, win.FALSE, uintptr(unsafe.Pointer(&rect)))
w.cachedBounds = base.Rectangle{
Min: bounds.Min.Add(base.Point{base.FromPixelsX(int(rect.Left)), base.FromPixelsY(int(rect.Top))}),
Max: bounds.Max.Add(base.Point{base.FromPixelsX(int(rect.Right)), base.FromPixelsY(int(rect.Bottom))}),
}
// Offset to handle insets
w.cachedBounds.Min.X += w.insets.Left - bounds.Min.X
w.cachedBounds.Min.Y += w.insets.Top - bounds.Min.Y
w.cachedBounds.Max.X -= w.insets.Right + bounds.Min.X
w.cachedBounds.Max.Y -= w.insets.Bottom + bounds.Min.Y
// Update bounds for the child
w.child.SetBounds(w.cachedBounds)
}
}
func (w *tabsElement) updateChildren(children []TabItem) error {
len1 := len(w.widgets)
len2 := len(children)
if len1 <= len2 {
// Change caption for tabs that already exist
for i, v := range children[:len1] {
text, err := syscall.UTF16PtrFromString(v.Caption)
if err != nil {
return err
}
item := win.TCITEM{
Mask: win.TCIF_TEXT,
PszText: text,
}
win.SendMessage(w.hWnd, win.TCM_SETITEM, uintptr(i), uintptr(unsafe.Pointer(&item)))
}
// Add new tabs to extend the list
for i, v := range children[len1:] {
text, err := syscall.UTF16PtrFromString(v.Caption)
if err != nil {
return err
}
item := win.TCITEM{
Mask: win.TCIF_TEXT,
PszText: text,
}
win.SendMessage(w.hWnd, win.TCM_INSERTITEM, uintptr(i+len1), uintptr(unsafe.Pointer(&item)))
}
} else {
// Change caption for tabs that already exist
for i, v := range children {
text, err := syscall.UTF16PtrFromString(v.Caption)
if err != nil {
return err
}
item := win.TCITEM{
Mask: win.TCIF_TEXT,
PszText: text,
}
win.SendMessage(w.hWnd, win.TCM_SETITEM, uintptr(i), uintptr(unsafe.Pointer(&item)))
}
// Delete excess tabs.
for i := len2; i < len1; i++ {
win.SendMessage(w.hWnd, win.TCM_DELETEITEM, uintptr(i), 0)
}
}
w.widgets = children
return nil
}
func (w *tabsElement) updateProps(data *Tabs) error {
// Update the tabs
err := w.updateChildren(data.Children)
if err != nil {
return err
}
// Update which tab is currently selected
if data.Value >= 0 && w.value != data.Value {
win.SendMessage(w.hWnd, win.TCM_SETCURSEL, uintptr(data.Value), 0)
w.value = data.Value
}
// Update event handlers
w.onChange = data.OnChange
return nil
}
func tabsBackgroundBrush(hwnd win.HWND, hdc win.HDC) (win.HBRUSH, bool, error) {
// If there is a global brush that can be used for all tab controls,
// use that brush
if tabs.hbrush != 0 {
return tabs.hbrush, true, nil
}
// We need to print the client area for the tab control to a bitmap, which
// can be used to create a brush.
cr := win.RECT{}
win.GetClientRect(hwnd, &cr)
// Configure a device context to capture contents of the control
cdc := win.CreateCompatibleDC(hdc)
if cdc == 0 {
return 0, false, syscall.GetLastError()
}
defer func() {
win.DeleteDC(cdc)
}()
hbitmap := win.CreateCompatibleBitmap(hdc, cr.Right-cr.Left, cr.Bottom-cr.Top)
if hbitmap == 0 {
return 0, false, syscall.GetLastError()
}
defer func() {
win.DeleteObject(win.HGDIOBJ(hbitmap))
}()
win.SelectObject(cdc, win.HGDIOBJ(hbitmap))
// Get bitmap of the control
win.SendMessage(hwnd, win.WM_PRINTCLIENT, uintptr(cdc), win.PRF_CLIENT)
// If possible, better to use a solid brush that does not need to be
// generated everytime the size of the control is changed.
if !tabs.hbrushFlag {
tabs.hbrushFlag = true
// Is the current bitmap a constant colour in the client area
win.SendMessage(hwnd, win.TCM_ADJUSTRECT, win.FALSE, uintptr(unsafe.Pointer(&cr)))
if clr := win.GetPixel(cdc, cr.Left, cr.Top); clr == win.GetPixel(cdc, (cr.Left+cr.Right)/2, cr.Top) && clr == win.GetPixel(cdc, cr.Left, (cr.Top+cr.Bottom)/2) {
hbrush := createBrush(color.RGBA{
R: uint8(clr & 0xFF),
G: uint8((clr >> 8) & 0xFF),
B: uint8((clr >> 16) & 0xFF),
A: 0xFF,
})
tabs.hbrush = hbrush
return hbrush, true, nil
}
}
// Convert the bitmap to a brush
brush := win.CreatePatternBrush(hbitmap)
if brush == 0 {
return 0, false, syscall.GetLastError()
}
return brush, false, nil
}
func tabsWindowProc(hwnd win.HWND, msg uint32, wParam uintptr, lParam uintptr) (result uintptr) {
switch msg {
case win.WM_DESTROY:
// Make sure that the data structure on the Go-side does not point to a non-existent
// window.
if brush := tabsGetPtr(hwnd).hbrush; brush != 0 {
win.DeleteObject(win.HGDIOBJ(brush))
}
tabsGetPtr(hwnd).hWnd = 0
// Defer to the old window proc
case win.WM_CTLCOLORSTATIC:
win.SetBkMode(win.HDC(wParam), win.TRANSPARENT)
w := tabsGetPtr(hwnd)
if w.hbrush == 0 {
if hbrush, solid, err := tabsBackgroundBrush(hwnd, win.HDC(wParam)); err != nil {
panic(err)
} else if solid {
return uintptr(hbrush)
} else {
w.hbrush = hbrush
}
}
// Set offset for the brush
cr := win.RECT{}
win.GetWindowRect(win.HWND(lParam), &cr)
origin := win.POINT{cr.Left, cr.Top}
win.ScreenToClient(hwnd, &origin)
win.SetBrushOrgEx(win.HDC(wParam), -origin.X, -origin.Y, nil)
return uintptr(w.hbrush)
case win.WM_HSCROLL:
if lParam != 0 {
// Message was sent by a child window. As for all other controls
// that notify the parent, resend to the child with the expectation
// that the child has been subclassed.
return win.SendMessage(win.HWND(lParam), win.WM_HSCROLL, wParam, 0)
}
// Defer to default window proc
case win.WM_COMMAND:
return windowprocWmCommand(wParam, lParam)
case win.WM_NOTIFY:
if n := (*win.NMHDR)(unsafe.Pointer(lParam)); true {
if n.HwndFrom == hwnd {
if n.Code == uint32(0x100000000+win.TCN_SELCHANGE) {
cursel := int(win.SendMessage(hwnd, win.TCM_GETCURSEL, 0, 0))
if w := tabsGetPtr(hwnd); w.value != cursel {
if w.onChange != nil {
w.onChange(cursel)
}
if w.value != cursel {
child, err := base.DiffChild(w.parent, w.child, w.widgets[cursel].Child)
if err != nil {
panic("Unhandled error!")
}
if child != nil {
child.SetOrder(w.hWnd)
child.Layout(base.Tight(base.Size{
Width: w.cachedBounds.Dx(),
Height: w.cachedBounds.Dy(),
}))
child.SetBounds(w.cachedBounds)
win.InvalidateRect(win.GetParent(w.hWnd), nil, false)
}
w.child = child
w.value = cursel
}
}
}
} else {
return windowprocWmNotify(wParam, lParam)
}
}
return 0
}
return win.CallWindowProc(tabs.oldWindowProc, hwnd, msg, wParam, lParam)
}
func tabsGetPtr(hwnd win.HWND) *tabsElement {
gwl := win.GetWindowLongPtr(hwnd, win.GWLP_USERDATA)
if gwl == 0 {
panic("Internal error.")
}
ptr := (*tabsElement)(unsafe.Pointer(gwl))
if ptr.hWnd != hwnd && ptr.hWnd != 0 {
panic("Internal error.")
}
return ptr
}