-
Notifications
You must be signed in to change notification settings - Fork 30
/
listview.go
547 lines (455 loc) · 14.3 KB
/
listview.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
/*
* Copyright (C) 2019 The Winc Authors. All Rights Reserved.
*/
package winc
import (
"errors"
"fmt"
"syscall"
"unsafe"
"github.com/tadvi/winc/w32"
)
// ListItem represents an item in a ListView widget.
type ListItem interface {
Text() []string // Text returns the text of the multi-column item.
ImageIndex() int // ImageIndex is used only if SetImageList is called on the listview
}
// ListItemChecker is used for checkbox support in ListView.
type ListItemChecker interface {
Checked() bool
SetChecked(checked bool)
}
// ListItemSetter is used in OnEndLabelEdit event.
type ListItemSetter interface {
SetText(s string) // set first item in the array via LabelEdit event
}
// StringListItem is helper for basic string lists.
type StringListItem struct {
ID int
Data string
Check bool
}
func (s StringListItem) Text() []string { return []string{s.Data} }
func (s StringListItem) Checked() bool { return s.Check }
func (s StringListItem) SetChecked(checked bool) { s.Check = checked }
func (s StringListItem) ImageIndex() int { return 0 }
type ListView struct {
ControlBase
iml *ImageList
lastIndex int
cols int // count of columns
item2Handle map[ListItem]uintptr
handle2Item map[uintptr]ListItem
onEndLabelEdit EventManager
onDoubleClick EventManager
onClick EventManager
onKeyDown EventManager
onItemChanging EventManager
onItemChanged EventManager
onCheckChanged EventManager
onViewChange EventManager
onEndScroll EventManager
}
func NewListView(parent Controller) *ListView {
lv := new(ListView)
lv.InitControl("SysListView32", parent /*w32.WS_EX_CLIENTEDGE*/, 0,
w32.WS_CHILD|w32.WS_VISIBLE|w32.WS_TABSTOP|w32.LVS_REPORT|w32.LVS_EDITLABELS|w32.LVS_SHOWSELALWAYS)
lv.item2Handle = make(map[ListItem]uintptr)
lv.handle2Item = make(map[uintptr]ListItem)
RegMsgHandler(lv)
lv.SetFont(DefaultFont)
lv.SetSize(200, 400)
if err := lv.SetTheme("Explorer"); err != nil {
// theme error is ignored
}
return lv
}
// FIXME: Changes the state of an item in a list-view control. Refer LVM_SETITEMSTATE message.
func (lv *ListView) setItemState(i int, state, mask uint) {
var item w32.LVITEM
item.State, item.StateMask = uint32(state), uint32(mask)
w32.SendMessage(lv.hwnd, w32.LVM_SETITEMSTATE, uintptr(i), uintptr(unsafe.Pointer(&item)))
}
func (lv *ListView) EnableSingleSelect(enable bool) {
ToggleStyle(lv.hwnd, enable, w32.LVS_SINGLESEL)
}
func (lv *ListView) EnableSortHeader(enable bool) {
ToggleStyle(lv.hwnd, enable, w32.LVS_NOSORTHEADER)
}
func (lv *ListView) EnableSortAscending(enable bool) {
ToggleStyle(lv.hwnd, enable, w32.LVS_SORTASCENDING)
}
func (lv *ListView) EnableEditLabels(enable bool) {
ToggleStyle(lv.hwnd, enable, w32.LVS_EDITLABELS)
}
func (lv *ListView) EnableFullRowSelect(enable bool) {
if enable {
w32.SendMessage(lv.hwnd, w32.LVM_SETEXTENDEDLISTVIEWSTYLE, 0, w32.LVS_EX_FULLROWSELECT)
} else {
w32.SendMessage(lv.hwnd, w32.LVM_SETEXTENDEDLISTVIEWSTYLE, w32.LVS_EX_FULLROWSELECT, 0)
}
}
func (lv *ListView) EnableDoubleBuffer(enable bool) {
if enable {
w32.SendMessage(lv.hwnd, w32.LVM_SETEXTENDEDLISTVIEWSTYLE, 0, w32.LVS_EX_DOUBLEBUFFER)
} else {
w32.SendMessage(lv.hwnd, w32.LVM_SETEXTENDEDLISTVIEWSTYLE, w32.LVS_EX_DOUBLEBUFFER, 0)
}
}
func (lv *ListView) EnableHotTrack(enable bool) {
if enable {
w32.SendMessage(lv.hwnd, w32.LVM_SETEXTENDEDLISTVIEWSTYLE, 0, w32.LVS_EX_TRACKSELECT)
} else {
w32.SendMessage(lv.hwnd, w32.LVM_SETEXTENDEDLISTVIEWSTYLE, w32.LVS_EX_TRACKSELECT, 0)
}
}
func (lv *ListView) SetItemCount(count int) bool {
return w32.SendMessage(lv.hwnd, w32.LVM_SETITEMCOUNT, uintptr(count), 0) != 0
}
func (lv *ListView) ItemCount() int {
return int(w32.SendMessage(lv.hwnd, w32.LVM_GETITEMCOUNT, 0, 0))
}
func (lv *ListView) ItemAt(x, y int) ListItem {
hti := w32.LVHITTESTINFO{Pt: w32.POINT{int32(x), int32(y)}}
w32.SendMessage(lv.hwnd, w32.LVM_HITTEST, 0, uintptr(unsafe.Pointer(&hti)))
return lv.findItemByIndex(int(hti.IItem))
}
func (lv *ListView) Items() (list []ListItem) {
for item := range lv.item2Handle {
list = append(list, item)
}
return list
}
func (lv *ListView) AddColumn(caption string, width int) {
var lc w32.LVCOLUMN
lc.Mask = w32.LVCF_TEXT
if width != 0 {
lc.Mask = lc.Mask | w32.LVCF_WIDTH
lc.Cx = int32(width)
}
lc.PszText = syscall.StringToUTF16Ptr(caption)
lv.insertLvColumn(&lc, lv.cols)
lv.cols++
}
// StretchLastColumn makes the last column take up all remaining horizontal
// space of the *ListView.
// The effect of this is not persistent.
func (lv *ListView) StretchLastColumn() error {
if lv.cols == 0 {
return nil
}
if w32.SendMessage(lv.hwnd, w32.LVM_SETCOLUMNWIDTH, uintptr(lv.cols-1), w32.LVSCW_AUTOSIZE_USEHEADER) == 0 {
//panic("LVM_SETCOLUMNWIDTH failed")
}
return nil
}
// CheckBoxes returns if the *TableView has check boxes.
func (lv *ListView) CheckBoxes() bool {
return w32.SendMessage(lv.hwnd, w32.LVM_GETEXTENDEDLISTVIEWSTYLE, 0, 0)&w32.LVS_EX_CHECKBOXES > 0
}
// SetCheckBoxes sets if the *TableView has check boxes.
func (lv *ListView) SetCheckBoxes(value bool) {
exStyle := w32.SendMessage(lv.hwnd, w32.LVM_GETEXTENDEDLISTVIEWSTYLE, 0, 0)
oldStyle := exStyle
if value {
exStyle |= w32.LVS_EX_CHECKBOXES
} else {
exStyle &^= w32.LVS_EX_CHECKBOXES
}
if exStyle != oldStyle {
w32.SendMessage(lv.hwnd, w32.LVM_SETEXTENDEDLISTVIEWSTYLE, 0, exStyle)
}
mask := w32.SendMessage(lv.hwnd, w32.LVM_GETCALLBACKMASK, 0, 0)
if value {
mask |= w32.LVIS_STATEIMAGEMASK
} else {
mask &^= w32.LVIS_STATEIMAGEMASK
}
if w32.SendMessage(lv.hwnd, w32.LVM_SETCALLBACKMASK, mask, 0) == w32.FALSE {
panic("SendMessage(LVM_SETCALLBACKMASK)")
}
}
func (lv *ListView) applyImage(lc *w32.LVITEM, imIndex int) {
if lv.iml != nil {
lc.Mask |= w32.LVIF_IMAGE
lc.IImage = int32(imIndex)
}
}
func (lv *ListView) AddItem(item ListItem) {
lv.InsertItem(item, lv.ItemCount())
}
func (lv *ListView) InsertItem(item ListItem, index int) {
text := item.Text()
li := &w32.LVITEM{
Mask: w32.LVIF_TEXT | w32.LVIF_PARAM,
PszText: syscall.StringToUTF16Ptr(text[0]),
IItem: int32(index),
}
lv.lastIndex++
ix := new(int)
*ix = lv.lastIndex
li.LParam = uintptr(*ix)
lv.handle2Item[li.LParam] = item
lv.item2Handle[item] = li.LParam
lv.applyImage(li, item.ImageIndex())
lv.insertLvItem(li)
for i := 1; i < len(text); i++ {
li.Mask = w32.LVIF_TEXT
li.PszText = syscall.StringToUTF16Ptr(text[i])
li.ISubItem = int32(i)
lv.setLvItem(li)
}
}
func (lv *ListView) UpdateItem(item ListItem) bool {
lparam, ok := lv.item2Handle[item]
if !ok {
return false
}
index := lv.findIndexByItem(item)
if index == -1 {
return false
}
text := item.Text()
li := &w32.LVITEM{
Mask: w32.LVIF_TEXT | w32.LVIF_PARAM,
PszText: syscall.StringToUTF16Ptr(text[0]),
LParam: lparam,
IItem: int32(index),
}
lv.applyImage(li, item.ImageIndex())
lv.setLvItem(li)
for i := 1; i < len(text); i++ {
li.Mask = w32.LVIF_TEXT
li.PszText = syscall.StringToUTF16Ptr(text[i])
li.ISubItem = int32(i)
lv.setLvItem(li)
}
return true
}
func (lv *ListView) insertLvColumn(lvColumn *w32.LVCOLUMN, iCol int) {
w32.SendMessage(lv.hwnd, w32.LVM_INSERTCOLUMN, uintptr(iCol), uintptr(unsafe.Pointer(lvColumn)))
}
func (lv *ListView) insertLvItem(lvItem *w32.LVITEM) {
w32.SendMessage(lv.hwnd, w32.LVM_INSERTITEM, 0, uintptr(unsafe.Pointer(lvItem)))
}
func (lv *ListView) setLvItem(lvItem *w32.LVITEM) {
w32.SendMessage(lv.hwnd, w32.LVM_SETITEM, 0, uintptr(unsafe.Pointer(lvItem)))
}
func (lv *ListView) DeleteAllItems() bool {
if w32.SendMessage(lv.hwnd, w32.LVM_DELETEALLITEMS, 0, 0) == w32.TRUE {
lv.item2Handle = make(map[ListItem]uintptr)
lv.handle2Item = make(map[uintptr]ListItem)
return true
}
return false
}
func (lv *ListView) DeleteItem(item ListItem) error {
index := lv.findIndexByItem(item)
if index == -1 {
return errors.New("item not found")
}
if w32.SendMessage(lv.hwnd, w32.LVM_DELETEITEM, uintptr(index), 0) == 0 {
return errors.New("SendMessage(TVM_DELETEITEM) failed")
}
h := lv.item2Handle[item]
delete(lv.item2Handle, item)
delete(lv.handle2Item, h)
return nil
}
func (lv *ListView) findIndexByItem(item ListItem) int {
lparam, ok := lv.item2Handle[item]
if !ok {
return -1
}
it := &w32.LVFINDINFO{
Flags: w32.LVFI_PARAM,
LParam: lparam,
}
var i int = -1
return int(w32.SendMessage(lv.hwnd, w32.LVM_FINDITEM, uintptr(i), uintptr(unsafe.Pointer(it))))
}
func (lv *ListView) findItemByIndex(i int) ListItem {
it := &w32.LVITEM{
Mask: w32.LVIF_PARAM,
IItem: int32(i),
}
if w32.SendMessage(lv.hwnd, w32.LVM_GETITEM, 0, uintptr(unsafe.Pointer(it))) == w32.TRUE {
if item, ok := lv.handle2Item[it.LParam]; ok {
return item
}
}
return nil
}
func (lv *ListView) EnsureVisible(item ListItem) bool {
if i := lv.findIndexByItem(item); i != -1 {
return w32.SendMessage(lv.hwnd, w32.LVM_ENSUREVISIBLE, uintptr(i), 1) == 0
}
return false
}
func (lv *ListView) SelectedItem() ListItem {
if items := lv.SelectedItems(); len(items) > 0 {
return items[0]
}
return nil
}
func (lv *ListView) SetSelectedItem(item ListItem) bool {
if i := lv.findIndexByItem(item); i > -1 {
lv.SetSelectedIndex(i)
return true
}
return false
}
// mask is used to set the LVITEM.Mask for ListView.GetItem which indicates which attributes you'd like to receive
// of LVITEM.
func (lv *ListView) SelectedItems() []ListItem {
var items []ListItem
var i int = -1
for {
if i = int(w32.SendMessage(lv.hwnd, w32.LVM_GETNEXTITEM, uintptr(i), uintptr(w32.LVNI_SELECTED))); i == -1 {
break
}
if item := lv.findItemByIndex(i); item != nil {
items = append(items, item)
}
}
return items
}
func (lv *ListView) SelectedCount() uint {
return uint(w32.SendMessage(lv.hwnd, w32.LVM_GETSELECTEDCOUNT, 0, 0))
}
// GetSelectedIndex first selected item index. Returns -1 if no item is selected.
func (lv *ListView) SelectedIndex() int {
var i int = -1
return int(w32.SendMessage(lv.hwnd, w32.LVM_GETNEXTITEM, uintptr(i), uintptr(w32.LVNI_SELECTED)))
}
// Set i to -1 to select all items.
func (lv *ListView) SetSelectedIndex(i int) {
lv.setItemState(i, w32.LVIS_SELECTED, w32.LVIS_SELECTED)
}
func (lv *ListView) SetImageList(imageList *ImageList) {
w32.SendMessage(lv.hwnd, w32.LVM_SETIMAGELIST, w32.LVSIL_SMALL, uintptr(imageList.Handle()))
lv.iml = imageList
}
// Event publishers
func (lv *ListView) OnEndLabelEdit() *EventManager {
return &lv.onEndLabelEdit
}
func (lv *ListView) OnDoubleClick() *EventManager {
return &lv.onDoubleClick
}
func (lv *ListView) OnClick() *EventManager {
return &lv.onClick
}
func (lv *ListView) OnKeyDown() *EventManager {
return &lv.onKeyDown
}
func (lv *ListView) OnItemChanging() *EventManager {
return &lv.onItemChanging
}
func (lv *ListView) OnItemChanged() *EventManager {
return &lv.onItemChanged
}
func (lv *ListView) OnCheckChanged() *EventManager {
return &lv.onCheckChanged
}
func (lv *ListView) OnViewChange() *EventManager {
return &lv.onViewChange
}
func (lv *ListView) OnEndScroll() *EventManager {
return &lv.onEndScroll
}
// Message processer
func (lv *ListView) WndProc(msg uint32, wparam, lparam uintptr) uintptr {
switch msg {
/*case w32.WM_ERASEBKGND:
lv.StretchLastColumn()
println("case w32.WM_ERASEBKGND")
return 1*/
case w32.WM_NOTIFY:
nm := (*w32.NMHDR)(unsafe.Pointer(lparam))
code := int32(nm.Code)
switch code {
case w32.LVN_BEGINLABELEDITW:
// println("Begin label edit")
case w32.LVN_ENDLABELEDITW:
nmdi := (*w32.NMLVDISPINFO)(unsafe.Pointer(lparam))
if nmdi.Item.PszText != nil {
fmt.Println(nmdi.Item.PszText, nmdi.Item)
if item, ok := lv.handle2Item[nmdi.Item.LParam]; ok {
lv.onEndLabelEdit.Fire(NewEvent(lv,
&LabelEditEventData{Item: item,
Text: w32.UTF16PtrToString(nmdi.Item.PszText)}))
}
return w32.TRUE
}
case w32.NM_DBLCLK:
lv.onDoubleClick.Fire(NewEvent(lv, nil))
case w32.NM_CLICK:
ac := (*w32.NMITEMACTIVATE)(unsafe.Pointer(lparam))
var hti w32.LVHITTESTINFO
hti.Pt = w32.POINT{ac.PtAction.X, ac.PtAction.Y}
w32.SendMessage(lv.hwnd, w32.LVM_HITTEST, 0, uintptr(unsafe.Pointer(&hti)))
if hti.Flags == w32.LVHT_ONITEMSTATEICON {
if item := lv.findItemByIndex(int(hti.IItem)); item != nil {
if item, ok := item.(ListItemChecker); ok {
checked := !item.Checked()
item.SetChecked(checked)
lv.onCheckChanged.Fire(NewEvent(lv, item))
if w32.SendMessage(lv.hwnd, w32.LVM_UPDATE, uintptr(hti.IItem), 0) == w32.FALSE {
panic("SendMessage(LVM_UPDATE)")
}
}
}
}
hti.Pt = w32.POINT{ac.PtAction.X, ac.PtAction.Y}
w32.SendMessage(lv.hwnd, w32.LVM_SUBITEMHITTEST, 0, uintptr(unsafe.Pointer(&hti)))
lv.onClick.Fire(NewEvent(lv, hti.ISubItem))
case w32.LVN_KEYDOWN:
nmkey := (*w32.NMLVKEYDOWN)(unsafe.Pointer(lparam))
if nmkey.WVKey == w32.VK_SPACE && lv.CheckBoxes() {
if item := lv.SelectedItem(); item != nil {
if item, ok := item.(ListItemChecker); ok {
checked := !item.Checked()
item.SetChecked(checked)
lv.onCheckChanged.Fire(NewEvent(lv, item))
}
index := lv.findIndexByItem(item)
if w32.SendMessage(lv.hwnd, w32.LVM_UPDATE, uintptr(index), 0) == w32.FALSE {
panic("SendMessage(LVM_UPDATE)")
}
}
}
lv.onKeyDown.Fire(NewEvent(lv, nmkey.WVKey))
key := nmkey.WVKey
w32.SendMessage(lv.Parent().Handle(), w32.WM_KEYDOWN, uintptr(key), 0)
case w32.LVN_ITEMCHANGING:
// This event also fires when listview has changed via code.
nmlv := (*w32.NMLISTVIEW)(unsafe.Pointer(lparam))
item := lv.findItemByIndex(int(nmlv.IItem))
lv.onItemChanging.Fire(NewEvent(lv, item))
case w32.LVN_ITEMCHANGED:
// This event also fires when listview has changed via code.
nmlv := (*w32.NMLISTVIEW)(unsafe.Pointer(lparam))
item := lv.findItemByIndex(int(nmlv.IItem))
lv.onItemChanged.Fire(NewEvent(lv, item))
case w32.LVN_GETDISPINFO:
nmdi := (*w32.NMLVDISPINFO)(unsafe.Pointer(lparam))
if nmdi.Item.StateMask&w32.LVIS_STATEIMAGEMASK > 0 {
if item, ok := lv.handle2Item[nmdi.Item.LParam]; ok {
if item, ok := item.(ListItemChecker); ok {
checked := item.Checked()
if checked {
nmdi.Item.State = 0x2000
} else {
nmdi.Item.State = 0x1000
}
}
}
}
lv.onViewChange.Fire(NewEvent(lv, nil))
case w32.LVN_ENDSCROLL:
lv.onEndScroll.Fire(NewEvent(lv, nil))
}
}
return w32.DefWindowProc(lv.hwnd, msg, wparam, lparam)
}