aboutsummaryrefslogtreecommitdiff
path: root/c/episodelistview.cpp
blob: 320f98fe2bb6740edc6cadb8530b4ba4fb811704 (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
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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
#include <cassert>
#include <vector>
#include <windows.h>
#include <commctrl.h>

#include "data.h"
#include "datalistview.h"
#include "episodelistview.h"
#include "ext.h"
#include "listview.h"
#include "res.h"
#include "util.h"
#include "win32.h"
#include "window.h"

EpisodeListView::EpisodeListView(Window& parent)
	: ListView(parent, reinterpret_cast<HMENU>(IDC_EPISODELISTVIEW), 0)
{
	LVCOLUMN lvc;

	lvc.mask = LVCF_WIDTH|LVCF_TEXT|LVCF_SUBITEM;
	lvc.iSubItem = ELVSIEPISODE;
	lvc.pszText = const_cast<wchar_t*>(L"#");
	lvc.cx = Dpi(42);
	ListView_InsertColumn(hWnd, ELVSIEPISODE, &lvc);

	lvc.iSubItem = ELVSITITLE;
	lvc.pszText = const_cast<wchar_t*>(L"Title");
	lvc.cx = 500;
	ListView_InsertColumn(hWnd, ELVSITITLE, &lvc);

	lvc.iSubItem = ELVSIRATING;
	lvc.pszText = const_cast<wchar_t*>(L"/");
	lvc.cx = Dpi(30);
	ListView_InsertColumn(hWnd, ELVSIRATING, &lvc);

	/* Get saved sort-by-column setting. */
	m_iSortCol = parent.cfg.iSortCol;
}

void EpisodeListView::EnsureFocusVisible()
{
	const int iEpFocus = ListView_GetNextItem(hWnd, -1, LVNI_FOCUSED);
	if (iEpFocus != -1)
		ListView_EnsureVisible(hWnd, iEpFocus, TRUE);
}

void EpisodeListView::HandleContextMenu(const WORD command)
{
	int cNotFound = 0;

	/* Look through selected items, applying the
	 * selected command to each one. */

	LVITEM lvi = {LVIF_PARAM, -1};
	while (FindNextItem(&lvi, LVNI_SELECTED)) {
		ElvDataA& e = parent.fvElv.At(lvi.lParam-1);

		if (ID_SUBGROUP(command) == IDG_CTX_RATE) {
			/* Process rate commands. */
			if (e.rating = ID_RATING(command))
				Swprintf(e.sRating, L"%d", e.rating);
			else
				e.sRating[0] = 0;
		} else {
			/* Process other commands. */
			switch (command) {
			case IDM_WATCH_LOCALLY:
				if(!OpenLocally(parent.cfg, parent.fvElv.At(lvi.lParam-1)))
					cNotFound++;
				break;

			case IDM_WATCH_ONLINE:
				OpenOnline(parent.cfg, lvi.lParam);
				break;

			case IDM_TOGGLE:
				e.bWatched = !e.bWatched;
				break;

			case IDM_WIKI:
				OpenWiki(parent.fvDlv.At(lvi.lParam-1));
				break;
			}
		}
	}

	Redraw();

	if (ID_SUBGROUP(command) == IDG_CTX_RATE) {
		/* If ratings changed, the episodes may need to be resorted. */
		Sort();
		ShowFocus();
	} else if (cNotFound) {
		/* Show warning if local episodes were not found. */
		if (cNotFound == 1)
			EBMessageBox(L"Episode could not be opened locally.", L"Error", MB_ICONWARNING);
		else {
			wchar_t msg[64] = {0};
			Swprintf(msg, L"%d episodes could not be opened locally.", cNotFound);
			EBMessageBox(msg, L"Error", MB_ICONWARNING);
		}
	}
}

LRESULT EpisodeListView::HandleNotify(const LPARAM lParam)
{
	const NMLISTVIEW* const nm = reinterpret_cast<NMLISTVIEW*>(lParam);

	switch (nm->hdr.code) {
	case LVN_GETDISPINFO:	/* Display item. */
	    {
		NMLVDISPINFO* const nm = reinterpret_cast<NMLVDISPINFO*>(lParam);
		ElvDataA& e = parent.fvElv.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)) {
			parent.dlv.ShowEpisode(nm->lParam);
		}
		return 0;

	case LVN_COLUMNCLICK:	/* Sort by column. */
	    {
		const int iCol = nm->iSubItem+1;

		/* The sign of m_iSortCol decides the sort order. */
		m_iSortCol = abs(m_iSortCol) == iCol? -m_iSortCol: iCol;

		parent.cfg.iSortCol = m_iSortCol;
		Sort();
		ShowFocus();
		return 0;
	    }

	case LVN_KEYDOWN:	/* Navigate episodes by keyboard. */
	    {
		const NMLVKEYDOWN *const nm = reinterpret_cast<NMLVKEYDOWN*>(lParam);
		switch (nm->wVKey) {
		case VK_LEFT:
			SelectUnwatched(-1);
			break;
		case VK_RIGHT:
			SelectUnwatched(1);
			break;
		}
		return 0;
	    }

	case NM_CUSTOMDRAW:	/* Make unwatched episodes bold. */
	    {
		const NMLVCUSTOMDRAW* const nm = reinterpret_cast<NMLVCUSTOMDRAW*>(lParam);
		switch (nm->nmcd.dwDrawStage) {
		case CDDS_PREPAINT:
			return CDRF_NOTIFYITEMDRAW;
			break;
		case CDDS_ITEMPREPAINT:
		    {
			const ElvDataA& e = parent.fvElv.At(nm->nmcd.lItemlParam-1);
			if (!e.bWatched) {
				extern HFONT g_hfBold;
				if (!SelectObject(nm->nmcd.hdc, g_hfBold))
					throw Err(WINDOWS, L"Bold font could not be selected");
				return CDRF_NEWFONT;
			}
			break;
		    }
		}
		return 0;
	    }

	case NM_DBLCLK:		/* Open clicked episode. */
	case NM_RETURN:		/* Open all selected episodes. */
	    {
		LVITEM lvi = {LVIF_PARAM, -1};
		while (FindNextItem(&lvi, LVNI_SELECTED))
			OpenLocally(parent.cfg, parent.fvElv.At(lvi.lParam-1)) || OpenOnline(parent.cfg, lvi.lParam);
		return 0;
	    }

	case NM_RCLICK:
	    {
		const DWORD pos = GetMessagePos();
		if (!TrackPopupMenu(parent.hMenuPopup, TPM_RIGHTBUTTON,
		    LOWORD(pos), HIWORD(pos), 0, parent.hWnd, nullptr))
			throw Err(WINDOWS, L"Context menu could not be opened");
		return 0;
	    }

	default:
		return 0;
	}
}

void EpisodeListView::Redraw()
{
	RedrawWindow(hWnd, nullptr, nullptr,
	    RDW_ERASE|RDW_FRAME|RDW_INVALIDATE|RDW_ALLCHILDREN);
}

void EpisodeListView::ResizeColumns(int w)
{
	ListView_SetColumnWidth(hWnd, ELVSIEPISODE, LVSCW_AUTOSIZE);
	int cx = ListView_GetColumnWidth(hWnd, ELVSIEPISODE)+Dpi(4);
	ListView_SetColumnWidth(hWnd, ELVSIEPISODE, cx);
	cx += ListView_GetColumnWidth(hWnd, ELVSIRATING);
	ListView_SetColumnWidth(hWnd, ELVSITITLE, w-cx-Metric<SM_CXVSCROLL>-Dpi(4));
}

/* Select previously focused episode. */
void EpisodeListView::RestoreFocus()
{
	int i, iEpisode, iItem;
	LVFINDINFO lvfi;

	iItem = 0;
	iEpisode = parent.cfg.iFocus;

	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:	parent.dlv.ShowEpisode(iEpisode);
	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);
}

void EpisodeListView::SaveFocus()
{
	LVITEM lvi = {LVIF_PARAM, -1};
	if (FindNextItem(&lvi, LVNI_FOCUSED))
		parent.cfg.iFocus = lvi.lParam;
}

void EpisodeListView::SetTop(const int iItem)
{
	const int iItemLast = ListView_GetItemCount(hWnd)-1;
	ListView_EnsureVisible(hWnd, iItemLast, TRUE);
	ListView_EnsureVisible(hWnd, iItem, TRUE);
}

void EpisodeListView::SelectUnwatched(int dir)
{
	dir = dir > 0? 1: -1;

	/* Get focused episode. */
	LVITEM lviFocus = {LVIF_PARAM, -1};
	if (!FindNextItem(&lviFocus, LVNI_FOCUSED))
		return;

	LVFINDINFO lvfi;
	int i, iEpNew, iItemNew;
	i = 0;
	lvfi.flags = LVFI_PARAM;
	lvfi.lParam = lviFocus.lParam;

	do {
		/* Loop through episodes to find next/previous
		 * unwatched episode. */
		iEpNew = lvfi.lParam;
		for (;;) {
			iEpNew += dir;
			if (iEpNew == 0
			    || static_cast<size_t>(iEpNew) > parent.fvElv.c
			    || !parent.fvElv[iEpNew-1].siEp[0])
				return;
			if (!parent.fvElv[iEpNew-1].bWatched)
				break;
		}

		/* Select the episode, if it is in the list view. If
		 * not, try again a thousand times. */
		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, reinterpret_cast<LPARAM>(this));
}

int CALLBACK EpisodeListView::SortProc(const LPARAM iItem1, const LPARAM iItem2, const LPARAM extra)
{
	EpisodeListView* const elv = reinterpret_cast<EpisodeListView*>(extra);

	LVITEM lvi1 = {LVIF_PARAM, static_cast<int>(iItem1)};
	if (!ListView_GetItem(elv->hWnd, &lvi1)) return 0;

	LVITEM lvi2 = {LVIF_PARAM, static_cast<int>(iItem2)};
	if (!ListView_GetItem(elv->hWnd, &lvi2)) return 0;

	/* abs(m_iSortCol) is the 1-based index of the column to sort by.
	 * If m_iSortCol is negative, the order is descending. */
	const int order = Cmp(elv->m_iSortCol, 0);

	const ElvDataA& e1 = elv->parent.fvElv[lvi1.lParam-1];
	const ElvDataA& e2 = elv->parent.fvElv.At(lvi2.lParam-1);

	switch (abs(elv->m_iSortCol)-1) {
	case ELVSIEPISODE:
		return order*Cmp(lvi1.lParam, lvi2.lParam);
	case ELVSIRATING:
	    {
		int rating1 = e1.rating;
		int rating2 = e2.rating;
		if (!rating1)
			rating1 = elv->m_iSortCol > 0? 99: -1;
		if (!rating2)
			rating2 = elv->m_iSortCol > 0? 99: -1;
		if (rating1 == rating2)
			return Cmp(lvi1.lParam, lvi2.lParam);
		return order*Cmp(rating1, rating2);
	    }
	case ELVSITITLE:
		return order*_wcsicmp(e1.title, e2.title);
	default:
		return 0;
	}
}

void EpisodeListView::Update()
{
	/* Save scrolling position. */
	int iEpTop = 0;
	{
		LVITEM lviTop = {LVIF_PARAM, ListView_GetTopIndex(hWnd)};
		ListView_GetItem(hWnd, &lviTop);
		iEpTop = lviTop.lParam;
	}

	/* Save selected episodes. */
	int iItemMark;
	std::vector<int> vEpSel;
	{
		LVITEM lvi = {LVIF_PARAM, -1};
		while (FindNextItem(&lvi, LVNI_SELECTED))
			vEpSel.push_back(lvi.lParam);
		iItemMark = ListView_GetSelectionMark(hWnd);
	}

	/* Save focus. */
	int iEpFocus = 0;
	{
		LVITEM lvi = {LVIF_PARAM, -1};
		if (FindNextItem(&lvi, LVNI_FOCUSED))
			iEpFocus = lvi.lParam;
	}

	{
		int cItem = 0;
		LVITEM lviEpisode = {LVIF_TEXT|LVIF_PARAM};

		/* Retrieve episode data and add list view items. */
		SendMessageW(hWnd, WM_SETREDRAW, FALSE, 0);
		ListView_DeleteAllItems(hWnd);
		for (int iEp = 1; iEp <= parent.cfg.cEp; iEp++) {
			ElvDataA& e = parent.fvElv.At(iEp-1);

			if (!e.siEp[0])
				continue;

			if (parent.cfg.limitToScreenwriter[0]
			    && wcscmp(parent.fvDlv.At(iEp-1).screenwriter, parent.cfg.limitToScreenwriter) != 0)
				continue;

			if (!parent.cfg.bViewWatched && e.bWatched)
				continue;

			if (!parent.cfg.bViewTVOriginal && e.bTVOriginal)
				continue;

			/* Insert item. */
			lviEpisode.iItem = cItem++;
			lviEpisode.iSubItem = ELVSIEPISODE;
			lviEpisode.pszText = LPSTR_TEXTCALLBACK;
			lviEpisode.lParam = iEp;
			ListView_InsertItem(hWnd, &lviEpisode);

			ListView_SetItemText(hWnd, lviEpisode.iItem, ELVSITITLE, LPSTR_TEXTCALLBACK);
			ListView_SetItemText(hWnd, lviEpisode.iItem, ELVSIRATING, LPSTR_TEXTCALLBACK);
		}

		/* Show number of displayed items in status bar. */
		wchar_t disp[16];
		Swprintf(disp, L"%d", cItem);
		parent.Status(disp, 1);
	}

	Sort();

	LVFINDINFO lvfi;
	lvfi.flags = LVFI_PARAM;

	/* Reset selection. */
	for (const int iEpSel : vEpSel) {
		int iItemSel;
		lvfi.lParam = iEpSel;
		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;
		int 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. */
	{
		int iItemTopNew;
		int i = 0;
		do
			lvfi.lParam = iEpTop+i;
		while ((iItemTopNew = ListView_FindItem(hWnd, -1, &lvfi)) == -1 && i++ < 100);
		if (iItemTopNew != -1)
			SetTop(iItemTopNew);
	}

	SendMessageW(hWnd, WM_SETREDRAW, TRUE, 0);
}

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 HandleNotify. */

		const LRESULT lResult = CallWindowProc(m_proc0, hWnd, uMsg, wParam, lParam);
		MSG* const msg = reinterpret_cast<MSG*>(lParam);
		if (lParam && msg->message == WM_KEYDOWN && msg->wParam == VK_RETURN)
			return DLGC_WANTMESSAGE;
		else
			return lResult;
	    }
	}

	return ListView::WndProc(hWnd, uMsg, wParam, lParam);
}