SetCurrentIndex sets the index of the current item. Call this with a value of -1 to have no current item.
(index int)
| 1165 | // |
| 1166 | // Call this with a value of -1 to have no current item. |
| 1167 | func (tv *TableView) SetCurrentIndex(index int) error { |
| 1168 | if tv.inSetCurrentIndex { |
| 1169 | return nil |
| 1170 | } |
| 1171 | tv.inSetCurrentIndex = true |
| 1172 | defer func() { |
| 1173 | tv.inSetCurrentIndex = false |
| 1174 | }() |
| 1175 | |
| 1176 | var lvi win.LVITEM |
| 1177 | |
| 1178 | lvi.StateMask = win.LVIS_FOCUSED | win.LVIS_SELECTED |
| 1179 | |
| 1180 | if tv.MultiSelection() { |
| 1181 | if win.FALSE == win.SendMessage(tv.hwndFrozenLV, win.LVM_SETITEMSTATE, ^uintptr(0), uintptr(unsafe.Pointer(&lvi))) { |
| 1182 | return newError("SendMessage(LVM_SETITEMSTATE)") |
| 1183 | } |
| 1184 | if win.FALSE == win.SendMessage(tv.hwndNormalLV, win.LVM_SETITEMSTATE, ^uintptr(0), uintptr(unsafe.Pointer(&lvi))) { |
| 1185 | return newError("SendMessage(LVM_SETITEMSTATE)") |
| 1186 | } |
| 1187 | } |
| 1188 | |
| 1189 | if index > -1 { |
| 1190 | lvi.State = win.LVIS_FOCUSED | win.LVIS_SELECTED |
| 1191 | } |
| 1192 | |
| 1193 | if win.FALSE == win.SendMessage(tv.hwndFrozenLV, win.LVM_SETITEMSTATE, uintptr(index), uintptr(unsafe.Pointer(&lvi))) { |
| 1194 | return newError("SendMessage(LVM_SETITEMSTATE)") |
| 1195 | } |
| 1196 | if win.FALSE == win.SendMessage(tv.hwndNormalLV, win.LVM_SETITEMSTATE, uintptr(index), uintptr(unsafe.Pointer(&lvi))) { |
| 1197 | return newError("SendMessage(LVM_SETITEMSTATE)") |
| 1198 | } |
| 1199 | |
| 1200 | if index > -1 { |
| 1201 | if win.FALSE == win.SendMessage(tv.hwndFrozenLV, win.LVM_ENSUREVISIBLE, uintptr(index), uintptr(0)) { |
| 1202 | return newError("SendMessage(LVM_ENSUREVISIBLE)") |
| 1203 | } |
| 1204 | // Windows bug? Sometimes a second LVM_ENSUREVISIBLE is required. |
| 1205 | if win.FALSE == win.SendMessage(tv.hwndFrozenLV, win.LVM_ENSUREVISIBLE, uintptr(index), uintptr(0)) { |
| 1206 | return newError("SendMessage(LVM_ENSUREVISIBLE)") |
| 1207 | } |
| 1208 | if win.FALSE == win.SendMessage(tv.hwndNormalLV, win.LVM_ENSUREVISIBLE, uintptr(index), uintptr(0)) { |
| 1209 | return newError("SendMessage(LVM_ENSUREVISIBLE)") |
| 1210 | } |
| 1211 | // Windows bug? Sometimes a second LVM_ENSUREVISIBLE is required. |
| 1212 | if win.FALSE == win.SendMessage(tv.hwndNormalLV, win.LVM_ENSUREVISIBLE, uintptr(index), uintptr(0)) { |
| 1213 | return newError("SendMessage(LVM_ENSUREVISIBLE)") |
| 1214 | } |
| 1215 | |
| 1216 | if ip, ok := tv.providedModel.(IDProvider); ok && tv.restoringCurrentItemOnReset { |
| 1217 | if id := ip.ID(index); id != tv.currentItemID { |
| 1218 | tv.currentItemID = id |
| 1219 | if tv.itemStateChangedEventDelay == 0 { |
| 1220 | defer tv.currentItemChangedPublisher.Publish() |
| 1221 | } |
| 1222 | } |
| 1223 | } |
| 1224 | } else { |
no test coverage detected