00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00043
00044 #include "stdafx.h"
00045 #include "GridCtrl.h"
00046 #include "GridCellBase.h"
00047
00048 #ifdef _DEBUG
00049 #define new DEBUG_NEW
00050 #undef THIS_FILE
00051 static char THIS_FILE[] = __FILE__;
00052 #endif
00053
00054 IMPLEMENT_DYNAMIC(CGridCellBase, CObject)
00055
00056
00057
00058
00059 CGridCellBase::CGridCellBase()
00060 {
00061 Reset();
00062 }
00063
00064 CGridCellBase::~CGridCellBase()
00065 {
00066 }
00067
00069
00070
00071 void CGridCellBase::Reset()
00072 {
00073 m_nState = 0;
00074 }
00075
00076 void CGridCellBase::operator=(const CGridCellBase& cell)
00077 {
00078 if (this == &cell) return;
00079
00080 SetGrid(cell.GetGrid());
00081
00082 SetText(cell.GetText());
00083 SetImage(cell.GetImage());
00084 SetData(cell.GetData());
00085 SetState(cell.GetState());
00086 SetFormat(cell.GetFormat());
00087 SetTextClr(cell.GetTextClr());
00088 SetBackClr(cell.GetBackClr());
00089 SetFont(cell.IsDefaultFont()? NULL : cell.GetFont());
00090 SetMargin(cell.GetMargin());
00091 }
00092
00094
00095
00096
00097 CGridCellBase* CGridCellBase::GetDefaultCell() const
00098 {
00099 if (GetGrid())
00100 return GetGrid()->GetDefaultCell(IsFixedRow(), IsFixedCol());
00101 return NULL;
00102 }
00103
00104
00106
00107
00108
00109
00110
00111
00112 BOOL CGridCellBase::Draw(CDC* pDC, int nRow, int nCol, CRect rect, BOOL bEraseBkgnd )
00113 {
00114
00115
00116
00117 CGridCtrl* pGrid = GetGrid();
00118 ASSERT(pGrid);
00119
00120 if (!pGrid || !pDC)
00121 return FALSE;
00122
00123 if( rect.Width() <= 0 || rect.Height() <= 0)
00124 return FALSE;
00125
00126
00127
00128 int nSavedDC = pDC->SaveDC();
00129 pDC->SetBkMode(TRANSPARENT);
00130
00131
00132
00133 CGridDefaultCell *pDefaultCell = (CGridDefaultCell*) GetDefaultCell();
00134 if (!pDefaultCell)
00135 return FALSE;
00136
00137
00138 COLORREF TextClr, TextBkClr;
00139
00140 TextClr = (GetTextClr() == CLR_DEFAULT)? pDefaultCell->GetTextClr() : GetTextClr();
00141 if (GetBackClr() == CLR_DEFAULT)
00142 TextBkClr = pDefaultCell->GetBackClr();
00143 else
00144 {
00145 bEraseBkgnd = TRUE;
00146 TextBkClr = GetBackClr();
00147 }
00148
00149
00150 if ( IsFocused() || IsDropHighlighted() )
00151 {
00152
00153
00154 if(GetState() & GVIS_SELECTED)
00155 {
00156 TextBkClr = ::GetSysColor(COLOR_HIGHLIGHT);
00157 TextClr = ::GetSysColor(COLOR_HIGHLIGHTTEXT);
00158 bEraseBkgnd = TRUE;
00159 }
00160
00161 rect.right++; rect.bottom++;
00162 if (bEraseBkgnd)
00163 {
00164 TRY
00165 {
00166 CBrush brush(TextBkClr);
00167 pDC->FillRect(rect, &brush);
00168 }
00169 CATCH(CResourceException, e)
00170 {
00171
00172 }
00173 END_CATCH
00174 }
00175
00176
00177
00178 if(pGrid->GetGridLines() != GVL_NONE)
00179 {
00180 rect.right--;
00181 rect.bottom--;
00182 }
00183
00184 if (pGrid->GetFrameFocusCell())
00185 {
00186
00187
00188 TRY
00189 {
00190 CBrush brush(TextClr);
00191 pDC->FrameRect(rect, &brush);
00192 }
00193 CATCH(CResourceException, e)
00194 {
00195
00196 }
00197 END_CATCH
00198 }
00199 pDC->SetTextColor(TextClr);
00200
00201
00202 if(pGrid->GetGridLines() == GVL_NONE)
00203 {
00204 rect.right--;
00205 rect.bottom--;
00206 }
00207
00208 rect.DeflateRect(0,1,1,1);
00209 }
00210 else if ((GetState() & GVIS_SELECTED))
00211 {
00212 rect.right++; rect.bottom++;
00213 pDC->FillSolidRect(rect, ::GetSysColor(COLOR_HIGHLIGHT));
00214 rect.right--; rect.bottom--;
00215 pDC->SetTextColor(::GetSysColor(COLOR_HIGHLIGHTTEXT));
00216 }
00217 else
00218 {
00219 if (bEraseBkgnd)
00220 {
00221 rect.right++; rect.bottom++;
00222 CBrush brush(TextBkClr);
00223 pDC->FillRect(rect, &brush);
00224 rect.right--; rect.bottom--;
00225 }
00226 pDC->SetTextColor(TextClr);
00227 }
00228
00229
00230 if (IsFixed() && pGrid->GetGridLines() != GVL_NONE)
00231 {
00232 CCellID FocusCell = pGrid->GetFocusCell();
00233
00234
00235
00236 BOOL bHiliteFixed = pGrid->GetTrackFocusCell() && pGrid->IsValid(FocusCell) &&
00237 (FocusCell.row == nRow || FocusCell.col == nCol);
00238
00239
00240
00241 if (bHiliteFixed)
00242 {
00243 rect.right++; rect.bottom++;
00244 pDC->DrawEdge(rect, BDR_SUNKENINNER , BF_RECT);
00245 rect.DeflateRect(1,1);
00246 }
00247 else
00248 {
00249 CPen lightpen(PS_SOLID, 1, ::GetSysColor(COLOR_3DHIGHLIGHT)),
00250 darkpen(PS_SOLID, 1, ::GetSysColor(COLOR_3DDKSHADOW)),
00251 *pOldPen = pDC->GetCurrentPen();
00252
00253 pDC->SelectObject(&lightpen);
00254 pDC->MoveTo(rect.right, rect.top);
00255 pDC->LineTo(rect.left, rect.top);
00256 pDC->LineTo(rect.left, rect.bottom);
00257
00258 pDC->SelectObject(&darkpen);
00259 pDC->MoveTo(rect.right, rect.top);
00260 pDC->LineTo(rect.right, rect.bottom);
00261 pDC->LineTo(rect.left, rect.bottom);
00262
00263 pDC->SelectObject(pOldPen);
00264 rect.DeflateRect(1,1);
00265 }
00266 }
00267
00268
00269 #if !defined(_WIN32_WCE_NO_PRINTING) && !defined(GRIDCONTROL_NO_PRINTING)
00270 if (!pDC->m_bPrinting)
00271 #endif
00272 {
00273 CFont *pFont = GetFontObject();
00274 ASSERT(pFont);
00275 if (pFont)
00276 pDC->SelectObject(pFont);
00277 }
00278
00279 rect.DeflateRect(GetMargin(), 0);
00280
00281 if (pGrid->GetImageList() && GetImage() >= 0)
00282 {
00283 IMAGEINFO Info;
00284 if (pGrid->GetImageList()->GetImageInfo(GetImage(), &Info))
00285 {
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295 int nImageWidth = Info.rcImage.right-Info.rcImage.left+1;
00296 int nImageHeight = Info.rcImage.bottom-Info.rcImage.top+1;
00297
00298 if( nImageWidth + rect.left <= rect.right + (int)(2*GetMargin())
00299 && nImageHeight + rect.top <= rect.bottom + (int)(2*GetMargin()) )
00300 {
00301 pGrid->GetImageList()->Draw(pDC, GetImage(), rect.TopLeft(), ILD_NORMAL);
00302 }
00303
00304
00305 }
00306 }
00307
00308
00309 if (pGrid->GetSortColumn() == nCol && nRow == 0)
00310 {
00311 CSize size = pDC->GetTextExtent(_T("M"));
00312 int nOffset = 2;
00313
00314
00315
00316
00317 size.cy -= (nOffset * 2);
00318
00319 if (size.cy >= rect.Height())
00320 size.cy = rect.Height() - (nOffset * 2);
00321
00322 size.cx = size.cy;
00323
00324
00325 BOOL bVertical = (GetFont()->lfEscapement == 900);
00326
00327
00328 if (size.cx + rect.left < rect.right + (int)(2*GetMargin()))
00329 {
00330 int nTriangleBase = rect.bottom - nOffset - size.cy;
00331
00332
00333
00334
00335
00336
00337
00338 int nTriangleLeft;
00339 if (bVertical)
00340 nTriangleLeft = (rect.right + rect.left - size.cx)/2;
00341 else
00342 nTriangleLeft = rect.right - size.cx;
00343
00344 CPen penShadow(PS_SOLID, 0, ::GetSysColor(COLOR_3DSHADOW));
00345 CPen penLight(PS_SOLID, 0, ::GetSysColor(COLOR_3DHILIGHT));
00346 if (pGrid->GetSortAscending())
00347 {
00348
00349 CPen *pOldPen = (CPen*) pDC->SelectObject(&penLight);
00350 pDC->MoveTo( nTriangleLeft + 1, nTriangleBase + size.cy + 1);
00351 pDC->LineTo( nTriangleLeft + (size.cx / 2) + 1, nTriangleBase + 1 );
00352 pDC->LineTo( nTriangleLeft + size.cx + 1, nTriangleBase + size.cy + 1);
00353 pDC->LineTo( nTriangleLeft + 1, nTriangleBase + size.cy + 1);
00354
00355 pDC->SelectObject(&penShadow);
00356 pDC->MoveTo( nTriangleLeft, nTriangleBase + size.cy );
00357 pDC->LineTo( nTriangleLeft + (size.cx / 2), nTriangleBase );
00358 pDC->LineTo( nTriangleLeft + size.cx, nTriangleBase + size.cy );
00359 pDC->LineTo( nTriangleLeft, nTriangleBase + size.cy );
00360 pDC->SelectObject(pOldPen);
00361 }
00362 else
00363 {
00364
00365 CPen *pOldPen = (CPen*) pDC->SelectObject(&penLight);
00366 pDC->MoveTo( nTriangleLeft + 1, nTriangleBase + 1 );
00367 pDC->LineTo( nTriangleLeft + (size.cx / 2) + 1, nTriangleBase + size.cy + 1 );
00368 pDC->LineTo( nTriangleLeft + size.cx + 1, nTriangleBase + 1 );
00369 pDC->LineTo( nTriangleLeft + 1, nTriangleBase + 1 );
00370
00371 pDC->SelectObject(&penShadow);
00372 pDC->MoveTo( nTriangleLeft, nTriangleBase );
00373 pDC->LineTo( nTriangleLeft + (size.cx / 2), nTriangleBase + size.cy );
00374 pDC->LineTo( nTriangleLeft + size.cx, nTriangleBase );
00375 pDC->LineTo( nTriangleLeft, nTriangleBase );
00376 pDC->SelectObject(pOldPen);
00377 }
00378
00379 if (!bVertical)
00380 rect.right -= size.cy;
00381 }
00382 }
00383
00384
00385 GetTextRect(rect);
00386 DrawText(pDC->m_hDC, GetText(), -1, rect, GetFormat() | DT_NOPREFIX);
00387
00388 pDC->RestoreDC(nSavedDC);
00389
00390 return TRUE;
00391 }
00392
00394
00395
00396
00397 void CGridCellBase::OnMouseEnter()
00398 {
00399 TRACE0("Mouse entered cell\n");
00400 }
00401
00402 void CGridCellBase::OnMouseOver()
00403 {
00404
00405 }
00406
00407
00408 void CGridCellBase::OnMouseLeave()
00409 {
00410 TRACE0("Mouse left cell\n");
00411 }
00412
00413 void CGridCellBase::OnClick( CPoint PointCellRelative)
00414 {
00415 UNUSED_ALWAYS(PointCellRelative);
00416 TRACE2("Mouse Left btn up in cell at x=%i y=%i\n", PointCellRelative.x, PointCellRelative.y);
00417 }
00418
00419 void CGridCellBase::OnClickDown( CPoint PointCellRelative)
00420 {
00421 UNUSED_ALWAYS(PointCellRelative);
00422 TRACE2("Mouse Left btn down in cell at x=%i y=%i\n", PointCellRelative.x, PointCellRelative.y);
00423 }
00424
00425 void CGridCellBase::OnRClick( CPoint PointCellRelative)
00426 {
00427 UNUSED_ALWAYS(PointCellRelative);
00428 TRACE2("Mouse right-clicked in cell at x=%i y=%i\n", PointCellRelative.x, PointCellRelative.y);
00429 }
00430
00431 void CGridCellBase::OnDblClick( CPoint PointCellRelative)
00432 {
00433 UNUSED_ALWAYS(PointCellRelative);
00434 TRACE2("Mouse double-clicked in cell at x=%i y=%i\n", PointCellRelative.x, PointCellRelative.y);
00435 }
00436
00437
00438 BOOL CGridCellBase::OnSetCursor()
00439 {
00440 #ifndef _WIN32_WCE_NO_CURSOR
00441 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
00442 #endif
00443 return TRUE;
00444 }
00445
00447
00448
00449 void CGridCellBase::OnEndEdit()
00450 {
00451 ASSERT( FALSE);
00452 }
00453
00454 BOOL CGridCellBase::ValidateEdit(LPCTSTR str)
00455 {
00456 UNUSED_ALWAYS(str);
00457 return TRUE;
00458 }
00459
00461
00462
00463 BOOL CGridCellBase::GetTextRect( LPRECT pRect)
00464 {
00465 if (GetImage() >= 0)
00466 {
00467 IMAGEINFO Info;
00468
00469 CGridCtrl* pGrid = GetGrid();
00470 CImageList* pImageList = pGrid->GetImageList();
00471
00472 if (pImageList && pImageList->GetImageInfo( GetImage(), &Info))
00473 {
00474 int nImageWidth = Info.rcImage.right-Info.rcImage.left+1;
00475 pRect->left += nImageWidth + GetMargin();
00476 }
00477 }
00478
00479 return TRUE;
00480 }
00481
00482
00483 CSize CGridCellBase::GetTextExtent(LPCTSTR szText, CDC* pDC )
00484 {
00485 CGridCtrl* pGrid = GetGrid();
00486 ASSERT(pGrid);
00487
00488 BOOL bReleaseDC = FALSE;
00489 if (pDC == NULL || szText == NULL)
00490 {
00491 if (szText)
00492 pDC = pGrid->GetDC();
00493 if (pDC == NULL || szText == NULL)
00494 {
00495 CGridDefaultCell* pDefCell = (CGridDefaultCell*) GetDefaultCell();
00496 ASSERT(pDefCell);
00497 return CSize(pDefCell->GetWidth(), pDefCell->GetHeight());
00498 }
00499 bReleaseDC = TRUE;
00500 }
00501
00502 CFont *pOldFont = NULL,
00503 *pFont = GetFontObject();
00504 if (pFont)
00505 pOldFont = pDC->SelectObject(pFont);
00506
00507 CSize size;
00508 int nFormat = GetFormat();
00509
00510
00511
00512 if ((nFormat & DT_WORDBREAK) && !(nFormat & DT_SINGLELINE))
00513 {
00514 CString str = szText;
00515 int nMaxWidth = 0;
00516 while (TRUE)
00517 {
00518 int nPos = str.Find(_T('\n'));
00519 CString TempStr = (nPos < 0)? str : str.Left(nPos);
00520 int nTempWidth = pDC->GetTextExtent(TempStr).cx;
00521 if (nTempWidth > nMaxWidth)
00522 nMaxWidth = nTempWidth;
00523
00524 if (nPos < 0)
00525 break;
00526 str = str.Mid(nPos + 1);
00527 }
00528
00529 CRect rect;
00530 rect.SetRect(0,0, nMaxWidth+1, 0);
00531 pDC->DrawText(szText, -1, rect, nFormat | DT_CALCRECT);
00532 size = rect.Size();
00533 }
00534 else
00535 size = pDC->GetTextExtent(szText, _tcslen(szText));
00536
00537 TEXTMETRIC tm;
00538 pDC->GetTextMetrics(&tm);
00539 size.cx += (tm.tmOverhang);
00540
00541 if (pOldFont)
00542 pDC->SelectObject(pOldFont);
00543
00544 size += CSize(4*GetMargin(), 2*GetMargin());
00545
00546
00547 LOGFONT *pLF = GetFont();
00548 if (pLF->lfEscapement == 900 || pLF->lfEscapement == -900)
00549 {
00550 int nTemp = size.cx;
00551 size.cx = size.cy;
00552 size.cy = nTemp;
00553 size += CSize(0, 4*GetMargin());
00554 }
00555
00556 if (bReleaseDC)
00557 pGrid->ReleaseDC(pDC);
00558
00559 return size;
00560 }
00561
00562 CSize CGridCellBase::GetCellExtent(CDC* pDC)
00563 {
00564 CSize size = GetTextExtent(GetText(), pDC);
00565 CSize ImageSize(0,0);
00566
00567 int nImage = GetImage();
00568 if (nImage >= 0)
00569 {
00570 CGridCtrl* pGrid = GetGrid();
00571 ASSERT(pGrid);
00572
00573 if (pGrid->GetImageList())
00574 {
00575 IMAGEINFO Info;
00576 if (pGrid->GetImageList()->GetImageInfo(nImage, &Info))
00577 ImageSize = CSize(Info.rcImage.right-Info.rcImage.left+1,
00578 Info.rcImage.bottom-Info.rcImage.top+1);
00579 }
00580 }
00581
00582 return CSize(size.cx + ImageSize.cx, max(size.cy, ImageSize.cy));
00583 }
00584
00585
00586
00587 BOOL CGridCellBase::PrintCell(CDC* pDC, int , int , CRect rect)
00588 {
00589 #if defined(_WIN32_WCE_NO_PRINTING) || defined(GRIDCONTROL_NO_PRINTING)
00590 return FALSE;
00591 #else
00592 COLORREF crFG, crBG;
00593 GV_ITEM Item;
00594
00595 CGridCtrl* pGrid = GetGrid();
00596 if (!pGrid || !pDC)
00597 return FALSE;
00598
00599 if( rect.Width() <= 0
00600 || rect.Height() <= 0)
00601 return FALSE;
00602
00603 int nSavedDC = pDC->SaveDC();
00604
00605 pDC->SetBkMode(TRANSPARENT);
00606
00607 if (pGrid->GetShadedPrintOut())
00608 {
00609
00610
00611 CGridDefaultCell *pDefaultCell = (CGridDefaultCell*) GetDefaultCell();
00612 if (!pDefaultCell)
00613 return FALSE;
00614
00615
00616
00617 if(IsFixed())
00618 crBG = (GetBackClr() != CLR_DEFAULT) ? GetBackClr() : pDefaultCell->GetBackClr();
00619 else
00620 crBG = (GetBackClr() != CLR_DEFAULT && GetBackClr() != pDefaultCell->GetBackClr()) ?
00621 GetBackClr() : CLR_DEFAULT;
00622
00623
00624
00625 if(IsFixed())
00626 crFG = (GetBackClr() != CLR_DEFAULT) ? GetTextClr() : pDefaultCell->GetTextClr();
00627 else
00628 crFG = (GetBackClr() != CLR_DEFAULT) ? GetTextClr() : pDefaultCell->GetTextClr();
00629
00630
00631
00632
00633
00634
00635 if(pDC->GetDeviceCaps(NUMCOLORS) == 2 && crBG == CLR_DEFAULT)
00636 crFG = RGB(GetRValue(crFG) * 0.30, GetGValue(crFG) * 0.59,
00637 GetBValue(crFG) * 0.11);
00638
00639
00640
00641 if(crBG != CLR_DEFAULT)
00642 {
00643 CBrush brush(crBG);
00644 rect.right++; rect.bottom++;
00645 pDC->FillRect(rect, &brush);
00646 rect.right--; rect.bottom--;
00647 }
00648 }
00649 else
00650 {
00651 crBG = CLR_DEFAULT;
00652 crFG = RGB(0, 0, 0);
00653 }
00654
00655 pDC->SetTextColor(crFG);
00656
00657 CFont *pFont = GetFontObject();
00658 if (pFont)
00659 pDC->SelectObject(pFont);
00660
00661
00662
00663
00664
00665
00666
00667
00668
00669
00670
00671
00672
00673
00674
00675
00676
00677
00678
00679
00680
00681
00682
00683
00684
00685
00686
00687
00688
00689 if(pGrid->GetGridLines() != GVL_NONE && IsFixed())
00690 {
00691 CPen lightpen(PS_SOLID, 1, ::GetSysColor(COLOR_3DHIGHLIGHT)),
00692 darkpen(PS_SOLID, 1, ::GetSysColor(COLOR_3DDKSHADOW)),
00693 *pOldPen = pDC->GetCurrentPen();
00694
00695 pDC->SelectObject(&lightpen);
00696 pDC->MoveTo(rect.right, rect.top);
00697 pDC->LineTo(rect.left, rect.top);
00698 pDC->LineTo(rect.left, rect.bottom);
00699
00700 pDC->SelectObject(&darkpen);
00701 pDC->MoveTo(rect.right, rect.top);
00702 pDC->LineTo(rect.right, rect.bottom);
00703 pDC->LineTo(rect.left, rect.bottom);
00704
00705 rect.DeflateRect(1,1);
00706 pDC->SelectObject(pOldPen);
00707 }
00708
00709 rect.DeflateRect(GetMargin(), 0);
00710
00711 if(pGrid->GetImageList() && GetImage() >= 0)
00712 {
00713
00714
00715
00716
00717
00718
00719 IMAGEINFO Info;
00720 if(pGrid->GetImageList()->GetImageInfo(GetImage(), &Info))
00721 {
00722 int nImageWidth = Info.rcImage.right-Info.rcImage.left;
00723 pGrid->GetImageList()->Draw(pDC, GetImage(), rect.TopLeft(), ILD_NORMAL);
00724 rect.left += nImageWidth+GetMargin();
00725 }
00726 }
00727
00728
00729
00730 DrawText(pDC->m_hDC, GetText(), -1, rect,
00731 GetFormat() | DT_NOPREFIX);
00732
00733 pDC->RestoreDC(nSavedDC);
00734
00735 return TRUE;
00736 #endif
00737 }
00738
00739
00740
00741
00742 LRESULT CGridCellBase::SendMessageToParent(int nRow, int nCol, int nMessage)
00743 {
00744 CGridCtrl* pGrid = GetGrid();
00745 if( pGrid)
00746 return pGrid->SendMessageToParent(nRow, nCol, nMessage);
00747 else
00748 return 0;
00749 }