GridCellBase.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747
  1. // GridCellBase.cpp : implementation file
  2. //
  3. // MFC Grid Control - Main grid cell base class
  4. //
  5. // Provides the implementation for the base cell type of the
  6. // grid control. No data is stored (except for state) but default
  7. // implementations of drawing, printingetc provided. MUST be derived
  8. // from to be used.
  9. //
  10. // Written by Chris Maunder <cmaunder@mail.com>
  11. // Copyright (c) 1998-2001. All Rights Reserved.
  12. //
  13. // This code may be used in compiled form in any way you desire. This
  14. // file may be redistributed unmodified by any means PROVIDING it is
  15. // not sold for profit without the authors written consent, and
  16. // providing that this notice and the authors name and all copyright
  17. // notices remains intact.
  18. //
  19. // An email letting me know how you are using it would be nice as well.
  20. //
  21. // This file is provided "as is" with no expressed or implied warranty.
  22. // The author accepts no liability for any damage/loss of business that
  23. // this product may cause.
  24. //
  25. // For use with CGridCtrl v2.22+
  26. //
  27. // History:
  28. // Ken Bertelson - 12 Apr 2000 - Split CGridCell into CGridCell and CGridCellBase
  29. // C Maunder - 19 May 2000 - Fixed sort arrow drawing (Ivan Ilinov)
  30. // C Maunder - 29 Aug 2000 - operator= checks for NULL font before setting (Martin Richter)
  31. // C Maunder - 15 Oct 2000 - GetTextExtent fixed (Martin Richter)
  32. // C Maunder - 1 Jan 2001 - Added ValidateEdit
  33. //
  34. // NOTES: Each grid cell should take care of it's own drawing, though the Draw()
  35. // method takes an "erase background" paramter that is called if the grid
  36. // decides to draw the entire grid background in on hit. Certain ambient
  37. // properties such as the default font to use, and hints on how to draw
  38. // fixed cells should be fetched from the parent grid. The grid trusts the
  39. // cells will behave in a certain way, and the cells trust the grid will
  40. // supply accurate information.
  41. //
  42. /////////////////////////////////////////////////////////////////////////////
  43. #include "stdafx.h"
  44. #include "GridCtrl.h"
  45. #include "GridCellBase.h"
  46. #ifdef _DEBUG
  47. #define new DEBUG_NEW
  48. #undef THIS_FILE
  49. static char THIS_FILE[] = __FILE__;
  50. #endif
  51. IMPLEMENT_DYNCREATE(CGridCellBase, CObject)
  52. /////////////////////////////////////////////////////////////////////////////
  53. // GridCellBase
  54. CGridCellBase::CGridCellBase()
  55. {
  56. Reset();
  57. }
  58. CGridCellBase::~CGridCellBase()
  59. {
  60. }
  61. /////////////////////////////////////////////////////////////////////////////
  62. // GridCellBase Operations
  63. void CGridCellBase::Reset()
  64. {
  65. m_nState = 0;
  66. }
  67. void CGridCellBase::operator=(const CGridCellBase& cell)
  68. {
  69. SetGrid(cell.GetGrid()); // do first in case of dependencies
  70. SetText(cell.GetText());
  71. SetImage(cell.GetImage());
  72. SetData(cell.GetData());
  73. SetState(cell.GetState());
  74. SetFormat(cell.GetFormat());
  75. SetTextClr(cell.GetTextClr());
  76. SetBackClr(cell.GetBackClr());
  77. SetFont(cell.IsDefaultFont() ? NULL : cell.GetFont());
  78. SetMargin(cell.GetMargin());
  79. }
  80. /////////////////////////////////////////////////////////////////////////////
  81. // CGridCellBase Attributes
  82. // Returns a pointer to a cell that holds default values for this particular type of cell
  83. CGridCellBase* CGridCellBase::GetDefaultCell() const
  84. {
  85. if (GetGrid())
  86. return GetGrid()->GetDefaultCell(IsFixedRow(), IsFixedCol());
  87. return NULL;
  88. }
  89. /////////////////////////////////////////////////////////////////////////////
  90. // CGridCellBase Operations
  91. // EFW - Various changes to make it draw cells better when using alternate
  92. // color schemes. Also removed printing references as that's now done
  93. // by PrintCell() and fixed the sort marker so that it doesn't draw out
  94. // of bounds.
  95. BOOL CGridCellBase::Draw(CDC* pDC, int nRow, int nCol, CRect rect, BOOL bEraseBkgnd /*=TRUE*/)
  96. {
  97. // Note - all through this function we totally brutalise 'rect'. Do not
  98. // depend on it's value being that which was passed in.
  99. CGridCtrl* pGrid = GetGrid();
  100. ASSERT(pGrid);
  101. if (!pGrid || !pDC)
  102. return FALSE;
  103. if (rect.Width() <= 0 || rect.Height() <= 0) // prevents imagelist item from drawing even
  104. return FALSE; // though cell is hidden
  105. //TRACE3("Drawing %scell %d, %d\n", IsFixed()? _T("Fixed ") : _T(""), nRow, nCol);
  106. int nSavedDC = pDC->SaveDC();
  107. pDC->SetBkMode(TRANSPARENT);
  108. // Get the default cell implementation for this kind of cell. We use it if this cell
  109. // has anything marked as "default"
  110. CGridDefaultCell *pDefaultCell = (CGridDefaultCell*)GetDefaultCell();
  111. if (!pDefaultCell)
  112. return FALSE;
  113. // Set up text and background colours
  114. COLORREF TextClr, TextBkClr;
  115. TextClr = (GetTextClr() == CLR_DEFAULT) ? pDefaultCell->GetTextClr() : GetTextClr();
  116. if (GetBackClr() == CLR_DEFAULT)
  117. TextBkClr = pDefaultCell->GetBackClr();
  118. else
  119. {
  120. bEraseBkgnd = TRUE;
  121. TextBkClr = GetBackClr();
  122. }
  123. // Draw cell background and highlighting (if necessary)
  124. if (IsFocused() || IsDropHighlighted())
  125. {
  126. // Always draw even in list mode so that we can tell where the
  127. // cursor is at. Use the highlight colors though.
  128. if (GetState() & GVIS_SELECTED)
  129. {
  130. TextBkClr = ::GetSysColor(COLOR_HIGHLIGHT);
  131. TextClr = ::GetSysColor(COLOR_HIGHLIGHTTEXT);
  132. bEraseBkgnd = TRUE;
  133. }
  134. rect.right++; rect.bottom++; // FillRect doesn't draw RHS or bottom
  135. if (bEraseBkgnd)
  136. {
  137. TRY
  138. {
  139. CBrush brush(TextBkClr);
  140. pDC->FillRect(rect, &brush);
  141. }
  142. CATCH(CResourceException, e)
  143. {
  144. //e->ReportError();
  145. }
  146. END_CATCH
  147. }
  148. // Don't adjust frame rect if no grid lines so that the
  149. // whole cell is enclosed.
  150. if (pGrid->GetGridLines() != GVL_NONE)
  151. {
  152. rect.right--;
  153. rect.bottom--;
  154. }
  155. if (pGrid->GetFrameFocusCell())
  156. {
  157. // Use same color as text to outline the cell so that it shows
  158. // up if the background is black.
  159. TRY
  160. {
  161. CBrush brush(TextClr);
  162. pDC->FrameRect(rect, &brush);
  163. }
  164. CATCH(CResourceException, e)
  165. {
  166. //e->ReportError();
  167. }
  168. END_CATCH
  169. }
  170. pDC->SetTextColor(TextClr);
  171. // Adjust rect after frame draw if no grid lines
  172. if (pGrid->GetGridLines() == GVL_NONE)
  173. {
  174. rect.right--;
  175. rect.bottom--;
  176. }
  177. rect.DeflateRect(1, 1);
  178. }
  179. else if ((GetState() & GVIS_SELECTED))
  180. {
  181. rect.right++; rect.bottom++; // FillRect doesn't draw RHS or bottom
  182. pDC->FillSolidRect(rect, ::GetSysColor(COLOR_HIGHLIGHT));
  183. rect.right--; rect.bottom--;
  184. pDC->SetTextColor(::GetSysColor(COLOR_HIGHLIGHTTEXT));
  185. }
  186. else
  187. {
  188. if (bEraseBkgnd)
  189. {
  190. rect.right++; rect.bottom++; // FillRect doesn't draw RHS or bottom
  191. CBrush brush(TextBkClr);
  192. pDC->FillRect(rect, &brush);
  193. rect.right--; rect.bottom--;
  194. }
  195. pDC->SetTextColor(TextClr);
  196. }
  197. // Draw lines only when wanted
  198. if (IsFixed() && pGrid->GetGridLines() != GVL_NONE)
  199. {
  200. CCellID FocusCell = pGrid->GetFocusCell();
  201. // As above, always show current location even in list mode so
  202. // that we know where the cursor is at.
  203. BOOL bHiliteFixed = pGrid->GetTrackFocusCell() && pGrid->IsValid(FocusCell) &&
  204. (FocusCell.row == nRow || FocusCell.col == nCol);
  205. // If this fixed cell is on the same row/col as the focus cell,
  206. // highlight it.
  207. if (bHiliteFixed)
  208. {
  209. rect.right++; rect.bottom++;
  210. pDC->DrawEdge(rect, BDR_SUNKENINNER /*EDGE_RAISED*/, BF_RECT);
  211. rect.DeflateRect(1, 1);
  212. }
  213. else
  214. {
  215. CPen lightpen(PS_SOLID, 1, ::GetSysColor(COLOR_3DHIGHLIGHT)),
  216. darkpen(PS_SOLID, 1, ::GetSysColor(COLOR_3DDKSHADOW)),
  217. *pOldPen = pDC->GetCurrentPen();
  218. pDC->SelectObject(&lightpen);
  219. pDC->MoveTo(rect.right, rect.top);
  220. pDC->LineTo(rect.left, rect.top);
  221. pDC->LineTo(rect.left, rect.bottom);
  222. pDC->SelectObject(&darkpen);
  223. pDC->MoveTo(rect.right, rect.top);
  224. pDC->LineTo(rect.right, rect.bottom);
  225. pDC->LineTo(rect.left, rect.bottom);
  226. pDC->SelectObject(pOldPen);
  227. rect.DeflateRect(1, 1);
  228. }
  229. }
  230. // Draw Text and image
  231. if (!pDC->m_bPrinting)
  232. {
  233. CFont *pFont = GetFontObject();
  234. if (pFont)
  235. pDC->SelectObject(pFont);
  236. }
  237. rect.DeflateRect(GetMargin(), 0);
  238. if (pGrid->GetImageList() && GetImage() >= 0)
  239. {
  240. IMAGEINFO Info;
  241. if (pGrid->GetImageList()->GetImageInfo(GetImage(), &Info))
  242. {
  243. // would like to use a clipping region but seems to have issue
  244. // working with CMemDC directly. Instead, don't display image
  245. // if any part of it cut-off
  246. //
  247. // CRgn rgn;
  248. // rgn.CreateRectRgnIndirect(rect);
  249. // pDC->SelectClipRgn(&rgn);
  250. // rgn.DeleteObject();
  251. int nImageWidth = Info.rcImage.right - Info.rcImage.left + 1;
  252. int nImageHeight = Info.rcImage.bottom - Info.rcImage.top + 1;
  253. if (nImageWidth + rect.left <= rect.right + (int)(2 * GetMargin())
  254. && nImageHeight + rect.top <= rect.bottom + (int)(2 * GetMargin()))
  255. {
  256. pGrid->GetImageList()->Draw(pDC, GetImage(), rect.TopLeft(), ILD_NORMAL);
  257. }
  258. //rect.left += nImageWidth+GetMargin();
  259. }
  260. }
  261. // Draw sort arrow
  262. if (pGrid->GetSortColumn() == nCol && nRow == 0)
  263. {
  264. CSize size = pDC->GetTextExtent(_T("M"));
  265. int nOffset = 2;
  266. // Base the size of the triangle on the smaller of the column
  267. // height or text height with a slight offset top and bottom.
  268. // Otherwise, it can get drawn outside the bounds of the cell.
  269. size.cy -= (nOffset * 2);
  270. if (size.cy >= rect.Height())
  271. size.cy = rect.Height() - (nOffset * 2);
  272. size.cx = size.cy; // Make the dimensions square
  273. // Kludge for vertical text
  274. BOOL bVertical = (GetFont()->lfEscapement == 900);
  275. // Only draw if it'll fit!
  276. if (size.cx + rect.left < rect.right + (int)(2 * GetMargin()))
  277. {
  278. int nTriangleBase = rect.bottom - nOffset - size.cy; // Triangle bottom right
  279. //int nTriangleBase = (rect.top + rect.bottom - size.cy)/2; // Triangle middle right
  280. //int nTriangleBase = rect.top + nOffset; // Triangle top right
  281. //int nTriangleLeft = rect.right - size.cx; // Triangle RHS
  282. //int nTriangleLeft = (rect.right + rect.left - size.cx)/2; // Triangle middle
  283. //int nTriangleLeft = rect.left; // Triangle LHS
  284. int nTriangleLeft;
  285. if (bVertical)
  286. nTriangleLeft = (rect.right + rect.left - size.cx) / 2; // Triangle middle
  287. else
  288. nTriangleLeft = rect.right - size.cx; // Triangle RHS
  289. CPen penShadow(PS_SOLID, 0, ::GetSysColor(COLOR_3DSHADOW));
  290. CPen penLight(PS_SOLID, 0, ::GetSysColor(COLOR_3DHILIGHT));
  291. if (pGrid->GetSortAscending())
  292. {
  293. // Draw triangle pointing upwards
  294. CPen *pOldPen = (CPen*)pDC->SelectObject(&penLight);
  295. pDC->MoveTo(nTriangleLeft + 1, nTriangleBase + size.cy + 1);
  296. pDC->LineTo(nTriangleLeft + (size.cx / 2) + 1, nTriangleBase + 1);
  297. pDC->LineTo(nTriangleLeft + size.cx + 1, nTriangleBase + size.cy + 1);
  298. pDC->LineTo(nTriangleLeft + 1, nTriangleBase + size.cy + 1);
  299. pDC->SelectObject(&penShadow);
  300. pDC->MoveTo(nTriangleLeft, nTriangleBase + size.cy);
  301. pDC->LineTo(nTriangleLeft + (size.cx / 2), nTriangleBase);
  302. pDC->LineTo(nTriangleLeft + size.cx, nTriangleBase + size.cy);
  303. pDC->LineTo(nTriangleLeft, nTriangleBase + size.cy);
  304. pDC->SelectObject(pOldPen);
  305. }
  306. else
  307. {
  308. // Draw triangle pointing downwards
  309. CPen *pOldPen = (CPen*)pDC->SelectObject(&penLight);
  310. pDC->MoveTo(nTriangleLeft + 1, nTriangleBase + 1);
  311. pDC->LineTo(nTriangleLeft + (size.cx / 2) + 1, nTriangleBase + size.cy + 1);
  312. pDC->LineTo(nTriangleLeft + size.cx + 1, nTriangleBase + 1);
  313. pDC->LineTo(nTriangleLeft + 1, nTriangleBase + 1);
  314. pDC->SelectObject(&penShadow);
  315. pDC->MoveTo(nTriangleLeft, nTriangleBase);
  316. pDC->LineTo(nTriangleLeft + (size.cx / 2), nTriangleBase + size.cy);
  317. pDC->LineTo(nTriangleLeft + size.cx, nTriangleBase);
  318. pDC->LineTo(nTriangleLeft, nTriangleBase);
  319. pDC->SelectObject(pOldPen);
  320. }
  321. if (!bVertical)
  322. rect.right -= size.cy;
  323. }
  324. }
  325. // We want to see '&' characters so use DT_NOPREFIX
  326. GetTextRect(rect);
  327. DrawText(pDC->m_hDC, GetText(), -1, rect, GetFormat() | DT_NOPREFIX);
  328. pDC->RestoreDC(nSavedDC);
  329. return TRUE;
  330. }
  331. /////////////////////////////////////////////////////////////////////////////
  332. // CGridCellBase Mouse and Cursor events
  333. // Not yet implemented
  334. void CGridCellBase::OnMouseEnter()
  335. {
  336. //TRACE0("Mouse entered cell\n");
  337. }
  338. void CGridCellBase::OnMouseOver()
  339. {
  340. //TRACE0("Mouse over cell\n");
  341. }
  342. // Not Yet Implemented
  343. void CGridCellBase::OnMouseLeave()
  344. {
  345. //TRACE0("Mouse left cell\n");
  346. }
  347. void CGridCellBase::OnClick(CPoint PointCellRelative)
  348. {
  349. UNUSED_ALWAYS(PointCellRelative);
  350. //TRACE2("Mouse Left btn up in cell at x=%i y=%i\n", PointCellRelative.x, PointCellRelative.y);
  351. }
  352. void CGridCellBase::OnClickDown(CPoint PointCellRelative)
  353. {
  354. UNUSED_ALWAYS(PointCellRelative);
  355. // TRACE2("Mouse Left btn down in cell at x=%i y=%i\n", PointCellRelative.x, PointCellRelative.y);
  356. }
  357. void CGridCellBase::OnRClick(CPoint PointCellRelative)
  358. {
  359. UNUSED_ALWAYS(PointCellRelative);
  360. //TRACE2("Mouse right-clicked in cell at x=%i y=%i\n", PointCellRelative.x, PointCellRelative.y);
  361. }
  362. void CGridCellBase::OnDblClick(CPoint PointCellRelative)
  363. {
  364. UNUSED_ALWAYS(PointCellRelative);
  365. //TRACE2("Mouse double-clicked in cell at x=%i y=%i\n", PointCellRelative.x, PointCellRelative.y);
  366. }
  367. // Return TRUE if you set the cursor
  368. BOOL CGridCellBase::OnSetCursor()
  369. {
  370. #ifndef _WIN32_WCE_NO_CURSOR
  371. SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
  372. #endif
  373. return TRUE;
  374. }
  375. /////////////////////////////////////////////////////////////////////////////
  376. // CGridCellBase editing
  377. void CGridCellBase::OnEndEdit()
  378. {
  379. ASSERT(FALSE);
  380. }
  381. BOOL CGridCellBase::ValidateEdit(LPCTSTR str)
  382. {
  383. UNUSED_ALWAYS(str);
  384. return TRUE;
  385. }
  386. /////////////////////////////////////////////////////////////////////////////
  387. // CGridCellBase Sizing
  388. BOOL CGridCellBase::GetTextRect(LPRECT pRect) // i/o: i=dims of cell rect; o=dims of text rect
  389. {
  390. if (GetImage() >= 0)
  391. {
  392. IMAGEINFO Info;
  393. CGridCtrl* pGrid = GetGrid();
  394. CImageList* pImageList = pGrid->GetImageList();
  395. if (pImageList && pImageList->GetImageInfo(GetImage(), &Info))
  396. {
  397. int nImageWidth = Info.rcImage.right - Info.rcImage.left + 1;
  398. pRect->left += nImageWidth + GetMargin();
  399. }
  400. }
  401. return TRUE;
  402. }
  403. // By default this uses the selected font (which is a bigger font)
  404. CSize CGridCellBase::GetTextExtent(LPCTSTR szText, CDC* pDC /*= NULL*/)
  405. {
  406. CGridCtrl* pGrid = GetGrid();
  407. ASSERT(pGrid);
  408. BOOL bReleaseDC = FALSE;
  409. if (pDC == NULL || szText == NULL)
  410. {
  411. if (szText)
  412. pDC = pGrid->GetDC();
  413. if (pDC == NULL || szText == NULL)
  414. {
  415. CGridDefaultCell* pDefCell = (CGridDefaultCell*)GetDefaultCell();
  416. ASSERT(pDefCell);
  417. return CSize(pDefCell->GetWidth(), pDefCell->GetHeight());
  418. }
  419. bReleaseDC = TRUE;
  420. }
  421. CFont *pOldFont = NULL,
  422. *pFont = GetFontObject();
  423. if (pFont)
  424. pOldFont = pDC->SelectObject(pFont);
  425. CSize size;
  426. int nFormat = GetFormat();
  427. // If the cell is a multiline cell, then use the width of the cell
  428. // to get the height
  429. if ((nFormat & DT_WORDBREAK) && !(nFormat & DT_SINGLELINE))
  430. {
  431. CString str = szText;
  432. int nMaxWidth = 0;
  433. while (TRUE)
  434. {
  435. int nPos = str.Find(_T('\n'));
  436. CString TempStr = (nPos < 0) ? str : str.Left(nPos);
  437. int nTempWidth = pDC->GetTextExtent(TempStr).cx;
  438. if (nTempWidth > nMaxWidth)
  439. nMaxWidth = nTempWidth;
  440. if (nPos < 0)
  441. break;
  442. str = str.Mid(nPos + 1); // Bug fix by Thomas Steinborn
  443. }
  444. CRect rect;
  445. rect.SetRect(0, 0, nMaxWidth + 1, 0);
  446. pDC->DrawText(szText, -1, rect, nFormat | DT_CALCRECT);
  447. size = rect.Size();
  448. }
  449. else
  450. size = pDC->GetTextExtent(szText, _tcslen(szText));
  451. TEXTMETRIC tm;
  452. pDC->GetTextMetrics(&tm);
  453. size.cx += (tm.tmOverhang);
  454. if (pOldFont)
  455. pDC->SelectObject(pOldFont);
  456. size += CSize(4 * GetMargin(), 2 * GetMargin());
  457. // Kludge for vertical text
  458. LOGFONT *pLF = GetFont();
  459. if (pLF->lfEscapement == 900 || pLF->lfEscapement == -900)
  460. {
  461. int nTemp = size.cx;
  462. size.cx = size.cy;
  463. size.cy = nTemp;
  464. size += CSize(0, 4 * GetMargin());
  465. }
  466. if (bReleaseDC)
  467. pGrid->ReleaseDC(pDC);
  468. return size;
  469. }
  470. CSize CGridCellBase::GetCellExtent(CDC* pDC)
  471. {
  472. CSize size = GetTextExtent(GetText(), pDC);
  473. CSize ImageSize(0, 0);
  474. int nImage = GetImage();
  475. if (nImage >= 0)
  476. {
  477. CGridCtrl* pGrid = GetGrid();
  478. ASSERT(pGrid);
  479. if (pGrid->GetImageList())
  480. {
  481. IMAGEINFO Info;
  482. if (pGrid->GetImageList()->GetImageInfo(nImage, &Info))
  483. ImageSize = CSize(Info.rcImage.right - Info.rcImage.left + 1,
  484. Info.rcImage.bottom - Info.rcImage.top + 1);
  485. }
  486. }
  487. return CSize(size.cx + ImageSize.cx, max(size.cy, ImageSize.cy));
  488. }
  489. // EFW - Added to print cells so that grids that use different colors are
  490. // printed correctly.
  491. BOOL CGridCellBase::PrintCell(CDC* pDC, int /*nRow*/, int /*nCol*/, CRect rect)
  492. {
  493. #if defined(_WIN32_WCE_NO_PRINTING) || defined(GRIDCONTROL_NO_PRINTING)
  494. return FALSE;
  495. #else
  496. COLORREF crFG, crBG;
  497. GV_ITEM Item;
  498. CGridCtrl* pGrid = GetGrid();
  499. if (!pGrid || !pDC)
  500. return FALSE;
  501. if (rect.Width() <= 0
  502. || rect.Height() <= 0) // prevents imagelist item from drawing even
  503. return FALSE; // though cell is hidden
  504. int nSavedDC = pDC->SaveDC();
  505. pDC->SetBkMode(TRANSPARENT);
  506. if (pGrid->GetShadedPrintOut())
  507. {
  508. // Get the default cell implementation for this kind of cell. We use it if this cell
  509. // has anything marked as "default"
  510. CGridDefaultCell *pDefaultCell = (CGridDefaultCell*)GetDefaultCell();
  511. if (!pDefaultCell)
  512. return FALSE;
  513. // Use custom color if it doesn't match the default color and the
  514. // default grid background color. If not, leave it alone.
  515. if (IsFixed())
  516. crBG = (GetBackClr() != CLR_DEFAULT) ? GetBackClr() : pDefaultCell->GetBackClr();
  517. else
  518. crBG = (GetBackClr() != CLR_DEFAULT && GetBackClr() != pDefaultCell->GetBackClr()) ?
  519. GetBackClr() : CLR_DEFAULT;
  520. // Use custom color if the background is different or if it doesn't
  521. // match the default color and the default grid text color. If not,
  522. // use black to guarantee the text is visible.
  523. if (IsFixed())
  524. crFG = (GetBackClr() != CLR_DEFAULT) ? GetTextClr() : pDefaultCell->GetTextClr();
  525. else
  526. crFG = (GetBackClr() != CLR_DEFAULT ||
  527. (GetTextClr() != CLR_DEFAULT && GetTextClr() != pDefaultCell->GetTextClr())) ?
  528. GetTextClr() : RGB(0, 0, 0);
  529. // If not printing on a color printer, adjust the foreground color
  530. // to a gray scale if the background color isn't used so that all
  531. // colors will be visible. If not, some colors turn to solid black
  532. // or white when printed and may not show up. This may be caused by
  533. // coarse dithering by the printer driver too (see image note below).
  534. if (pDC->GetDeviceCaps(NUMCOLORS) == 2 && crBG == CLR_DEFAULT)
  535. crFG = RGB(GetRValue(crFG) * 0.30, GetGValue(crFG) * 0.59,
  536. GetBValue(crFG) * 0.11);
  537. // Only erase the background if the color is not the default
  538. // grid background color.
  539. if (crBG != CLR_DEFAULT)
  540. {
  541. CBrush brush(crBG);
  542. rect.right++; rect.bottom++;
  543. pDC->FillRect(rect, &brush);
  544. rect.right--; rect.bottom--;
  545. }
  546. }
  547. else
  548. {
  549. crBG = CLR_DEFAULT;
  550. crFG = RGB(0, 0, 0);
  551. }
  552. pDC->SetTextColor(crFG);
  553. CFont *pFont = GetFontObject();
  554. if (pFont)
  555. pDC->SelectObject(pFont);
  556. /*
  557. // ***************************************************
  558. // Disabled - if you need this functionality then you'll need to rewrite.
  559. // Create the appropriate font and select into DC.
  560. CFont Font;
  561. // Bold the fixed cells if not shading the print out. Use italic
  562. // font it it is enabled.
  563. const LOGFONT* plfFont = GetFont();
  564. if(IsFixed() && !pGrid->GetShadedPrintOut())
  565. {
  566. Font.CreateFont(plfFont->lfHeight, 0, 0, 0, FW_BOLD, plfFont->lfItalic, 0, 0,
  567. ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
  568. #ifndef _WIN32_WCE
  569. PROOF_QUALITY,
  570. #else
  571. DEFAULT_QUALITY,
  572. #endif
  573. VARIABLE_PITCH | FF_SWISS, plfFont->lfFaceName);
  574. }
  575. else
  576. Font.CreateFontIndirect(plfFont);
  577. pDC->SelectObject(&Font);
  578. // ***************************************************
  579. */
  580. // Draw lines only when wanted on fixed cells. Normal cell grid lines
  581. // are handled in OnPrint.
  582. if (pGrid->GetGridLines() != GVL_NONE && IsFixed())
  583. {
  584. CPen lightpen(PS_SOLID, 1, ::GetSysColor(COLOR_3DHIGHLIGHT)),
  585. darkpen(PS_SOLID, 1, ::GetSysColor(COLOR_3DDKSHADOW)),
  586. *pOldPen = pDC->GetCurrentPen();
  587. pDC->SelectObject(&lightpen);
  588. pDC->MoveTo(rect.right, rect.top);
  589. pDC->LineTo(rect.left, rect.top);
  590. pDC->LineTo(rect.left, rect.bottom);
  591. pDC->SelectObject(&darkpen);
  592. pDC->MoveTo(rect.right, rect.top);
  593. pDC->LineTo(rect.right, rect.bottom);
  594. pDC->LineTo(rect.left, rect.bottom);
  595. rect.DeflateRect(1, 1);
  596. pDC->SelectObject(pOldPen);
  597. }
  598. rect.DeflateRect(GetMargin(), 0);
  599. if (pGrid->GetImageList() && GetImage() >= 0)
  600. {
  601. // NOTE: If your printed images look like fuzzy garbage, check the
  602. // settings on your printer driver. If it's using coarse
  603. // dithering and/or vector graphics, they may print wrong.
  604. // Changing to fine dithering and raster graphics makes them
  605. // print properly. My HP 4L had that problem.
  606. IMAGEINFO Info;
  607. if (pGrid->GetImageList()->GetImageInfo(GetImage(), &Info))
  608. {
  609. int nImageWidth = Info.rcImage.right - Info.rcImage.left;
  610. pGrid->GetImageList()->Draw(pDC, GetImage(), rect.TopLeft(), ILD_NORMAL);
  611. rect.left += nImageWidth + GetMargin();
  612. }
  613. }
  614. // Draw without clipping so as not to lose text when printed for real
  615. // DT_NOCLIP removed 01.01.01. Slower, but who cares - we are printing!
  616. DrawText(pDC->m_hDC, GetText(), -1, rect,
  617. GetFormat() | /*DT_NOCLIP | */ DT_NOPREFIX);
  618. pDC->RestoreDC(nSavedDC);
  619. return TRUE;
  620. #endif
  621. }
  622. /*****************************************************************************
  623. Callable by derived classes, only
  624. *****************************************************************************/
  625. LRESULT CGridCellBase::SendMessageToParent(int nRow, int nCol, int nMessage)
  626. {
  627. CGridCtrl* pGrid = GetGrid();
  628. if (pGrid)
  629. return pGrid->SendMessageToParent(nRow, nCol, nMessage);
  630. else
  631. return 0;
  632. }