aboutsummaryrefslogtreecommitdiff
path: root/c/datalistview.cpp
blob: 05d46a42f6895b92087e4868bce7cc875f20e108 (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
#include <stdio.h>
#include <windows.h>
#include <commctrl.h>
#include <SWI-Prolog.h>

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

extern EpisodeListView g_elv;

void
DataListView::Create()
{
	LVCOLUMN lvc;

	ListView::Create((HMENU)IDC_DATALISTVIEW, LVS_NOCOLUMNHEADER);

	lvc.mask = LVCF_WIDTH|LVCF_TEXT|LVCF_SUBITEM;
	lvc.iSubItem = DLVSIKEY;
	lvc.pszText = TEXT("Key");

	lvc.cx = Dpi(42);
	ListView_InsertColumn(m_hWnd, DLVSIKEY, &lvc);

	lvc.iSubItem = DLVSIVALUE;
	lvc.pszText = TEXT("Value");
	lvc.cx = 500;
	ListView_InsertColumn(m_hWnd, DLVSIVALUE, &lvc);
}

void
DataListView::ShowEpisode(int iEpisode)
{
	ListView_DeleteAllItems(m_hWnd);

	LVITEM lviKey, lviValue;
	lviKey.mask = LVIF_TEXT;
	lviValue.mask = LVIF_TEXT;

	term_t t = PL_new_term_refs(3);
	if (!Plp(t,"I",iEpisode)) return;
	qid_t q = PL_open_query(NULL, PL_Q_NORMAL,
	    PL_predicate("episode_datum", 3, "episode_data"), t);

	for (int i = 0; PL_next_solution(q); i++) {
		char *szKey;
		char *szValue;
		TCHAR *tszKey, *tszValue;

		if (!Plg(t+1,"ss",&szKey,&szValue)) continue;

		tszKey = TszFromSz(szKey, CP_UTF8);
		if (!tszKey) continue;
		tszValue = TszFromSz(szValue, CP_UTF8);
		if (!tszValue) goto c;

		lviKey.mask = LVIF_TEXT;
		lviKey.iItem = i;
		lviKey.iSubItem = 0;
		lviKey.pszText = tszKey;
		ListView_InsertItem(m_hWnd, &lviKey);

		lviValue.iItem = i;
		lviValue.iSubItem = 1;
		lviValue.pszText = tszValue;
		ListView_SetItem(m_hWnd, &lviValue);

		free(tszValue);
	c:	free(tszKey);
	}

	PL_cut_query(q);
	UpdateLayout();

	LVFINDINFO lvfi;
	lvfi.flags = LVFI_PARAM;
	lvfi.lParam = iEpisode;
	int iItem = ListView_FindItem(g_elv.HWnd(), -1, &lvfi);
	if (iItem != -1)
		ListView_EnsureVisible(g_elv.HWnd(), iItem, TRUE);
}