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

#include "listview.h"
#include "layout.h"
#include "win.h"

/* Actual window procedure for all list views, which calls the
 * appropriate WndProc member function. */
static LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

ListView::ListView(const HWND hWndParent, const HMENU hMenu, const DWORD dwStyle)
{
	extern HFONT g_hfNormal;

	m_hWndParent = hWndParent;
	hWnd = Require(CreateWindowExW(
	    WS_EX_CLIENTEDGE,
	    WC_LISTVIEW,
	    L"",
	    dwStyle|WS_CHILD|WS_VISIBLE|WS_VSCROLL|WS_TABSTOP|LVS_REPORT|LVS_SHOWSELALWAYS,
	    0, 0, 0, 0,
	    hWndParent, hMenu, GetModuleHandle(nullptr), this));
	m_proc0 = reinterpret_cast<WNDPROC>(SetWindowLongPtr(
	    hWnd, GWLP_WNDPROC, reinterpret_cast<LONG_PTR>(::WndProc)));

	Require(SetPropW(hWnd, L"this", reinterpret_cast<HANDLE>(this)));
	ListView_SetExtendedListViewStyle(hWnd, LVS_EX_FULLROWSELECT|LVS_EX_DOUBLEBUFFER);
	SendMessageW(hWnd, WM_SETFONT, reinterpret_cast<WPARAM>(g_hfNormal), MAKELPARAM(FALSE, 0));
}

int ListView::Height()
{
	const int cItem = ListView_GetItemCount(hWnd);
	return Dpi(4)+cItem*Dpi(19);
}

void ListView::ResizeColumns(int) {}

void ListView::UpdateTheme(const bool bThemeActive)
{
	const wchar_t* theme;
	WORD action;
	extern BOOL (*SetWindowTheme)(HWND, const wchar_t*, const wchar_t*);

	if (!SetWindowTheme) return;
	if (bThemeActive) {
		theme = L"Explorer";
		action = UIS_SET;
	} else {
		theme = nullptr;
		action = UIS_CLEAR;
	}

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

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

LRESULT CALLBACK ListView::WndProc(const HWND hWnd, const UINT uMsg,
    const WPARAM wParam, const LPARAM lParam)
{
	extern DlvDragger g_dragDlv;
	switch (uMsg) {
	case WM_NOTIFY:
		switch (reinterpret_cast<NMHDR*>(lParam)->code) {
		case HDN_ENDTRACK:
			UpdateLayout();
			return TRUE;
		}
		break;
	case WM_LBUTTONDOWN:
	case WM_NCLBUTTONDOWN:
		if (g_dragDlv.HandleLButtonDown())
			return 0;
		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 = reinterpret_cast<ListView*>(GetProp(hWnd, L"this"));

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

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