GridCell.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. // GridCell.cpp : implementation file
  2. //
  3. // MFC Grid Control - Main grid cell class
  4. //
  5. // Provides the implementation for the "default" cell type of the
  6. // grid control. Adds in cell editing.
  7. //
  8. // Written by Chris Maunder <cmaunder@mail.com>
  9. // Copyright (c) 1998-2001. All Rights Reserved.
  10. //
  11. // This code may be used in compiled form in any way you desire. This
  12. // file may be redistributed unmodified by any means PROVIDING it is
  13. // not sold for profit without the authors written consent, and
  14. // providing that this notice and the authors name and all copyright
  15. // notices remains intact.
  16. //
  17. // An email letting me know how you are using it would be nice as well.
  18. //
  19. // This file is provided "as is" with no expressed or implied warranty.
  20. // The author accepts no liability for any damage/loss of business that
  21. // this product may cause.
  22. //
  23. // For use with CGridCtrl v2.20+
  24. //
  25. // History:
  26. // Eric Woodruff - 20 Feb 2000 - Added PrintCell() plus other minor changes
  27. // Ken Bertelson - 12 Apr 2000 - Split CGridCell into CGridCell and CGridCellBase
  28. // <kenbertelson@hotmail.com>
  29. // C Maunder - 17 Jun 2000 - Font handling optimsed, Added CGridDefaultCell
  30. //
  31. /////////////////////////////////////////////////////////////////////////////
  32. #include "stdafx.h"
  33. #include "GridCell.h"
  34. #include "InPlaceEdit.h"
  35. #include "GridCtrl.h"
  36. #ifdef _DEBUG
  37. #define new DEBUG_NEW
  38. #undef THIS_FILE
  39. static char THIS_FILE[] = __FILE__;
  40. #endif
  41. IMPLEMENT_DYNCREATE(CGridCell, CGridCellBase)
  42. IMPLEMENT_DYNCREATE(CGridDefaultCell, CGridCell)
  43. /////////////////////////////////////////////////////////////////////////////
  44. // GridCell
  45. CGridCell::CGridCell()
  46. {
  47. m_plfFont = NULL;
  48. Reset();
  49. }
  50. CGridCell::~CGridCell()
  51. {
  52. delete m_plfFont;
  53. }
  54. /////////////////////////////////////////////////////////////////////////////
  55. // GridCell Attributes
  56. void CGridCell::operator=(const CGridCell& cell)
  57. {
  58. CGridCellBase::operator=(cell);
  59. }
  60. void CGridCell::Reset()
  61. {
  62. CGridCellBase::Reset();
  63. m_strText.Empty();
  64. m_nImage = -1;
  65. m_pGrid = NULL;
  66. m_bEditing = FALSE;
  67. m_pEditWnd = NULL;
  68. m_nFormat = (DWORD)-1; // Use default from CGridDefaultCell
  69. m_crBkClr = CLR_DEFAULT; // Background colour (or CLR_DEFAULT)
  70. m_crFgClr = CLR_DEFAULT; // Forground colour (or CLR_DEFAULT)
  71. m_nMargin = (UINT)-1; // Use default from CGridDefaultCell
  72. delete m_plfFont;
  73. m_plfFont = NULL; // Cell font
  74. }
  75. void CGridCell::SetFont(const LOGFONT* plf)
  76. {
  77. if (plf == NULL)
  78. {
  79. delete m_plfFont;
  80. m_plfFont = NULL;
  81. }
  82. else
  83. {
  84. if (!m_plfFont)
  85. m_plfFont = new LOGFONT;
  86. if (m_plfFont)
  87. memcpy(m_plfFont, plf, sizeof(LOGFONT));
  88. }
  89. }
  90. LOGFONT* CGridCell::GetFont() const
  91. {
  92. if (m_plfFont == NULL)
  93. {
  94. CGridDefaultCell *pDefaultCell = (CGridDefaultCell*)GetDefaultCell();
  95. if (!pDefaultCell)
  96. return NULL;
  97. return pDefaultCell->GetFont();
  98. }
  99. return m_plfFont;
  100. }
  101. CFont* CGridCell::GetFontObject() const
  102. {
  103. // If the default font is specified, use the default cell implementation
  104. if (m_plfFont == NULL)
  105. {
  106. CGridDefaultCell *pDefaultCell = (CGridDefaultCell*)GetDefaultCell();
  107. if (!pDefaultCell)
  108. return NULL;
  109. return pDefaultCell->GetFontObject();
  110. }
  111. else
  112. {
  113. static CFont Font;
  114. Font.DeleteObject();
  115. Font.CreateFontIndirect(m_plfFont);
  116. return &Font;
  117. }
  118. }
  119. DWORD CGridCell::GetFormat() const
  120. {
  121. if (m_nFormat == (DWORD)-1)
  122. {
  123. CGridDefaultCell *pDefaultCell = (CGridDefaultCell*)GetDefaultCell();
  124. if (!pDefaultCell)
  125. return 0;
  126. return pDefaultCell->GetFormat();
  127. }
  128. return m_nFormat;
  129. }
  130. UINT CGridCell::GetMargin() const
  131. {
  132. if (m_nMargin == (UINT)-1)
  133. {
  134. CGridDefaultCell *pDefaultCell = (CGridDefaultCell*)GetDefaultCell();
  135. if (!pDefaultCell)
  136. return 0;
  137. return pDefaultCell->GetMargin();
  138. }
  139. return m_nMargin;
  140. }
  141. /////////////////////////////////////////////////////////////////////////////
  142. // GridCell Operations
  143. BOOL CGridCell::Edit(int nRow, int nCol, CRect rect, CPoint /* point */, UINT nID, UINT nChar)
  144. {
  145. if (m_bEditing)
  146. {
  147. if (m_pEditWnd)
  148. m_pEditWnd->SendMessage(WM_CHAR, nChar);
  149. }
  150. else
  151. {
  152. DWORD dwStyle = ES_LEFT;
  153. if (GetFormat() & DT_RIGHT)
  154. dwStyle = ES_RIGHT;
  155. else if (GetFormat() & DT_CENTER)
  156. dwStyle = ES_CENTER;
  157. m_bEditing = TRUE;
  158. // InPlaceEdit auto-deletes itself
  159. CGridCtrl* pGrid = GetGrid();
  160. m_pEditWnd = new CInPlaceEdit(pGrid, rect, dwStyle, nID, nRow, nCol, GetText(), nChar);
  161. }
  162. return TRUE;
  163. }
  164. void CGridCell::EndEdit()
  165. {
  166. if (m_pEditWnd)
  167. ((CInPlaceEdit*)m_pEditWnd)->EndEdit();
  168. }
  169. void CGridCell::OnEndEdit()
  170. {
  171. m_bEditing = FALSE;
  172. m_pEditWnd = NULL;
  173. }
  174. /////////////////////////////////////////////////////////////////////////////
  175. // CGridDefaultCell
  176. CGridDefaultCell::CGridDefaultCell()
  177. {
  178. #ifdef _WIN32_WCE
  179. m_nFormat = DT_LEFT|DT_VCENTER|DT_SINGLELINE|DT_NOPREFIX;
  180. #else
  181. m_nFormat = DT_LEFT | DT_VCENTER | DT_SINGLELINE | DT_NOPREFIX | DT_END_ELLIPSIS;
  182. #endif
  183. m_crFgClr = CLR_DEFAULT;
  184. m_crBkClr = CLR_DEFAULT;
  185. m_Size = CSize(30, 10);
  186. m_dwStyle = 0;
  187. #ifdef _WIN32_WCE
  188. LOGFONT lf;
  189. GetObject(GetStockObject(SYSTEM_FONT), sizeof(LOGFONT), &lf);
  190. SetFont(&lf);
  191. #else // not CE
  192. NONCLIENTMETRICS ncm;
  193. ncm.cbSize = sizeof(NONCLIENTMETRICS);
  194. VERIFY(SystemParametersInfo(SPI_GETNONCLIENTMETRICS, sizeof(NONCLIENTMETRICS), &ncm, 0));
  195. SetFont(&(ncm.lfMessageFont));
  196. #endif
  197. }
  198. CGridDefaultCell::~CGridDefaultCell()
  199. {
  200. m_Font.DeleteObject();
  201. }
  202. void CGridDefaultCell::SetFont(const LOGFONT* plf)
  203. {
  204. ASSERT(plf);
  205. if (!plf) return;
  206. m_Font.DeleteObject();
  207. m_Font.CreateFontIndirect(plf);
  208. CGridCell::SetFont(plf);
  209. // Get the font size and hence the default cell size
  210. CDC* pDC = CDC::FromHandle(::GetDC(NULL));
  211. if (pDC)
  212. {
  213. CFont* pOldFont = pDC->SelectObject(&m_Font);
  214. SetMargin(pDC->GetTextExtent(_T(" "), 1).cx);
  215. m_Size = pDC->GetTextExtent(_T(" XXXXXXXXXXXX "), 14);
  216. m_Size.cy = (m_Size.cy * 3) / 2;
  217. pDC->SelectObject(pOldFont);
  218. //ReleaseDC(NULL, pDC->GetSafeHdc());//?????
  219. }
  220. else
  221. {
  222. SetMargin(3);
  223. m_Size = CSize(40, 16);
  224. }
  225. }
  226. LOGFONT* CGridDefaultCell::GetFont() const
  227. {
  228. ASSERT(m_plfFont); // This is the default - it CAN'T be NULL!
  229. return m_plfFont;
  230. }
  231. CFont* CGridDefaultCell::GetFontObject() const
  232. {
  233. ASSERT(m_Font.GetSafeHandle());
  234. return (CFont*)&m_Font;
  235. }