aboutsummaryrefslogtreecommitdiff
path: root/c/listview.cpp
blob: 4e9d75484592ca2a249d4290edbde0cd9c1f3d3c (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 <uxtheme.h>

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

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

ListView::ListView(HMENU hMenu, DWORD dwStyle)
{
	m_bHeader = !(dwStyle & LVS_NOCOLUMNHEADER);

	m_hWnd = CreateWindowEx(
	    WS_EX_CLIENTEDGE,
	    WC_LISTVIEW,
	    TEXT(""),
	    dwStyle|WS_CHILD|WS_VISIBLE|WS_VSCROLL|WS_TABSTOP|LVS_REPORT|LVS_SHOWSELALWAYS,
	    0, 0, 0, 0,
	    g_hWnd, hMenu, GetModuleHandle(NULL), this
	);

	if (SetProp(m_hWnd, TEXT("this"), (HANDLE)this))
		m_prevProc = (WNDPROC)SetWindowLongPtr(m_hWnd,
		    GWLP_WNDPROC, (LONG_PTR)::WndProc);

	ListView_SetExtendedListViewStyle(m_hWnd, LVS_EX_FULLROWSELECT);

	SendMessage(m_hWnd, WM_SETFONT, (WPARAM)g_hfNormal, MAKELPARAM(FALSE, 0));
}

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

HWND ListView::HWnd() const
{
	return m_hWnd;
}

void ListView::UpdateTheme(BOOL bThemeActive)
{
	DWORD dwStyle;
	LPTSTR tszTheme;
	WORD wAction;
	extern int g_bThemes;

	if (!g_bThemes) return;
	if (bThemeActive) {
		dwStyle = LVS_EX_DOUBLEBUFFER;
		tszTheme = TEXT("Explorer");
		wAction = UIS_SET;
	} else {
		dwStyle = 0;
		tszTheme = NULL;
		wAction = UIS_CLEAR;
	}

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

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

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

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

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

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

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

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