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

#include "resource.h"
#include "common.h"
#include "datalistview.h"
#include "episodelistview.h"
#include "listview.h"
#include "main.h"

extern EpisodeListView *g_lpElv;

DataListView::DataListView(HWND hWndParent)
	: ListView(hWndParent, (HMENU)IDC_DATALISTVIEW, LVS_NOCOLUMNHEADER)
{
	LVCOLUMN lvc;

	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 (!PL_put_integer(t,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;

		if (!(PL_get_atom_chars(t+1,&szKey) && PL_get_atom_chars(t+2,&szValue)))
			continue;

		std::basic_string<TCHAR> tsmKey = TsmFromSz(szKey, CP_UTF8);
		std::basic_string<TCHAR> tsmValue = TsmFromSz(szValue, CP_UTF8);

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

		lviValue.iItem = i;
		lviValue.iSubItem = 1;
		lviValue.pszText = tsmValue.data();
		ListView_SetItem(m_hWnd, &lviValue);
	}

	PL_cut_query(q);
	UpdateLayout();

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