-
Notifications
You must be signed in to change notification settings - Fork 0
/
tableview.go
2471 lines (2009 loc) · 66.1 KB
/
tableview.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
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 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 (
"encoding/json"
"fmt"
"math/big"
"reflect"
"syscall"
"time"
"unsafe"
"github.com/lxn/win"
)
const tableViewWindowClass = `\o/ Walk_TableView_Class \o/`
var (
white = win.COLORREF(RGB(255, 255, 255))
checkmark = string([]byte{0xE2, 0x9C, 0x94})
tableViewFrozenLVWndProcPtr uintptr
tableViewNormalLVWndProcPtr uintptr
tableViewHdrWndProcPtr uintptr
)
func init() {
AppendToWalkInit(func() {
MustRegisterWindowClass(tableViewWindowClass)
tableViewFrozenLVWndProcPtr = syscall.NewCallback(tableViewFrozenLVWndProc)
tableViewNormalLVWndProcPtr = syscall.NewCallback(tableViewNormalLVWndProc)
tableViewHdrWndProcPtr = syscall.NewCallback(tableViewHdrWndProc)
})
}
const (
tableViewCurrentIndexChangedTimerId = 1 + iota
tableViewSelectedIndexesChangedTimerId
)
type TableViewCfg struct {
Style uint32
CustomHeaderHeight int
CustomRowHeight int
}
// TableView is a model based widget for record centric, tabular data.
//
// TableView is implemented as a virtual mode list view to support quite large
// amounts of data.
type TableView struct {
WidgetBase
hwndFrozenLV win.HWND
hwndFrozenHdr win.HWND
frozenLVOrigWndProcPtr uintptr
frozenHdrOrigWndProcPtr uintptr
hwndNormalLV win.HWND
hwndNormalHdr win.HWND
normalLVOrigWndProcPtr uintptr
normalHdrOrigWndProcPtr uintptr
state *tableViewState
columns *TableViewColumnList
model TableModel
providedModel interface{}
itemChecker ItemChecker
imageProvider ImageProvider
styler CellStyler
style CellStyle
itemFont *Font
hIml win.HIMAGELIST
usingSysIml bool
imageUintptr2Index map[uintptr]int32
filePath2IconIndex map[string]int32
rowsResetHandlerHandle int
rowChangedHandlerHandle int
rowsInsertedHandlerHandle int
rowsRemovedHandlerHandle int
sortChangedHandlerHandle int
selectedIndexes []int
prevIndex int
currentIndex int
itemIndexOfLastMouseButtonDown int
hwndItemChanged win.HWND
currentIndexChangedPublisher EventPublisher
selectedIndexesChangedPublisher EventPublisher
itemActivatedPublisher EventPublisher
columnClickedPublisher IntEventPublisher
columnsOrderableChangedPublisher EventPublisher
columnsSizableChangedPublisher EventPublisher
itemCountChangedPublisher EventPublisher
publishNextSelClear bool
inSetSelectedIndexes bool
lastColumnStretched bool
persistent bool
itemStateChangedEventDelay int
themeNormalBGColor Color
themeNormalTextColor Color
themeSelectedBGColor Color
themeSelectedTextColor Color
themeSelectedNotFocusedBGColor Color
itemBGColor Color
itemTextColor Color
alternatingRowBGColor Color
alternatingRowTextColor Color
alternatingRowBG bool
delayedCurrentIndexChangedCanceled bool
sortedColumnIndex int
sortOrder SortOrder
formActivatingHandle int
customHeaderHeight int
customRowHeight int
scrolling bool
inSetCurrentIndex bool
inMouseEvent bool
hasFrozenColumn bool
inEraseBkgnd bool
focused bool
ignoreNowhere bool
updateLVSizesNeedsSpecialCare bool
}
// NewTableView creates and returns a *TableView as child of the specified
// Container.
func NewTableView(parent Container) (*TableView, error) {
return NewTableViewWithStyle(parent, win.LVS_SHOWSELALWAYS)
}
// NewTableViewWithStyle creates and returns a *TableView as child of the specified
// Container and with the provided additional style bits set.
func NewTableViewWithStyle(parent Container, style uint32) (*TableView, error) {
return NewTableViewWithCfg(parent, &TableViewCfg{Style: style})
}
// NewTableViewWithCfg creates and returns a *TableView as child of the specified
// Container and with the provided additional configuration.
func NewTableViewWithCfg(parent Container, cfg *TableViewCfg) (*TableView, error) {
tv := &TableView{
imageUintptr2Index: make(map[uintptr]int32),
filePath2IconIndex: make(map[string]int32),
formActivatingHandle: -1,
customHeaderHeight: cfg.CustomHeaderHeight,
customRowHeight: cfg.CustomRowHeight,
}
tv.columns = newTableViewColumnList(tv)
if err := InitWidget(
tv,
parent,
tableViewWindowClass,
win.WS_BORDER|win.WS_VISIBLE,
win.WS_EX_CONTROLPARENT); err != nil {
return nil, err
}
succeeded := false
defer func() {
if !succeeded {
tv.Dispose()
}
}()
var rowHeightStyle uint32
if cfg.CustomRowHeight > 0 {
rowHeightStyle = win.LVS_OWNERDRAWFIXED
}
if tv.hwndFrozenLV = win.CreateWindowEx(
0,
syscall.StringToUTF16Ptr("SysListView32"),
nil,
win.WS_CHILD|win.WS_CLIPSIBLINGS|win.WS_TABSTOP|win.WS_VISIBLE|win.LVS_OWNERDATA|win.LVS_REPORT|cfg.Style|rowHeightStyle,
win.CW_USEDEFAULT,
win.CW_USEDEFAULT,
win.CW_USEDEFAULT,
win.CW_USEDEFAULT,
tv.hWnd,
0,
0,
nil,
); tv.hwndFrozenLV == 0 {
return nil, newError("creating frozen lv failed")
}
tv.frozenLVOrigWndProcPtr = win.SetWindowLongPtr(tv.hwndFrozenLV, win.GWLP_WNDPROC, tableViewFrozenLVWndProcPtr)
if tv.frozenLVOrigWndProcPtr == 0 {
return nil, lastError("SetWindowLongPtr")
}
tv.hwndFrozenHdr = win.HWND(win.SendMessage(tv.hwndFrozenLV, win.LVM_GETHEADER, 0, 0))
tv.frozenHdrOrigWndProcPtr = win.SetWindowLongPtr(tv.hwndFrozenHdr, win.GWLP_WNDPROC, tableViewHdrWndProcPtr)
if tv.frozenHdrOrigWndProcPtr == 0 {
return nil, lastError("SetWindowLongPtr")
}
if tv.hwndNormalLV = win.CreateWindowEx(
0,
syscall.StringToUTF16Ptr("SysListView32"),
nil,
win.WS_CHILD|win.WS_CLIPSIBLINGS|win.WS_TABSTOP|win.WS_VISIBLE|win.LVS_OWNERDATA|win.LVS_REPORT|cfg.Style|rowHeightStyle,
win.CW_USEDEFAULT,
win.CW_USEDEFAULT,
win.CW_USEDEFAULT,
win.CW_USEDEFAULT,
tv.hWnd,
0,
0,
nil,
); tv.hwndNormalLV == 0 {
return nil, newError("creating normal lv failed")
}
tv.normalLVOrigWndProcPtr = win.SetWindowLongPtr(tv.hwndNormalLV, win.GWLP_WNDPROC, tableViewNormalLVWndProcPtr)
if tv.normalLVOrigWndProcPtr == 0 {
return nil, lastError("SetWindowLongPtr")
}
tv.hwndNormalHdr = win.HWND(win.SendMessage(tv.hwndNormalLV, win.LVM_GETHEADER, 0, 0))
tv.normalHdrOrigWndProcPtr = win.SetWindowLongPtr(tv.hwndNormalHdr, win.GWLP_WNDPROC, tableViewHdrWndProcPtr)
if tv.normalHdrOrigWndProcPtr == 0 {
return nil, lastError("SetWindowLongPtr")
}
tv.SetPersistent(true)
exStyle := win.SendMessage(tv.hwndFrozenLV, win.LVM_GETEXTENDEDLISTVIEWSTYLE, 0, 0)
exStyle |= win.LVS_EX_DOUBLEBUFFER | win.LVS_EX_FULLROWSELECT | win.LVS_EX_HEADERDRAGDROP | win.LVS_EX_LABELTIP | win.LVS_EX_SUBITEMIMAGES
win.SendMessage(tv.hwndFrozenLV, win.LVM_SETEXTENDEDLISTVIEWSTYLE, 0, exStyle)
win.SendMessage(tv.hwndNormalLV, win.LVM_SETEXTENDEDLISTVIEWSTYLE, 0, exStyle)
if hr := win.SetWindowTheme(tv.hwndFrozenLV, syscall.StringToUTF16Ptr("Explorer"), nil); win.FAILED(hr) {
return nil, errorFromHRESULT("SetWindowTheme", hr)
}
if hr := win.SetWindowTheme(tv.hwndNormalLV, syscall.StringToUTF16Ptr("Explorer"), nil); win.FAILED(hr) {
return nil, errorFromHRESULT("SetWindowTheme", hr)
}
win.SendMessage(tv.hwndFrozenLV, win.WM_CHANGEUISTATE, uintptr(win.MAKELONG(win.UIS_SET, win.UISF_HIDEFOCUS)), 0)
win.SendMessage(tv.hwndNormalLV, win.WM_CHANGEUISTATE, uintptr(win.MAKELONG(win.UIS_SET, win.UISF_HIDEFOCUS)), 0)
tv.applyFont(parent.Font())
tv.style.dpi = tv.DPI()
tv.ApplySysColors()
tv.currentIndex = -1
tv.GraphicsEffects().Add(InteractionEffect)
tv.GraphicsEffects().Add(FocusEffect)
tv.MustRegisterProperty("ColumnsOrderable", NewBoolProperty(
func() bool {
return tv.ColumnsOrderable()
},
func(b bool) error {
tv.SetColumnsOrderable(b)
return nil
},
tv.columnsOrderableChangedPublisher.Event()))
tv.MustRegisterProperty("ColumnsSizable", NewBoolProperty(
func() bool {
return tv.ColumnsSizable()
},
func(b bool) error {
return tv.SetColumnsSizable(b)
},
tv.columnsSizableChangedPublisher.Event()))
tv.MustRegisterProperty("CurrentIndex", NewProperty(
func() interface{} {
return tv.CurrentIndex()
},
func(v interface{}) error {
return tv.SetCurrentIndex(assertIntOr(v, -1))
},
tv.CurrentIndexChanged()))
tv.MustRegisterProperty("CurrentItem", NewReadOnlyProperty(
func() interface{} {
if i := tv.CurrentIndex(); i > -1 {
if rm, ok := tv.providedModel.(reflectModel); ok {
return reflect.ValueOf(rm.Items()).Index(i).Interface()
}
}
return nil
},
tv.CurrentIndexChanged()))
tv.MustRegisterProperty("HasCurrentItem", NewReadOnlyBoolProperty(
func() bool {
return tv.CurrentIndex() != -1
},
tv.CurrentIndexChanged()))
tv.MustRegisterProperty("ItemCount", NewReadOnlyProperty(
func() interface{} {
if tv.model == nil {
return 0
}
return tv.model.RowCount()
},
tv.itemCountChangedPublisher.Event()))
tv.MustRegisterProperty("SelectedCount", NewReadOnlyProperty(
func() interface{} {
return len(tv.selectedIndexes)
},
tv.SelectedIndexesChanged()))
succeeded = true
return tv, nil
}
func (tv *TableView) asTableView() *TableView {
return tv
}
// Dispose releases the operating system resources, associated with the
// *TableView.
func (tv *TableView) Dispose() {
tv.columns.unsetColumnsTV()
tv.disposeImageListAndCaches()
if tv.hWnd != 0 {
if !win.KillTimer(tv.hWnd, tableViewCurrentIndexChangedTimerId) {
lastError("KillTimer")
}
if !win.KillTimer(tv.hWnd, tableViewSelectedIndexesChangedTimerId) {
lastError("KillTimer")
}
}
if tv.hwndFrozenLV != 0 {
win.DestroyWindow(tv.hwndFrozenLV)
tv.hwndFrozenLV = 0
}
if tv.hwndNormalLV != 0 {
win.DestroyWindow(tv.hwndNormalLV)
tv.hwndNormalLV = 0
}
if tv.formActivatingHandle > -1 {
if form := tv.Form(); form != nil {
form.Activating().Detach(tv.formActivatingHandle)
}
tv.formActivatingHandle = -1
}
tv.WidgetBase.Dispose()
}
func (tv *TableView) applyEnabled(enabled bool) {
tv.WidgetBase.applyEnabled(enabled)
win.EnableWindow(tv.hwndFrozenLV, enabled)
win.EnableWindow(tv.hwndNormalLV, enabled)
}
func (tv *TableView) applyFont(font *Font) {
if tv.customHeaderHeight > 0 || tv.customRowHeight > 0 {
return
}
tv.WidgetBase.applyFont(font)
hFont := uintptr(font.handleForDPI(tv.DPI()))
win.SendMessage(tv.hwndFrozenLV, win.WM_SETFONT, hFont, 0)
win.SendMessage(tv.hwndNormalLV, win.WM_SETFONT, hFont, 0)
}
func (tv *TableView) ApplyDPI(dpi int) {
tv.style.dpi = dpi
if tv.style.canvas != nil {
tv.style.canvas.dpix = dpi
tv.style.canvas.dpiy = dpi
}
tv.WidgetBase.ApplyDPI(dpi)
for _, column := range tv.columns.items {
column.update()
}
tv.disposeImageListAndCaches()
if bmp, err := NewBitmap(tv.SizeFrom96DPI(Size{16, 16})); err == nil {
tv.applyImageListForImage(bmp)
bmp.Dispose()
}
}
func (tv *TableView) ApplySysColors() {
tv.WidgetBase.ApplySysColors()
// As some combinations of property and state may be invalid for any theme,
// we set some defaults here.
tv.themeNormalBGColor = Color(win.GetSysColor(win.COLOR_WINDOW))
tv.themeNormalTextColor = Color(win.GetSysColor(win.COLOR_WINDOWTEXT))
tv.themeSelectedBGColor = tv.themeNormalBGColor
tv.themeSelectedTextColor = tv.themeNormalTextColor
tv.themeSelectedNotFocusedBGColor = tv.themeNormalBGColor
tv.alternatingRowBGColor = Color(win.GetSysColor(win.COLOR_BTNFACE))
tv.alternatingRowTextColor = Color(win.GetSysColor(win.COLOR_BTNTEXT))
type item struct {
stateID int32
propertyID int32
color *Color
}
getThemeColor := func(theme win.HTHEME, partId int32, items []item) {
for _, item := range items {
var c win.COLORREF
if result := win.GetThemeColor(theme, partId, item.stateID, item.propertyID, &c); !win.FAILED(result) {
(*item.color) = Color(c)
}
}
}
if hThemeListView := win.OpenThemeData(tv.hwndNormalLV, syscall.StringToUTF16Ptr("Listview")); hThemeListView != 0 {
defer win.CloseThemeData(hThemeListView)
getThemeColor(hThemeListView, win.LVP_LISTITEM, []item{
{win.LISS_NORMAL, win.TMT_FILLCOLOR, &tv.themeNormalBGColor},
{win.LISS_NORMAL, win.TMT_TEXTCOLOR, &tv.themeNormalTextColor},
{win.LISS_SELECTED, win.TMT_FILLCOLOR, &tv.themeSelectedBGColor},
{win.LISS_SELECTED, win.TMT_TEXTCOLOR, &tv.themeSelectedTextColor},
{win.LISS_SELECTEDNOTFOCUS, win.TMT_FILLCOLOR, &tv.themeSelectedNotFocusedBGColor},
})
} else {
// The others already have been retrieved above.
tv.themeSelectedBGColor = Color(win.GetSysColor(win.COLOR_HIGHLIGHT))
tv.themeSelectedTextColor = Color(win.GetSysColor(win.COLOR_HIGHLIGHTTEXT))
tv.themeSelectedNotFocusedBGColor = Color(win.GetSysColor(win.COLOR_BTNFACE))
}
if hThemeButton := win.OpenThemeData(tv.hwndNormalLV, syscall.StringToUTF16Ptr("BUTTON")); hThemeButton != 0 {
defer win.CloseThemeData(hThemeButton)
getThemeColor(hThemeButton, win.BP_PUSHBUTTON, []item{
{win.PBS_NORMAL, win.TMT_FILLCOLOR, &tv.alternatingRowBGColor},
{win.PBS_NORMAL, win.TMT_TEXTCOLOR, &tv.alternatingRowTextColor},
})
}
win.SendMessage(tv.hwndNormalLV, win.LVM_SETBKCOLOR, 0, uintptr(tv.themeNormalBGColor))
win.SendMessage(tv.hwndFrozenLV, win.LVM_SETBKCOLOR, 0, uintptr(tv.themeNormalBGColor))
}
// ColumnsOrderable returns if the user can reorder columns by dragging and
// dropping column headers.
func (tv *TableView) ColumnsOrderable() bool {
exStyle := win.SendMessage(tv.hwndNormalLV, win.LVM_GETEXTENDEDLISTVIEWSTYLE, 0, 0)
return exStyle&win.LVS_EX_HEADERDRAGDROP > 0
}
// SetColumnsOrderable sets if the user can reorder columns by dragging and
// dropping column headers.
func (tv *TableView) SetColumnsOrderable(enabled bool) {
var hwnd win.HWND
if tv.hasFrozenColumn {
hwnd = tv.hwndFrozenLV
} else {
hwnd = tv.hwndNormalLV
}
exStyle := win.SendMessage(hwnd, win.LVM_GETEXTENDEDLISTVIEWSTYLE, 0, 0)
if enabled {
exStyle |= win.LVS_EX_HEADERDRAGDROP
} else {
exStyle &^= win.LVS_EX_HEADERDRAGDROP
}
win.SendMessage(tv.hwndFrozenLV, win.LVM_SETEXTENDEDLISTVIEWSTYLE, 0, exStyle)
win.SendMessage(tv.hwndNormalLV, win.LVM_SETEXTENDEDLISTVIEWSTYLE, 0, exStyle)
tv.columnsOrderableChangedPublisher.Publish()
}
// ColumnsSizable returns if the user can change column widths by dragging
// dividers in the header.
func (tv *TableView) ColumnsSizable() bool {
style := win.GetWindowLong(tv.hwndNormalHdr, win.GWL_STYLE)
return style&win.HDS_NOSIZING == 0
}
// SetColumnsSizable sets if the user can change column widths by dragging
// dividers in the header.
func (tv *TableView) SetColumnsSizable(b bool) error {
updateStyle := func(headerHWnd win.HWND) error {
style := win.GetWindowLong(headerHWnd, win.GWL_STYLE)
if b {
style &^= win.HDS_NOSIZING
} else {
style |= win.HDS_NOSIZING
}
if 0 == win.SetWindowLong(headerHWnd, win.GWL_STYLE, style) {
return lastError("SetWindowLong(GWL_STYLE)")
}
return nil
}
if err := updateStyle(tv.hwndFrozenHdr); err != nil {
return err
}
if err := updateStyle(tv.hwndNormalHdr); err != nil {
return err
}
tv.columnsSizableChangedPublisher.Publish()
return nil
}
// ContextMenuLocation returns selected item position in screen coordinates.
func (tv *TableView) ContextMenuLocation() Point {
idx := win.SendMessage(tv.hwndNormalLV, win.LVM_GETSELECTIONMARK, 0, 0)
rc := win.RECT{Left: win.LVIR_BOUNDS}
if 0 == win.SendMessage(tv.hwndNormalLV, win.LVM_GETITEMRECT, idx, uintptr(unsafe.Pointer(&rc))) {
return tv.WidgetBase.ContextMenuLocation()
}
var pt win.POINT
if tv.RightToLeftReading() {
pt.X = rc.Right
} else {
pt.X = rc.Left
}
pt.X = rc.Bottom
windowTrimToClientBounds(tv.hwndNormalLV, &pt)
win.ClientToScreen(tv.hwndNormalLV, &pt)
return Point{int(pt.X), int(pt.Y)}
}
// SortableByHeaderClick returns if the user can change sorting by clicking the header.
func (tv *TableView) SortableByHeaderClick() bool {
return !hasWindowLongBits(tv.hwndFrozenLV, win.GWL_STYLE, win.LVS_NOSORTHEADER) ||
!hasWindowLongBits(tv.hwndNormalLV, win.GWL_STYLE, win.LVS_NOSORTHEADER)
}
// HeaderHidden returns whether the column header is hidden.
func (tv *TableView) HeaderHidden() bool {
style := win.GetWindowLong(tv.hwndNormalLV, win.GWL_STYLE)
return style&win.LVS_NOCOLUMNHEADER != 0
}
// SetHeaderHidden sets whether the column header is hidden.
func (tv *TableView) SetHeaderHidden(hidden bool) error {
updateStyle := func(hwnd win.HWND) error {
style := win.GetWindowLong(hwnd, win.GWL_STYLE)
if hidden {
style |= win.LVS_NOCOLUMNHEADER
} else {
style &^= win.LVS_NOCOLUMNHEADER
}
if 0 == win.SetWindowLong(hwnd, win.GWL_STYLE, style) {
return lastError("SetWindowLong(GWL_STYLE)")
}
return nil
}
if err := updateStyle(tv.hwndFrozenLV); err != nil {
return err
}
return updateStyle(tv.hwndNormalLV)
}
// AlternatingRowBG returns the alternating row background.
func (tv *TableView) AlternatingRowBG() bool {
return tv.alternatingRowBG
}
// SetAlternatingRowBG sets the alternating row background.
func (tv *TableView) SetAlternatingRowBG(enabled bool) {
tv.alternatingRowBG = enabled
tv.Invalidate()
}
// Gridlines returns if the rows are separated by grid lines.
func (tv *TableView) Gridlines() bool {
exStyle := win.SendMessage(tv.hwndNormalLV, win.LVM_GETEXTENDEDLISTVIEWSTYLE, 0, 0)
return exStyle&win.LVS_EX_GRIDLINES > 0
}
// SetGridlines sets if the rows are separated by grid lines.
func (tv *TableView) SetGridlines(enabled bool) {
var hwnd win.HWND
if tv.hasFrozenColumn {
hwnd = tv.hwndFrozenLV
} else {
hwnd = tv.hwndNormalLV
}
exStyle := win.SendMessage(hwnd, win.LVM_GETEXTENDEDLISTVIEWSTYLE, 0, 0)
if enabled {
exStyle |= win.LVS_EX_GRIDLINES
} else {
exStyle &^= win.LVS_EX_GRIDLINES
}
win.SendMessage(tv.hwndFrozenLV, win.LVM_SETEXTENDEDLISTVIEWSTYLE, 0, exStyle)
win.SendMessage(tv.hwndNormalLV, win.LVM_SETEXTENDEDLISTVIEWSTYLE, 0, exStyle)
}
// Columns returns the list of columns.
func (tv *TableView) Columns() *TableViewColumnList {
return tv.columns
}
// VisibleColumnsInDisplayOrder returns a slice of visible columns in display
// order.
func (tv *TableView) VisibleColumnsInDisplayOrder() []*TableViewColumn {
visibleCols := tv.visibleColumns()
indices := make([]int32, len(visibleCols))
frozenCount := tv.visibleFrozenColumnCount()
normalCount := len(visibleCols) - frozenCount
if frozenCount > 0 {
if win.FALSE == win.SendMessage(tv.hwndFrozenLV, win.LVM_GETCOLUMNORDERARRAY, uintptr(frozenCount), uintptr(unsafe.Pointer(&indices[0]))) {
newError("LVM_GETCOLUMNORDERARRAY")
return nil
}
}
if normalCount > 0 {
if win.FALSE == win.SendMessage(tv.hwndNormalLV, win.LVM_GETCOLUMNORDERARRAY, uintptr(normalCount), uintptr(unsafe.Pointer(&indices[frozenCount]))) {
newError("LVM_GETCOLUMNORDERARRAY")
return nil
}
}
orderedCols := make([]*TableViewColumn, len(visibleCols))
for i, j := range indices {
if i >= frozenCount {
j += int32(frozenCount)
}
orderedCols[i] = visibleCols[j]
}
return orderedCols
}
// RowsPerPage returns the number of fully visible rows.
func (tv *TableView) RowsPerPage() int {
return int(win.SendMessage(tv.hwndNormalLV, win.LVM_GETCOUNTPERPAGE, 0, 0))
}
func (tv *TableView) Invalidate() error {
win.InvalidateRect(tv.hwndFrozenLV, nil, true)
win.InvalidateRect(tv.hwndNormalLV, nil, true)
return tv.WidgetBase.Invalidate()
}
// UpdateItem ensures the item at index will be redrawn.
//
// If the model supports sorting, it will be resorted.
func (tv *TableView) UpdateItem(index int) error {
if s, ok := tv.model.(Sorter); ok {
if err := s.Sort(s.SortedColumn(), s.SortOrder()); err != nil {
return err
}
return tv.Invalidate()
} else {
if win.FALSE == win.SendMessage(tv.hwndFrozenLV, win.LVM_UPDATE, uintptr(index), 0) {
return newError("LVM_UPDATE")
}
if win.FALSE == win.SendMessage(tv.hwndNormalLV, win.LVM_UPDATE, uintptr(index), 0) {
return newError("LVM_UPDATE")
}
}
return nil
}
func (tv *TableView) attachModel() {
tv.rowsResetHandlerHandle = tv.model.RowsReset().Attach(func() {
tv.setItemCount()
tv.SetCurrentIndex(-1)
tv.itemCountChangedPublisher.Publish()
})
tv.rowChangedHandlerHandle = tv.model.RowChanged().Attach(func(row int) {
tv.UpdateItem(row)
})
tv.rowsInsertedHandlerHandle = tv.model.RowsInserted().Attach(func(from, to int) {
i := tv.currentIndex
tv.setItemCount()
if from <= i {
i += 1 + to - from
tv.SetCurrentIndex(i)
}
tv.itemCountChangedPublisher.Publish()
})
tv.rowsRemovedHandlerHandle = tv.model.RowsRemoved().Attach(func(from, to int) {
i := tv.currentIndex
tv.setItemCount()
index := i
if from <= i && i <= to {
index = -1
} else if from < i {
index -= 1 + to - from
}
if index != i {
tv.SetCurrentIndex(index)
}
tv.itemCountChangedPublisher.Publish()
})
if sorter, ok := tv.model.(Sorter); ok {
tv.sortChangedHandlerHandle = sorter.SortChanged().Attach(func() {
col := sorter.SortedColumn()
tv.setSortIcon(col, sorter.SortOrder())
tv.Invalidate()
})
}
}
func (tv *TableView) detachModel() {
tv.model.RowsReset().Detach(tv.rowsResetHandlerHandle)
tv.model.RowChanged().Detach(tv.rowChangedHandlerHandle)
tv.model.RowsInserted().Detach(tv.rowsInsertedHandlerHandle)
tv.model.RowsRemoved().Detach(tv.rowsRemovedHandlerHandle)
if sorter, ok := tv.model.(Sorter); ok {
sorter.SortChanged().Detach(tv.sortChangedHandlerHandle)
}
}
// ItemCountChanged returns the event that is published when the number of items
// in the model of the TableView changed.
func (tv *TableView) ItemCountChanged() *Event {
return tv.itemCountChangedPublisher.Event()
}
// Model returns the model of the TableView.
func (tv *TableView) Model() interface{} {
return tv.providedModel
}
// SetModel sets the model of the TableView.
//
// It is required that mdl either implements walk.TableModel,
// walk.ReflectTableModel or be a slice of pointers to struct or a
// []map[string]interface{}. A walk.TableModel implementation must also
// implement walk.Sorter to support sorting, all other options get sorting for
// free. To support item check boxes and icons, mdl must implement
// walk.ItemChecker and walk.ImageProvider, respectively. On-demand model
// population for a walk.ReflectTableModel or slice requires mdl to implement
// walk.Populator.
func (tv *TableView) SetModel(mdl interface{}) error {
model, ok := mdl.(TableModel)
if !ok && mdl != nil {
var err error
if model, err = newReflectTableModel(mdl); err != nil {
if model, err = newMapTableModel(mdl); err != nil {
return err
}
}
}
tv.SetSuspended(true)
defer tv.SetSuspended(false)
if tv.model != nil {
tv.detachModel()
tv.disposeImageListAndCaches()
}
oldProvidedModelStyler, _ := tv.providedModel.(CellStyler)
if styler, ok := mdl.(CellStyler); ok || tv.styler == oldProvidedModelStyler {
tv.styler = styler
}
tv.providedModel = mdl
tv.model = model
tv.itemChecker, _ = model.(ItemChecker)
tv.imageProvider, _ = model.(ImageProvider)
if model != nil {
tv.attachModel()
if dms, ok := model.(dataMembersSetter); ok {
// FIXME: This depends on columns to be initialized before
// calling this method.
dataMembers := make([]string, len(tv.columns.items))
for i, col := range tv.columns.items {
dataMembers[i] = col.DataMemberEffective()
}
dms.setDataMembers(dataMembers)
}
if lfs, ok := model.(lessFuncsSetter); ok {
lessFuncs := make([]func(i, j int) bool, tv.columns.Len())
for i, c := range tv.columns.items {
lessFuncs[i] = c.lessFunc
}
lfs.setLessFuncs(lessFuncs)
}
if sorter, ok := tv.model.(Sorter); ok {
if tv.sortedColumnIndex >= tv.visibleColumnCount() {
tv.sortedColumnIndex = maxi(-1, mini(0, tv.visibleColumnCount()-1))
tv.sortOrder = SortAscending
}
sorter.Sort(tv.sortedColumnIndex, tv.sortOrder)
}
}
tv.SetCurrentIndex(-1)
tv.setItemCount()
tv.itemCountChangedPublisher.Publish()
return nil
}
// TableModel returns the TableModel of the TableView.
func (tv *TableView) TableModel() TableModel {
return tv.model
}
// ItemChecker returns the ItemChecker of the TableView.
func (tv *TableView) ItemChecker() ItemChecker {
return tv.itemChecker
}
// SetItemChecker sets the ItemChecker of the TableView.
func (tv *TableView) SetItemChecker(itemChecker ItemChecker) {
tv.itemChecker = itemChecker
}
// CellStyler returns the CellStyler of the TableView.
func (tv *TableView) CellStyler() CellStyler {
return tv.styler
}
// SetCellStyler sets the CellStyler of the TableView.
func (tv *TableView) SetCellStyler(styler CellStyler) {
tv.styler = styler
}
func (tv *TableView) setItemCount() error {
var count int
if tv.model != nil {
count = tv.model.RowCount()
}
if 0 == win.SendMessage(tv.hwndFrozenLV, win.LVM_SETITEMCOUNT, uintptr(count), win.LVSICF_NOSCROLL) {
return newError("SendMessage(LVM_SETITEMCOUNT)")
}
if 0 == win.SendMessage(tv.hwndNormalLV, win.LVM_SETITEMCOUNT, uintptr(count), win.LVSICF_NOSCROLL) {
return newError("SendMessage(LVM_SETITEMCOUNT)")
}
return nil
}
// CheckBoxes returns if the *TableView has check boxes.
func (tv *TableView) CheckBoxes() bool {
var hwnd win.HWND
if tv.hasFrozenColumn {
hwnd = tv.hwndFrozenLV
} else {
hwnd = tv.hwndNormalLV
}
return win.SendMessage(hwnd, win.LVM_GETEXTENDEDLISTVIEWSTYLE, 0, 0)&win.LVS_EX_CHECKBOXES > 0
}
// SetCheckBoxes sets if the *TableView has check boxes.
func (tv *TableView) SetCheckBoxes(checkBoxes bool) {
var hwnd, hwndOther win.HWND
if tv.hasFrozenColumn {
hwnd, hwndOther = tv.hwndFrozenLV, tv.hwndNormalLV
} else {
hwnd, hwndOther = tv.hwndNormalLV, tv.hwndFrozenLV
}
exStyle := win.SendMessage(hwnd, win.LVM_GETEXTENDEDLISTVIEWSTYLE, 0, 0)
oldStyle := exStyle
if checkBoxes {
exStyle |= win.LVS_EX_CHECKBOXES
} else {
exStyle &^= win.LVS_EX_CHECKBOXES
}
if exStyle != oldStyle {
win.SendMessage(hwnd, win.LVM_SETEXTENDEDLISTVIEWSTYLE, 0, exStyle)
}
win.SendMessage(hwndOther, win.LVM_SETEXTENDEDLISTVIEWSTYLE, 0, exStyle&^win.LVS_EX_CHECKBOXES)
mask := win.SendMessage(hwnd, win.LVM_GETCALLBACKMASK, 0, 0)
if checkBoxes {
mask |= win.LVIS_STATEIMAGEMASK
} else {
mask &^= win.LVIS_STATEIMAGEMASK
}
if win.FALSE == win.SendMessage(hwnd, win.LVM_SETCALLBACKMASK, mask, 0) {
newError("SendMessage(LVM_SETCALLBACKMASK)")
}
}
func (tv *TableView) fromLVColIdx(frozen bool, index int32) int {
var idx int32
for i, tvc := range tv.columns.items {
if frozen == tvc.frozen && tvc.visible {
if idx == index {
return i
}
idx++
}
}
return -1
}
func (tv *TableView) toLVColIdx(index int) int32 {
var idx int32
for i, tvc := range tv.columns.items {
if tvc.visible {
if i == index {
return idx
}
idx++
}
}
return -1
}
func (tv *TableView) visibleFrozenColumnCount() int {
var count int
for _, tvc := range tv.columns.items {
if tvc.frozen && tvc.visible {
count++
}
}
return count
}
func (tv *TableView) visibleColumnCount() int {
var count int
for _, tvc := range tv.columns.items {
if tvc.visible {
count++
}
}
return count
}
func (tv *TableView) visibleColumns() []*TableViewColumn {