File: NstCtrlListView.cpp

package info (click to toggle)
nestopia 1.52.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 12,140 kB
  • sloc: cpp: 127,444; xml: 27,234; ansic: 3,635; makefile: 949; sh: 19
file content (275 lines) | stat: -rw-r--r-- 6,699 bytes parent folder | download | duplicates (3)
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
////////////////////////////////////////////////////////////////////////////////////////
//
// Nestopia - NES/Famicom emulator written in C++
//
// Copyright (C) 2003-2008 Martin Freij
//
// This file is part of Nestopia.
//
// Nestopia is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// Nestopia is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Nestopia; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
////////////////////////////////////////////////////////////////////////////////////////

#include "NstWindowCustom.hpp"
#include "NstCtrlListView.hpp"
#include <CommCtrl.h>

namespace Nestopia
{
	namespace Window
	{
		namespace Control
		{
			void ListView::ColumnsProxy::Insert(const uint index,wcstring const text) const
			{
				NST_ASSERT( text );

				LVCOLUMN lvColumn;

				lvColumn.mask = LVCF_FMT|LVCF_TEXT;
				lvColumn.fmt = LVCFMT_LEFT;
				lvColumn.pszText = const_cast<wchar_t*>( text );

				ListView_InsertColumn( control, index, &lvColumn );
			}

			void ListView::ColumnsProxy::Align() const
			{
				for (int i=0; ListView_SetColumnWidth( control, i, LVSCW_AUTOSIZE_USEHEADER ); ++i)
				{
					const int header = ListView_GetColumnWidth( control, i );

					ListView_SetColumnWidth( control, i, LVSCW_AUTOSIZE );

					const int width = ListView_GetColumnWidth( control, i );

					if (width < header)
						ListView_SetColumnWidth( control, i, header );
				}
			}

			void ListView::ColumnsProxy::GetOrder(int* const array,const uint count) const
			{
				ListView_GetColumnOrderArray( control, count, array );
			}

			uint ListView::ColumnsProxy::GetIndex(const uint index) const
			{
				LVCOLUMN lvColumn;
				lvColumn.mask = LVCF_ORDER;

				ListView_GetColumn( control, index, &lvColumn );

				return NST_MAX(lvColumn.iOrder,0);
			}

			void ListView::ColumnsProxy::Clear() const
			{
				while (ListView_DeleteColumn( control, 0 ));
			}

			ListView::StyleExProxy::operator uint () const
			{
				return ListView_GetExtendedListViewStyle( control );
			}

			void ListView::StyleExProxy::operator = (const uint style) const
			{
				ListView_SetExtendedListViewStyle( control, style );
			}

			void ListView::Item::TextProxy::operator << (wcstring const text) const
			{
				ListView_SetItemText( item.control, item.index, index, const_cast<wchar_t*>(text) );
			}

			void ListView::Item::TextProxy::GetText(Buffer& buffer) const
			{
				NST_ASSERT( buffer.Empty() );

				LVITEM lvItem;
				lvItem.iSubItem = index;

				do
				{
					buffer.Resize( buffer.Length() + BLOCK_SIZE );

					lvItem.cchTextMax = buffer.Length() + 1;
					lvItem.pszText = buffer.Ptr();

					lvItem.cchTextMax = item.control.Send( LVM_GETITEMTEXT, item.index, &lvItem );
				}
				while (*(buffer.Ptr() + buffer.Length()));

				buffer.ShrinkTo( lvItem.cchTextMax );
			}

			void ListView::Item::DataProxy::operator = (const LPARAM data) const
			{
				LVITEM lvItem;

				lvItem.mask = LVIF_PARAM;
				lvItem.iItem = item.index;
				lvItem.iSubItem = 0;
				lvItem.lParam = data;

				ListView_SetItem( item.control, &lvItem );
			}

			ListView::Item::DataProxy::operator LPARAM () const
			{
				LVITEM lvItem;

				lvItem.mask = LVIF_PARAM;
				lvItem.iItem = item.index;
				lvItem.iSubItem = 0;
				lvItem.lParam = 0;

				ListView_GetItem( item.control, &lvItem );

				return lvItem.lParam;
			}

			bool ListView::Item::Delete() const
			{
				return index != ~0U ? ListView_DeleteItem( control, index ) : false;
			}

			void ListView::Item::Select(const bool state) const
			{
				ListView_SetItemState( control, index, state ? LVIS_SELECTED : 0, LVIS_SELECTED );
			}

			void ListView::Item::Check(const bool state) const
			{
				ListView_SetCheckState( control, index, state );
			}

			bool ListView::Item::Checked() const
			{
				return ListView_GetCheckState( control, index );
			}

			void ListView::Item::Show() const
			{
				ListView_EnsureVisible( control, index, false );
			}

			ListView::Item ListView::Selection() const
			{
				return Item( control, ListView_GetNextItem( control, -1, LVNI_SELECTED ) );
			}

			int ListView::Add(GenericString text,const LPARAM data,const bool checked) const
			{
				HeapString tmp;

				LVITEM lvItem;

				if (text.Length())
				{
					if (text.NullTerminated())
					{
						lvItem.pszText = const_cast<wchar_t*>(text.Ptr());
					}
					else
					{
						tmp = text;
						lvItem.pszText = tmp.Ptr();
					}
				}
				else
				{
					lvItem.pszText = LPSTR_TEXTCALLBACK;
				}

				lvItem.mask = LVIF_TEXT;

				if (data)
					lvItem.mask |= LVIF_PARAM;

				lvItem.iItem = INT_MAX;
				lvItem.iSubItem = 0;
				lvItem.lParam = data;

				const int index = ListView_InsertItem( control, &lvItem );

				if (checked)
					ListView_SetCheckState( control, index, true );

				return index;
			}

			uint ListView::Size() const
			{
				const int count = ListView_GetItemCount( control );
				return NST_MAX(count,0);
			}

			void ListView::Clear() const
			{
				ListView_DeleteAllItems( control );
			}

			void ListView::Reserve(const uint capacity) const
			{
				if (capacity > Size())
					ListView_SetItemCount( control, capacity );
			}

			void ListView::SetBkColor(const uint color) const
			{
				ListView_SetBkColor( control, color );
			}

			void ListView::SetTextColor(const uint color) const
			{
				ListView_SetTextColor( control, color );
			}

			void ListView::SetTextBkColor(const uint color) const
			{
				ListView_SetTextBkColor( control, color );
			}

			bool ListView::HitTest(const uint x,const uint y) const
			{
				LVHITTESTINFO lvHitTestInfo;

				lvHitTestInfo.pt.x = x;
				lvHitTestInfo.pt.y = y;

				::ScreenToClient( control, &lvHitTestInfo.pt );

				return ListView_HitTest( control, &lvHitTestInfo ) != -1;
			}

			void ListView::Sort(const SortFunction& sortFunction) const
			{
				if (Size() > 1)
					ListView_SortItems( control, SortProc, reinterpret_cast<WPARAM>(&sortFunction) );
			}

			int CALLBACK ListView::SortProc(LPARAM obj1,LPARAM obj2,LPARAM ref)
			{
				return (*reinterpret_cast<const SortFunction*>(ref))
				(
					reinterpret_cast<const void*>( obj1 ),
					reinterpret_cast<const void*>( obj2 )
				);
			}
		}
	}
}