Skip to content

Commit 738d2db

Browse files
committed
Fix ListView messages and improve basic demo
1 parent 4addc96 commit 738d2db

File tree

3 files changed

+29
-10
lines changed

3 files changed

+29
-10
lines changed

examples/basic/main.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,12 @@ func bindWidgets(dlg *wingui.Dialog) {
198198
idx := lv1.InsertItem(-1, "Item "+strconv.Itoa(i))
199199
lv1.SetItemText(idx, 1, "V"+strconv.Itoa(i))
200200
}
201+
lv1.OnItemChanged = func(index int) {
202+
sel := lv1.SelectedIndex()
203+
if sel >= 0 {
204+
tab1.SetText("Page1 - selected: " + lv1.GetItemText(sel, 0))
205+
}
206+
}
201207
lv1.OnItemActivate = func(index int) {
202208
if index < 0 {
203209
return
@@ -211,7 +217,12 @@ func bindWidgets(dlg *wingui.Dialog) {
211217
tabBtn.OnClicked = func() {
212218
n := int(atomic.AddInt32(&addSeq, 1) - 1)
213219
idx := lv1.InsertItem(-1, "Item "+strconv.Itoa(n))
220+
if idx < 0 {
221+
tabBtn.MessageBox("InsertItem failed", "ListView", win.MB_OK|win.MB_ICONERROR)
222+
return
223+
}
214224
lv1.SetItemText(idx, 1, "V"+strconv.Itoa(n))
225+
tab1.SetText("Page1 - count: " + strconv.Itoa(lv1.ItemCount()))
215226
}
216227
tabcontrol.AddDialogPage("Page1", tab1)
217228

listview.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"unsafe"
66

77
"github.com/lxn/win"
8-
"github.com/whtiehack/wingui/winapi"
98
)
109

1110
// ListView is a wrapper for the Win32 SysListView32 control.
@@ -21,6 +20,8 @@ type ListView struct {
2120
OnItemChanged func(index int)
2221
}
2322

23+
const lvmGetItemCount = win.LVM_FIRST + 4
24+
2425
func (lv *ListView) WndProc(msg uint32, wParam, lParam uintptr) uintptr {
2526
switch msg {
2627
case win.WM_NOTIFY:
@@ -48,7 +49,7 @@ func (lv *ListView) WndProc(msg uint32, wParam, lParam uintptr) uintptr {
4849

4950
// SetExtendedStyle sets extended list-view styles (e.g. LVS_EX_FULLROWSELECT).
5051
func (lv *ListView) SetExtendedStyle(style uint32) {
51-
lv.SendMessage(winapi.LVM_SETEXTENDEDLISTVIEWSTYLE, 0, uintptr(style))
52+
lv.SendMessage(win.LVM_SETEXTENDEDLISTVIEWSTYLE, 0, uintptr(style))
5253
}
5354

5455
// InsertColumn inserts a column at index and returns the actual inserted index (or -1).
@@ -61,7 +62,7 @@ func (lv *ListView) InsertColumn(index int, text string, width int, fmt int32) i
6162
PszText: &utf16[0],
6263
ISubItem: int32(index),
6364
}
64-
ret := lv.SendMessage(winapi.LVM_INSERTCOLUMNW, uintptr(index), uintptr(unsafe.Pointer(&col)))
65+
ret := lv.SendMessage(win.LVM_INSERTCOLUMN, uintptr(index), uintptr(unsafe.Pointer(&col)))
6566
return int(int32(ret))
6667
}
6768

@@ -77,7 +78,7 @@ func (lv *ListView) InsertItem(index int, text string) int {
7778
ISubItem: 0,
7879
PszText: &utf16[0],
7980
}
80-
ret := lv.SendMessage(winapi.LVM_INSERTITEMW, 0, uintptr(unsafe.Pointer(&item)))
81+
ret := lv.SendMessage(win.LVM_INSERTITEM, 0, uintptr(unsafe.Pointer(&item)))
8182
return int(int32(ret))
8283
}
8384

@@ -89,7 +90,7 @@ func (lv *ListView) SetItemText(itemIndex int, subItem int, text string) bool {
8990
ISubItem: int32(subItem),
9091
PszText: &utf16[0],
9192
}
92-
ret := lv.SendMessage(winapi.LVM_SETITEMTEXTW, uintptr(itemIndex), uintptr(unsafe.Pointer(&item)))
93+
ret := lv.SendMessage(win.LVM_SETITEMTEXT, uintptr(itemIndex), uintptr(unsafe.Pointer(&item)))
9394
return ret != 0
9495
}
9596

@@ -102,28 +103,28 @@ func (lv *ListView) GetItemText(itemIndex int, subItem int) string {
102103
CchTextMax: int32(len(buf)),
103104
PszText: &buf[0],
104105
}
105-
lv.SendMessage(winapi.LVM_GETITEMTEXTW, uintptr(itemIndex), uintptr(unsafe.Pointer(&item)))
106+
lv.SendMessage(win.LVM_GETITEMTEXT, uintptr(itemIndex), uintptr(unsafe.Pointer(&item)))
106107
return syscall.UTF16ToString(buf)
107108
}
108109

109110
// ItemCount returns the number of items in the list.
110111
func (lv *ListView) ItemCount() int {
111-
return int(int32(lv.SendMessage(winapi.LVM_GETITEMCOUNT, 0, 0)))
112+
return int(int32(lv.SendMessage(lvmGetItemCount, 0, 0)))
112113
}
113114

114115
// DeleteAllItems deletes all items.
115116
func (lv *ListView) DeleteAllItems() bool {
116-
return lv.SendMessage(winapi.LVM_DELETEALLITEMS, 0, 0) != 0
117+
return lv.SendMessage(win.LVM_DELETEALLITEMS, 0, 0) != 0
117118
}
118119

119120
// DeleteItem deletes an item at index.
120121
func (lv *ListView) DeleteItem(index int) bool {
121-
return lv.SendMessage(winapi.LVM_DELETEITEM, uintptr(index), 0) != 0
122+
return lv.SendMessage(win.LVM_DELETEITEM, uintptr(index), 0) != 0
122123
}
123124

124125
// SelectedIndex returns the first selected item index, or -1 if none.
125126
func (lv *ListView) SelectedIndex() int {
126-
ret := lv.SendMessage(winapi.LVM_GETNEXTITEM, ^uintptr(0), uintptr(win.LVNI_SELECTED))
127+
ret := lv.SendMessage(win.LVM_GETNEXTITEM, ^uintptr(0), uintptr(win.LVNI_SELECTED))
127128
return int(int32(ret))
128129
}
129130

wingui.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,20 @@ import (
5353
"runtime"
5454
"sync/atomic"
5555
"syscall"
56+
"unsafe"
5657
)
5758

5859
var hInstance win.HINSTANCE
5960

6061
func init() {
6162
log.SetOutput(os.Stdout)
6263
InitHInstance("")
64+
// Ensure common controls classes (ListView/Tab/Progress etc.) are registered.
65+
icc := win.INITCOMMONCONTROLSEX{
66+
DwSize: uint32(unsafe.Sizeof(win.INITCOMMONCONTROLSEX{})),
67+
DwICC: win.ICC_WIN95_CLASSES,
68+
}
69+
win.InitCommonControlsEx(&icc)
6370
}
6471

6572
// InitHInstance init hInstance,used by Dialog APIs.

0 commit comments

Comments
 (0)