|
| 1 | +package wingui |
| 2 | + |
| 3 | +import ( |
| 4 | + "syscall" |
| 5 | + "unsafe" |
| 6 | + |
| 7 | + "github.com/lxn/win" |
| 8 | + "github.com/whtiehack/wingui/winapi" |
| 9 | +) |
| 10 | + |
| 11 | +// ListView is a wrapper for the Win32 SysListView32 control. |
| 12 | +// Notes: |
| 13 | +// - Most notifications are delivered as WM_NOTIFY to the parent dialog (routed by Dialog.dialogWndProc). |
| 14 | +// - For report view with columns, prefer creating the control with LVS_REPORT style in resources. |
| 15 | +type ListView struct { |
| 16 | + WindowBase |
| 17 | + |
| 18 | + // OnItemActivate fires on item activation (double click / Enter), if available. |
| 19 | + OnItemActivate func(index int) |
| 20 | + // OnItemChanged fires on LVN_ITEMCHANGED. |
| 21 | + OnItemChanged func(index int) |
| 22 | +} |
| 23 | + |
| 24 | +func (lv *ListView) WndProc(msg uint32, wParam, lParam uintptr) uintptr { |
| 25 | + switch msg { |
| 26 | + case win.WM_NOTIFY: |
| 27 | + nmhdr := (*win.NMHDR)(unsafe.Pointer(lParam)) |
| 28 | + code := uint32(nmhdr.Code) |
| 29 | + |
| 30 | + switch code { |
| 31 | + case win.LVN_ITEMCHANGED: |
| 32 | + if lv.OnItemChanged != nil { |
| 33 | + nmlv := (*win.NMLISTVIEW)(unsafe.Pointer(lParam)) |
| 34 | + lv.OnItemChanged(int(nmlv.IItem)) |
| 35 | + } |
| 36 | + return 0 |
| 37 | + case win.NM_DBLCLK: |
| 38 | + if lv.OnItemActivate != nil { |
| 39 | + nmia := (*win.NMITEMACTIVATE)(unsafe.Pointer(lParam)) |
| 40 | + lv.OnItemActivate(int(nmia.IItem)) |
| 41 | + } |
| 42 | + return 0 |
| 43 | + } |
| 44 | + return 0 |
| 45 | + } |
| 46 | + return lv.AsWindowBase().WndProc(msg, wParam, lParam) |
| 47 | +} |
| 48 | + |
| 49 | +// SetExtendedStyle sets extended list-view styles (e.g. LVS_EX_FULLROWSELECT). |
| 50 | +func (lv *ListView) SetExtendedStyle(style uint32) { |
| 51 | + lv.SendMessage(winapi.LVM_SETEXTENDEDLISTVIEWSTYLE, 0, uintptr(style)) |
| 52 | +} |
| 53 | + |
| 54 | +// InsertColumn inserts a column at index and returns the actual inserted index (or -1). |
| 55 | +func (lv *ListView) InsertColumn(index int, text string, width int, fmt int32) int { |
| 56 | + utf16 := syscall.StringToUTF16(text) |
| 57 | + col := win.LVCOLUMN{ |
| 58 | + Mask: win.LVCF_TEXT | win.LVCF_WIDTH | win.LVCF_FMT | win.LVCF_SUBITEM, |
| 59 | + Fmt: fmt, |
| 60 | + Cx: int32(width), |
| 61 | + PszText: &utf16[0], |
| 62 | + ISubItem: int32(index), |
| 63 | + } |
| 64 | + ret := lv.SendMessage(winapi.LVM_INSERTCOLUMNW, uintptr(index), uintptr(unsafe.Pointer(&col))) |
| 65 | + return int(int32(ret)) |
| 66 | +} |
| 67 | + |
| 68 | +// InsertItem inserts a new item at index (or appends if index < 0) and returns its index (or -1). |
| 69 | +func (lv *ListView) InsertItem(index int, text string) int { |
| 70 | + if index < 0 { |
| 71 | + index = lv.ItemCount() |
| 72 | + } |
| 73 | + utf16 := syscall.StringToUTF16(text) |
| 74 | + item := win.LVITEM{ |
| 75 | + Mask: win.LVIF_TEXT, |
| 76 | + IItem: int32(index), |
| 77 | + ISubItem: 0, |
| 78 | + PszText: &utf16[0], |
| 79 | + } |
| 80 | + ret := lv.SendMessage(winapi.LVM_INSERTITEMW, 0, uintptr(unsafe.Pointer(&item))) |
| 81 | + return int(int32(ret)) |
| 82 | +} |
| 83 | + |
| 84 | +// SetItemText sets the text for an item/subitem. |
| 85 | +func (lv *ListView) SetItemText(itemIndex int, subItem int, text string) bool { |
| 86 | + utf16 := syscall.StringToUTF16(text) |
| 87 | + item := win.LVITEM{ |
| 88 | + IItem: int32(itemIndex), |
| 89 | + ISubItem: int32(subItem), |
| 90 | + PszText: &utf16[0], |
| 91 | + } |
| 92 | + ret := lv.SendMessage(winapi.LVM_SETITEMTEXTW, uintptr(itemIndex), uintptr(unsafe.Pointer(&item))) |
| 93 | + return ret != 0 |
| 94 | +} |
| 95 | + |
| 96 | +// GetItemText returns the text for an item/subitem. |
| 97 | +func (lv *ListView) GetItemText(itemIndex int, subItem int) string { |
| 98 | + buf := make([]uint16, 512) |
| 99 | + item := win.LVITEM{ |
| 100 | + IItem: int32(itemIndex), |
| 101 | + ISubItem: int32(subItem), |
| 102 | + CchTextMax: int32(len(buf)), |
| 103 | + PszText: &buf[0], |
| 104 | + } |
| 105 | + lv.SendMessage(winapi.LVM_GETITEMTEXTW, uintptr(itemIndex), uintptr(unsafe.Pointer(&item))) |
| 106 | + return syscall.UTF16ToString(buf) |
| 107 | +} |
| 108 | + |
| 109 | +// ItemCount returns the number of items in the list. |
| 110 | +func (lv *ListView) ItemCount() int { |
| 111 | + return int(int32(lv.SendMessage(winapi.LVM_GETITEMCOUNT, 0, 0))) |
| 112 | +} |
| 113 | + |
| 114 | +// DeleteAllItems deletes all items. |
| 115 | +func (lv *ListView) DeleteAllItems() bool { |
| 116 | + return lv.SendMessage(winapi.LVM_DELETEALLITEMS, 0, 0) != 0 |
| 117 | +} |
| 118 | + |
| 119 | +// DeleteItem deletes an item at index. |
| 120 | +func (lv *ListView) DeleteItem(index int) bool { |
| 121 | + return lv.SendMessage(winapi.LVM_DELETEITEM, uintptr(index), 0) != 0 |
| 122 | +} |
| 123 | + |
| 124 | +// SelectedIndex returns the first selected item index, or -1 if none. |
| 125 | +func (lv *ListView) SelectedIndex() int { |
| 126 | + ret := lv.SendMessage(winapi.LVM_GETNEXTITEM, ^uintptr(0), uintptr(win.LVNI_SELECTED)) |
| 127 | + return int(int32(ret)) |
| 128 | +} |
| 129 | + |
| 130 | +// NewListView creates a new ListView, need bind to Dialog before use. |
| 131 | +func NewListView(idd uintptr) *ListView { |
| 132 | + return &ListView{WindowBase: WindowBase{idd: idd}} |
| 133 | +} |
| 134 | + |
| 135 | +// BindNewListView creates a new ListView and bind to target dlg. |
| 136 | +func BindNewListView(idd uintptr, dlg *Dialog) (*ListView, error) { |
| 137 | + lv := NewListView(idd) |
| 138 | + err := dlg.BindWidgets(lv) |
| 139 | + return lv, err |
| 140 | +} |
0 commit comments