-
Notifications
You must be signed in to change notification settings - Fork 0
/
tabwidget.go
653 lines (497 loc) · 13.9 KB
/
tabwidget.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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
// Copyright 2011 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 (
"strconv"
"syscall"
"unsafe"
)
import (
"github.com/lxn/win"
)
const tabWidgetWindowClass = `\o/ Walk_TabWidget_Class \o/`
func init() {
MustRegisterWindowClass(tabWidgetWindowClass)
}
type TabWidget struct {
WidgetBase
hWndTab win.HWND
tabOrigWndProcPtr uintptr
imageList *ImageList
pages *TabPageList
currentIndex int
currentIndexChangedPublisher EventPublisher
persistent bool
}
func NewTabWidget(parent Container) (*TabWidget, error) {
tw := &TabWidget{currentIndex: -1}
tw.pages = newTabPageList(tw)
if err := InitWidget(
tw,
parent,
tabWidgetWindowClass,
win.WS_VISIBLE,
win.WS_EX_CONTROLPARENT); err != nil {
return nil, err
}
succeeded := false
defer func() {
if !succeeded {
tw.Dispose()
}
}()
tw.SetPersistent(true)
tw.hWndTab = win.CreateWindowEx(
0, syscall.StringToUTF16Ptr("SysTabControl32"), nil,
win.WS_CHILD|win.WS_CLIPSIBLINGS|win.WS_TABSTOP|win.WS_VISIBLE,
0, 0, 0, 0, tw.hWnd, 0, 0, nil)
if tw.hWndTab == 0 {
return nil, lastError("CreateWindowEx")
}
win.SetWindowLongPtr(tw.hWndTab, win.GWLP_USERDATA, uintptr(unsafe.Pointer(tw)))
tw.tabOrigWndProcPtr = win.SetWindowLongPtr(tw.hWndTab, win.GWLP_WNDPROC, tabWidgetTabWndProcPtr)
win.SendMessage(tw.hWndTab, win.WM_SETFONT, uintptr(defaultFont.handleForDPI(0)), 1)
setWindowFont(tw.hWndTab, tw.Font())
tw.MustRegisterProperty("HasCurrentPage", NewReadOnlyBoolProperty(
func() bool {
return tw.CurrentIndex() != -1
},
tw.CurrentIndexChanged()))
succeeded = true
return tw, nil
}
func (tw *TabWidget) Dispose() {
tw.WidgetBase.Dispose()
if tw.imageList != nil {
tw.imageList.Dispose()
tw.imageList = nil
}
}
func (tw *TabWidget) LayoutFlags() LayoutFlags {
if tw.pages.Len() == 0 {
return ShrinkableHorz | ShrinkableVert | GrowableHorz | GrowableVert | GreedyHorz | GreedyVert
}
var flags LayoutFlags
for i := tw.pages.Len() - 1; i >= 0; i-- {
flags |= tw.pages.At(i).LayoutFlags()
}
return flags
}
func (tw *TabWidget) MinSizeHint() Size {
if tw.pages.Len() == 0 {
return tw.SizeHint()
}
var min Size
for i := tw.pages.Len() - 1; i >= 0; i-- {
s := tw.pages.At(i).MinSizeHint()
min.Width = maxi(min.Width, s.Width)
min.Height = maxi(min.Height, s.Height)
}
b := tw.Bounds()
pb := tw.pages.At(0).Bounds()
size := Size{b.Width - pb.Width + min.Width, b.Height - pb.Height + min.Height}
return size
}
func (tw *TabWidget) SizeHint() Size {
return Size{100, 100}
}
func (tw *TabWidget) applyEnabled(enabled bool) {
tw.WidgetBase.applyEnabled(enabled)
setWindowEnabled(tw.hWndTab, enabled)
applyEnabledToDescendants(tw, enabled)
}
func (tw *TabWidget) applyFont(font *Font) {
tw.WidgetBase.applyFont(font)
setWindowFont(tw.hWndTab, font)
applyFontToDescendants(tw, font)
}
func (tw *TabWidget) CurrentIndex() int {
return tw.currentIndex
}
func (tw *TabWidget) SetCurrentIndex(index int) error {
if index == tw.currentIndex {
return nil
}
if index < 0 || index >= tw.pages.Len() {
return newError("invalid index")
}
ret := int(win.SendMessage(tw.hWndTab, win.TCM_SETCURSEL, uintptr(index), 0))
if ret == -1 {
return newError("SendMessage(TCM_SETCURSEL) failed")
}
// FIXME: The SendMessage(TCM_SETCURSEL) call above doesn't cause a
// TCN_SELCHANGE notification, so we use this workaround.
tw.onSelChange()
return nil
}
func (tw *TabWidget) CurrentIndexChanged() *Event {
return tw.currentIndexChangedPublisher.Event()
}
func (tw *TabWidget) Pages() *TabPageList {
return tw.pages
}
func (tw *TabWidget) Persistent() bool {
return tw.persistent
}
func (tw *TabWidget) SetPersistent(value bool) {
tw.persistent = value
}
func (tw *TabWidget) SaveState() error {
tw.WriteState(strconv.Itoa(tw.CurrentIndex()))
for _, page := range tw.pages.items {
if err := page.SaveState(); err != nil {
return err
}
}
return nil
}
func (tw *TabWidget) RestoreState() error {
state, err := tw.ReadState()
if err != nil {
return err
}
if state == "" {
return nil
}
index, err := strconv.Atoi(state)
if err != nil {
return err
}
if index >= 0 && index < tw.pages.Len() {
if err := tw.SetCurrentIndex(index); err != nil {
return err
}
}
for _, page := range tw.pages.items {
if err := page.RestoreState(); err != nil {
return err
}
}
return nil
}
func (tw *TabWidget) resizePages() {
var r win.RECT
if !win.GetWindowRect(tw.hWndTab, &r) {
lastError("GetWindowRect")
return
}
p := win.POINT{
r.Left,
r.Top,
}
if !win.ScreenToClient(tw.hWnd, &p) {
newError("ScreenToClient failed")
return
}
r = win.RECT{
p.X,
p.Y,
r.Right - r.Left + p.X,
r.Bottom - r.Top + p.Y,
}
win.SendMessage(tw.hWndTab, win.TCM_ADJUSTRECT, 0, uintptr(unsafe.Pointer(&r)))
for _, page := range tw.pages.items {
if err := page.SetBounds(
Rectangle{
int(r.Left - 2),
int(r.Top),
int(r.Right - r.Left + 2),
int(r.Bottom - r.Top),
}); err != nil {
return
}
}
}
func (tw *TabWidget) onResize(lParam uintptr) {
r := win.RECT{0, 0, win.GET_X_LPARAM(lParam), win.GET_Y_LPARAM(lParam)}
if !win.MoveWindow(tw.hWndTab, r.Left, r.Top, r.Right-r.Left, r.Bottom-r.Top, true) {
lastError("MoveWindow")
return
}
tw.resizePages()
}
func (tw *TabWidget) onSelChange() {
pageCount := tw.pages.Len()
if tw.currentIndex > -1 && tw.currentIndex < pageCount {
page := tw.pages.At(tw.currentIndex)
page.SetVisible(false)
}
tw.currentIndex = int(win.SendMessage(tw.hWndTab, win.TCM_GETCURSEL, 0, 0))
if tw.currentIndex > -1 && tw.currentIndex < pageCount {
page := tw.pages.At(tw.currentIndex)
page.SetVisible(true)
page.Invalidate()
}
tw.Invalidate()
tw.currentIndexChangedPublisher.Publish()
}
func (tw *TabWidget) WndProc(hwnd win.HWND, msg uint32, wParam, lParam uintptr) uintptr {
if tw.hWndTab != 0 {
switch msg {
case win.WM_ERASEBKGND:
return 1
//case win.WM_PAINT:
// return 0
case win.WM_SIZE, win.WM_SIZING:
tw.onResize(lParam)
case win.WM_NOTIFY:
nmhdr := (*win.NMHDR)(unsafe.Pointer(lParam))
switch int32(nmhdr.Code) {
case win.TCN_SELCHANGE:
tw.onSelChange()
}
}
}
return tw.WidgetBase.WndProc(hwnd, msg, wParam, lParam)
}
var tabWidgetTabWndProcPtr = syscall.NewCallback(tabWidgetTabWndProc)
func tabWidgetTabWndProc(hwnd win.HWND, msg uint32, wParam, lParam uintptr) uintptr {
tw := (*TabWidget)(unsafe.Pointer(win.GetWindowLongPtr(hwnd, win.GWLP_USERDATA)))
switch msg {
case win.WM_MOUSEMOVE:
win.InvalidateRect(hwnd, nil, true)
case win.WM_ERASEBKGND:
return 1
case win.WM_PAINT:
var ps win.PAINTSTRUCT
hdc := win.BeginPaint(hwnd, &ps)
defer win.EndPaint(hwnd, &ps)
cb := tw.ClientBounds()
bitmap, err := NewBitmap(cb.Size())
if err != nil {
break
}
defer bitmap.Dispose()
canvas, err := NewCanvasFromImage(bitmap)
if err != nil {
break
}
defer canvas.Dispose()
themed := win.IsAppThemed()
if !themed {
if err := canvas.FillRectangle(sysColorBtnFaceBrush, cb); err != nil {
break
}
}
win.SendMessage(hwnd, win.WM_PRINTCLIENT, uintptr(canvas.hdc), uintptr(win.PRF_CLIENT|win.PRF_CHILDREN|win.PRF_ERASEBKGND))
parent := tw.Parent()
if parent == nil {
break
}
// Draw background of free area not occupied by tab items.
if bg, wnd := parent.AsWindowBase().backgroundEffective(); bg != nil {
tw.prepareDCForBackground(canvas.hdc, hwnd, wnd)
hRgn := win.CreateRectRgn(0, 0, 0, 0)
defer win.DeleteObject(win.HGDIOBJ(hRgn))
var rc win.RECT
count := tw.pages.Len()
for i := 0; i < count; i++ {
if 0 == win.SendMessage(hwnd, win.TCM_GETITEMRECT, uintptr(i), uintptr(unsafe.Pointer(&rc))) {
break
}
if i == tw.currentIndex {
rc.Left -= 2
rc.Top -= 2
rc.Right += 2
} else {
if i == count-1 && themed {
rc.Right -= 2
}
}
hRgnTab := win.CreateRectRgn(rc.Left, rc.Top, rc.Right, rc.Bottom)
win.CombineRgn(hRgn, hRgn, hRgnTab, win.RGN_OR)
win.DeleteObject(win.HGDIOBJ(hRgnTab))
}
hRgnRC := win.CreateRectRgn(0, 0, int32(cb.Width), rc.Bottom)
win.CombineRgn(hRgn, hRgnRC, hRgn, win.RGN_DIFF)
win.DeleteObject(win.HGDIOBJ(hRgnRC))
if !win.FillRgn(canvas.hdc, hRgn, bg.handle()) {
break
}
}
// Draw current tab item.
if tw.currentIndex != -1 {
page := tw.pages.At(tw.CurrentIndex())
if bg, wnd := page.AsWindowBase().backgroundEffective(); bg != nil &&
bg != tabPageBackgroundBrush &&
(page.layout == nil || !page.layout.Margins().isZero()) {
tw.prepareDCForBackground(canvas.hdc, hwnd, wnd)
var rc win.RECT
if 0 == win.SendMessage(hwnd, win.TCM_GETITEMRECT, uintptr(tw.currentIndex), uintptr(unsafe.Pointer(&rc))) {
break
}
hRgn := win.CreateRectRgn(rc.Left, rc.Top, rc.Right, rc.Bottom+2)
defer win.DeleteObject(win.HGDIOBJ(hRgn))
if !win.FillRgn(canvas.hdc, hRgn, bg.handle()) {
break
}
if page.image != nil {
x := rc.Left + 6
y := rc.Top
s := int32(16)
if imageCanvas, err := NewCanvasFromImage(page.image); err == nil {
defer imageCanvas.Dispose()
if !win.TransparentBlt(
canvas.hdc, x, y, s, s,
imageCanvas.hdc, 0, 0, int32(page.image.size.Width), int32(page.image.size.Height),
0) {
break
}
}
rc.Left += s + 6
}
rc.Left += 6
rc.Top += 1
title := syscall.StringToUTF16(page.title)
if themed {
hTheme := win.OpenThemeData(hwnd, syscall.StringToUTF16Ptr("tab"))
defer win.CloseThemeData(hTheme)
options := win.DTTOPTS{DwFlags: win.DTT_GLOWSIZE, IGlowSize: 3}
options.DwSize = uint32(unsafe.Sizeof(options))
if hr := win.DrawThemeTextEx(hTheme, canvas.hdc, 0, win.TIS_SELECTED, &title[0], int32(len(title)), 0, &rc, &options); !win.SUCCEEDED(hr) {
break
}
} else {
if 0 == win.DrawTextEx(canvas.hdc, &title[0], int32(len(title)), &rc, 0, nil) {
break
}
}
}
}
if !win.BitBlt(hdc, 0, 0, int32(cb.Width), int32(cb.Height), canvas.hdc, 0, 0, win.SRCCOPY) {
break
}
return 0
}
return win.CallWindowProc(tw.tabOrigWndProcPtr, hwnd, msg, wParam, lParam)
}
func (tw *TabWidget) onPageChanged(page *TabPage) (err error) {
index := tw.pages.Index(page)
item := tw.tcitemFromPage(page)
if 0 == win.SendMessage(tw.hWndTab, win.TCM_SETITEM, uintptr(index), uintptr(unsafe.Pointer(item))) {
return newError("SendMessage(TCM_SETITEM) failed")
}
return nil
}
func (tw *TabWidget) onInsertingPage(index int, page *TabPage) (err error) {
return nil
}
func (tw *TabWidget) onInsertedPage(index int, page *TabPage) (err error) {
item := tw.tcitemFromPage(page)
if idx := int(win.SendMessage(tw.hWndTab, win.TCM_INSERTITEM, uintptr(index), uintptr(unsafe.Pointer(item)))); idx == -1 {
return newError("SendMessage(TCM_INSERTITEM) failed")
}
page.SetVisible(false)
style := uint32(win.GetWindowLong(page.hWnd, win.GWL_STYLE))
if style == 0 {
return lastError("GetWindowLong")
}
style |= win.WS_CHILD
style &^= win.WS_POPUP
win.SetLastError(0)
if win.SetWindowLong(page.hWnd, win.GWL_STYLE, int32(style)) == 0 {
return lastError("SetWindowLong")
}
if win.SetParent(page.hWnd, tw.hWnd) == 0 {
return lastError("SetParent")
}
if tw.pages.Len() == 1 {
page.SetVisible(true)
tw.SetCurrentIndex(0)
}
tw.resizePages()
page.tabWidget = tw
page.applyFont(tw.Font())
tw.Invalidate()
return
}
func (tw *TabWidget) removePage(page *TabPage) (err error) {
page.SetVisible(false)
style := uint32(win.GetWindowLong(page.hWnd, win.GWL_STYLE))
if style == 0 {
return lastError("GetWindowLong")
}
style &^= win.WS_CHILD
style |= win.WS_POPUP
win.SetLastError(0)
if win.SetWindowLong(page.hWnd, win.GWL_STYLE, int32(style)) == 0 {
return lastError("SetWindowLong")
}
page.tabWidget = nil
return page.SetParent(nil)
}
func (tw *TabWidget) onRemovingPage(index int, page *TabPage) (err error) {
return nil
}
func (tw *TabWidget) onRemovedPage(index int, page *TabPage) (err error) {
err = tw.removePage(page)
if err != nil {
return
}
win.SendMessage(tw.hWndTab, win.TCM_DELETEITEM, uintptr(index), 0)
if tw.pages.Len() > 0 {
tw.currentIndex = 0
win.SendMessage(tw.hWndTab, win.TCM_SETCURSEL, uintptr(tw.currentIndex), 0)
} else {
tw.currentIndex = -1
}
tw.onSelChange()
return
// FIXME: Either make use of this unreachable code or remove it.
if index == tw.currentIndex {
// removal of current visible tabpage...
tw.currentIndex = -1
// select new tabpage if any :
if tw.pages.Len() > 0 {
// are we removing the rightmost page ?
if index == tw.pages.Len()-1 {
// If so, select the page on the left
index -= 1
}
}
}
tw.SetCurrentIndex(index)
tw.Invalidate()
return
}
func (tw *TabWidget) onClearingPages(pages []*TabPage) (err error) {
return nil
}
func (tw *TabWidget) onClearedPages(pages []*TabPage) (err error) {
win.SendMessage(tw.hWndTab, win.TCM_DELETEALLITEMS, 0, 0)
for _, page := range pages {
tw.removePage(page)
}
tw.currentIndex = -1
tw.Invalidate()
return nil
}
func (tw *TabWidget) tcitemFromPage(page *TabPage) *win.TCITEM {
imageIndex, _ := tw.imageIndex(page.image)
text := syscall.StringToUTF16(page.title)
item := &win.TCITEM{
Mask: win.TCIF_IMAGE | win.TCIF_TEXT,
IImage: imageIndex,
PszText: &text[0],
CchTextMax: int32(len(text)),
}
return item
}
func (tw *TabWidget) imageIndex(image *Bitmap) (index int32, err error) {
index = -1
if image != nil {
if tw.imageList == nil {
if tw.imageList, err = NewImageList(Size{16, 16}, 0); err != nil {
return
}
win.SendMessage(tw.hWndTab, win.TCM_SETIMAGELIST, 0, uintptr(tw.imageList.hIml))
}
// FIXME: Protect against duplicate insertion
if index, err = tw.imageList.AddMasked(image); err != nil {
return
}
}
return
}