#include #include #include #include #include "resource.h" #include "common.h" #include "datalistview.h" #include "episodelistview.h" #include "listview.h" #include "pl.h" EpisodeListView::EpisodeListView(const HWND hWndParent) : ListView(hWndParent, (HMENU)IDC_EPISODELISTVIEW, 0) { LVCOLUMN lvc; lvc.mask = LVCF_WIDTH|LVCF_TEXT|LVCF_SUBITEM; lvc.iSubItem = ELVSIEPISODE; lvc.pszText = (wchar_t*)L"#"; lvc.cx = Dpi(42); ListView_InsertColumn(hWnd, ELVSIEPISODE, &lvc); lvc.iSubItem = ELVSITITLE; lvc.pszText = (wchar_t*)L"Title"; lvc.cx = 500; ListView_InsertColumn(hWnd, ELVSITITLE, &lvc); lvc.iSubItem = ELVSIRATING; lvc.pszText = (wchar_t*)L"/"; lvc.cx = Dpi(30); ListView_InsertColumn(hWnd, ELVSIRATING, &lvc); if (!Pl("cfg","get_sort",&m_iSort)) m_iSort = 1; } void EpisodeListView::EnsureFocusVisible() { const int iEpFocus = ListView_GetNextItem(hWnd, -1, LVNI_FOCUSED); if (iEpFocus == -1) return; ListView_EnsureVisible(hWnd, iEpFocus, TRUE); } LRESULT EpisodeListView::HandleNotify(const LPARAM lParam) { const NMLISTVIEW* const pNmLv = (NMLISTVIEW*)lParam; switch (pNmLv->hdr.code) { case LVN_ITEMCHANGED: /* Select/focus episode. */ if ((pNmLv->uChanged & LVIF_STATE) && (pNmLv->uNewState & LVIS_FOCUSED)) { extern DataListView* const g_pDlv; UpdateItem(pNmLv->iItem, pNmLv->lParam); g_pDlv->ShowEpisode(pNmLv->lParam); } break; case LVN_COLUMNCLICK: /* Sort by column. */ { const int iColumn = pNmLv->iSubItem+1; m_iSort = abs(m_iSort) == iColumn? -m_iSort: iColumn; Pl("cfg","set_sort",m_iSort); Sort(); ShowFocus(); break; } case LVN_KEYDOWN: /* Navigate episodes by keyboard. */ { const NMLVKEYDOWN *const pNmLvKd = (NMLVKEYDOWN*)lParam; switch (pNmLvKd->wVKey) { case VK_LEFT: SelectUnwatched(-1); break; case VK_RIGHT: SelectUnwatched(1); break; } break; } case NM_CUSTOMDRAW: /* Make unwatched episodes bold. */ { const NMLVCUSTOMDRAW* const pLvCd = (NMLVCUSTOMDRAW*)lParam; switch (pLvCd->nmcd.dwDrawStage) { case CDDS_PREPAINT: return CDRF_NOTIFYITEMDRAW; break; case CDDS_ITEMPREPAINT: { extern HFONT g_hfBold; if (!Pl("track_episodes","watched",pLvCd->nmcd.lItemlParam)) { require(SelectObject(pLvCd->nmcd.hdc, g_hfBold)); return CDRF_NEWFONT; } break; } } break; } case NM_DBLCLK: /* Open clicked episode. */ { LVITEM lvi = {LVIF_PARAM, -1}; if (FindNextItem(&lvi, LVNI_FOCUSED)) Pl("local_episodes","open_episode_locally",lvi.lParam) || Pl("local_episodes","open_episode_online",lvi.lParam); break; } case NM_RETURN: /* Open all selected episodes. */ { LVITEM lvi = {LVIF_PARAM, -1}; while (FindNextItem(&lvi, LVNI_SELECTED)) Pl("local_episodes","open_episode_locally",lvi.lParam) || Pl("local_episodes","open_episode_online",lvi.lParam); break; } case NM_RCLICK: { extern HMENU g_hPopupMenu; const DWORD dwPos = GetMessagePos(); require(TrackPopupMenu(g_hPopupMenu, TPM_RIGHTBUTTON, LOWORD(dwPos), HIWORD(dwPos), 0, m_hWndParent, (const RECT*)NULL)); break; } } return 0; } void EpisodeListView::Redraw() { RedrawWindow(hWnd, NULL, NULL, RDW_ERASE|RDW_FRAME|RDW_INVALIDATE|RDW_ALLCHILDREN); } void EpisodeListView::ResizeColumns(int w) { ListView_SetColumnWidth(hWnd, ELVSIEPISODE, LVSCW_AUTOSIZE); int cxColumn = ListView_GetColumnWidth(hWnd, ELVSIEPISODE)+Dpi(4); ListView_SetColumnWidth(hWnd, ELVSIEPISODE, cxColumn); cxColumn += ListView_GetColumnWidth(hWnd, ELVSIRATING); ListView_SetColumnWidth(hWnd, ELVSITITLE, w-cxColumn-Metric-Dpi(4)); } /* Select previously focused episode. */ void EpisodeListView::RestoreFocus() { int i, iEpisode, iItem; LVFINDINFO lvfi; extern DataListView* const g_pDlv; iItem = 0; if (!Pl("cfg","get_focus",&iEpisode)) return; lvfi.flags = LVFI_PARAM; lvfi.lParam = iEpisode; i = 0; while ((iItem = ListView_FindItem(hWnd, -1, &lvfi)) == -1 && i++ < 100) lvfi.lParam = ++iEpisode; if (iItem != -1) goto s; iEpisode -= 100; lvfi.lParam = iEpisode; i = 0; while ((iItem = ListView_FindItem(hWnd, -1, &lvfi)) == -1 && i++ < 100) lvfi.lParam = --iEpisode; if (iItem != -1) goto s; return; s: UpdateItem(iItem, iEpisode); g_pDlv->ShowEpisode(iEpisode); ListView_SetItemState(hWnd, -1, LVIF_STATE, LVIS_SELECTED); SetTop(iItem > 5? iItem-5: 0); ListView_SetItemState(hWnd, iItem, LVIS_SELECTED|LVIS_FOCUSED, LVIS_SELECTED|LVIS_FOCUSED); } void EpisodeListView::SaveFocus() { LVITEM lvi = {LVIF_PARAM, -1}; if (FindNextItem(&lvi, LVNI_FOCUSED)) Pl("cfg","set_focus",lvi.lParam); } void EpisodeListView::SetTop(const int iItem) { const int iLast = ListView_GetItemCount(hWnd)-1; ListView_EnsureVisible(hWnd, iLast, TRUE); ListView_EnsureVisible(hWnd, iItem, TRUE); } /* Select next/previous unwatched episode. */ void EpisodeListView::SelectUnwatched(int iDir) { /* Get focused episode. */ LVITEM lviFocus = {LVIF_PARAM, -1}; if (!FindNextItem(&lviFocus, LVNI_FOCUSED)) return; LVFINDINFO lvfi; int i, iEpNew, iItemNew; i = 0; lvfi.flags = LVFI_PARAM; lvfi.lParam = lviFocus.lParam; do { if (!Pl("track_episodes",iDir > 0? "next_unwatched": "previous_unwatched", lvfi.lParam,&iEpNew)) return; lvfi.lParam = iEpNew; if ((iItemNew = ListView_FindItem(hWnd, -1, &lvfi)) != -1) { ListView_SetItemState(hWnd,-1,LVIF_STATE,LVIS_SELECTED); ListView_SetSelectionMark(hWnd, iItemNew); ListView_SetItemState(hWnd, iItemNew, LVIS_SELECTED|LVIS_FOCUSED, LVIS_SELECTED|LVIS_FOCUSED); Redraw(); ListView_EnsureVisible(hWnd, iItemNew, TRUE); return; } } while (i++ < 1000); } void EpisodeListView::ShowFocus() { const int iEpFocus = ListView_GetNextItem(hWnd, -1, LVNI_FOCUSED); if (iEpFocus == -1) return; ListView_EnsureVisible(hWnd, iEpFocus, TRUE); } void EpisodeListView::Sort() { ListView_SortItemsEx(hWnd, EpisodeListView::SortProc, (LPARAM)this); } int CALLBACK EpisodeListView::SortProc(const LPARAM iItem1, const LPARAM iItem2, const LPARAM lExtra) { EpisodeListView* const pElv = (EpisodeListView*)lExtra; LVITEM lvi1 = {LVIF_PARAM, (int)iItem1}; if (!ListView_GetItem(pElv->hWnd, &lvi1)) return 0; LVITEM lvi2 = {LVIF_PARAM, (int)iItem2}; if (!ListView_GetItem(pElv->hWnd, &lvi2)) return 0; /* abs(m_iSort) is the 1-based index of the column to sort by. * If m_iSort is negative, the order is descending. */ const int iOrder = Cmp(pElv->m_iSort, 0); switch (abs(pElv->m_iSort)-1) { case ELVSIEPISODE: return iOrder*Cmp(lvi1.lParam, lvi2.lParam); case ELVSIRATING: { int iRating1, iRating2; iRating1 = pElv->m_iSort > 0? 99: -1; iRating2 = pElv->m_iSort > 0? 99: -1; Pl("episode_data","episode_rating",lvi1.lParam,&iRating1); Pl("episode_data","episode_rating",lvi2.lParam,&iRating2); if (iRating1 == iRating2) return Cmp(lvi1.lParam, lvi2.lParam); return iOrder*Cmp(iRating1, iRating2); } case ELVSITITLE: { Mark m; wstring_owner wso1, wso2; int cch, cch1, cch2; if (!Pl("episode_data","episode_title",lvi1.lParam,&wso1)) return 0; if (!Pl("episode_data","episode_title",lvi2.lParam,&wso2)) return 0; cch1 = wcslen(wso1.p); cch2 = wcslen(wso2.p); cch = cch1 > cch2? cch2: cch1; return iOrder*_wcsnicmp(wso1.p, wso2.p, cch); } default: return 0; } } /* Update episode list. */ void EpisodeListView::Update() { if (!Pl("episode_data","ensure_episode_data")) return; /* Save scrolling position. */ int iEpTop = 0; { LVITEM lviTop = {LVIF_PARAM, ListView_GetTopIndex(hWnd)}; ListView_GetItem(hWnd, &lviTop); iEpTop = lviTop.lParam; } /* Save selected episodes. */ int iItemMark; static std::vector vEpSel; { vEpSel.clear(); LVITEM lvi = {LVIF_PARAM, -1}; while (FindNextItem(&lvi, LVNI_SELECTED)) vEpSel.push_back(lvi.lParam); iItemMark = ListView_GetSelectionMark(hWnd); } /* Save focus. */ int iEpFocus = 0; { LVITEM lvi = {LVIF_PARAM, -1}; if (FindNextItem(&lvi, LVNI_FOCUSED)) iEpFocus = lvi.lParam; } SendMessage(hWnd, WM_SETREDRAW, FALSE, 0); ListView_DeleteAllItems(hWnd); int cEp; if (!Pl("episode_data","episode_count",&cEp)) return; int cItem = 0; { wchar_t wszEpisode[16]; LVITEM lviEpisode = {LVIF_TEXT|LVIF_PARAM}; for (int iEp = 1; iEp <= cEp; iEp++) { extern char g_szLimitScreenwriter[]; extern int g_bViewTVOriginal, g_bViewWatched; if (g_szLimitScreenwriter[0] && !Pl("episode_data","episode_datum", iEp,"Screenwriter",g_szLimitScreenwriter)) continue; if (!g_bViewWatched) if (Pl("track_episodes","watched",iEp)) continue; if (!g_bViewTVOriginal) if (Pl("episode_data","tv_original",iEp)) continue; swprintf_s(wszEpisode, sizeof(wszEpisode)/sizeof(*wszEpisode), L"%d", iEp); /* Insert item. */ lviEpisode.iItem = cItem++; lviEpisode.iSubItem = ELVSIEPISODE; lviEpisode.pszText = wszEpisode; lviEpisode.lParam = iEp; ListView_InsertItem(hWnd, &lviEpisode); UpdateItem(lviEpisode.iItem, lviEpisode.lParam); } } Sort(); LVFINDINFO lvfi; lvfi.flags = LVFI_PARAM; /* Reset selection. */ for (const int iEpSel : vEpSel) { int iItemSel; lvfi.lParam = iEpSel; if ((iItemSel = ListView_FindItem(hWnd, -1, &lvfi)) != -1) ListView_SetItemState(hWnd, iItemSel, LVIS_SELECTED, LVIS_SELECTED); } if (iItemMark != -1) ListView_SetSelectionMark(hWnd, iItemMark); /* Reset focus. */ if (iEpFocus) { int iItemFocus; int i = 0; do lvfi.lParam = iEpFocus+i; while ((iItemFocus = ListView_FindItem(hWnd, -1, &lvfi)) == -1 && i++ < 100); if (iItemFocus != -1) ListView_SetItemState(hWnd, 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. */ { int iItemTopNew; int i = 0; do lvfi.lParam = iEpTop+i; while ((iItemTopNew = ListView_FindItem(hWnd, -1, &lvfi)) == -1 && i++ < 100) ; if (iItemTopNew != -1) SetTop(iItemTopNew); } /* Show number of displayed items in status bar. */ extern HWND g_hWndStatus; wchar_t wszDisp[16]; swprintf_s(wszDisp, sizeof(wszDisp)/sizeof(*wszDisp), L"%d", cItem); SendMessage(g_hWndStatus, SB_SETTEXT, MAKEWPARAM(1,0), (LPARAM)wszDisp); SendMessage(hWnd, WM_SETREDRAW, TRUE, 0); } /* Update episode name and rating. */ void EpisodeListView::UpdateItem(const int iItem, const LPARAM lParam) { wstring_owner wsoName; if (!Pl("episode_data","episode_title",lParam,&wsoName)) { if (!Pl("episode_data","update_episode_data")) goto r; if (!Pl("episode_data","episode_title",lParam,&wsoName)) goto r; } ListView_SetItemText(hWnd, iItem, ELVSITITLE, wsoName.p); r: int iRating; if (!Pl("episode_data","episode_rating",lParam,&iRating)) { ListView_SetItemText(hWnd, iItem, ELVSIRATING, (wchar_t*)L""); return; } wchar_t wszRating[3]; swprintf_s(wszRating, sizeof(wszRating)/sizeof(*wszRating), L"%d", iRating); ListView_SetItemText(hWnd, iItem, ELVSIRATING, wszRating); } LRESULT CALLBACK EpisodeListView::WndProc(const HWND hWnd, const UINT uMsg, const WPARAM wParam, const LPARAM lParam) { switch (uMsg) { case WM_GETDLGCODE: { /* For the episode list view, the Enter key should not * be handled by the dialog manager, but instead be sent * along to the main window procedure, so that it may be * handled by the NM_RETURN case in HandleNotify. */ const LRESULT lResult = CallWindowProc(m_prevProc, hWnd, uMsg, wParam, lParam); if (lParam && ((MSG*)lParam)->message == WM_KEYDOWN && ((MSG*)lParam)->wParam == VK_RETURN) return DLGC_WANTMESSAGE; return lResult; } } return ListView::WndProc(hWnd, uMsg, wParam, lParam); }