summaryrefslogtreecommitdiffstats
path: root/Src/nu/listview.cpp
blob: 05e60efb938f9c376d0b703137a08d87be9e1eb8 (plain) (blame)
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
** Copyright  (C) 2003 Nullsoft, Inc.
**
** This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held 
** liable for any damages arising from the use of this software. 
**
** Permission is granted to anyone to use this software for any purpose, including commercial applications, and to 
** alter it and redistribute it freely, subject to the following restrictions:
**
**   1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. 
**      If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
**
**   2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
**
**   3. This notice may not be removed or altered from any source distribution.
**
*/

#include <windows.h>
#include <commctrl.h>

#include "listview.h"

void W_ListView::AddImageCol( int w )
{
	LVCOLUMN lvc = { 0, };
	lvc.mask     = LVCF_WIDTH | LVCF_FMT | LVCF_SUBITEM;
	lvc.fmt      = LVCFMT_IMAGE;
	lvc.iSubItem = m_col;

	if ( w )
		lvc.cx = w;

	ListView_InsertColumn( m_hwnd, m_col, &lvc );
	++m_col;
}

int W_ListView::GetColumnWidth( int col )
{
	if ( col < 0 || col >= m_col )
		return 0;

	return ListView_GetColumnWidth( m_hwnd, col );
}

int W_ListView::GetParam( int p )
{
	LVITEM lvi = { 0, };
	lvi.mask  = LVIF_PARAM;
	lvi.iItem = p;

	ListView_GetItem( m_hwnd, &lvi );

	return (int)(INT_PTR)lvi.lParam;
}

void W_ListView::SetItemParam( int p, int param )
{
	LVITEM lvi = { 0, };
	lvi.iItem  = p;
	lvi.mask   = LVIF_PARAM;
	lvi.lParam = param;

	ListView_SetItem( m_hwnd, &lvi );
}

void W_ListView::SetFont( HFONT newFont )
{
	if ( m_font )
	{
		if ( m_hwnd )
			SetWindowFont( m_hwnd, NULL, FALSE );

		DeleteFont( m_font );
	}

	m_font = NULL;
	if ( m_hwnd )
	{
		SetWindowFont( m_hwnd, newFont, FALSE );
		InvalidateRect( m_hwnd, NULL, TRUE );
	}
}

int W_ListView::FindItemByPoint( int x, int y )
{
	int l = GetCount();
	for ( int i = 0; i < l; i++ )
	{
		RECT r;
		GetItemRect( i, &r );
		if ( r.left <= x && r.right >= x && r.top <= y && r.bottom >= y )
			return i;
	}
	return -1;
}

W_ListView::W_ListView()
{
	m_hwnd = NULL;
	m_col  = 0;
	m_font = NULL;
}

W_ListView::W_ListView( HWND hwnd )
{
	m_hwnd = NULL;
	m_col  = 0;
	m_font = NULL;

	if ( IsWindow( hwnd ) )
	{
		m_hwnd = hwnd;
		ListView_SetExtendedListViewStyleEx( m_hwnd, LVS_EX_FULLROWSELECT | LVS_EX_INFOTIP, LVS_EX_FULLROWSELECT | LVS_EX_INFOTIP );
	}
}

W_ListView::W_ListView( HWND hwndDlg, int resourceId )
{
	m_hwnd = NULL;
	m_col  = 0;
	m_font = NULL;

	m_hwnd = GetDlgItem( hwndDlg, resourceId );
	if ( IsWindow( m_hwnd ) )
	{
		ListView_SetExtendedListViewStyleEx( m_hwnd, LVS_EX_FULLROWSELECT | LVS_EX_INFOTIP, LVS_EX_FULLROWSELECT | LVS_EX_INFOTIP );
	}
}

W_ListView::~W_ListView()
{
	if ( m_font )
		DeleteFont( m_font );

	m_font = 0;
}

void W_ListView::SetTextColors(COLORREF foregroundColor, COLORREF backgroundColor)
{
	ListView_SetTextColor(m_hwnd, foregroundColor);
	ListView_SetTextBkColor(m_hwnd, backgroundColor);
}

void W_ListView::InvertSelection()
{
	int n = GetCount();
	for (int i = 0; i < n; i++)
	{
		if (GetSelected(i))
			Unselect(i);
		else
			SetSelected(i);
	}
}

/* unicode / ansi trouble spots go below this line */
void W_ListView::AddAutoCol(LPTSTR text)
{
	LVCOLUMN lvc = {0};
	lvc.mask     = LVCF_TEXT;
	lvc.pszText  = text;

	ListView_InsertColumn(m_hwnd, m_col, &lvc);
	m_col++;
}

void W_ListView::AddCol(const wchar_t *text, int w)
{
	LVCOLUMN lvc = {0};
	lvc.mask     = LVCF_TEXT | LVCF_WIDTH;
	lvc.pszText  = (LPTSTR)text;

	if (w) 
		lvc.cx = w;

	SendMessageW(m_hwnd, LVM_INSERTCOLUMNW, (WPARAM)m_col, (LPARAM)&lvc);
	m_col++;
}

void W_ListView::AddCol(const char *text, int w)
{
	LVCOLUMN lvc = {0};
	lvc.mask = LVCF_TEXT | LVCF_WIDTH;
	lvc.pszText = (LPTSTR) text;
	if (w) lvc.cx = w;
	SendMessageA(m_hwnd, LVM_INSERTCOLUMNA, (WPARAM)m_col, (LPARAM)&lvc);
	m_col++;
}

int W_ListView::AppendItem(LPCWSTR text, LPARAM param)
{
	LVITEM lvi = {0};
	lvi.mask       = LVIF_TEXT | LVIF_PARAM;
	lvi.iItem      = GetCount();
	lvi.pszText    = (LPTSTR) text;
	lvi.cchTextMax = wcslen(text);
	lvi.lParam     = param;

	return (int)(INT_PTR)SendMessageW(m_hwnd, LVM_INSERTITEMW, 0, (LPARAM)&lvi);
}

int W_ListView::InsertItem(int p, const wchar_t *text, LPARAM param)
{
	LVITEM lvi = {0};
	lvi.mask       = LVIF_TEXT | LVIF_PARAM;
	lvi.iItem      = p;
	lvi.pszText    = (LPTSTR) text;
	lvi.cchTextMax = wcslen(text);
	lvi.lParam     = param;

	return (int)(INT_PTR)SendMessageW(m_hwnd, LVM_INSERTITEMW, 0, (LPARAM)&lvi);
}

int W_ListView::InsertItem(int p, const char *text, LPARAM param)
{
	LVITEM lvi = {0};
	lvi.mask       = LVIF_TEXT | LVIF_PARAM;
	lvi.iItem      = p;
	lvi.pszText    = (LPTSTR) text;
	lvi.cchTextMax = lstrlenA(text);
	lvi.lParam     = param;

	return (int)(INT_PTR)SendMessageA(m_hwnd, LVM_INSERTITEMA, 0, (LPARAM)&lvi);
}

void W_ListView::SetItemText(int p, int si, const wchar_t *text)
{
	LVITEM lvi = {0};
	lvi.iItem      = p;
	lvi.iSubItem   = si;
	lvi.mask       = LVIF_TEXT;
	lvi.pszText    = (LPTSTR)text;
	lvi.cchTextMax = wcslen(text);

	SendMessageW(m_hwnd, LVM_SETITEMW, 0, (LPARAM)&lvi);
}

void W_ListView::SetItemText(int p, int si, const char *text)
{
	LVITEM lvi = {0};
	lvi.iItem      = p;
	lvi.iSubItem   = si;
	lvi.mask       = LVIF_TEXT;
	lvi.pszText    = (LPTSTR)text;
	lvi.cchTextMax = lstrlenA(text);

	SendMessageA(m_hwnd, LVM_SETITEMA, 0, (LPARAM)&lvi);
}

void W_ListView::setwnd (HWND hwnd)
{
	m_hwnd = hwnd;
	if (IsWindow(hwnd))
	{
		ListView_SetExtendedListViewStyleEx(hwnd, LVS_EX_FULLROWSELECT | LVS_EX_INFOTIP, LVS_EX_FULLROWSELECT | LVS_EX_INFOTIP);
#if defined(_UNICODE) || defined(UNICODE)
		SendMessageW(hwnd, CCM_SETUNICODEFORMAT, TRUE, 0);
#endif
	}
}

void W_ListView::GetText(int p, int si, wchar_t *text, int maxlen)
{
	LVITEM lvi = {0};
	lvi.iItem      = p;
	lvi.iSubItem   = si;
	lvi.mask       = LVIF_TEXT;
	lvi.pszText    = (LPTSTR)text;
	lvi.cchTextMax = maxlen;

	SendMessageW(m_hwnd, LVM_GETITEMTEXTW, p,  (LPARAM)&lvi);
}

void W_ListView::GetText(int p, int si, char *text, int maxlen)
{
	LVITEM lvi = {0};
	lvi.iItem      = p;
	lvi.iSubItem   = si;
	lvi.mask       = LVIF_TEXT;
	lvi.pszText    = (LPTSTR)text;
	lvi.cchTextMax = maxlen;

	SendMessageA(m_hwnd, LVM_GETITEMTEXTA, p,  (LPARAM)&lvi);
}