aboutsummaryrefslogtreecommitdiff
path: root/c/episodelistview.c
diff options
context:
space:
mode:
authorJohn Ankarström <john@ankarstrom.se>2022-02-15 16:29:59 +0100
committerJohn Ankarström <john@ankarstrom.se>2022-02-15 16:29:59 +0100
commit52fb337856497cb151081f3738e7cfa4bc2883bd (patch)
tree3309edbd0834bf90a5635b93038d1d088224e96e /c/episodelistview.c
parent09ec144a99f1234a37d04854bdfb81485540be97 (diff)
downloadEpisodeBrowser-52fb337856497cb151081f3738e7cfa4bc2883bd.tar.gz
Rework list view code.
Diffstat (limited to 'c/episodelistview.c')
-rw-r--r--c/episodelistview.c200
1 files changed, 200 insertions, 0 deletions
diff --git a/c/episodelistview.c b/c/episodelistview.c
new file mode 100644
index 0000000..82f460a
--- /dev/null
+++ b/c/episodelistview.c
@@ -0,0 +1,200 @@
+#include <windows.h>
+#include <commctrl.h>
+#include <TCHAR.H>
+#include <SWI-Prolog.h>
+
+#include "resource.h"
+#include "common.h"
+#include "listview.h"
+#include "episodelistview.h"
+#include "main.h"
+
+extern HFONT g_GUIFontBold;
+
+HWND gElv_hWnd;
+WNDPROC g_PrevElvProc;
+int g_SelectedItem = -1; /* Remembered after refresh. */
+
+HWND
+ElvCreate(HWND hWnd)
+{
+ HMODULE hModule;
+ HWND hListView;
+ LVCOLUMN lvc;
+
+ gElv_hWnd = hWnd;
+ hListView = LvCreate(hWnd, (HMENU)IDC_EPISODELISTVIEW);
+
+ lvc.mask = LVCF_WIDTH|LVCF_TEXT|LVCF_SUBITEM;
+
+ lvc.iSubItem = 0;
+ lvc.pszText = TEXT("#");
+ lvc.cx = 42;
+ ListView_InsertColumn(hListView, 0, &lvc);
+
+ lvc.iSubItem = 1;
+ lvc.pszText = TEXT("Title");
+ lvc.cx = 500;
+ ListView_InsertColumn(hListView, 1, &lvc);
+
+ ElvUpdate();
+
+ return hListView;
+}
+
+LRESULT
+ElvHandleNotify(LPARAM lParam)
+{
+ NMLISTVIEW *pNmLv;
+
+ pNmLv = (NMLISTVIEW *)lParam;
+
+ switch (pNmLv->hdr.code) {
+ case LVN_ITEMCHANGED:
+ if ((pNmLv->uChanged & LVIF_STATE)
+ && (pNmLv->uNewState & LVIS_FOCUSED)) {
+ g_SelectedItem = pNmLv->iItem;
+ ElvUpdateName(pNmLv);
+ //DlvShowEpisode(pNmLv->lParam);
+ }
+ break;
+ case NM_CUSTOMDRAW:
+ {
+ NMLVCUSTOMDRAW *pLvCd;
+ pLvCd = (NMLVCUSTOMDRAW *)lParam;
+ switch (pLvCd->nmcd.dwDrawStage) {
+ case CDDS_PREPAINT:
+ return CDRF_NOTIFYITEMDRAW;
+ break;
+ case CDDS_ITEMPREPAINT:
+ if (!Watched(pLvCd->nmcd.lItemlParam)) {
+ SelectObject(pLvCd->nmcd.hdc,
+ g_GUIFontBold);
+ return CDRF_NEWFONT;
+ }
+ break;
+ }
+ }
+ break;
+ }
+
+ return 0;
+}
+
+/* Update episode list. */
+void
+ElvUpdate()
+{
+ HWND hListView;
+ LVITEM lviEpisode, lviName;
+ qid_t q;
+ term_t t;
+
+ hListView = GetDlgItem(gElv_hWnd, IDC_EPISODELISTVIEW);
+
+ ListView_DeleteAllItems(hListView);
+
+ lviEpisode.mask = LVIF_TEXT|LVIF_PARAM;
+ lviName.mask = LVIF_TEXT;
+
+ t = PL_new_term_refs(2);
+ PL_call_predicate(NULL, PL_Q_NORMAL,
+ PL_predicate("update_tracked_episodes", 0, "track_episodes"),
+ t);
+
+ q = PL_open_query(NULL, PL_Q_NORMAL,
+ PL_predicate("episode_file", 2, "local_episodes"),
+ t);
+
+ for (int i = 0; PL_next_solution(q); i++) {
+ char *szName;
+ int cb, iEpisode, iItem, r;
+ TCHAR *tszEpisode, *tszName;
+ term_t t2;
+
+ /* Lookup episode name. */
+
+ if (!PL_get_integer(t+0, &iEpisode))
+ goto close;
+
+ t2 = PL_new_term_refs(3);
+ if(!PL_put_integer(t2+0, iEpisode))
+ return;
+
+ r = PL_call_predicate(NULL, PL_Q_NORMAL,
+ PL_predicate("lookup_episode_local", 3, "episode_data"),
+ t2);
+
+ /* Format episode, name strings. */
+
+ tszName = NULL;
+ if (r && PL_get_atom_chars(t2+1, &szName)) {
+ tszName = TSZFromSZ(szName, CP_UTF8);
+ if (!tszName)
+ continue;
+ }
+
+ cb = 100;
+ tszEpisode = malloc(cb*sizeof(TCHAR));
+ if (!tszEpisode)
+ continue;
+ _stprintf_s(tszEpisode, cb, TEXT("%d"),
+ iEpisode);
+
+ /* Insert item. */
+
+ lviEpisode.mask = LVIF_TEXT|LVIF_STATE|LVIF_PARAM;
+ lviEpisode.iItem = i;
+ lviEpisode.iSubItem = 0;
+ lviEpisode.pszText = tszEpisode;
+ lviEpisode.lParam = iEpisode;
+ ListView_InsertItem(hListView, &lviEpisode);
+
+ if (tszName) {
+ lviName.iItem = i;
+ lviName.iSubItem = 1;
+ lviName.pszText = tszName;
+ ListView_SetItem(hListView, &lviName);
+ }
+
+ free(tszName);
+ free(tszEpisode);
+ }
+
+ if (g_SelectedItem != -1) {
+ ListView_SetItemState(hListView, g_SelectedItem,
+ LVIS_SELECTED, LVIS_SELECTED);
+ ListView_EnsureVisible(hListView, g_SelectedItem, TRUE);
+ }
+
+close:
+ PL_close_query(q);
+}
+
+/* Update episode name. */
+void
+ElvUpdateName(NMLISTVIEW *pNmLv)
+{
+ char *szName;
+ HWND hListView;
+ TCHAR *tszName;
+ term_t t;
+
+ t = PL_new_term_refs(3);
+ if (!PL_put_integer(t+0, pNmLv->lParam))
+ return;
+
+ PL_call_predicate(NULL, PL_Q_NORMAL,
+ PL_predicate("lookup_episode", 3, "episode_data"),
+ t);
+
+ if (!PL_get_atom_chars(t+1, &szName))
+ return;
+
+ tszName = TSZFromSZ(szName, CP_UTF8);
+ if (!tszName)
+ return;
+
+ hListView = GetDlgItem(gElv_hWnd, IDC_EPISODELISTVIEW);
+ ListView_SetItemText(hListView, pNmLv->iItem, 1, tszName);
+}