aboutsummaryrefslogtreecommitdiff
path: root/c/episodelistview.c
blob: 8ca64d516ebf70b58da8fc3af9c6de9255372ca6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#include <windows.h>
#include <commctrl.h>
#include <TCHAR.H>
#include <SWI-Prolog.h>

#include "resource.h"
#include "defs.h"

extern HFONT HfBold;
extern HWND HWnd;
HWND HElv;
LPLVITEM LpLviElvSelection;

HWND
ElvCreate()
{
	LVCOLUMN lvc;

	LpLviElvSelection = malloc(sizeof(LVITEM));
	if (!LpLviElvSelection) return NULL;
	HElv = LvCreate((HMENU)IDC_EPISODELISTVIEW);

	lvc.mask = LVCF_WIDTH|LVCF_TEXT|LVCF_SUBITEM;
	lvc.iSubItem = 0;
	lvc.pszText = TEXT("#");
	lvc.cx = 42;
	ListView_InsertColumn(HElv, 0, &lvc);

	lvc.iSubItem = 1;
	lvc.pszText = TEXT("Title");
	lvc.cx = 500;
	ListView_InsertColumn(HElv, 1, &lvc);

	ElvUpdate();
	ElvSelectRecent();
	return HElv;
}

LRESULT
ElvHandleNotify(LPARAM lParam)
{
	LPNMLISTVIEW lpNmLv;
	lpNmLv = (LPNMLISTVIEW)lParam;

	switch (lpNmLv->hdr.code) {
	case LVN_ITEMCHANGED:
		if ((lpNmLv->uChanged & LVIF_STATE)
			&& (lpNmLv->uNewState & LVIS_FOCUSED)) {
			LpLviElvSelection->iItem = lpNmLv->iItem;
			LpLviElvSelection->lParam = lpNmLv->lParam;
			ElvUpdateName(LpLviElvSelection);
			DlvShowEpisode(lpNmLv->lParam);
		}
		break;
	case NM_CUSTOMDRAW:
	{
		LPNMLVCUSTOMDRAW lpLvCd;
		lpLvCd = (LPNMLVCUSTOMDRAW)lParam;
		switch (lpLvCd->nmcd.dwDrawStage) {
		case CDDS_PREPAINT:
			return CDRF_NOTIFYITEMDRAW;
			break;
		case CDDS_ITEMPREPAINT:
			if (!Watched(lpLvCd->nmcd.lItemlParam)) {
				SelectObject(lpLvCd->nmcd.hdc, HfBold);
				return CDRF_NEWFONT;
			}
			break;
		}
		break;
	}
	case NM_DBLCLK:
	{
		fid_t f;
		term_t t;
		F(f);
		t = T(1);
		PI(t+0, LpLviElvSelection->lParam) goto b;
		P("local_episodes","open_episode",1,t);
b:		Fd(f);
		break;
	}
	case NM_RCLICK:
	{
		DWORD dwPos;
		extern HMENU HPopupMenu;
		dwPos = GetMessagePos();
		TrackPopupMenu(HPopupMenu, TPM_RIGHTBUTTON,
			LOWORD(dwPos), HIWORD(dwPos), 0,
			HWnd, NULL);
		break;
	}
	}

	return 0;
}
void
ElvRedraw()
{
	RedrawWindow(HElv, NULL, NULL,
		RDW_ERASE|RDW_FRAME|RDW_INVALIDATE|RDW_ALLCHILDREN);
}

void
ElvSetTop(int iItem)
{
	int iLast;
	iLast = ListView_GetItemCount(HElv)-1;
	ListView_EnsureVisible(HElv, iLast, TRUE);
	ListView_EnsureVisible(HElv, iItem, TRUE);
}

/* Select most recent episode. */
void
ElvSelectRecent()
{
	fid_t f;
	int iEpisode, iItem;
	LVFINDINFO lvfi;
	term_t t;

	F(f);
	t = T(1);
	P("track_episodes","most_recently_watched",1,t) goto e;
	GI(t,&iEpisode) goto e;

	lvfi.flags = LVFI_PARAM;
	lvfi.lParam = iEpisode;

	iItem = ListView_FindItem(HElv, -1, &lvfi);
	if (iItem == -1) goto e;

	if (iItem > 5)
		ElvSetTop(iItem-5);
	ListView_SetItemState(HElv, iItem,
		LVIS_SELECTED|LVIS_FOCUSED, LVIS_SELECTED|LVIS_FOCUSED);
	LpLviElvSelection->iItem = iItem;
	LpLviElvSelection->lParam = iEpisode;
	ElvUpdateName(LpLviElvSelection);
	DlvShowEpisode(iEpisode);

e:	Fd(f);
}

/* Update episode list. */
void
ElvUpdate()
{
	fid_t f;
	int iTop;
	LVITEM lviEpisode, lviName;
	qid_t q;
	term_t t;

	SendMessage(HElv, WM_SETREDRAW, FALSE, 0);
	iTop = ListView_GetTopIndex(HElv);
	ListView_DeleteAllItems(HElv);

	lviEpisode.mask = LVIF_TEXT|LVIF_PARAM;
	lviName.mask = LVIF_TEXT;

	F(f);
	t = T(2);
	q = Q("local_episodes","episode_file",2,t);

	for (int i = 0; Qn(q); i++) {
		char *szName;
		fid_t f2;
		int cb, iEpisode, iItem, r;
		TCHAR *tszEpisode, *tszName;
		term_t t2;

		/* Format name string. */

		GI(t,&iEpisode) goto e;
		F(f2);
		t2 = T(3);
		PI(t2,iEpisode) goto e;
		tszName = NULL;
		P("episode_data","lookup_episode_local",3,t2) goto skipname;
		GAC(t2+1,&szName) goto skipname;
		tszName = TszFromSz(szName, CP_UTF8);
		if (!tszName) goto e;

		/* Format episode string. */
skipname:
		cb = 100;
		tszEpisode = malloc(cb*sizeof(TCHAR));
		if (!tszEpisode) goto e1;
		_stprintf_s(tszEpisode, cb, TEXT("%d"), iEpisode);

		/* Insert item. */

		lviEpisode.iItem = i;
		lviEpisode.iSubItem = 0;
		lviEpisode.pszText = tszEpisode;
		lviEpisode.lParam = iEpisode;
		ListView_InsertItem(HElv, &lviEpisode);

		if (tszName) {
			lviName.iItem = i;
			lviName.iSubItem = 1;
			lviName.pszText = tszName;
			ListView_SetItem(HElv, &lviName);
		}

		free(tszEpisode);
e1:		if (tszName) free(tszName);
e:		Fd(f2);
	}

	/* Reset selection and scrolling position. */

	ElvSetTop(iTop);
	if (LpLviElvSelection)
		ListView_SetItemState(HElv, LpLviElvSelection->iItem,
			LVIS_SELECTED, LVIS_SELECTED);

	SendMessage(HElv, WM_SETREDRAW, TRUE, 0);
	ElvRedraw();
	Qc(q);
	Fd(f);
}

/* Update episode name. */
void
ElvUpdateName(LPLVITEM lpLvi)
{
	char *szName;
	fid_t f;
	TCHAR *tszName;
	term_t t;

	F(f);
	t = T(3);
	PI(t,lpLvi->lParam) goto e;
	P("episode_data","lookup_episode",3,t) goto e;
	GAC(t+1,&szName) goto e;

	tszName = TszFromSz(szName, CP_UTF8);
	if (!tszName) goto e;

	ListView_SetItemText(HElv, lpLvi->iItem, 1, tszName);

e:	Fd(f);
	return;
}