From 246fd1369dce903dad48730478bebbe9733ac88e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Ankarstr=C3=B6m?= Date: Mon, 15 Aug 2022 22:24:31 +0200 Subject: Start moving data into C++. This is the first step in the process of getting rid of the SWI Prolog dependency. We'll see how it goes. --- c/episodelistview.cpp | 115 +++++++++++++++++++++++--------------------------- 1 file changed, 53 insertions(+), 62 deletions(-) (limited to 'c/episodelistview.cpp') diff --git a/c/episodelistview.cpp b/c/episodelistview.cpp index 3856ce0..e591952 100644 --- a/c/episodelistview.cpp +++ b/c/episodelistview.cpp @@ -4,6 +4,7 @@ #include #include "resource.h" +#include "data.h" #include "datalistview.h" #include "episodelistview.h" #include "listview.h" @@ -49,10 +50,18 @@ LRESULT EpisodeListView::HandleNotify(const LPARAM lParam) const NMLISTVIEW* const nm = reinterpret_cast(lParam); switch (nm->hdr.code) { + case LVN_GETDISPINFO: /* Display item. */ + { + NMLVDISPINFO* const nm = reinterpret_cast(lParam); + ElvData& e = m_vData.at(nm->item.lParam-1); + wchar_t* vs[] = {e.siEp, e.title, e.sRating}; /* ELVSIEPISODE, ELVSITITLE, ELVSIRATING */ + nm->item.pszText = vs[nm->item.iSubItem]; + return 0; + } + case LVN_ITEMCHANGED: /* Select/focus episode. */ if ((nm->uChanged & LVIF_STATE) && (nm->uNewState & LVIS_FOCUSED)) { extern DataListView* const g_dlv; - UpdateItem(nm->iItem, nm->lParam); g_dlv->ShowEpisode(nm->lParam); } return 0; @@ -174,8 +183,7 @@ void EpisodeListView::RestoreFocus() if (iItem != -1) goto s; return; -s: UpdateItem(iItem, iEpisode); - g_dlv->ShowEpisode(iEpisode); +s: g_dlv->ShowEpisode(iEpisode); ListView_SetItemState(hWnd, -1, LVIF_STATE, LVIS_SELECTED); SetTop(iItem > 5? iItem-5: 0); ListView_SetItemState(hWnd, iItem, @@ -210,7 +218,7 @@ void EpisodeListView::SelectUnwatched(int dir) lvfi.lParam = lviFocus.lParam; do { - if (!Pl("track_episodes",dir > 0? "next_unwatched": "previous_unwatched", + if (!Pl("track_episodes",dir > 0? "push_unwatched": "previous_unwatched", lvfi.lParam,&iEpNew)) return; @@ -285,7 +293,8 @@ int CALLBACK EpisodeListView::SortProc(const LPARAM iItem1, const LPARAM iItem2, void EpisodeListView::Update() { - if (!Pl("episode_data","ensure_episode_data")) return; + if (!Pl("episode_data","ensure_episode_data")) + return; /* Save scrolling position. */ int iEpTop = 0; @@ -314,41 +323,53 @@ void EpisodeListView::Update() 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 siEp[16]; + int cEp; + if (!Pl("episode_data","episode_count",&cEp)) + return; + + int cItem = 0; LVITEM lviEpisode = {LVIF_TEXT|LVIF_PARAM}; + + /* Retrieve episode data and add list view items. */ + SendMessage(hWnd, WM_SETREDRAW, FALSE, 0); + m_vData.clear(); + ListView_DeleteAllItems(hWnd); for (int iEp = 1; iEp <= cEp; iEp++) { - extern char g_limitScreenwriter[]; - extern int g_bViewTVOriginal, g_bViewWatched; + ElvData e; - if (g_limitScreenwriter[0] - && !Pl("episode_data","episode_datum", - iEp,"Screenwriter",g_limitScreenwriter)) - continue; + if (!FromProlog(iEp, e)) + goto push; - if (!g_bViewWatched) - if (Pl("track_episodes","watched",iEp)) continue; + if (extern char g_limitScreenwriter[]; + g_limitScreenwriter[0] && !Pl("episode_data","episode_datum",iEp, + "Screenwriter",g_limitScreenwriter)) + goto push; - if (!g_bViewTVOriginal) - if (Pl("episode_data","tv_original",iEp)) continue; + if (extern int g_bViewWatched; !g_bViewWatched && e.bWatched) + goto push; - swprintf_s(siEp, sizeof(siEp)/sizeof(*siEp), L"%d", iEp); + if (extern int g_bViewTVOriginal; !g_bViewTVOriginal && e.bTVOriginal) + goto push; /* Insert item. */ lviEpisode.iItem = cItem++; lviEpisode.iSubItem = ELVSIEPISODE; - lviEpisode.pszText = siEp; + lviEpisode.pszText = LPSTR_TEXTCALLBACK; lviEpisode.lParam = iEp; ListView_InsertItem(hWnd, &lviEpisode); - UpdateItem(lviEpisode.iItem, lviEpisode.lParam); + + ListView_SetItemText(hWnd, lviEpisode.iItem, ELVSITITLE, LPSTR_TEXTCALLBACK); + ListView_SetItemText(hWnd, lviEpisode.iItem, ELVSIRATING, LPSTR_TEXTCALLBACK); + + push: m_vData.push_back(std::move(e)); } + + /* Show number of displayed items in status bar. */ + extern HWND g_hWndStatus; + wchar_t disp[16]; + Swprintf(disp, L"%d", cItem); + SendMessage(g_hWndStatus, SB_SETTEXT, MAKEWPARAM(1,0), reinterpret_cast(disp)); } Sort(); @@ -361,8 +382,7 @@ void EpisodeListView::Update() int iItemSel; lvfi.lParam = iEpSel; if ((iItemSel = ListView_FindItem(hWnd, -1, &lvfi)) != -1) - ListView_SetItemState(hWnd, iItemSel, - LVIS_SELECTED, LVIS_SELECTED); + ListView_SetItemState(hWnd, iItemSel, LVIS_SELECTED, LVIS_SELECTED); } if (iItemMark != -1) ListView_SetSelectionMark(hWnd, iItemMark); @@ -373,11 +393,9 @@ void EpisodeListView::Update() int i = 0; do lvfi.lParam = iEpFocus+i; - while ((iItemFocus = ListView_FindItem(hWnd, -1, &lvfi)) == -1 - && i++ < 100); + while ((iItemFocus = ListView_FindItem(hWnd, -1, &lvfi)) == -1 && i++ < 100); if (iItemFocus != -1) - ListView_SetItemState(hWnd, iItemFocus, - LVIS_FOCUSED, LVIS_FOCUSED); + ListView_SetItemState(hWnd, iItemFocus, LVIS_FOCUSED, LVIS_FOCUSED); } /* Try to reset scrolling position. Note that this must be @@ -387,42 +405,14 @@ void EpisodeListView::Update() int i = 0; do lvfi.lParam = iEpTop+i; - while ((iItemTopNew = ListView_FindItem(hWnd, -1, &lvfi)) == -1 - && i++ < 100) - ; + 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 disp[16]; - swprintf_s(disp, sizeof(disp)/sizeof(*disp), L"%d", cItem); - SendMessage(g_hWndStatus, SB_SETTEXT, MAKEWPARAM(1,0), reinterpret_cast(disp)); - SendMessage(hWnd, WM_SETREDRAW, TRUE, 0); } -void EpisodeListView::UpdateItem(const int iItem, const LPARAM lParam) -{ - WcharPtr name; - if (!Pl("episode_data","episode_title",lParam,&name)) { - if (!Pl("episode_data","update_episode_data")) goto r; - if (!Pl("episode_data","episode_title",lParam,&name)) goto r; - } - ListView_SetItemText(hWnd, iItem, ELVSITITLE, name); - -r: int rating; - if (!Pl("episode_data","episode_rating",lParam,&rating)) { - ListView_SetItemText(hWnd, iItem, ELVSIRATING, const_cast(L"")); - return; - } - - wchar_t sRating[3]; - Swprintf(sRating, L"%d", rating); - ListView_SetItemText(hWnd, iItem, ELVSIRATING, sRating); -} - LRESULT CALLBACK EpisodeListView::WndProc(const HWND hWnd, const UINT uMsg, const WPARAM wParam, const LPARAM lParam) { @@ -438,7 +428,8 @@ LRESULT CALLBACK EpisodeListView::WndProc(const HWND hWnd, const UINT uMsg, MSG* const msg = reinterpret_cast(lParam); if (lParam && msg->message == WM_KEYDOWN && msg->wParam == VK_RETURN) return DLGC_WANTMESSAGE; - return lResult; + else + return lResult; } } -- cgit v1.2.3