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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
|
#include <windows.h>
#include <commctrl.h>
#include <TCHAR.H>
#include <SWI-Prolog.h>
#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 = TEXT("#");
lvc.cx = Dpi(42);
ListView_InsertColumn(hWnd, ELVSIEPISODE, &lvc);
lvc.iSubItem = ELVSITITLE;
lvc.pszText = TEXT("Title");
lvc.cx = 500;
ListView_InsertColumn(hWnd, ELVSITITLE, &lvc);
lvc.iSubItem = ELVSIRATING;
lvc.pszText = TEXT("/");
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;
m_lviFocus.iItem = pNmLv->iItem;
m_lviFocus.lParam = pNmLv->lParam;
UpdateItem(&m_lviFocus);
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. */
{
if (!Pl("local_episodes","open_episode_locally",
m_lviFocus.lParam))
Pl("local_episodes","open_episode_online",
m_lviFocus.lParam);
break;
}
case NM_RETURN: /* Open all selected episodes. */
{
LVITEM lvi;
lvi.mask = LVIF_PARAM;
lvi.iItem = -1;
while ((lvi.iItem = ListView_GetNextItem(
hWnd, lvi.iItem, LVNI_SELECTED)) != -1) {
if (!ListView_GetItem(hWnd, &lvi)) goto b;
if (!Pl("local_episodes","open_episode_locally",
lvi.lParam))
Pl("local_episodes","open_episode_online",
lvi.lParam);
}
b: 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);
}
/* 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: 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);
m_lviFocus.iItem = iItem;
m_lviFocus.lParam = iEpisode;
UpdateItem(&m_lviFocus);
g_pDlv->ShowEpisode(iEpisode);
}
void EpisodeListView::SaveFocus()
{
LVITEM lvi;
lvi.mask = LVIF_PARAM;
if ((lvi.iItem = ListView_GetNextItem(hWnd, -1, LVNI_FOCUSED)) != -1
&& ListView_GetItem(hWnd, &lvi))
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;
lviFocus.mask = LVIF_PARAM;
if ((lviFocus.iItem = ListView_GetNextItem(hWnd, -1, LVNI_FOCUSED)) != -1
&& ListView_GetItem(hWnd, &lviFocus))
;
else
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, lvi2;
lvi1.mask = lvi2.mask = LVIF_PARAM;
lvi1.iItem = iItem1; lvi2.iItem = iItem2;
if (!ListView_GetItem(pElv->hWnd, &lvi1)) return 0;
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;
char* sz1,* sz2;
int cch, cch1, cch2;
if (!Pl("episode_data","episode_title",lvi1.lParam,&sz1))
return 0;
if (!Pl("episode_data","episode_title",lvi2.lParam,&sz2))
return 0;
cch1 = strlen(sz1);
cch2 = strlen(sz2);
cch = cch1 > cch2? cch2: cch1;
return iOrder*_strnicmp(sz1, sz2, cch);
}
default:
return 0;
}
}
/* Update episode list. */
void EpisodeListView::Update()
{
int cEp, i, iEp, iEpFocus, iItem, iItemMark, iItemTopNew;
LVITEM lvi, lviEpisode, lviTop;
LVFINDINFO lvfi;
extern HWND g_hWndStatus;
static TCHAR tszDisp[16], tszEpisode[16];
static int aEpSel[2048];
lviEpisode.mask = LVIF_TEXT|LVIF_PARAM;
/* Save scrolling position. */
lviTop.iItem = ListView_GetTopIndex(hWnd);
lviTop.mask = LVIF_PARAM;
ListView_GetItem(hWnd, &lviTop);
/* Save selected episodes. */
i = 0;
lvi.mask = LVIF_PARAM;
lvi.iItem = -1;
while ((lvi.iItem = ListView_GetNextItem(
hWnd, lvi.iItem, LVNI_SELECTED)) != -1
&& i < 2048)
if (ListView_GetItem(hWnd, &lvi))
aEpSel[i++] = lvi.lParam;
aEpSel[i] = 0;
iItemMark = ListView_GetSelectionMark(hWnd);
/* Save focus. */
iEpFocus = 0;
if ((lvi.iItem = ListView_GetNextItem(hWnd, -1, LVNI_FOCUSED)) != -1
&& ListView_GetItem(hWnd, &lvi))
iEpFocus = lvi.lParam;
SendMessage(hWnd, WM_SETREDRAW, FALSE, 0);
ListView_DeleteAllItems(hWnd);
if (!Pl("episode_data","ensure_episode_data")) return;
if (!Pl("episode_data","episode_count",&cEp)) return;
for (iEp = 1, iItem = 0; iEp <= cEp; iEp++) {
Mark m;
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;
/* Format episode number string. */
_stprintf_s(tszEpisode, sizeof(tszEpisode), TEXT("%d"), iEp);
/* Insert item. */
lviEpisode.iItem = iItem++;
lviEpisode.iSubItem = ELVSIEPISODE;
lviEpisode.pszText = tszEpisode;
lviEpisode.lParam = iEp;
ListView_InsertItem(hWnd, &lviEpisode);
UpdateItem(&lviEpisode);
}
Sort();
lvfi.flags = LVFI_PARAM;
/* Reset selection. */
for (i = 0; aEpSel[i]; i++) {
int iItemSel;
lvfi.lParam = aEpSel[i];
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;
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. */
i = 0;
do
lvfi.lParam = lviTop.lParam+i;
while ((iItemTopNew = ListView_FindItem(hWnd, -1, &lvfi)) == -1
&& i++ < 100);
if (iItemTopNew != -1)
SetTop(iItemTopNew);
_stprintf_s(tszDisp, sizeof(tszDisp), TEXT("%d"), iItem);
SendMessage(g_hWndStatus, SB_SETTEXT, MAKEWPARAM(1,0), (LPARAM)tszDisp);
SendMessage(hWnd, WM_SETREDRAW, TRUE, 0);
}
/* Update episode name and rating. */
void EpisodeListView::UpdateItem(const LVITEM* const pLvi)
{
Mark m;
TCHAR* tszName;
int iRating;
static TCHAR tszRating[3];
if (!Pl("episode_data","episode_title",pLvi->lParam,&tszName)) {
if (!Pl("episode_data","update_episode_data")) goto r;
if (!Pl("episode_data","episode_title",pLvi->lParam,&tszName))
goto r;
}
ListView_SetItemText(hWnd, pLvi->iItem, ELVSITITLE, tszName);
r: if (!Pl("episode_data","episode_rating",pLvi->lParam,&iRating)) {
ListView_SetItemText(hWnd, pLvi->iItem, ELVSIRATING, TEXT(""));
return;
}
_stprintf_s(tszRating, sizeof(tszRating), TEXT("%d"), iRating);
ListView_SetItemText(hWnd, pLvi->iItem, ELVSIRATING, tszRating);
}
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 ElvHandleNotify. */
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);
}
|