GridCell.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. #include "pch.h"
  2. #include "GridCell.h"
  3. #include "InPlaceEdit.h"
  4. IMPLEMENT_DYNCREATE(CGridCell, CGridCellBase)
  5. IMPLEMENT_DYNCREATE(CGridDefaultCell, CGridCell)
  6. /////////////////////////////////////////////////////////////////////////////
  7. // GridCell
  8. CGridCell::CGridCell()
  9. {
  10. m_plfFont = NULL;
  11. CGridCell::Reset();
  12. }
  13. CGridCell::~CGridCell()
  14. {
  15. delete m_plfFont;
  16. }
  17. /////////////////////////////////////////////////////////////////////////////
  18. // GridCell Attributes
  19. //Used for merge cells,remove const
  20. //by Huang Wei
  21. void CGridCell::operator=(CGridCell& cell)
  22. {
  23. if (this != &cell) CGridCellBase::operator=(cell);
  24. }
  25. void CGridCell::Reset()
  26. {
  27. CGridCellBase::Reset();
  28. m_strText.Empty();
  29. m_nImage = -1;
  30. m_pGrid = NULL;
  31. m_bEditing = FALSE;
  32. m_pEditWnd = NULL;
  33. m_nFormat = (DWORD)-1; // Use default from CGridDefaultCell
  34. m_crBkClr = CLR_DEFAULT; // Background colour (or CLR_DEFAULT)
  35. m_crFgClr = CLR_DEFAULT; // Forground colour (or CLR_DEFAULT)
  36. m_nMargin = (UINT)-1; // Use default from CGridDefaultCell
  37. m_nBorder = 0;
  38. m_crBorderClr = CLR_DEFAULT;
  39. delete m_plfFont;
  40. m_plfFont = NULL; // Cell font
  41. }
  42. void CGridCell::SetFont(const LOGFONT* plf)
  43. {
  44. if (plf == NULL)
  45. {
  46. delete m_plfFont;
  47. m_plfFont = NULL;
  48. }
  49. else
  50. {
  51. if (!m_plfFont)
  52. m_plfFont = new LOGFONT;
  53. if (m_plfFont)
  54. memcpy(m_plfFont, plf, sizeof(LOGFONT));
  55. }
  56. }
  57. LOGFONT* CGridCell::GetFont() const
  58. {
  59. if (m_plfFont == NULL)
  60. {
  61. CGridDefaultCell *pDefaultCell = (CGridDefaultCell*)GetDefaultCell();
  62. if (!pDefaultCell)
  63. return NULL;
  64. return pDefaultCell->GetFont();
  65. }
  66. return m_plfFont;
  67. }
  68. CFont* CGridCell::GetFontObject() const
  69. {
  70. // If the default font is specified, use the default cell implementation
  71. if (m_plfFont == NULL)
  72. {
  73. CGridDefaultCell *pDefaultCell = (CGridDefaultCell*)GetDefaultCell();
  74. if (!pDefaultCell)
  75. return NULL;
  76. return pDefaultCell->GetFontObject();
  77. }
  78. else
  79. {
  80. static CFont Font;
  81. Font.DeleteObject();
  82. Font.CreateFontIndirect(m_plfFont);
  83. return &Font;
  84. }
  85. }
  86. DWORD CGridCell::GetFormat() const
  87. {
  88. if (m_nFormat == (DWORD)-1)
  89. {
  90. CGridDefaultCell *pDefaultCell = (CGridDefaultCell*)GetDefaultCell();
  91. if (!pDefaultCell)
  92. return 0;
  93. return pDefaultCell->GetFormat();
  94. }
  95. return m_nFormat;
  96. }
  97. UINT CGridCell::GetMargin() const
  98. {
  99. if (m_nMargin == (UINT)-1)
  100. {
  101. CGridDefaultCell *pDefaultCell = (CGridDefaultCell*)GetDefaultCell();
  102. if (!pDefaultCell)
  103. return 0;
  104. return pDefaultCell->GetMargin();
  105. }
  106. return m_nMargin;
  107. }
  108. /////////////////////////////////////////////////////////////////////////////
  109. // GridCell Operations
  110. BOOL CGridCell::Edit(int nRow, int nCol, CRect rect, CPoint /* point */, UINT nID, UINT nChar)
  111. {
  112. if (m_bEditing)
  113. {
  114. if (m_pEditWnd)
  115. m_pEditWnd->SendMessage(WM_CHAR, nChar);
  116. }
  117. else
  118. {
  119. DWORD dwStyle = ES_LEFT;
  120. if (GetFormat() & DT_RIGHT)
  121. dwStyle = ES_RIGHT;
  122. else if (GetFormat() & DT_CENTER)
  123. dwStyle = ES_CENTER;
  124. m_bEditing = TRUE;
  125. // InPlaceEdit auto-deletes itself
  126. CGridCtrl* pGrid = GetGrid();
  127. m_pEditWnd = new CInPlaceEdit((CWnd*)pGrid, rect, dwStyle, nID, nRow, nCol, GetText(), nChar);
  128. }
  129. return TRUE;
  130. }
  131. void CGridCell::EndEdit()
  132. {
  133. if (m_pEditWnd)
  134. ((CInPlaceEdit*)m_pEditWnd)->EndEdit();
  135. }
  136. void CGridCell::OnEndEdit()
  137. {
  138. m_bEditing = FALSE;
  139. m_pEditWnd = NULL;
  140. }
  141. /////////////////////////////////////////////////////////////////////////////
  142. // CGridDefaultCell
  143. CGridDefaultCell::CGridDefaultCell()
  144. {
  145. #ifdef _WIN32_WCE
  146. m_nFormat = DT_LEFT | DT_VCENTER | DT_SINGLELINE | DT_NOPREFIX;
  147. #else
  148. m_nFormat = DT_LEFT | DT_VCENTER | DT_SINGLELINE | DT_NOPREFIX | DT_END_ELLIPSIS;
  149. #endif
  150. m_crFgClr = CLR_DEFAULT;
  151. m_crBkClr = CLR_DEFAULT;
  152. m_Size = CSize(30, 10);
  153. m_dwStyle = 0;
  154. m_crBorderClr = CLR_DEFAULT;
  155. m_nBorder = 0;
  156. #ifdef _WIN32_WCE
  157. LOGFONT lf;
  158. GetObject(GetStockObject(SYSTEM_FONT), sizeof(LOGFONT), &lf);
  159. SetFont(&lf);
  160. #else // not CE
  161. NONCLIENTMETRICS ncm;
  162. ncm.cbSize = sizeof(NONCLIENTMETRICS);
  163. VERIFY(SystemParametersInfo(SPI_GETNONCLIENTMETRICS, sizeof(NONCLIENTMETRICS), &ncm, 0));
  164. SetFont(&(ncm.lfMessageFont));
  165. #endif
  166. }
  167. CGridDefaultCell::~CGridDefaultCell()
  168. {
  169. m_Font.DeleteObject();
  170. }
  171. void CGridDefaultCell::SetFont(const LOGFONT* plf)
  172. {
  173. ASSERT(plf);
  174. if (!plf) return;
  175. m_Font.DeleteObject();
  176. m_Font.CreateFontIndirect(plf);
  177. CGridCell::SetFont(plf);
  178. // Get the font size and hence the default cell size
  179. CDC* pDC = CDC::FromHandle(::GetDC(NULL));
  180. if (pDC)
  181. {
  182. CFont* pOldFont = pDC->SelectObject(&m_Font);
  183. SetMargin(pDC->GetTextExtent(_T(" "), 1).cx);
  184. m_Size = pDC->GetTextExtent(_T(" XXXXXXXXXXXX "), 14);
  185. m_Size.cy = (m_Size.cy * 3) / 2;
  186. pDC->SelectObject(pOldFont);
  187. ReleaseDC(NULL, pDC->GetSafeHdc());
  188. }
  189. else
  190. {
  191. SetMargin(3);
  192. m_Size = CSize(40, 16);
  193. }
  194. }
  195. LOGFONT* CGridDefaultCell::GetFont() const
  196. {
  197. ASSERT(m_plfFont); // This is the default - it CAN'T be NULL!
  198. return m_plfFont;
  199. }
  200. CFont* CGridDefaultCell::GetFontObject() const
  201. {
  202. ASSERT(m_Font.GetSafeHandle());
  203. return (CFont*)&m_Font;
  204. }