diff options
author | John Ankarström <john@ankarstrom.se> | 2022-07-11 00:34:44 +0200 |
---|---|---|
committer | John Ankarström <john@ankarstrom.se> | 2022-07-11 00:40:16 +0200 |
commit | 8122c83bb3be30325f49d50dbafcd2f14c9b0d2e (patch) | |
tree | e0313f5e6fad84c2b5d8517a6efb9e4c82dc953f /c/episodelistview.cpp | |
parent | ca56ce2c32b4479af32bf7fb5d065ee9a3a45d8a (diff) | |
download | EpisodeBrowser-8122c83bb3be30325f49d50dbafcd2f14c9b0d2e.tar.gz |
Improve ElvSort.
Diffstat (limited to 'c/episodelistview.cpp')
-rw-r--r-- | c/episodelistview.cpp | 93 |
1 files changed, 48 insertions, 45 deletions
diff --git a/c/episodelistview.cpp b/c/episodelistview.cpp index 7c79043..88fb7ac 100644 --- a/c/episodelistview.cpp +++ b/c/episodelistview.cpp @@ -7,11 +7,6 @@ #include "defs.h" extern DataListView g_dlv; - -HWND HSortLv; - -int ISort; -LVITEM LviElvFocus; /* Focused episode. */ static int CALLBACK ElvSort(LPARAM, LPARAM, LPARAM); void @@ -37,47 +32,51 @@ EpisodeListView::Create() lvc.cx = Dpi(30); ListView_InsertColumn(m_hWnd, ELVSIRATING, &lvc); - if (!Pl("cfg","get_sort","i",&ISort)) - ISort = 1; + if (!Pl("cfg","get_sort","i",&m_iSort)) + m_iSort = 1; } void EpisodeListView::DoSort() { - HSortLv = m_hWnd; - ListView_SortItemsEx(m_hWnd, ElvSort, ISort); + ListView_SortItemsEx(m_hWnd, ElvSort, (LPARAM)this); +} + +void +EpisodeListView::EnsureFocusVisible() +{ + int iEpFocus = ListView_GetNextItem(m_hWnd, -1, LVNI_FOCUSED); + if (iEpFocus == -1) return; + ListView_EnsureVisible(m_hWnd, iEpFocus, TRUE); } LRESULT EpisodeListView::HandleNotify(LPARAM lParam) { - LPNMLISTVIEW lpNmLv; - lpNmLv = (LPNMLISTVIEW)lParam; + LPNMLISTVIEW lpNmLv = (LPNMLISTVIEW)lParam; switch (lpNmLv->hdr.code) { case LVN_ITEMCHANGED: /* Select/focus episode. */ if ((lpNmLv->uChanged & LVIF_STATE) && (lpNmLv->uNewState & LVIS_FOCUSED)) { - LviElvFocus.iItem = lpNmLv->iItem; - LviElvFocus.lParam = lpNmLv->lParam; - UpdateItem(&LviElvFocus); + m_lviFocus.iItem = lpNmLv->iItem; + m_lviFocus.lParam = lpNmLv->lParam; + UpdateItem(&m_lviFocus); g_dlv.ShowEpisode(lpNmLv->lParam); } break; case LVN_COLUMNCLICK: /* Sort by column. */ { - int iColumn; - iColumn = lpNmLv->iSubItem+1; - ISort = abs(ISort) == iColumn? -ISort: iColumn; - Pl("cfg","set_sort","I",ISort); + int iColumn = lpNmLv->iSubItem+1; + m_iSort = abs(m_iSort) == iColumn? -m_iSort: iColumn; + Pl("cfg","set_sort","I",m_iSort); DoSort(); ShowFocus(); break; } case LVN_KEYDOWN: /* Navigate episodes by keyboard. */ { - LPNMLVKEYDOWN lpNmLvKd; - lpNmLvKd = (LPNMLVKEYDOWN)lParam; + LPNMLVKEYDOWN lpNmLvKd = (LPNMLVKEYDOWN)lParam; switch (lpNmLvKd->wVKey) { case VK_LEFT: SelectUnwatched(-1); @@ -90,8 +89,7 @@ EpisodeListView::HandleNotify(LPARAM lParam) } case NM_CUSTOMDRAW: /* Make unwatched episodes bold. */ { - LPNMLVCUSTOMDRAW lpLvCd; - lpLvCd = (LPNMLVCUSTOMDRAW)lParam; + LPNMLVCUSTOMDRAW lpLvCd = (LPNMLVCUSTOMDRAW)lParam; switch (lpLvCd->nmcd.dwDrawStage) { case CDDS_PREPAINT: return CDRF_NOTIFYITEMDRAW; @@ -111,9 +109,9 @@ EpisodeListView::HandleNotify(LPARAM lParam) case NM_DBLCLK: /* Open clicked episode. */ { if (!Pl("local_episodes","open_episode_locally","I", - LviElvFocus.lParam)) + m_lviFocus.lParam)) Pl("local_episodes","open_episode_online","I", - LviElvFocus.lParam); + m_lviFocus.lParam); break; } case NM_RETURN: /* Open all selected episodes. */ @@ -133,10 +131,9 @@ EpisodeListView::HandleNotify(LPARAM lParam) } case NM_RCLICK: { - DWORD dwPos; extern HWND g_hWnd; extern HMENU g_hPopupMenu; - dwPos = GetMessagePos(); + DWORD dwPos = GetMessagePos(); TrackPopupMenu(g_hPopupMenu, TPM_RIGHTBUTTON, LOWORD(dwPos), HIWORD(dwPos), 0, g_hWnd, NULL); @@ -147,6 +144,12 @@ EpisodeListView::HandleNotify(LPARAM lParam) return 0; } +int +EpisodeListView::ISort() const +{ + return m_iSort; +} + void EpisodeListView::Redraw() { @@ -167,8 +170,7 @@ EpisodeListView::SaveFocus() void EpisodeListView::SetTop(int iItem) { - int iLast; - iLast = ListView_GetItemCount(m_hWnd)-1; + int iLast = ListView_GetItemCount(m_hWnd)-1; ListView_EnsureVisible(m_hWnd, iLast, TRUE); ListView_EnsureVisible(m_hWnd, iItem, TRUE); } @@ -202,9 +204,9 @@ s: ListView_SetItemState(m_hWnd, -1, LVIF_STATE, LVIS_SELECTED); SetTop(iItem > 5? iItem-5: 0); ListView_SetItemState(m_hWnd, iItem, LVIS_SELECTED|LVIS_FOCUSED, LVIS_SELECTED|LVIS_FOCUSED); - LviElvFocus.iItem = iItem; - LviElvFocus.lParam = iEpisode; - UpdateItem(&LviElvFocus); + m_lviFocus.iItem = iItem; + m_lviFocus.lParam = iEpisode; + UpdateItem(&m_lviFocus); g_dlv.ShowEpisode(iEpisode); } @@ -212,11 +214,8 @@ s: ListView_SetItemState(m_hWnd, -1, LVIF_STATE, LVIS_SELECTED); void EpisodeListView::SelectUnwatched(int iDir) { - int i, iEpNew, iItemNew; - LVFINDINFO lvfi; - LVITEM lviFocus; - /* Get focused episode. */ + LVITEM lviFocus; lviFocus.mask = LVIF_PARAM; if ((lviFocus.iItem = ListView_GetNextItem(m_hWnd, -1, LVNI_FOCUSED)) != -1 && ListView_GetItem(m_hWnd, &lviFocus)) @@ -224,6 +223,8 @@ EpisodeListView::SelectUnwatched(int iDir) else return; + LVFINDINFO lvfi; + int i, iEpNew, iItemNew; i = 0; lvfi.flags = LVFI_PARAM; lvfi.lParam = lviFocus.lParam; @@ -250,8 +251,7 @@ EpisodeListView::SelectUnwatched(int iDir) void EpisodeListView::ShowFocus() { - int iEpFocus; - iEpFocus = ListView_GetNextItem(m_hWnd, -1, LVNI_FOCUSED); + int iEpFocus = ListView_GetNextItem(m_hWnd, -1, LVNI_FOCUSED); if (iEpFocus == -1) return; ListView_EnsureVisible(m_hWnd, iEpFocus, TRUE); } @@ -425,24 +425,27 @@ EpisodeListView::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) /* 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 -ElvSort(LPARAM iItem1, LPARAM iItem2, LPARAM iSort) +ElvSort(LPARAM iItem1, LPARAM iItem2, LPARAM lpLv) { - int iOrder; + EpisodeListView *lpThis = (EpisodeListView *)lpLv; + LVITEM lvi1, lvi2; lvi1.mask = lvi2.mask = LVIF_PARAM; lvi1.iItem = iItem1; lvi2.iItem = iItem2; - if (!ListView_GetItem(HSortLv, &lvi1)) return 0; - if (!ListView_GetItem(HSortLv, &lvi2)) return 0; - iOrder = Cmp(iSort, 0); - switch (abs(iSort)-1) { + if (!ListView_GetItem(lpThis->HWnd(), &lvi1)) return 0; + if (!ListView_GetItem(lpThis->HWnd(), &lvi2)) return 0; + + int iOrder = Cmp(lpThis->ISort(), 0); + + switch (abs(lpThis->ISort())-1) { case ELVSIEPISODE: return iOrder*Cmp(lvi1.lParam, lvi2.lParam); break; case ELVSIRATING: { int iRating1, iRating2; - iRating1 = iSort > 0? 99: -1; - iRating2 = iSort > 0? 99: -1; + iRating1 = lpThis->ISort() > 0? 99: -1; + iRating2 = lpThis->ISort() > 0? 99: -1; Pl("episode_data","episode_rating","Ii",lvi1.lParam,&iRating1); Pl("episode_data","episode_rating","Ii",lvi2.lParam,&iRating2); if (iRating1 == iRating2) |