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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
|
#include <windows.h>
#include <commctrl.h>
#include <TCHAR.H>
#include <SWI-Prolog.h>
#include "resource.h"
#include "defs.h"
HWND HElv;
LPLVITEM LpLviElvSelection; /* Most recently selected episode. */
HWND
ElvCreate()
{
LVCOLUMN lvc;
LpLviElvSelection = malloc(sizeof(LVITEM));
if (!LpLviElvSelection) return NULL;
HElv = LvCreate((HMENU)IDC_EPISODELISTVIEW, 0);
lvc.mask = LVCF_WIDTH|LVCF_TEXT|LVCF_SUBITEM;
lvc.iSubItem = 0;
lvc.pszText = TEXT("#");
lvc.cx = Dpi(42);
ListView_InsertColumn(HElv, 0, &lvc);
lvc.iSubItem = 1;
lvc.pszText = TEXT("/");
lvc.cx = Dpi(20);
ListView_InsertColumn(HElv, 1, &lvc);
lvc.iSubItem = 1;
lvc.pszText = TEXT("Title");
lvc.cx = 500;
ListView_InsertColumn(HElv, 2, &lvc);
return HElv;
}
/* 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)
{
int iOrder;
LVITEM lvi1, lvi2;
lvi1.mask = lvi2.mask = LVIF_PARAM;
lvi1.iItem = iItem1; lvi2.iItem = iItem2;
if (!ListView_GetItem(HElv, &lvi1)) return 0;
if (!ListView_GetItem(HElv, &lvi2)) return 0;
switch (abs(iSort)) {
case 1: /* Sort by episode number. */
iOrder = lvi1.lParam > lvi2.lParam? 1: -1;
break;
case 2: /* Sort by rating. */
{
int iRating1, iRating2;
term_t t, t2;
iRating1 = iSort > 0? 99: -1;
iRating2 = iSort > 0? 99: -1;
t = T(2);
PI(t,lvi1.lParam) goto e;
P("episode_data","episode_rating",2,t) goto e;
GI(t+1,&iRating1);
e: t2 = T(2);
PI(t2,lvi2.lParam) goto f;
P("episode_data","episode_rating",2,t2) goto f;
GI(t2+1,&iRating2);
f: iOrder = iRating1 > iRating2? 1: -1;
break;
}
case 3: /* Sort by title. */
{
char *sz1, *sz2;
int cch, cch1, cch2;
term_t t, t2;
t = T(2);
PI(t,lvi1.lParam) return 0;
P("episode_data","episode_title",2,t) return 0;
GAC(t+1,&sz1) return 0;
t2 = T(2);
PI(t2,lvi2.lParam) return 0;
P("episode_data","episode_title",2,t2) return 0;
GAC(t2+1,&sz2) return 0;
cch1 = strlen(sz1);
cch2 = strlen(sz2);
cch = cch1 > cch2? cch2: cch1;
iOrder = _strnicmp(sz1, sz2, cch);
break;
}
default:
return 0;
}
return iSort > 0? iOrder: iOrder*-1;
}
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;
ElvUpdateItem(LpLviElvSelection);
DlvShowEpisode(lpNmLv->lParam);
}
break;
case LVN_COLUMNCLICK:
{
int iColumn, iEpFocus;
static int iSort = 1; /* TODO: Load from cfg. */
iColumn = lpNmLv->iSubItem+1;
iSort = abs(iSort) == iColumn? -iSort: iColumn;
ListView_SortItemsEx(HElv, ElvSort, iSort);
iEpFocus = ListView_GetNextItem(HElv, -1, LVNI_FOCUSED);
if (iEpFocus == -1) break;
ListView_EnsureVisible(HElv, iEpFocus, TRUE);
break;
}
case NM_CUSTOMDRAW: /* Make unwatched episodes bold. */
{
LPNMLVCUSTOMDRAW lpLvCd;
lpLvCd = (LPNMLVCUSTOMDRAW)lParam;
switch (lpLvCd->nmcd.dwDrawStage) {
case CDDS_PREPAINT:
return CDRF_NOTIFYITEMDRAW;
break;
case CDDS_ITEMPREPAINT:
{
term_t t;
extern HFONT HfBold;
t = T(1);
PI(t,lpLvCd->nmcd.lItemlParam) break;
P("track_episodes","watched",1,t) {
SelectObject(lpLvCd->nmcd.hdc, HfBold);
return CDRF_NEWFONT;
}
break;
}
}
break;
}
case NM_DBLCLK: /* Open clicked episode. */
{
term_t t;
t = T(1);
PI(t,LpLviElvSelection->lParam) break;
P("local_episodes","open_episode",1,t);
break;
}
case NM_RETURN: /* Open all selected episodes. */
{
LVITEM lvi;
term_t t;
extern HWND HElv;
lvi.mask = LVIF_PARAM;
lvi.iItem = -1;
t = T(1);
while ((lvi.iItem = ListView_GetNextItem(
HElv, lvi.iItem, LVNI_SELECTED)) != -1) {
if (!ListView_GetItem(HElv, &lvi)) goto b;
PI(t,lvi.lParam) goto b;
P("local_episodes","open_episode",1,t);
}
b: break;
}
case NM_RCLICK:
{
DWORD dwPos;
extern HWND HWnd;
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()
{
int i, iEpisode, iItem;
LVFINDINFO lvfi;
term_t t;
t = T(1);
iItem = 0;
P("track_episodes","most_recently_watched",1,t) goto s;
GI(t,&iEpisode) return;
i = 0;
lvfi.flags = LVFI_PARAM;
lvfi.lParam = iEpisode;
do
lvfi.lParam = iEpisode+i;
while ((iItem = ListView_FindItem(HElv, -1, &lvfi)) == -1 && i++ < 100);
if (iItem == -1) return;
s: ListView_SetItemState(HElv, -1, LVIF_STATE, LVIS_SELECTED);
ElvSetTop(iItem > 5? iItem-5: 0);
ListView_SetItemState(HElv, iItem,
LVIS_SELECTED|LVIS_FOCUSED, LVIS_SELECTED|LVIS_FOCUSED);
LpLviElvSelection->iItem = iItem;
LpLviElvSelection->lParam = iEpisode;
ElvUpdateItem(LpLviElvSelection);
DlvShowEpisode(iEpisode);
}
/* Update episode list. */
void
ElvUpdate()
{
int cEp, i, iEp, iEpFocus, iItem, iItemTopNew;
LVITEM lvi, lviEpisode, lviTop;
LVFINDINFO lvfi;
term_t t;
extern HWND HWndStatus;
static TCHAR tszDisp[16], tszEpisode[16], tszTotal[16];
static int aEpSel[2048];
lviEpisode.mask = LVIF_TEXT|LVIF_PARAM;
/* Save scrolling position. */
lviTop.iItem = ListView_GetTopIndex(HElv);
lviTop.mask = LVIF_PARAM;
ListView_GetItem(HElv, &lviTop);
/* Save selected episodes. */
i = 0;
lvi.mask = LVIF_PARAM;
lvi.iItem = -1;
while ((lvi.iItem = ListView_GetNextItem(
HElv, lvi.iItem, LVNI_SELECTED)) != -1
&& i < 2048) {
if (!ListView_GetItem(HElv, &lvi)) continue;
aEpSel[i++] = lvi.lParam;
}
aEpSel[i] = 0;
/* Save focus. */
iEpFocus = 0;
while ((lvi.iItem = ListView_GetNextItem(
HElv, lvi.iItem, LVNI_FOCUSED)) != -1) {
if (!ListView_GetItem(HElv, &lvi)) continue;
iEpFocus = lvi.lParam;
}
SendMessage(HElv, WM_SETREDRAW, FALSE, 0);
ListView_DeleteAllItems(HElv);
t = T(1);
P("episode_data","ensure_episode_data",0,t) return;
P("episode_data","episode_count",1,t) return;
GI(t,&cEp) return;
for (iEp = 1, iItem = 0; iEp <= cEp; iEp++) {
extern int BViewTVOriginal, BViewWatched;
if (!BViewWatched) {
term_t t;
t = T(1);
PI(t,iEp) goto ep;
P("track_episodes","watched",1,t) goto ep;
continue;
}
if (!BViewTVOriginal) {
term_t t;
t = T(1);
PI(t,iEp) goto ep;
P("episode_data","tv_original",1,t) goto ep;
continue;
}
/* Format episode number string. */
ep: _stprintf_s(tszEpisode, sizeof(tszEpisode), TEXT("%d"), iEp);
/* Insert item. */
lviEpisode.iItem = iItem++;
lviEpisode.iSubItem = 0;
lviEpisode.pszText = tszEpisode;
lviEpisode.lParam = iEp;
ListView_InsertItem(HElv, &lviEpisode);
ElvUpdateItem(&lviEpisode);
}
lvfi.flags = LVFI_PARAM;
/* Reset scrolling position (roughly). */
i = 0;
do
lvfi.lParam = lviTop.lParam+i;
while ((iItemTopNew = ListView_FindItem(HElv, -1, &lvfi)) == -1
&& i++ < 100);
if (iItemTopNew != -1)
ElvSetTop(iItemTopNew);
/* Reset selection. */
for (i = 0; aEpSel[i]; i++) {
int iSel;
lvfi.lParam = aEpSel[i];
if ((iSel = ListView_FindItem(HElv, -1, &lvfi)) != -1)
ListView_SetItemState(HElv, iSel,
LVIS_SELECTED, LVIS_SELECTED);
}
/* Reset focus. */
if (iEpFocus) {
int iFocus;
i = 0;
do
lvfi.lParam = iEpFocus+i;
while ((iFocus = ListView_FindItem(HElv, -1, &lvfi)) == -1
&& i++ < 100);
if (iFocus != -1)
ListView_SetItemState(HElv, iFocus,
LVIS_FOCUSED, LVIS_FOCUSED);
}
_stprintf_s(tszDisp, sizeof(tszDisp), TEXT("%d"), iItem);
SendMessage(HWndStatus, SB_SETTEXT, MAKEWPARAM(1,0), (LPARAM)tszDisp);
_stprintf_s(tszTotal, sizeof(tszTotal), TEXT("%d"), cEp);
SendMessage(HWndStatus, SB_SETTEXT, MAKEWPARAM(2,0), (LPARAM)tszTotal);
SendMessage(HElv, WM_SETREDRAW, TRUE, 0);
ElvRedraw();
}
/* Update episode name and rating. */
void
ElvUpdateItem(LPLVITEM lpLvi)
{
char *szName;
int iRating;
TCHAR *tszName, tszRating[3];
term_t t, t2;
t = T(2);
tszName = NULL;
PI(t,lpLvi->lParam) goto r;
P("episode_data","episode_title",2,t) {
P("episode_data","fetch_episode_data",0,t) goto r;
P("episode_data","episode_title",2,t) goto r;
}
GAC(t+1,&szName) goto r;
tszName = TszFromSz(szName, CP_UTF8);
if (tszName)
ListView_SetItemText(HElv, lpLvi->iItem, 2, tszName);
r: t2 = T(2);
PI(t2,lpLvi->lParam) goto f;
P("episode_data","episode_rating",2,t2) {
ListView_SetItemText(HElv, lpLvi->iItem, 1, TEXT(""));
goto f;
}
GI(t2+1,&iRating) goto f;
_itot(iRating % 11, tszRating, sizeof(tszRating));
ListView_SetItemText(HElv, lpLvi->iItem, 1, tszRating);
f: if (tszName) free(tszName);
}
|