diff --git a/redo/table.go b/redo/table.go index 4dee331..8720c14 100644 --- a/redo/table.go +++ b/redo/table.go @@ -39,7 +39,13 @@ type Table interface { // LoadImageList loads the given ImageList into the Table. // This function copies; changes to the ImageList made later will not be reflected by the Table. - LoadImageList(ImageList) + LoadImageList(imagelist ImageList) + + // Selected and Select get and set the currently selected item in the Table. + // Selected returns -1 if no item is selected. + // Pass -1 to Select to deselect all items. + Selected() int + Select(index int) } type tablebase struct { diff --git a/redo/table_windows.c b/redo/table_windows.c index e10611e..a34e47a 100644 --- a/redo/table_windows.c +++ b/redo/table_windows.c @@ -122,3 +122,33 @@ void tableSetCheckboxImageList(HWND hwnd) if (SendMessageW(hwnd, LVM_SETCALLBACKMASK, LVIS_STATEIMAGEMASK, 0) == FALSE) xpanic("error marking state image list as application-managed", GetLastError()); } + +// because Go won't let me do C.WPARAM(-1) +intptr_t tableSelectedItem(HWND hwnd) +{ + return (intptr_t) SendMessageW(hwnd, LVM_GETNEXTITEM, (WPARAM) -1, LVNI_SELECTED); +} + +void tableSelectItem(HWND hwnd, intptr_t index) +{ + LVITEMW item; + LRESULT current; + + // via http://support.microsoft.com/kb/131284 + // we don't need to clear the other bits; Tables don't support cutting or drag/drop + current = SendMessageW(hwnd, LVM_GETNEXTITEM, (WPARAM) -1, LVNI_SELECTED); + if (current != (LRESULT) -1) { + ZeroMemory(&item, sizeof (LVITEMW)); + item.mask = LVIF_STATE; + item.state = 0; + item.stateMask = LVIS_FOCUSED | LVIS_SELECTED; + if (SendMessageW(hwnd, LVM_SETITEMSTATE, (WPARAM) current, (LPARAM) (&item)) == FALSE) + xpanic("error deselecting current Table item", GetLastError()); + } + ZeroMemory(&item, sizeof (LVITEMW)); + item.mask = LVIF_STATE; + item.state = LVIS_FOCUSED | LVIS_SELECTED; + item.stateMask = LVIS_FOCUSED | LVIS_SELECTED; + if (SendMessageW(hwnd, LVM_SETITEMSTATE, (WPARAM) index, (LPARAM) (&item)) == FALSE) + xpanic("error selecting new Table item", GetLastError()); +} diff --git a/redo/table_windows.go b/redo/table_windows.go index 71049b5..41aa363 100644 --- a/redo/table_windows.go +++ b/redo/table_windows.go @@ -62,6 +62,18 @@ func (t *table) LoadImageList(il ImageList) { il.apply(t._hwnd, C.LVM_SETIMAGELIST, C.LVSIL_SMALL) } +func (t *table) Selected() int { + t.RLock() + defer t.RUnlock() + return int(C.tableSelectedItem(t._hwnd)) +} + +func (t *table) Select(index int) { + t.RLock() + defer t.RUnlock() + C.tableSelectItem(t._hwnd, C.intptr_t(index)) +} + //export tableGetCell func tableGetCell(data unsafe.Pointer, item *C.LVITEMW) { t := (*table)(data) diff --git a/redo/winapi_windows.h b/redo/winapi_windows.h index cf0f2b3..18db8db 100644 --- a/redo/winapi_windows.h +++ b/redo/winapi_windows.h @@ -105,6 +105,8 @@ extern void tableUpdate(HWND, int); extern void tableAddExtendedStyles(HWND, LPARAM); extern void tableAutosizeColumns(HWND, int); extern void tableSetCheckboxImageList(HWND); +extern intptr_t tableSelectedItem(HWND); +extern void tableSelectItem(HWND, intptr_t); // container_windows.c extern DWORD makeContainerWindowClass(char **);