|
| 1 | +package wingui |
| 2 | + |
| 3 | +import ( |
| 4 | + "syscall" |
| 5 | + "unsafe" |
| 6 | + |
| 7 | + "github.com/lxn/win" |
| 8 | +) |
| 9 | + |
| 10 | +// TreeView is a wrapper for the Win32 SysTreeView32 control. |
| 11 | +type TreeView struct { |
| 12 | + WindowBase |
| 13 | + |
| 14 | + // OnSelChanged fires after selection has changed. |
| 15 | + OnSelChanged func(item win.HTREEITEM) |
| 16 | + // OnDblClick fires on NM_DBLCLK; item can be 0 if none. |
| 17 | + OnDblClick func(item win.HTREEITEM) |
| 18 | + // OnItemExpanding fires on TVN_ITEMEXPANDING. Return true to cancel. |
| 19 | + OnItemExpanding func(item win.HTREEITEM, action uint32) (cancel bool) |
| 20 | +} |
| 21 | + |
| 22 | +const ( |
| 23 | + tvgnRoot = 0 |
| 24 | + tvgnNext = 1 |
| 25 | + tvgnPrevious = 2 |
| 26 | + tvgnParent = 3 |
| 27 | + tvgnChild = 4 |
| 28 | +) |
| 29 | + |
| 30 | +func (tv *TreeView) WndProc(msg uint32, wParam, lParam uintptr) uintptr { |
| 31 | + switch msg { |
| 32 | + case win.WM_NOTIFY: |
| 33 | + nmhdr := (*win.NMHDR)(unsafe.Pointer(lParam)) |
| 34 | + code := uint32(nmhdr.Code) |
| 35 | + |
| 36 | + switch code { |
| 37 | + case win.TVN_SELCHANGED: |
| 38 | + if tv.OnSelChanged != nil { |
| 39 | + nmtv := (*win.NMTREEVIEW)(unsafe.Pointer(lParam)) |
| 40 | + tv.OnSelChanged(nmtv.ItemNew.HItem) |
| 41 | + } |
| 42 | + return 0 |
| 43 | + case win.TVN_ITEMEXPANDING: |
| 44 | + if tv.OnItemExpanding != nil { |
| 45 | + nmtv := (*win.NMTREEVIEW)(unsafe.Pointer(lParam)) |
| 46 | + if tv.OnItemExpanding(nmtv.ItemNew.HItem, nmtv.Action) { |
| 47 | + return 1 |
| 48 | + } |
| 49 | + } |
| 50 | + return 0 |
| 51 | + case win.NM_DBLCLK: |
| 52 | + if tv.OnDblClick != nil { |
| 53 | + tv.OnDblClick(tv.GetSelection()) |
| 54 | + } |
| 55 | + return 0 |
| 56 | + } |
| 57 | + return 0 |
| 58 | + } |
| 59 | + return tv.AsWindowBase().WndProc(msg, wParam, lParam) |
| 60 | +} |
| 61 | + |
| 62 | +// ItemCount returns the total number of items. |
| 63 | +func (tv *TreeView) ItemCount() int { |
| 64 | + return int(int32(tv.SendMessage(win.TVM_GETCOUNT, 0, 0))) |
| 65 | +} |
| 66 | + |
| 67 | +// InsertItem inserts a new item and returns its handle (0 on failure). |
| 68 | +// parent can be win.TVI_ROOT; insertAfter can be win.TVI_LAST / win.TVI_SORT, etc. |
| 69 | +func (tv *TreeView) InsertItem(parent, insertAfter win.HTREEITEM, text string) win.HTREEITEM { |
| 70 | + utf16 := syscall.StringToUTF16(text) |
| 71 | + ins := win.TVINSERTSTRUCT{ |
| 72 | + HParent: parent, |
| 73 | + HInsertAfter: insertAfter, |
| 74 | + Item: win.TVITEM{ |
| 75 | + Mask: win.TVIF_TEXT, |
| 76 | + PszText: uintptr(unsafe.Pointer(&utf16[0])), |
| 77 | + }, |
| 78 | + } |
| 79 | + ret := tv.SendMessage(win.TVM_INSERTITEM, 0, uintptr(unsafe.Pointer(&ins))) |
| 80 | + return win.HTREEITEM(ret) |
| 81 | +} |
| 82 | + |
| 83 | +// DeleteItem deletes an item (or all children if item is win.TVI_ROOT) and returns success. |
| 84 | +func (tv *TreeView) DeleteItem(item win.HTREEITEM) bool { |
| 85 | + return tv.SendMessage(win.TVM_DELETEITEM, 0, uintptr(item)) != 0 |
| 86 | +} |
| 87 | + |
| 88 | +// Expand expands/collapses an item. action is one of win.TVE_*. |
| 89 | +func (tv *TreeView) Expand(item win.HTREEITEM, action uint32) bool { |
| 90 | + return tv.SendMessage(win.TVM_EXPAND, uintptr(action), uintptr(item)) != 0 |
| 91 | +} |
| 92 | + |
| 93 | +// EnsureVisible scrolls the tree view so the item is visible. |
| 94 | +func (tv *TreeView) EnsureVisible(item win.HTREEITEM) bool { |
| 95 | + return tv.SendMessage(win.TVM_ENSUREVISIBLE, 0, uintptr(item)) != 0 |
| 96 | +} |
| 97 | + |
| 98 | +// GetSelection returns the currently selected item (0 if none). |
| 99 | +func (tv *TreeView) GetSelection() win.HTREEITEM { |
| 100 | + return win.HTREEITEM(tv.SendMessage(win.TVM_GETNEXTITEM, win.TVGN_CARET, 0)) |
| 101 | +} |
| 102 | + |
| 103 | +// SelectItem selects an item. |
| 104 | +func (tv *TreeView) SelectItem(item win.HTREEITEM) bool { |
| 105 | + return tv.SendMessage(win.TVM_SELECTITEM, win.TVGN_CARET, uintptr(item)) != 0 |
| 106 | +} |
| 107 | + |
| 108 | +// SetItemText updates the item text. |
| 109 | +func (tv *TreeView) SetItemText(item win.HTREEITEM, text string) bool { |
| 110 | + utf16 := syscall.StringToUTF16(text) |
| 111 | + tvi := win.TVITEM{ |
| 112 | + Mask: win.TVIF_TEXT, |
| 113 | + HItem: item, |
| 114 | + PszText: uintptr(unsafe.Pointer(&utf16[0])), |
| 115 | + } |
| 116 | + return tv.SendMessage(win.TVM_SETITEM, 0, uintptr(unsafe.Pointer(&tvi))) != 0 |
| 117 | +} |
| 118 | + |
| 119 | +// GetItemText reads the item text. |
| 120 | +func (tv *TreeView) GetItemText(item win.HTREEITEM) string { |
| 121 | + buf := make([]uint16, 512) |
| 122 | + tvi := win.TVITEM{ |
| 123 | + Mask: win.TVIF_TEXT, |
| 124 | + HItem: item, |
| 125 | + PszText: uintptr(unsafe.Pointer(&buf[0])), |
| 126 | + CchTextMax: int32(len(buf)), |
| 127 | + } |
| 128 | + if tv.SendMessage(win.TVM_GETITEM, 0, uintptr(unsafe.Pointer(&tvi))) == 0 { |
| 129 | + return "" |
| 130 | + } |
| 131 | + return syscall.UTF16ToString(buf) |
| 132 | +} |
| 133 | + |
| 134 | +// ParentItem returns the parent item or 0. |
| 135 | +func (tv *TreeView) ParentItem(item win.HTREEITEM) win.HTREEITEM { |
| 136 | + return win.HTREEITEM(tv.SendMessage(win.TVM_GETNEXTITEM, tvgnParent, uintptr(item))) |
| 137 | +} |
| 138 | + |
| 139 | +// NextSibling returns the next sibling item or 0. |
| 140 | +func (tv *TreeView) NextSibling(item win.HTREEITEM) win.HTREEITEM { |
| 141 | + return win.HTREEITEM(tv.SendMessage(win.TVM_GETNEXTITEM, tvgnNext, uintptr(item))) |
| 142 | +} |
| 143 | + |
| 144 | +// PrevSibling returns the previous sibling item or 0. |
| 145 | +func (tv *TreeView) PrevSibling(item win.HTREEITEM) win.HTREEITEM { |
| 146 | + return win.HTREEITEM(tv.SendMessage(win.TVM_GETNEXTITEM, tvgnPrevious, uintptr(item))) |
| 147 | +} |
| 148 | + |
| 149 | +// ChildItem returns the first child item or 0. |
| 150 | +func (tv *TreeView) ChildItem(item win.HTREEITEM) win.HTREEITEM { |
| 151 | + return win.HTREEITEM(tv.SendMessage(win.TVM_GETNEXTITEM, tvgnChild, uintptr(item))) |
| 152 | +} |
| 153 | + |
| 154 | +// NewTreeView creates a new TreeView, need bind to Dialog before use. |
| 155 | +func NewTreeView(idd uintptr) *TreeView { |
| 156 | + return &TreeView{WindowBase: WindowBase{idd: idd}} |
| 157 | +} |
| 158 | + |
| 159 | +// BindNewTreeView creates a new TreeView and bind to target dlg. |
| 160 | +func BindNewTreeView(idd uintptr, dlg *Dialog) (*TreeView, error) { |
| 161 | + tv := NewTreeView(idd) |
| 162 | + err := dlg.BindWidgets(tv) |
| 163 | + return tv, err |
| 164 | +} |
0 commit comments