aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Ankarström <john@ankarstrom.se>2022-04-28 15:12:34 +0200
committerJohn Ankarström <john@ankarstrom.se>2022-04-28 15:12:34 +0200
commit23eea8a9202d4c20eb9353907137bbc51e676e88 (patch)
tree3c18672acafd3740823c948e1ad5839e22951938
parent00d22febd2c73f5ccb058ca2211294b1a1d99e9c (diff)
downloadEpisodeBrowser-23eea8a9202d4c20eb9353907137bbc51e676e88.tar.gz
Fix selection and update-related bugs.
-rw-r--r--c/defs.h1
-rw-r--r--c/episodelistview.c89
-rw-r--r--c/main.c1
3 files changed, 48 insertions, 43 deletions
diff --git a/c/defs.h b/c/defs.h
index 1e463dd..7f22389 100644
--- a/c/defs.h
+++ b/c/defs.h
@@ -22,6 +22,7 @@ void ElvRedraw(void);
void ElvSetTop(int);
void ElvSelectRecent(void);
void ElvSelectUnwatched(int);
+void ElvShowFocus(void);
void ElvUpdate(void);
void ElvUpdateItem(LPLVITEM);
diff --git a/c/episodelistview.c b/c/episodelistview.c
index ad32b81..3b23c30 100644
--- a/c/episodelistview.c
+++ b/c/episodelistview.c
@@ -45,11 +45,7 @@ ElvCreate()
void
ElvDoSort()
{
- int iEpFocus;
ListView_SortItemsEx(HElv, ElvSort, ISort);
- iEpFocus = ListView_GetNextItem(HElv, -1, LVNI_FOCUSED);
- if (iEpFocus == -1) return;
- ListView_EnsureVisible(HElv, iEpFocus, TRUE);
}
LRESULT
@@ -78,6 +74,7 @@ ElvHandleNotify(LPARAM lParam)
PI(t,ISort) goto s;
P("cfg","set_sort",1,t);
s: ElvDoSort();
+ ElvShowFocus();
break;
}
case LVN_KEYDOWN: /* Navigate episodes by keyboard. */
@@ -156,6 +153,7 @@ ElvHandleNotify(LPARAM lParam)
return 0;
}
+
void
ElvRedraw()
{
@@ -221,16 +219,15 @@ ElvSelectUnwatched(int iDir)
/* Get focused episode. */
lviFocus.mask = LVIF_PARAM;
- lviFocus.iItem = -1;
- while ((lviFocus.iItem = ListView_GetNextItem(
- HElv, lviFocus.iItem, LVNI_FOCUSED)) != -1) {
- if (!ListView_GetItem(HElv, &lviFocus)) continue;
- }
+ if ((lviFocus.iItem = ListView_GetNextItem(HElv, -1, LVNI_FOCUSED)) != -1
+ && ListView_GetItem(HElv, &lviFocus))
+ ;
+ else
+ return;
i = 0;
lvfi.flags = LVFI_PARAM;
lvfi.lParam = lviFocus.lParam;
- bShift = GetAsyncKeyState(VK_SHIFT);
do {
t = T(2);
@@ -241,10 +238,8 @@ ElvSelectUnwatched(int iDir)
lvfi.lParam = iEpNew;
if ((iItemNew = ListView_FindItem(HElv, -1, &lvfi)) != -1) {
- if (!bShift) {
- ListView_SetItemState(HElv, -1,
- LVIF_STATE, LVIS_SELECTED);
- }
+ ListView_SetItemState(HElv,-1,LVIF_STATE,LVIS_SELECTED);
+ ListView_SetSelectionMark(HElv, iItemNew);
ListView_SetItemState(HElv, iItemNew,
LVIS_SELECTED|LVIS_FOCUSED,
LVIS_SELECTED|LVIS_FOCUSED);
@@ -255,6 +250,15 @@ ElvSelectUnwatched(int iDir)
} while (i++ < 1000);
}
+void
+ElvShowFocus()
+{
+ int iEpFocus;
+ iEpFocus = ListView_GetNextItem(HElv, -1, LVNI_FOCUSED);
+ if (iEpFocus == -1) return;
+ ListView_EnsureVisible(HElv, iEpFocus, TRUE);
+}
+
/* Sort list view items, iSort being the 1-based index of the column
* to sort by. If iSort is negative, the order is descending. */
int CALLBACK
@@ -318,7 +322,7 @@ ElvSort(LPARAM iItem1, LPARAM iItem2, LPARAM iSort)
void
ElvUpdate()
{
- int cEp, i, iEp, iEpFocus, iItem, iItemTopNew;
+ int cEp, i, iEp, iEpFocus, iItem, iItemMark, iItemTopNew;
LVITEM lvi, lviEpisode, lviTop;
LVFINDINFO lvfi;
term_t t;
@@ -339,20 +343,17 @@ ElvUpdate()
lvi.iItem = -1;
while ((lvi.iItem = ListView_GetNextItem(
HElv, lvi.iItem, LVNI_SELECTED)) != -1
- && i < 2048) {
- if (!ListView_GetItem(HElv, &lvi)) continue;
- aEpSel[i++] = lvi.lParam;
- }
+ && i < 2048)
+ if (ListView_GetItem(HElv, &lvi))
+ aEpSel[i++] = lvi.lParam;
aEpSel[i] = 0;
+ iItemMark = ListView_GetSelectionMark(HElv);
/* Save focus. */
iEpFocus = 0;
- lvi.iItem = -1;
- while ((lvi.iItem = ListView_GetNextItem(
- HElv, lvi.iItem, LVNI_FOCUSED)) != -1) {
- if (!ListView_GetItem(HElv, &lvi)) continue;
+ if ((lvi.iItem = ListView_GetNextItem(HElv, -1, LVNI_FOCUSED)) != -1
+ && ListView_GetItem(HElv, &lvi))
iEpFocus = lvi.lParam;
- }
SendMessage(HElv, WM_SETREDRAW, FALSE, 0);
ListView_DeleteAllItems(HElv);
@@ -393,47 +394,49 @@ ElvUpdate()
ElvUpdateItem(&lviEpisode);
}
- lvfi.flags = LVFI_PARAM;
+ ElvDoSort();
- /* Reset scrolling position (roughly). */
- i = 0;
- do
- lvfi.lParam = lviTop.lParam+i;
- while ((iItemTopNew = ListView_FindItem(HElv, -1, &lvfi)) == -1
- && i++ < 100);
- if (iItemTopNew != -1)
- ElvSetTop(iItemTopNew);
+ lvfi.flags = LVFI_PARAM;
/* Reset selection. */
for (i = 0; aEpSel[i]; i++) {
- int iSel;
+ int iItemSel;
lvfi.lParam = aEpSel[i];
- if ((iSel = ListView_FindItem(HElv, -1, &lvfi)) != -1)
- ListView_SetItemState(HElv, iSel,
+ if ((iItemSel = ListView_FindItem(HElv, -1, &lvfi)) != -1)
+ ListView_SetItemState(HElv, iItemSel,
LVIS_SELECTED, LVIS_SELECTED);
}
+ if (iItemMark != -1)
+ ListView_SetSelectionMark(HElv, iItemMark);
/* Reset focus. */
if (iEpFocus) {
- int iFocus;
+ int iItemFocus;
i = 0;
do
lvfi.lParam = iEpFocus+i;
- while ((iFocus = ListView_FindItem(HElv, -1, &lvfi)) == -1
+ while ((iItemFocus = ListView_FindItem(HElv, -1, &lvfi)) == -1
&& i++ < 100);
- if (iFocus != -1)
- ListView_SetItemState(HElv, iFocus,
+ if (iItemFocus != -1)
+ ListView_SetItemState(HElv, iItemFocus,
LVIS_FOCUSED, LVIS_FOCUSED);
}
+ /* Try to reset scrolling position. Note that this must be
+ * done last, as focusing an item scrolls it into view. */
+ i = 0;
+ do
+ lvfi.lParam = lviTop.lParam+i;
+ while ((iItemTopNew = ListView_FindItem(HElv, -1, &lvfi)) == -1
+ && i++ < 100);
+ if (iItemTopNew != -1)
+ ElvSetTop(iItemTopNew);
+
_stprintf_s(tszDisp, sizeof(tszDisp), TEXT("%d"), iItem);
SendMessage(HWndStatus, SB_SETTEXT, MAKEWPARAM(1,0), (LPARAM)tszDisp);
_stprintf_s(tszTotal, sizeof(tszTotal), TEXT("%d"), cEp);
SendMessage(HWndStatus, SB_SETTEXT, MAKEWPARAM(2,0), (LPARAM)tszTotal);
SendMessage(HElv, WM_SETREDRAW, TRUE, 0);
- if (ISort != 1)
- ListView_SortItemsEx(HElv, ElvSort, ISort);
- ElvRedraw();
}
/* Update episode name and rating. */
diff --git a/c/main.c b/c/main.c
index dc18a4f..c4482c5 100644
--- a/c/main.c
+++ b/c/main.c
@@ -363,6 +363,7 @@ WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
case IDM_RATE0:
ElvRedraw();
ElvDoSort();
+ ElvShowFocus();
}
b: break;
}