GridCtrl.h 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864
  1. #pragma once
  2. #include <afxtempl.h>
  3. #include "CellRange.h"
  4. #include "GridCell.h"
  5. ///////////////////////////////////////////////////////////////////////////////////
  6. // Defines - these determine the features (and the final size) of the final code
  7. ///////////////////////////////////////////////////////////////////////////////////
  8. //#define GRIDCONTROL_NO_TITLETIPS // Do not use titletips for cells with large data
  9. //#define GRIDCONTROL_NO_DRAGDROP // Do not use OLE drag and drop
  10. //#define GRIDCONTROL_NO_CLIPBOARD // Do not use clipboard routines
  11. #ifdef _WIN32_WCE
  12. # define GRIDCONTROL_NO_TITLETIPS // Do not use titletips for cells with large data
  13. # define GRIDCONTROL_NO_DRAGDROP // Do not use OLE drag and drop
  14. # define GRIDCONTROL_NO_CLIPBOARD // Do not use clipboard routines
  15. # define GRIDCONTROL_NO_PRINTING // Do not use printing routines
  16. # ifdef WCE_NO_PRINTING // Older versions of CE had different #def's
  17. # define _WIN32_WCE_NO_PRINTING
  18. # endif
  19. # ifdef WCE_NO_CURSOR
  20. # define _WIN32_WCE_NO_CURSOR
  21. # endif
  22. #endif // _WIN32_WCE
  23. // Use this as the classname when inserting this control as a custom control
  24. // in the MSVC++ dialog editor
  25. #define GRIDCTRL_CLASSNAME _T("MFCGridCtrl") // Window class name
  26. #define IDC_INPLACE_CONTROL 8 // ID of inplace edit controls
  27. ///////////////////////////////////////////////////////////////////////////////////
  28. // Conditional includes
  29. ///////////////////////////////////////////////////////////////////////////////////
  30. #ifndef GRIDCONTROL_NO_TITLETIPS
  31. # include "TitleTip.h"
  32. #endif
  33. #ifndef GRIDCONTROL_NO_DRAGDROP
  34. # include "GridDropTarget.h"
  35. # undef GRIDCONTROL_NO_CLIPBOARD // Force clipboard functions on
  36. #endif
  37. #ifndef GRIDCONTROL_NO_CLIPBOARD
  38. # include <afxole.h>
  39. #endif
  40. ///////////////////////////////////////////////////////////////////////////////////
  41. // Helper functions
  42. ///////////////////////////////////////////////////////////////////////////////////
  43. // Handy functions
  44. #define IsSHIFTpressed() ( (GetKeyState(VK_SHIFT) & (1 << (sizeof(SHORT)*8-1))) != 0 )
  45. #define IsCTRLpressed() ( (GetKeyState(VK_CONTROL) & (1 << (sizeof(SHORT)*8-1))) != 0 )
  46. // Backwards compatibility for pre 2.20 grid versions
  47. #define DDX_GridControl(pDX, nIDC, rControl) DDX_Control(pDX, nIDC, rControl)
  48. ///////////////////////////////////////////////////////////////////////////////////
  49. // Structures
  50. ///////////////////////////////////////////////////////////////////////////////////
  51. // This structure sent to Grid's parent in a WM_NOTIFY message
  52. typedef struct tagNM_GRIDVIEW {
  53. NMHDR hdr;
  54. int iRow;
  55. int iColumn;
  56. } NM_GRIDVIEW;
  57. // This is sent to the Grid from child in-place edit controls
  58. typedef struct tagGV_DISPINFO {
  59. NMHDR hdr;
  60. GV_ITEM item;
  61. } GV_DISPINFO;
  62. // This is sent to the Grid from child in-place edit controls
  63. typedef struct tagGV_CACHEHINT {
  64. NMHDR hdr;
  65. CCellRange range;
  66. } GV_CACHEHINT;
  67. // storage typedef for each row in the grid
  68. typedef CTypedPtrArray<CObArray, CGridCellBase*> GRID_ROW;
  69. // For virtual mode callback
  70. typedef BOOL(CALLBACK* GRIDCALLBACK)(GV_DISPINFO *, LPARAM);
  71. ///////////////////////////////////////////////////////////////////////////////////
  72. // Defines
  73. ///////////////////////////////////////////////////////////////////////////////////
  74. // Grid line/scrollbar selection
  75. #define GVL_NONE 0L // Neither
  76. #define GVL_HORZ 1L // Horizontal line or scrollbar
  77. #define GVL_VERT 2L // Vertical line or scrollbar
  78. #define GVL_BOTH 3L // Both
  79. // Autosizing option
  80. #define GVS_DEFAULT 0
  81. #define GVS_HEADER 1 // Size using column fixed cells data only
  82. #define GVS_DATA 2 // Size using column non-fixed cells data only
  83. #define GVS_BOTH 3 // Size using column fixed and non-fixed
  84. // Cell Searching options
  85. #define GVNI_FOCUSED 0x0001
  86. #define GVNI_SELECTED 0x0002
  87. #define GVNI_DROPHILITED 0x0004
  88. #define GVNI_READONLY 0x0008
  89. #define GVNI_FIXED 0x0010
  90. #define GVNI_MODIFIED 0x0020
  91. #define GVNI_ABOVE LVNI_ABOVE
  92. #define GVNI_BELOW LVNI_BELOW
  93. #define GVNI_TOLEFT LVNI_TOLEFT
  94. #define GVNI_TORIGHT LVNI_TORIGHT
  95. #define GVNI_ALL (LVNI_BELOW|LVNI_TORIGHT|LVNI_TOLEFT)
  96. #define GVNI_AREA (LVNI_BELOW|LVNI_TORIGHT)
  97. // Hit test values (not yet implemented)
  98. #define GVHT_DATA 0x0000
  99. #define GVHT_TOPLEFT 0x0001
  100. #define GVHT_COLHDR 0x0002
  101. #define GVHT_ROWHDR 0x0004
  102. #define GVHT_COLSIZER 0x0008
  103. #define GVHT_ROWSIZER 0x0010
  104. #define GVHT_LEFT 0x0020
  105. #define GVHT_RIGHT 0x0040
  106. #define GVHT_ABOVE 0x0080
  107. #define GVHT_BELOW 0x0100
  108. // Messages sent to the grid's parent (More will be added in future)
  109. #define GVN_BEGINDRAG LVN_BEGINDRAG // LVN_FIRST-9
  110. #define GVN_BEGINLABELEDIT LVN_BEGINLABELEDIT // LVN_FIRST-5
  111. #define GVN_BEGINRDRAG LVN_BEGINRDRAG
  112. #define GVN_COLUMNCLICK LVN_COLUMNCLICK
  113. #define GVN_DELETEITEM LVN_DELETEITEM
  114. #define GVN_ENDLABELEDIT LVN_ENDLABELEDIT // LVN_FIRST-6
  115. #define GVN_SELCHANGING LVN_ITEMCHANGING
  116. #define GVN_SELCHANGED LVN_ITEMCHANGED
  117. #define GVN_GETDISPINFO LVN_GETDISPINFO
  118. #define GVN_ODCACHEHINT LVN_ODCACHEHINT
  119. class CGridCtrl;
  120. /////////////////////////////////////////////////////////////////////////////
  121. // CGridCtrl window
  122. class CGridCtrl : public CWnd
  123. {
  124. DECLARE_DYNCREATE(CGridCtrl)
  125. friend class CGridCell;
  126. friend class CGridCellBase;
  127. // Construction
  128. public:
  129. CGridCtrl(int nRows = 0, int nCols = 0, int nFixedRows = 0, int nFixedCols = 0);
  130. BOOL Create(const RECT& rect, CWnd* parent, UINT nID,
  131. DWORD dwStyle = WS_CHILD | WS_BORDER | WS_TABSTOP | WS_VISIBLE);
  132. ///////////////////////////////////////////////////////////////////////////////////
  133. // Attributes
  134. ///////////////////////////////////////////////////////////////////////////////////
  135. public:
  136. int GetRowCount() const { return m_nRows; }
  137. int GetColumnCount() const { return m_nCols; }
  138. int GetFixedRowCount() const { return m_nFixedRows; }
  139. int GetFixedColumnCount() const { return m_nFixedCols; }
  140. BOOL SetRowCount(int nRows = 10);
  141. BOOL SetColumnCount(int nCols = 10);
  142. BOOL SetFixedRowCount(int nFixedRows = 1);
  143. BOOL SetFixedColumnCount(int nFixedCols = 1);
  144. int GetRowHeight(int nRow) const;
  145. BOOL SetRowHeight(int row, int height);
  146. int GetColumnWidth(int nCol) const;
  147. BOOL SetColumnWidth(int col, int width);
  148. BOOL GetCellOrigin(int nRow, int nCol, LPPOINT p);
  149. BOOL GetCellOrigin(const CCellID& cell, LPPOINT p);
  150. BOOL GetCellRect(int nRow, int nCol, LPRECT pRect);
  151. BOOL GetCellRect(const CCellID& cell, LPRECT pRect);
  152. BOOL GetTextRect(const CCellID& cell, LPRECT pRect);
  153. BOOL GetTextRect(int nRow, int nCol, LPRECT pRect);
  154. CCellID GetCellFromPt(CPoint point, BOOL bAllowFixedCellCheck = TRUE);
  155. int GetFixedRowHeight() const;
  156. int GetFixedColumnWidth() const;
  157. long GetVirtualWidth() const;
  158. long GetVirtualHeight() const;
  159. CSize GetTextExtent(int nRow, int nCol, LPCTSTR str);
  160. // EFW - Get extent of current text in cell
  161. inline CSize GetCellTextExtent(int nRow, int nCol) { return GetTextExtent(nRow, nCol, GetItemText(nRow, nCol)); }
  162. void SetGridBkColor(COLORREF clr) { m_crGridBkColour = clr; }
  163. COLORREF GetGridBkColor() const { return m_crGridBkColour; }
  164. void SetGridLineColor(COLORREF clr) { m_crGridLineColour = clr; }
  165. COLORREF GetGridLineColor() const { return m_crGridLineColour; }
  166. void SetTitleTipBackClr(COLORREF clr = CLR_DEFAULT) { m_crTTipBackClr = clr; }
  167. COLORREF GetTitleTipBackClr() { return m_crTTipBackClr; }
  168. void SetTitleTipTextClr(COLORREF clr = CLR_DEFAULT) { m_crTTipTextClr = clr; }
  169. COLORREF GetTitleTipTextClr() { return m_crTTipTextClr; }
  170. // ***************************************************************************** //
  171. // These have been deprecated. Use GetDefaultCell and then set the colors
  172. void SetTextColor(COLORREF clr) { m_cellDefault.SetTextClr(clr); }
  173. COLORREF GetTextColor() { return m_cellDefault.GetTextClr(); }
  174. void SetTextBkColor(COLORREF clr) { m_cellDefault.SetBackClr(clr); }
  175. COLORREF GetTextBkColor() { return m_cellDefault.GetBackClr(); }
  176. void SetFixedTextColor(COLORREF clr) {
  177. m_cellFixedRowDef.SetTextClr(clr);
  178. m_cellFixedColDef.SetTextClr(clr);
  179. m_cellFixedRowColDef.SetTextClr(clr);
  180. }
  181. COLORREF GetFixedTextColor() const { return m_cellFixedRowDef.GetTextClr(); }
  182. void SetFixedBkColor(COLORREF clr) {
  183. m_cellFixedRowDef.SetBackClr(clr);
  184. m_cellFixedColDef.SetBackClr(clr);
  185. m_cellFixedRowColDef.SetBackClr(clr);
  186. }
  187. COLORREF GetFixedBkColor() const { return m_cellFixedRowDef.GetBackClr(); }
  188. void SetGridColor(COLORREF clr) { SetGridLineColor(clr); }
  189. COLORREF GetGridColor() { return GetGridLineColor(); }
  190. void SetBkColor(COLORREF clr) { SetGridBkColor(clr); }
  191. COLORREF GetBkColor() { return GetGridBkColor(); }
  192. void SetDefCellMargin(int nMargin) {
  193. m_cellDefault.SetMargin(nMargin);
  194. m_cellFixedRowDef.SetMargin(nMargin);
  195. m_cellFixedColDef.SetMargin(nMargin);
  196. m_cellFixedRowColDef.SetMargin(nMargin);
  197. }
  198. int GetDefCellMargin() const { return m_cellDefault.GetMargin(); }
  199. int GetDefCellHeight() const { return m_cellDefault.GetHeight(); }
  200. void SetDefCellHeight(int nHeight) {
  201. m_cellDefault.SetHeight(nHeight);
  202. m_cellFixedRowDef.SetHeight(nHeight);
  203. m_cellFixedColDef.SetHeight(nHeight);
  204. m_cellFixedRowColDef.SetHeight(nHeight);
  205. }
  206. int GetDefCellWidth() const { return m_cellDefault.GetWidth(); }
  207. void SetDefCellWidth(int nWidth) {
  208. m_cellDefault.SetWidth(nWidth);
  209. m_cellFixedRowDef.SetWidth(nWidth);
  210. m_cellFixedColDef.SetWidth(nWidth);
  211. m_cellFixedRowColDef.SetWidth(nWidth);
  212. }
  213. // ***************************************************************************** //
  214. int GetSelectedCount() const { return (int)m_SelectedCellMap.GetCount(); }
  215. CCellID SetFocusCell(CCellID cell);
  216. CCellID SetFocusCell(int nRow, int nCol);
  217. CCellID GetFocusCell() const { return m_idCurrentCell; }
  218. void SetVirtualMode(BOOL bVirtual);
  219. BOOL GetVirtualMode() const { return m_bVirtualMode; }
  220. void SetCallbackFunc(GRIDCALLBACK pCallback,
  221. LPARAM lParam) {
  222. m_pfnCallback = pCallback; m_lParam = lParam;
  223. }
  224. GRIDCALLBACK GetCallbackFunc() { return m_pfnCallback; }
  225. void SetImageList(CImageList* pList) { m_pImageList = pList; }
  226. CImageList* GetImageList() const { return m_pImageList; }
  227. void SetGridLines(int nWhichLines = GVL_BOTH);
  228. int GetGridLines() const { return m_nGridLines; }
  229. void SetEditable(BOOL bEditable = TRUE) { m_bEditable = bEditable; }
  230. BOOL IsEditable() const { return m_bEditable; }
  231. void SetListMode(BOOL bEnableListMode = TRUE);
  232. BOOL GetListMode() const { return m_bListMode; }
  233. void SetSingleRowSelection(BOOL bSing = TRUE) { m_bSingleRowSelection = bSing; }
  234. BOOL GetSingleRowSelection() { return m_bSingleRowSelection & m_bListMode; }
  235. void SetSingleColSelection(BOOL bSing = TRUE) { m_bSingleColSelection = bSing; }
  236. BOOL GetSingleColSelection() { return m_bSingleColSelection; }
  237. void EnableSelection(BOOL bEnable = TRUE) { ResetSelectedRange(); m_bEnableSelection = bEnable; ResetSelectedRange(); }
  238. BOOL IsSelectable() const { return m_bEnableSelection; }
  239. void SetFixedColumnSelection(BOOL bSelect) { m_bFixedColumnSelection = bSelect; }
  240. BOOL GetFixedColumnSelection() { return m_bFixedColumnSelection; }
  241. void SetFixedRowSelection(BOOL bSelect) { m_bFixedRowSelection = bSelect; }
  242. BOOL GetFixedRowSelection() { return m_bFixedRowSelection; }
  243. void EnableDragAndDrop(BOOL bAllow = TRUE) { m_bAllowDragAndDrop = bAllow; }
  244. BOOL GetDragAndDrop() const { return m_bAllowDragAndDrop; }
  245. void SetRowResize(BOOL bResize = TRUE) { m_bAllowRowResize = bResize; }
  246. BOOL GetRowResize() const { return m_bAllowRowResize; }
  247. void SetColumnResize(BOOL bResize = TRUE) { m_bAllowColumnResize = bResize; }
  248. BOOL GetColumnResize() const { return m_bAllowColumnResize; }
  249. void SetHeaderSort(BOOL bSortOnClick = TRUE) { m_bSortOnClick = bSortOnClick; }
  250. BOOL GetHeaderSort() const { return m_bSortOnClick; }
  251. void SetHandleTabKey(BOOL bHandleTab = TRUE) { m_bHandleTabKey = bHandleTab; }
  252. BOOL GetHandleTabKey() const { return m_bHandleTabKey; }
  253. void SetDoubleBuffering(BOOL bBuffer = TRUE) { m_bDoubleBuffer = bBuffer; }
  254. BOOL GetDoubleBuffering() const { return m_bDoubleBuffer; }
  255. void EnableTitleTips(BOOL bEnable = TRUE) { m_bTitleTips = bEnable; }
  256. BOOL GetTitleTips() { return m_bTitleTips; }
  257. void SetSortColumn(int nCol);
  258. int GetSortColumn() const { return m_nSortColumn; }
  259. void SetSortAscending(BOOL bAscending) { m_bAscending = bAscending; }
  260. BOOL GetSortAscending() const { return m_bAscending; }
  261. void SetTrackFocusCell(BOOL bTrack) { m_bTrackFocusCell = bTrack; }
  262. BOOL GetTrackFocusCell() { return m_bTrackFocusCell; }
  263. void SetFrameFocusCell(BOOL bFrame) { m_bFrameFocus = bFrame; }
  264. BOOL GetFrameFocusCell() { return m_bFrameFocus; }
  265. void SetAutoSizeStyle(int nStyle = GVS_BOTH) { m_nAutoSizeColumnStyle = nStyle; }
  266. int GetAutoSizeStyle() { return m_nAutoSizeColumnStyle; }
  267. void EnableHiddenColUnhide(BOOL bEnable = TRUE) { m_bHiddenColUnhide = bEnable; }
  268. BOOL GetHiddenColUnhide() { return m_bHiddenColUnhide; }
  269. void EnableHiddenRowUnhide(BOOL bEnable = TRUE) { m_bHiddenRowUnhide = bEnable; }
  270. BOOL GetHiddenRowUnhide() { return m_bHiddenRowUnhide; }
  271. void EnableColumnHide(BOOL bEnable = TRUE) { m_bAllowColHide = bEnable; }
  272. BOOL GetColumnHide() { return m_bAllowColHide; }
  273. void EnableRowHide(BOOL bEnable = TRUE) { m_bAllowRowHide = bEnable; }
  274. BOOL GetRowHide() { return m_bAllowRowHide; }
  275. ///////////////////////////////////////////////////////////////////////////////////
  276. // default Grid cells. Use these for setting default values such as colors and fonts
  277. ///////////////////////////////////////////////////////////////////////////////////
  278. public:
  279. CGridCellBase* GetDefaultCell(BOOL bFixedRow, BOOL bFixedCol) const;
  280. ///////////////////////////////////////////////////////////////////////////////////
  281. // Grid cell Attributes
  282. ///////////////////////////////////////////////////////////////////////////////////
  283. public:
  284. CGridCellBase* GetCell(int nRow, int nCol) const; // Get the actual cell!
  285. void SetModified(BOOL bModified = TRUE, int nRow = -1, int nCol = -1);
  286. BOOL GetModified(int nRow = -1, int nCol = -1);
  287. BOOL IsCellFixed(int nRow, int nCol);
  288. BOOL SetItem(const GV_ITEM* pItem);
  289. BOOL GetItem(GV_ITEM* pItem);
  290. BOOL SetItemText(int nRow, int nCol, LPCTSTR str);
  291. // The following was virtual. If you want to override, use
  292. // CGridCellBase-derived class's GetText() to accomplish same thing
  293. CString GetItemText(int nRow, int nCol) const;
  294. // EFW - 06/13/99 - Added to support printf-style formatting codes.
  295. // Also supports use with a string resource ID
  296. #if !defined(_WIN32_WCE) || (_WIN32_WCE >= 210)
  297. BOOL SetItemTextFmt(int nRow, int nCol, LPCTSTR szFmt, ...);
  298. BOOL SetItemTextFmtID(int nRow, int nCol, UINT nID, ...);
  299. #endif
  300. BOOL SetItemData(int nRow, int nCol, LPARAM lParam);
  301. LPARAM GetItemData(int nRow, int nCol) const;
  302. BOOL SetItemImage(int nRow, int nCol, int iImage);
  303. int GetItemImage(int nRow, int nCol) const;
  304. BOOL SetItemState(int nRow, int nCol, UINT state);
  305. UINT GetItemState(int nRow, int nCol) const;
  306. BOOL SetItemFormat(int nRow, int nCol, UINT nFormat);
  307. UINT GetItemFormat(int nRow, int nCol) const;
  308. BOOL SetItemBkColour(int nRow, int nCol, COLORREF cr = CLR_DEFAULT);
  309. COLORREF GetItemBkColour(int nRow, int nCol) const;
  310. BOOL SetItemFgColour(int nRow, int nCol, COLORREF cr = CLR_DEFAULT);
  311. COLORREF GetItemFgColour(int nRow, int nCol) const;
  312. BOOL SetItemFont(int nRow, int nCol, const LOGFONT* lf);
  313. const LOGFONT* GetItemFont(int nRow, int nCol);
  314. BOOL IsItemEditing(int nRow, int nCol);
  315. BOOL SetCellType(int nRow, int nCol, CRuntimeClass* pRuntimeClass);
  316. BOOL SetDefaultCellType(CRuntimeClass* pRuntimeClass);
  317. ///////////////////////////////////////////////////////////////////////////////////
  318. // Operations
  319. ///////////////////////////////////////////////////////////////////////////////////
  320. public:
  321. int InsertColumn(LPCTSTR strHeading, UINT nFormat = DT_CENTER | DT_VCENTER | DT_SINGLELINE,
  322. int nColumn = -1);
  323. int InsertRow(LPCTSTR strHeading, int nRow = -1);
  324. BOOL DeleteColumn(int nColumn);
  325. BOOL DeleteRow(int nRow);
  326. BOOL DeleteNonFixedRows();
  327. BOOL DeleteAllItems();
  328. void ClearCells(CCellRange Selection);
  329. BOOL AutoSizeRow(int nRow, BOOL bResetScroll = TRUE);
  330. BOOL AutoSizeColumn(int nCol, UINT nAutoSizeStyle = GVS_DEFAULT, BOOL bResetScroll = TRUE);
  331. void AutoSizeRows();
  332. void AutoSizeColumns(UINT nAutoSizeStyle = GVS_DEFAULT);
  333. void AutoSize(UINT nAutoSizeStyle = GVS_DEFAULT);
  334. void ExpandColumnsToFit(BOOL bExpandFixed = TRUE);
  335. void ExpandLastColumn();
  336. void ExpandRowsToFit(BOOL bExpandFixed = TRUE);
  337. void ExpandToFit(BOOL bExpandFixed = TRUE);
  338. void Refresh();
  339. void AutoFill(); // Fill grid with blank cells
  340. void EnsureVisible(CCellID &cell) { EnsureVisible(cell.row, cell.col); }
  341. void EnsureVisible(int nRow, int nCol);
  342. BOOL IsCellVisible(int nRow, int nCol);
  343. BOOL IsCellVisible(CCellID cell);
  344. BOOL IsCellEditable(int nRow, int nCol) const;
  345. BOOL IsCellEditable(CCellID &cell) const;
  346. BOOL IsCellSelected(int nRow, int nCol) const;
  347. BOOL IsCellSelected(CCellID &cell) const;
  348. // SetRedraw stops/starts redraws on things like changing the # rows/columns
  349. // and autosizing, but not for user-intervention such as resizes
  350. void SetRedraw(BOOL bAllowDraw, BOOL bResetScrollBars = FALSE);
  351. BOOL RedrawCell(int nRow, int nCol, CDC* pDC = NULL);
  352. BOOL RedrawCell(const CCellID& cell, CDC* pDC = NULL);
  353. BOOL RedrawRow(int row);
  354. BOOL RedrawColumn(int col);
  355. #ifndef _WIN32_WCE
  356. BOOL Save(LPCTSTR filename, TCHAR chSeparator = _T(','));
  357. BOOL Load(LPCTSTR filename, TCHAR chSeparator = _T(','));
  358. #endif
  359. ///////////////////////////////////////////////////////////////////////////////////
  360. // Cell Ranges
  361. ///////////////////////////////////////////////////////////////////////////////////
  362. public:
  363. CCellRange GetCellRange() const;
  364. CCellRange GetSelectedCellRange() const;
  365. void SetSelectedRange(const CCellRange& Range, BOOL bForceRepaint = FALSE, BOOL bSelectCells = TRUE);
  366. void SetSelectedRange(int nMinRow, int nMinCol, int nMaxRow, int nMaxCol,
  367. BOOL bForceRepaint = FALSE, BOOL bSelectCells = TRUE);
  368. BOOL IsValid(int nRow, int nCol) const;
  369. BOOL IsValid(const CCellID& cell) const;
  370. BOOL IsValid(const CCellRange& range) const;
  371. ///////////////////////////////////////////////////////////////////////////////////
  372. // Clipboard, drag and drop, and cut n' paste operations
  373. ///////////////////////////////////////////////////////////////////////////////////
  374. #ifndef GRIDCONTROL_NO_CLIPBOARD
  375. virtual void CutSelectedText();
  376. virtual COleDataSource* CopyTextFromGrid();
  377. virtual BOOL PasteTextToGrid(CCellID cell, COleDataObject* pDataObject, BOOL bSelectPastedCells = TRUE);
  378. #endif
  379. #ifndef GRIDCONTROL_NO_DRAGDROP
  380. public:
  381. virtual void OnBeginDrag();
  382. virtual DROPEFFECT OnDragEnter(COleDataObject* pDataObject, DWORD dwKeyState, CPoint point);
  383. virtual DROPEFFECT OnDragOver(COleDataObject* pDataObject, DWORD dwKeyState, CPoint point);
  384. virtual void OnDragLeave();
  385. virtual BOOL OnDrop(COleDataObject* pDataObject, DROPEFFECT dropEffect, CPoint point);
  386. #endif
  387. #ifndef GRIDCONTROL_NO_CLIPBOARD
  388. virtual void OnEditCut();
  389. virtual void OnEditCopy();
  390. virtual void OnEditPaste();
  391. #endif
  392. virtual void OnEditSelectAll();
  393. ///////////////////////////////////////////////////////////////////////////////////
  394. // Misc.
  395. ///////////////////////////////////////////////////////////////////////////////////
  396. public:
  397. CCellID GetNextItem(CCellID& cell, int nFlags) const;
  398. BOOL SortItems(int nCol, BOOL bAscending, LPARAM data = 0);
  399. BOOL SortTextItems(int nCol, BOOL bAscending, LPARAM data = 0);
  400. BOOL SortItems(PFNLVCOMPARE pfnCompare, int nCol, BOOL bAscending, LPARAM data = 0);
  401. void SetCompareFunction(PFNLVCOMPARE pfnCompare);
  402. // in-built sort functions
  403. static int CALLBACK pfnCellTextCompare(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort);
  404. static int CALLBACK pfnCellNumericCompare(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort);
  405. ///////////////////////////////////////////////////////////////////////////////////
  406. // Printing
  407. ///////////////////////////////////////////////////////////////////////////////////
  408. #if !defined(_WIN32_WCE_NO_PRINTING) && !defined(GRIDCONTROL_NO_PRINTING)
  409. public:
  410. void Print(CPrintDialog* pPrntDialog = NULL);
  411. // EFW - New printing support functions
  412. void EnableWysiwygPrinting(BOOL bEnable = TRUE) { m_bWysiwygPrinting = bEnable; }
  413. BOOL GetWysiwygPrinting() { return m_bWysiwygPrinting; }
  414. void SetShadedPrintOut(BOOL bEnable = TRUE) { m_bShadedPrintOut = bEnable; }
  415. BOOL GetShadedPrintOut(void) { return m_bShadedPrintOut; }
  416. // Use -1 to have it keep the existing value
  417. void SetPrintMarginInfo(int nHeaderHeight, int nFooterHeight,
  418. int nLeftMargin, int nRightMargin, int nTopMargin,
  419. int nBottomMargin, int nGap);
  420. void GetPrintMarginInfo(int &nHeaderHeight, int &nFooterHeight,
  421. int &nLeftMargin, int &nRightMargin, int &nTopMargin,
  422. int &nBottomMargin, int &nGap);
  423. ///////////////////////////////////////////////////////////////////////////////////
  424. // Printing overrides for derived classes
  425. ///////////////////////////////////////////////////////////////////////////////////
  426. public:
  427. virtual void OnBeginPrinting(CDC *pDC, CPrintInfo *pInfo);
  428. virtual void OnPrint(CDC *pDC, CPrintInfo *pInfo);
  429. virtual void OnEndPrinting(CDC *pDC, CPrintInfo *pInfo);
  430. #endif // #if !defined(_WIN32_WCE_NO_PRINTING) && !defined(GRIDCONTROL_NO_PRINTING)
  431. // Implementation
  432. public:
  433. //Merge the selected cells
  434. //by Huang Wei
  435. CGridCellBase* GetCell(CCellID cell);
  436. CCellID GetMergeCellID(CCellID cell);
  437. void UnMergeSelectedCells();
  438. void MergeSelectedCells();
  439. void UnMergeCells(int nStartRow, int nStartCol, int nEndRow, int nEndCol);
  440. void MergeCells(int nStartRow, int nStartCol, int nEndRow, int nEndCol);
  441. int GetMergeCellWidth(CCellID cell);
  442. int GetMergeCellHeight(CCellID cell);
  443. BOOL GetCellOriginNoMerge(int nRow, int nCol, LPPOINT p);
  444. BOOL GetCellOriginNoMerge(const CCellID& cell, LPPOINT p);
  445. virtual ~CGridCtrl();
  446. protected:
  447. BOOL RegisterWindowClass();
  448. BOOL Initialise();
  449. void SetupDefaultCells();
  450. LRESULT SendMessageToParent(int nRow, int nCol, int nMessage) const;
  451. LRESULT SendDisplayRequestToParent(GV_DISPINFO* pDisplayInfo) const;
  452. LRESULT SendCacheHintToParent(const CCellRange& range) const;
  453. BOOL InvalidateCellRect(const int row, const int col);
  454. BOOL InvalidateCellRect(const CCellID& cell);
  455. BOOL InvalidateCellRect(const CCellRange& cellRange);
  456. void EraseBkgnd(CDC* pDC);
  457. BOOL GetCellRangeRect(const CCellRange& cellRange, LPRECT lpRect);
  458. BOOL SetCell(int nRow, int nCol, CGridCellBase* pCell);
  459. int SetMouseMode(int nMode) { int nOldMode = m_MouseMode; m_MouseMode = nMode; return nOldMode; }
  460. int GetMouseMode() const { return m_MouseMode; }
  461. BOOL MouseOverRowResizeArea(CPoint& point);
  462. BOOL MouseOverColumnResizeArea(CPoint& point);
  463. CCellID GetTopleftNonFixedCell(BOOL bForceRecalculation = FALSE);
  464. CCellRange GetUnobstructedNonFixedCellRange(BOOL bForceRecalculation = FALSE);
  465. CCellRange GetVisibleNonFixedCellRange(LPRECT pRect = NULL, BOOL bForceRecalculation = FALSE);
  466. BOOL IsVisibleVScroll() { return ((m_nBarState & GVL_VERT) > 0); }
  467. BOOL IsVisibleHScroll() { return ((m_nBarState & GVL_HORZ) > 0); }
  468. void ResetSelectedRange();
  469. void ResetScrollBars();
  470. void EnableScrollBars(int nBar, BOOL bEnable = TRUE);
  471. int GetScrollPos32(int nBar, BOOL bGetTrackPos = FALSE);
  472. BOOL SetScrollPos32(int nBar, int nPos, BOOL bRedraw = TRUE);
  473. BOOL SortTextItems(int nCol, BOOL bAscending, int low, int high);
  474. BOOL SortItems(PFNLVCOMPARE pfnCompare, int nCol, BOOL bAscending, LPARAM data,
  475. int low, int high);
  476. CPoint GetPointClicked(int nRow, int nCol, const CPoint& point);
  477. void ValidateAndModifyCellContents(int nRow, int nCol, LPCTSTR strText);
  478. // Overrrides
  479. // ClassWizard generated virtual function overrides
  480. //{{AFX_VIRTUAL(CGridCtrl)
  481. protected:
  482. virtual void PreSubclassWindow();
  483. //}}AFX_VIRTUAL
  484. protected:
  485. #if !defined(_WIN32_WCE_NO_PRINTING) && !defined(GRIDCONTROL_NO_PRINTING)
  486. // Printing
  487. virtual void PrintFixedRowCells(int nStartColumn, int nStopColumn, int& row, CRect& rect,
  488. CDC *pDC, BOOL& bFirst);
  489. virtual void PrintColumnHeadings(CDC *pDC, CPrintInfo *pInfo);
  490. virtual void PrintHeader(CDC *pDC, CPrintInfo *pInfo);
  491. virtual void PrintFooter(CDC *pDC, CPrintInfo *pInfo);
  492. virtual void PrintRowButtons(CDC *pDC, CPrintInfo* /*pInfo*/);
  493. #endif
  494. #ifndef GRIDCONTROL_NO_DRAGDROP
  495. // Drag n' drop
  496. virtual CImageList* CreateDragImage(CPoint *pHotSpot); // no longer necessary
  497. #endif
  498. // Mouse Clicks
  499. virtual void OnFixedColumnClick(CCellID& cell);
  500. virtual void OnFixedRowClick(CCellID& cell);
  501. // Editing
  502. virtual void OnEditCell(int nRow, int nCol, CPoint point, UINT nChar);
  503. virtual void OnEndEditCell(int nRow, int nCol, CString str);
  504. virtual BOOL ValidateEdit(int nRow, int nCol, LPCTSTR str);
  505. virtual void EndEditing();
  506. // Drawing
  507. virtual void OnDraw(CDC* pDC);
  508. // CGridCellBase Creation and Cleanup
  509. virtual CGridCellBase* CreateCell(int nRow, int nCol);
  510. virtual void DestroyCell(int nRow, int nCol);
  511. // Attributes
  512. protected:
  513. // General attributes
  514. COLORREF m_crFixedTextColour, m_crFixedBkColour;
  515. COLORREF m_crGridBkColour, m_crGridLineColour;
  516. COLORREF m_crWindowText, m_crWindowColour, m_cr3DFace, // System colours
  517. m_crShadow;
  518. COLORREF m_crTTipBackClr, m_crTTipTextClr; // Titletip colours - FNA
  519. BOOL m_bVirtualMode;
  520. LPARAM m_lParam; // lParam for callback
  521. GRIDCALLBACK m_pfnCallback; // The callback function
  522. int m_nGridLines;
  523. BOOL m_bEditable;
  524. BOOL m_bModified;
  525. BOOL m_bAllowDragAndDrop;
  526. BOOL m_bListMode;
  527. BOOL m_bSingleRowSelection;
  528. BOOL m_bSingleColSelection;
  529. BOOL m_bAllowDraw;
  530. BOOL m_bEnableSelection;
  531. BOOL m_bFixedRowSelection, m_bFixedColumnSelection;
  532. BOOL m_bSortOnClick;
  533. BOOL m_bHandleTabKey;
  534. BOOL m_bDoubleBuffer;
  535. BOOL m_bTitleTips;
  536. int m_nBarState;
  537. BOOL m_bWysiwygPrinting;
  538. BOOL m_bHiddenColUnhide, m_bHiddenRowUnhide;
  539. BOOL m_bAllowColHide, m_bAllowRowHide;
  540. BOOL m_bAutoSizeSkipColHdr;
  541. BOOL m_bTrackFocusCell;
  542. BOOL m_bFrameFocus;
  543. UINT m_nAutoSizeColumnStyle;
  544. // Cell size details
  545. int m_nRows, m_nFixedRows, m_nCols, m_nFixedCols;
  546. CUIntArray m_arRowHeights, m_arColWidths;
  547. int m_nVScrollMax, m_nHScrollMax;
  548. // Fonts and images
  549. CRuntimeClass* m_pRtcDefault; // determines kind of Grid Cell created by default
  550. CGridDefaultCell m_cellDefault; // "default" cell. Contains default colours, font etc.
  551. CGridDefaultCell m_cellFixedColDef, m_cellFixedRowDef, m_cellFixedRowColDef;
  552. CFont m_PrinterFont; // for the printer
  553. CImageList* m_pImageList;
  554. // Cell data
  555. CTypedPtrArray<CObArray, GRID_ROW*> m_RowData;
  556. // Mouse operations such as cell selection
  557. int m_MouseMode;
  558. BOOL m_bLMouseButtonDown, m_bRMouseButtonDown;
  559. CPoint m_LeftClickDownPoint, m_LastMousePoint;
  560. CCellID m_LeftClickDownCell, m_SelectionStartCell;
  561. CCellID m_idCurrentCell, m_idTopLeftCell;
  562. int m_nTimerID;
  563. int m_nTimerInterval;
  564. int m_nResizeCaptureRange;
  565. BOOL m_bAllowRowResize, m_bAllowColumnResize;
  566. int m_nRowsPerWheelNotch;
  567. CMap<DWORD, DWORD, CCellID, CCellID&> m_SelectedCellMap, m_PrevSelectedCellMap;
  568. #ifndef GRIDCONTROL_NO_TITLETIPS
  569. CTitleTip m_TitleTip; // Title tips for cells
  570. #endif
  571. // Drag and drop
  572. CCellID m_LastDragOverCell;
  573. #ifndef GRIDCONTROL_NO_DRAGDROP
  574. CGridDropTarget m_DropTarget; // OLE Drop target for the grid
  575. #endif
  576. // Printing information
  577. CSize m_CharSize;
  578. int m_nPageHeight;
  579. CSize m_LogicalPageSize, // Page size in gridctrl units.
  580. m_PaperSize; // Page size in device units.
  581. // additional properties to support Wysiwyg printing
  582. int m_nPageWidth;
  583. int m_nPrintColumn;
  584. int m_nCurrPrintRow;
  585. int m_nNumPages;
  586. int m_nPageMultiplier;
  587. // sorting
  588. int m_bAscending;
  589. int m_nSortColumn;
  590. PFNLVCOMPARE m_pfnCompare;
  591. // EFW - Added to support shaded/unshaded printout. If true, colored
  592. // cells will print as-is. If false, all text prints as black on white.
  593. BOOL m_bShadedPrintOut;
  594. // EFW - Added support for user-definable margins. Top and bottom are in
  595. // lines. Left, right, and gap are in characters (avg width is used).
  596. int m_nHeaderHeight, m_nFooterHeight, m_nLeftMargin,
  597. m_nRightMargin, m_nTopMargin, m_nBottomMargin, m_nGap;
  598. protected:
  599. void SelectAllCells();
  600. void SelectColumns(CCellID currentCell, BOOL bForceRedraw = FALSE, BOOL bSelectCells = TRUE);
  601. void SelectRows(CCellID currentCell, BOOL bForceRedraw = FALSE, BOOL bSelectCells = TRUE);
  602. void SelectCells(CCellID currentCell, BOOL bForceRedraw = FALSE, BOOL bSelectCells = TRUE);
  603. void OnSelecting(const CCellID& currentCell);
  604. // Generated message map functions
  605. //{{AFX_MSG(CGridCtrl)
  606. afx_msg void OnPaint();
  607. afx_msg void OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar);
  608. afx_msg void OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar);
  609. afx_msg void OnSize(UINT nType, int cx, int cy);
  610. afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
  611. afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
  612. afx_msg void OnMouseMove(UINT nFlags, CPoint point);
  613. afx_msg void OnTimer(UINT_PTR nIDEvent);
  614. afx_msg UINT OnGetDlgCode();
  615. afx_msg void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags);
  616. afx_msg void OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags);
  617. afx_msg void OnChar(UINT nChar, UINT nRepCnt, UINT nFlags);
  618. afx_msg void OnLButtonDblClk(UINT nFlags, CPoint point);
  619. afx_msg BOOL OnEraseBkgnd(CDC* pDC);
  620. afx_msg void OnSysKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags);
  621. afx_msg void OnUpdateEditSelectAll(CCmdUI* pCmdUI);
  622. //}}AFX_MSG
  623. #ifndef _WIN32_WCE_NO_CURSOR
  624. afx_msg BOOL OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message);
  625. #endif
  626. #ifndef _WIN32_WCE
  627. afx_msg void OnRButtonDown(UINT nFlags, CPoint point);
  628. afx_msg void OnRButtonUp(UINT nFlags, CPoint point); // EFW - Added
  629. afx_msg void OnSysColorChange();
  630. #endif
  631. #ifndef _WIN32_WCE_NO_CURSOR
  632. afx_msg void OnCaptureChanged(CWnd *pWnd);
  633. #endif
  634. #ifndef GRIDCONTROL_NO_CLIPBOARD
  635. afx_msg void OnUpdateEditCopy(CCmdUI* pCmdUI);
  636. afx_msg void OnUpdateEditCut(CCmdUI* pCmdUI);
  637. afx_msg void OnUpdateEditPaste(CCmdUI* pCmdUI);
  638. #endif
  639. #if (_MFC_VER >= 0x0421) || (_WIN32_WCE >= 210)
  640. afx_msg void OnSettingChange(UINT uFlags, LPCTSTR lpszSection);
  641. #endif
  642. #if !defined(_WIN32_WCE) && (_MFC_VER >= 0x0421)
  643. afx_msg BOOL OnMouseWheel(UINT nFlags, short zDelta, CPoint pt);
  644. #endif
  645. afx_msg LRESULT OnSetFont(WPARAM hFont, LPARAM lParam);
  646. afx_msg LRESULT OnGetFont(WPARAM hFont, LPARAM lParam);
  647. afx_msg LRESULT OnImeChar(WPARAM wCharCode, LPARAM lParam);
  648. afx_msg void OnEndInPlaceEdit(NMHDR* pNMHDR, LRESULT* pResult);
  649. DECLARE_MESSAGE_MAP()
  650. enum eMouseModes {
  651. MOUSE_NOTHING, MOUSE_SELECT_ALL, MOUSE_SELECT_COL, MOUSE_SELECT_ROW,
  652. MOUSE_SELECT_CELLS, MOUSE_SCROLLING_CELLS,
  653. MOUSE_OVER_ROW_DIVIDE, MOUSE_SIZING_ROW,
  654. MOUSE_OVER_COL_DIVIDE, MOUSE_SIZING_COL,
  655. MOUSE_PREPARE_EDIT,
  656. #ifndef GRIDCONTROL_NO_DRAGDROP
  657. MOUSE_PREPARE_DRAG, MOUSE_DRAGGING
  658. #endif
  659. };
  660. };
  661. // Returns the default cell implementation for the given grid region
  662. inline CGridCellBase* CGridCtrl::GetDefaultCell(BOOL bFixedRow, BOOL bFixedCol) const
  663. {
  664. if (bFixedRow && bFixedCol) return (CGridCellBase*)&m_cellFixedRowColDef;
  665. if (bFixedRow) return (CGridCellBase*)&m_cellFixedRowDef;
  666. if (bFixedCol) return (CGridCellBase*)&m_cellFixedColDef;
  667. return (CGridCellBase*)&m_cellDefault;
  668. }
  669. inline CGridCellBase* CGridCtrl::GetCell(int nRow, int nCol) const
  670. {
  671. if (nRow < 0 || nRow >= m_nRows || nCol < 0 || nCol >= m_nCols)
  672. return NULL;
  673. if (GetVirtualMode())
  674. {
  675. CGridCellBase* pCell = GetDefaultCell(nRow < m_nFixedRows, nCol < m_nFixedCols);
  676. static GV_DISPINFO gvdi;
  677. gvdi.item.row = nRow;
  678. gvdi.item.col = nCol;
  679. gvdi.item.mask = 0xFFFFFFFF;
  680. gvdi.item.nState = 0;
  681. gvdi.item.nFormat = pCell->GetFormat();
  682. gvdi.item.iImage = pCell->GetImage();
  683. gvdi.item.crBkClr = pCell->GetBackClr();
  684. gvdi.item.crFgClr = pCell->GetTextClr();
  685. gvdi.item.lParam = pCell->GetData();
  686. memcpy(&gvdi.item.lfFont, pCell->GetFont(), sizeof(LOGFONT));
  687. gvdi.item.nMargin = pCell->GetMargin();
  688. gvdi.item.strText.Empty();
  689. // Fix the state bits
  690. if (IsCellSelected(nRow, nCol)) gvdi.item.nState |= GVIS_SELECTED;
  691. if (nRow < GetFixedRowCount()) gvdi.item.nState |= (GVIS_FIXED | GVIS_FIXEDROW);
  692. if (nCol < GetFixedColumnCount()) gvdi.item.nState |= (GVIS_FIXED | GVIS_FIXEDCOL);
  693. if (GetFocusCell() == CCellID(nRow, nCol)) gvdi.item.nState |= GVIS_FOCUSED;
  694. if (m_pfnCallback)
  695. m_pfnCallback(&gvdi, m_lParam);
  696. else
  697. SendDisplayRequestToParent(&gvdi);
  698. static CGridCell cell;
  699. cell.SetState(gvdi.item.nState);
  700. cell.SetFormat(gvdi.item.nFormat);
  701. cell.SetImage(gvdi.item.iImage);
  702. cell.SetBackClr(gvdi.item.crBkClr);
  703. cell.SetTextClr(gvdi.item.crFgClr);
  704. cell.SetData(gvdi.item.lParam);
  705. cell.SetFont(&(gvdi.item.lfFont));
  706. cell.SetMargin(gvdi.item.nMargin);
  707. cell.SetText(gvdi.item.strText);
  708. cell.SetGrid((CGridCtrl*)this);
  709. return (CGridCellBase*)&cell;
  710. }
  711. GRID_ROW* pRow = m_RowData[nRow];
  712. if (!pRow) return NULL;
  713. return pRow->GetAt(nCol);
  714. }
  715. inline BOOL CGridCtrl::SetCell(int nRow, int nCol, CGridCellBase* pCell)
  716. {
  717. if (GetVirtualMode())
  718. return FALSE;
  719. if (nRow < 0 || nRow >= m_nRows || nCol < 0 || nCol >= m_nCols)
  720. return FALSE;
  721. GRID_ROW* pRow = m_RowData[nRow];
  722. if (!pRow) return FALSE;
  723. pCell->SetCoords(nRow, nCol);
  724. pRow->SetAt(nCol, pCell);
  725. return TRUE;
  726. }