aboutsummaryrefslogtreecommitdiff
path: root/c/listview.cpp
blob: 6dfa88803816cd96fb57b624abaf807c49d0c0f1 (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
#include <windows.h>
#include <commctrl.h>
#include <uxtheme.h>

#include "common.h"
#include "listview.h"
#include "main.h"

extern HFONT g_hfNormal;
static LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

ListView::ListView(const HWND hWndParent, const HMENU hMenu, const DWORD dwStyle)
{
	m_hWndParent = hWndParent;
	m_bHeader = !(dwStyle & LVS_NOCOLUMNHEADER);
	hWnd = require(CreateWindowEx(
	    WS_EX_CLIENTEDGE,
	    WC_LISTVIEW,
	    L"",
	    dwStyle|WS_CHILD|WS_VISIBLE|WS_VSCROLL|WS_TABSTOP|LVS_REPORT|LVS_SHOWSELALWAYS,
	    0, 0, 0, 0,
	    m_hWndParent, hMenu, GetModuleHandle(NULL), this));

	if (require(SetProp(hWnd, L"this", (HANDLE)this)))
		m_proc0 = (WNDPROC)SetWindowLongPtr(hWnd,
		    GWLP_WNDPROC, (LONG_PTR)::WndProc);

	ListView_SetExtendedListViewStyle(hWnd, LVS_EX_FULLROWSELECT);
	SendMessage(hWnd, WM_SETFONT, (WPARAM)g_hfNormal, MAKELPARAM(FALSE, 0));
}

/* Retrieve next matching list view item. */
bool ListView::FindNextItem(LVITEM* const lvi, const LPARAM lParam)
{
	if ((lvi->iItem = ListView_GetNextItem(hWnd, lvi->iItem, lParam)) == -1)
		return false;
	return ListView_GetItem(hWnd, lvi);
}

/* Naively calculate height of list view. */
int ListView::Height(int bHeader)
{
	if (bHeader == -1)
		bHeader = m_bHeader;
	const int cItem = ListView_GetItemCount(hWnd);
	return cItem? Dpi(bHeader? 27: 4)+cItem*Dpi(19): 0;
}

void ListView::ResizeColumns(int) {}

void ListView::UpdateTheme(const BOOL bThemeActive)
{
	DWORD dwStyle;
	const wchar_t* theme;
	WORD action;
	extern int g_bThemes;

	if (!g_bThemes) return;
	if (bThemeActive) {
		dwStyle = LVS_EX_DOUBLEBUFFER;
		theme = L"Explorer";
		action = UIS_SET;
	} else {
		dwStyle = 0;
		theme = NULL;
		action = UIS_CLEAR;
	}

	/* Use modern "Explorer" theme. */
	SetWindowTheme(hWnd, theme, NULL);

	/* The modern theme requires double buffering. */
	ListView_SetExtendedListViewStyleEx(hWnd, LVS_EX_DOUBLEBUFFER, dwStyle);

	/* Hide focus rectangles. */
	SendMessage(hWnd, WM_UPDATEUISTATE, MAKEWPARAM(action, UISF_HIDEFOCUS), 0);
}

LRESULT CALLBACK ListView::WndProc(const HWND hWnd, const UINT uMsg,
    const WPARAM wParam, const LPARAM lParam)
{
	switch (uMsg) {
	case WM_NOTIFY:
		switch (((NMHDR*)lParam)->code) {
		case HDN_ENDTRACK:
			UpdateLayout();
			return TRUE;
		}
		break;
	}

	return CallWindowProc(m_proc0, hWnd, uMsg, wParam, lParam);
}

LRESULT CALLBACK WndProc(const HWND hWnd, const UINT uMsg, const WPARAM wParam,
    const LPARAM lParam)
{
	ListView* const lv = (ListView*)GetProp(hWnd, L"this");

	if (uMsg == WM_DESTROY)
		RemoveProp(hWnd, L"this");

	return lv? lv->WndProc(hWnd, uMsg, wParam, lParam): FALSE;
}