Skip to content

Commit 8ea757a

Browse files
committed
Select the next list item after removing one
1 parent 178007d commit 8ea757a

2 files changed

Lines changed: 31 additions & 1 deletion

File tree

‎list.c‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,11 @@ LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
176176
int selectedIndex = SendMessage(hList, LB_GETCURSEL, 0, 0);
177177
if (selectedIndex != LB_ERR) {
178178
RemoveShape(ShapeIndexForListItem(shapes, numShapes, selectedIndex));
179-
SendMessage(hList, LB_DELETESTRING, selectedIndex, 0);
179+
int remaining = (int)SendMessage(hList, LB_DELETESTRING, selectedIndex, 0);
180+
/* keeps a selection, so that Delete can be pressed again for the next item */
181+
if (remaining != LB_ERR && remaining > 0) {
182+
SendMessage(hList, LB_SETCURSEL, selectedIndex < remaining ? selectedIndex : remaining - 1, 0);
183+
}
180184
UpdateListScrollExtent();
181185
InvalidateRect(hwnd, NULL, FALSE);
182186
}

‎tests/list_keys_test.c‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,33 @@ int main(int argc, char **argv) {
7979
}
8080
}
8181

82+
/* the item that took the place of the removed one is selected, so Delete works again */
83+
if (SendMessage(listBox, LB_GETCURSEL, 0, 0) != 1) {
84+
puts("FAIL: no item is selected after Delete");
85+
failed = 1;
86+
}
87+
keybd_event(VK_DELETE, 0, 0, 0);
88+
keybd_event(VK_DELETE, 0, KEYEVENTF_KEYUP, 0);
89+
Sleep(300);
90+
if (ListCount() != 1) {
91+
puts("FAIL: a second Delete did not remove the next item");
92+
failed = 1;
93+
} else if (SendMessage(listBox, LB_GETCURSEL, 0, 0) != 0) {
94+
puts("FAIL: the last item is not selected after Delete");
95+
failed = 1;
96+
}
97+
keybd_event(VK_DELETE, 0, 0, 0);
98+
keybd_event(VK_DELETE, 0, KEYEVENTF_KEYUP, 0);
99+
Sleep(300);
100+
if (ListCount() != 0) {
101+
puts("FAIL: Delete did not remove the last item");
102+
failed = 1;
103+
}
104+
AddItem("again");
105+
AddItem("more");
106+
82107
/* Delete with nothing selected removes nothing */
108+
SendMessage(listBox, LB_SETCURSEL, (WPARAM)-1, 0);
83109
keybd_event(VK_DELETE, 0, 0, 0);
84110
keybd_event(VK_DELETE, 0, KEYEVENTF_KEYUP, 0);
85111
Sleep(300);

0 commit comments

Comments
 (0)