-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathReportCtrl.h
304 lines (264 loc) · 13.6 KB
/
ReportCtrl.h
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
///////////////////////////////////////////////////////////////////////////////
// ReportCtrl.h
//
// CReportCtrl, a CListCtrl derived class that is specialized on "Report View"
// style.
//
// Features:
//
// 1, Item sorting by clicking on column header.
// 2, Sub-item text edit.
// 3, Item repositioning.
// 4, Customizing checkbox styles, including "single" and "disabled".
// 5, Sending a message to parent window when user clicks on a checkbox.
// 6, Convenient item insertion, deletion, moving, and sub-item text changing.
// 7, Sub-item images and color
// 8, And much more...
//
// This code may be used in compiled form in any way you desire. This file may be
// redistributed unmodified by any means PROVIDING it is not sold for profit without
// the authors written consent, and providing that this notice and the authors name
// is included. If the source code in this file is used in any commercial application
// then acknowledgement must be made to the author of this file .
//
// This file is provided "as is" with no expressed or implied warranty.
//
// Written by Bin Liu ([email protected])
//
// History
//
// Nov. 26, 2003 - Initial release.
// Dec. 03, 2003 - Fixed a bug in "EndEdit" where item text were not preperly committed.
// Completed the implementation of the "Sort-Separator" feature.
// Jan. 01, 2004 - Fixed a bug in "SetItemData".
// - Added message "WM_EDIT_COMMITTED" which is sent to the parent window
// when an item text editing is committed.
// - Fixed a bug in "SetItemText"(double type).
// - Fixed a bug where item sorting does not work properly when there
// are multiple CReportCtrl objects on same window.
//
///////////////////////////////////////////////////////////////////////////////
#ifndef __REPORTCTRL_H__
#define __REPORTCTRL_H__
// Sent to parent window when user clicked on the checkbox of an item:
// wParam: The item index in the list ctrl
// lParam: The mouse event type(WM_LBUTTONDOWN, WM_RBUTTONDOWN, etc) which generated this event.
// Note: This message is not sent when the checkbox states were altered programmatically
// by calling "SetItem", it is only sent when the user "physically" clicked the
// checkbox using mouse or joystick etc.
#define WM_ON_CHKBOX (WM_APP + 10000)
// Sent to parent window when a column of items were sorted
// wParam: The column index
// lParam: The sort method, either 0(descending) or 1(ascending)
#define WM_ITEM_SORTED (WM_APP + 10001)
// Sent to parent window when an item text editing was committed
// wParam: The item index
// lParam: The column index
#define WM_EDIT_COMMITTED (WM_APP + 10002)
// Checkbox styles.
#define RC_CHKBOX_NONE 0 // No checkbox displayed
#define RC_CHKBOX_NORMAL 1 // Normal, multiple check allowed
#define RC_CHKBOX_SINGLE 2 // Single check only
#define RC_CHKBOX_DISABLED 3 // Disabled, cannot be checked/unchecked by user input,
// but can be by your code.
// Item state flags for selection, deletion, etc.
// Multiple flags can be combined together using the bit-or operator.
// Note: If RC_ITEM_ALL is set, all other flags are ignored
#define RC_ITEM_NONE 0x0000 // Void, indicates invalid items only
#define RC_ITEM_ALL 0x0001 // All items regardless of states
#define RC_ITEM_SELECTED 0x0002 // Selected items
#define RC_ITEM_UNSELECTED 0x0004 // Unselected items
#define RC_ITEM_CHECKED 0x0008 // Checked items
#define RC_ITEM_UNCHECKED 0x0010 // Unchecked items
#define RC_ITEM_FOCUSED 0x0020 // Focused item
#define RC_ITEM_UNFOCUSED 0x0040 // Unfocused items
// Item inverting types
#define RC_INVERT_SELECTION 0 // Invert item selection
#define RC_INVERT_CHECKMARK 1 // Invert item check mark
// Removes any custom color from item text and item backgroun
#define COLOR_INVALID 0xffffffff
//////////////////////////////////////////////////////////////////////////
// The CReportCtrl Class Definition
//////////////////////////////////////////////////////////////////////////
class CReportCtrl : public CListCtrl
{
public:
//////////////////////////////////////////////////////////////////////
// Constructor & Destructor
//////////////////////////////////////////////////////////////////////
CReportCtrl();
virtual ~CReportCtrl();
//////////////////////////////////////////////////////////////////////
// Run-time Creation
//////////////////////////////////////////////////////////////////////
virtual BOOL Create(CWnd* pParentWnd, UINT nID, LPCRECT lpRect = NULL, DWORD dwStyle = WS_BORDER | WS_TABSTOP);
///////////////////////////////////////////////////////////////////////
// Column Header attributes
///////////////////////////////////////////////////////////////////////
BOOL SetColumnHeader(const CString& strHeadings); // Set columns and their formats.
int GetColumnCount() const; // Get the column count.
BOOL DeleteAllColumns();
CString GetHeaderText(int nColumn) const;
BOOL SetHeaderText(int nColumn, LPCTSTR lpText);
BOOL HasColumnHeader() const; // FALSE if the list control has LVS_NOCOLUMNHEADER flag
const CHeaderCtrl* GetHeaderCtrl() const;
///////////////////////////////////////////////////////////////////////
// Images & Color
///////////////////////////////////////////////////////////////////////
// Column header images
BOOL SetHeaderImage(int nColumn, int nImageIndex, BOOL bLeftSide = TRUE);
int GetHeaderImage(int nColumn) const;
CImageList* SetHeaderImageList(UINT nBitmapID, COLORREF crMask = RGB(255, 0, 255));
CImageList* SetHeaderImageList(CImageList* pImageList);
// Sub-item images
BOOL SetItemImage(int nItem, int nSubItem, int nImageIndex);
int GetItemImage(int nItem, int nSubItem) const;
CImageList* SetImageList(UINT nBitmapID, COLORREF crMask = RGB(255, 0, 255));
CImageList* SetImageList(CImageList* pImageList);
CImageList* GetImageList() const;
// Sub-item Text & Background Color
void SetItemTextColor(int nItem = -1, int nSubItem = -1, COLORREF color = COLOR_INVALID, BOOL bRedraw = TRUE);
COLORREF GetItemTextColor(int nItem, int nSubItem) const;
void SetItemBkColor(int nItem = -1, int nSubItem = -1, COLORREF color = COLOR_INVALID, BOOL bRedraw = TRUE);
COLORREF GetItemBkColor(int nItem, int nSubItem) const;
//////////////////////////////////////////////////////////////////////
// Control Styles & Attributes
//////////////////////////////////////////////////////////////////////
void ResizeToFitParent(); // Resize the list control to occupy parent's client area
void SetGridLines(BOOL bSet = TRUE); // Show grid lines.
void SetCheckboxeStyle(int nStyle = RC_CHKBOX_NORMAL); // Set checkbox styles.
int GetCheckboxStyle() const;
BOOL IsSortable() const; // Is sort allowed?
BOOL SetSortable(BOOL bSet);// Allow/disallow sorting
BOOL IsEditable() const; // Is Item text editable?
void SetEditable(BOOL bSet = TRUE); // Allow item text editting
///////////////////////////////////////////////////////////////////////
// Item attributes & operations
///////////////////////////////////////////////////////////////////////
// Item states operation
int GetFirstItem(DWORD dwStates = RC_ITEM_ALL, int nStartAfter = -1) const;
int GetLastItem(DWORD dwStates = RC_ITEM_ALL, int nStartBefore = -1) const;
int GetItemCount(DWORD dwStates = RC_ITEM_ALL) const;
DWORD GetItemStates(int nItem) const;
BOOL ExamItemStates(int nItem, DWORD dwStates) const;
BOOL SetItemStates(int nItem, DWORD dwNewStates);
int SetAllItemStates(DWORD dwOldStates, DWORD dwNewStates);
void InvertItems(int nType); // RC_INVERT_SELECTION or RC_INVERT_CHECKMARK
// Item Insertion & Deletion
int InsertItemEx(int nItem, LPCTSTR lpText, ...);
BOOL DeleteItem(int nItem, BOOL bSelectNextItem = FALSE); // Delete an item.
int DeleteAllItems(DWORD dwStates = RC_ITEM_ALL); // Delete all qualified items.
// Item positioning
int MoveUp(int nItem, int nCount = 1); // Move an item upwards by "nCount" positions.
int MoveDown(int nItem, int nCount = 1); // Move an item downwards by "nCount" positions.
int MoveToTop(int nItem); // Move an item up to the top.
int MoveToBottom(int nItem); // Move an item down to the bottom.
int MoveTo(int nItem, int nNewPosition); // Move an item to a particular position
BOOL SwapItems(int nItem1, int nItem2); // Swap two items in the list, including all attributes.
// Convenient versions of "CListCtrl::SetItemText"
BOOL SetItemText(int nItem, int nSubItem, INT val);
BOOL SetItemText(int nItem, int nSubItem, UINT val);
BOOL SetItemText(int nItem, int nSubItem, LONG val);
BOOL SetItemText(int nItem, int nSubItem, ULONG val);
BOOL SetItemText(int nItem, int nSubItem, TCHAR val);
BOOL SetItemText(int nItem, int nSubItem, DOUBLE val, int nPrecision = -1);
BOOL SetItemText(int nItem, int nSubItem, const COleDateTime& dateTime, DWORD dwFlags = 0);
// Sorting
BOOL IsSortAscending() const;
int GetSortedColumn() const;
void SortItems(int nColumn, BOOL bAscending); // Sort a specified column.
void SetSortSeparator(LPCTSTR lpSortSeparator = NULL); // Sort-separator, NULL=disabled
LPCTSTR GetSortSeparator() const;
// Item text edit
BOOL StartEdit(int nItem, int nSubItem); // Display the editbox, previous edit are committed
BOOL EndEdit(BOOL bCommit = TRUE); // Commit/cancel text edit, hide the editbox
CEdit* GetEditControl();
///////////////////////////////////////////////////////////////////////
// Necessary overloading but Irrelevant to Users
///////////////////////////////////////////////////////////////////////
int InsertColumn(int nCol, LPCTSTR lpColumnHeading, int nFormat = LVCFMT_LEFT, int nWidth = -1, int nSubItem = -1);
int InsertColumn(int nCol, const LVCOLUMN* pColumn);
BOOL DeleteColumn(int nCol);
BOOL SetCheck(int nItem, BOOL bCheck = TRUE); // overloaded to guard the "single" checkbox style
BOOL SetItem(int nItem, int nSubItem, UINT nMask, LPCTSTR lpszItem, int nImage, UINT nState, UINT nStateMask, LPARAM lParam);
BOOL SetItem(const LVITEM* pItem);
BOOL GetItem(LVITEM* pItem) const;
DWORD GetItemData(int nItem) const;
BOOL SetItemData(int nItem, DWORD dwData);
DWORD SetExtendedStyle(DWORD dwNewStyle);
BOOL ModifyStyle(DWORD dwRemove, DWORD dwAdd, UINT nFlags = 0);
BOOL ModifyStyleEx(DWORD dwRemove, DWORD dwAdd, UINT nFlags = 0);
int InsertItem(UINT nMask, int nItem, LPCTSTR lpItem, UINT nState, UINT nStateMask, int nImage, LPARAM lParam);
int InsertItem(int nItem, LPCTSTR lpItem, int nImage);
int InsertItem(const LVITEM* pItem);
int InsertItem(int nIndex, LPCTSTR lpText);
BOOL SetItemText(int nItem, int nSubItem, LPCTSTR lpText);
protected:
// Helper functions for internal use
BOOL _IsValidIndex(int nIndex) const;
BOOL _ItemCheckMonitor(int nIndex, BOOL bBefore, BOOL bAfter, UINT nMsg);
BOOL _IsEditVisible() const;
void _MouseClkMonitor(UINT nMsg, UINT nFlags, CPoint point, BOOL bTriggerEdit);
static void _StringSplit(const CString &str, CStringArray& arr, TCHAR chDelimitior);
void _UnsetSortedColumn();
BOOL _SetItemStatesNoVerify(int nItem, DWORD dwNewStates);
void _EnsureSingleCheck(int nItem);
DWORD _GetHeaderTextFormat(int nColumn) const;
void _UpdateColumn(int nColumn, BOOL bInsert);
void _AllocItemMemory(int nItem);
void _FreeItemMemory(int nItem);
BOOL _PartialSort(int nStart, int nEnd);
int _FindSeparator(int nStartAfter, int nColumn) const;
void _QuickSortRecursive(int* pArr, int nLow, int nHigh);
int _CompareItems(int nItem1, int nItem2);
// Member data
CEdit* m_pWndEdit; // Edit control, for subitem edit
LPTSTR m_pszSeparator; // Sort separator
BOOL m_bAllowEdit; // Is subitem edit allowed?
POINT m_ptEditting; // Position of the subitem that is currently being edited
int m_nChkStyle; // Checkbox style
DWORD m_dwPrevEditFmt; // Format of previously edited subitem
CImageList m_imgList; // Image list for items
CImageList m_headerImgList; // Image list for the header control
int m_nSortCol; // The sorted column, -1 if none
BOOL m_bSortAscending; // Is sort ascending?
public:
void SetNoEditCol(int* pNoEditCol, int nNoEditColNum);
private:
bool IsNoEditCol(int col);
private:
int* m_pNoEditCol;
int m_nNoEditColNum;
//////////////////////////////////////////////////////////////////////
// Wizard Generated Stuff
//////////////////////////////////////////////////////////////////////
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CReportCtrl)
public:
virtual BOOL PreTranslateMessage(MSG* pMsg);
protected:
virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
virtual void PreSubclassWindow();
//}}AFX_VIRTUAL
// Generated message map functions
protected:
//{{AFX_MSG(CReportCtrl)
afx_msg void OnColumnclick(NMHDR* pNMHDR, LRESULT* pResult);
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
afx_msg void OnDestroy();
afx_msg void OnLButtonDblClk(UINT nFlags, CPoint point);
afx_msg void OnMButtonDown(UINT nFlags, CPoint point);
afx_msg void OnMButtonDblClk(UINT nFlags, CPoint point);
afx_msg void OnRButtonDown(UINT nFlags, CPoint point);
afx_msg void OnRButtonDblClk(UINT nFlags, CPoint point);
afx_msg void OnCustomDraw(NMHDR* pNMHDR, LRESULT* pResult);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
//////////////////////////////////////////////////////////////////////////
// End of CReportCtrl Class Definition
//////////////////////////////////////////////////////////////////////////
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // __REPORTCTRL_H__