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

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

HWND Dlv_hWnd;

HWND
DlvCreate(HWND hWnd)
{
	HWND hDlv;
	LVCOLUMN lvc;

	Dlv_hWnd = hWnd;
	hDlv = LvCreate(hWnd, (HMENU)IDC_DATALISTVIEW);

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

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

	return hDlv;
}

/* Show episode data. */
void
DlvShowEpisode(int iEpisode)
{
	fid_t f;
	HWND hDlv;
	LVITEM lviKey, lviValue;
	term_t t;

	hDlv = GetDlgItem(Dlv_hWnd, IDC_DATALISTVIEW);
	ListView_DeleteAllItems(hDlv);

	lviKey.mask = LVIF_TEXT;
	lviValue.mask = LVIF_TEXT;

	F(f);
	t = T(3);
	PI(t,iEpisode) goto e;
	P("episode_data","lookup_episode_local",3,t) goto e;

	/* The episode data is a list of unary compounds,
	 * whose functor is the key and whose argument is the value. */

	{
		term_t tHead, tList;

		tHead = PL_new_term_ref();
		tList = PL_copy_term_ref(t+2);

		for (int i = 0; PL_get_list(tList, tHead, tList); i++) {
			atom_t aKey;
			const char *szKey;
			char *szValue;
			TCHAR *tszKey, *tszValue;
			term_t tValue;
			size_t iArity;

			if (!PL_get_name_arity(tHead,&aKey,&iArity)) continue;
			szKey = PL_atom_chars(aKey);
			if (!szKey) continue;

			tValue = PL_new_term_ref();
			if (!PL_get_arg(1, tHead, tValue)) continue;
			GAC(tValue,&szValue) continue;

			tszKey = TSZFromSZ(szKey, CP_UTF8);
			if (!tszKey) continue;
			tszValue = TSZFromSZ(szValue, CP_UTF8);
			if (!tszValue) goto e1;

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

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

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

e:	Fd(f);
}