123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747 |
- #include "stdafx.h"
- #include "GridCtrl.h"
- #include "GridCellBase.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- IMPLEMENT_DYNCREATE(CGridCellBase, CObject)
- CGridCellBase::CGridCellBase()
- {
- Reset();
- }
- CGridCellBase::~CGridCellBase()
- {
- }
- void CGridCellBase::Reset()
- {
- m_nState = 0;
- }
- void CGridCellBase::operator=(const CGridCellBase& cell)
- {
- SetGrid(cell.GetGrid());
- SetText(cell.GetText());
- SetImage(cell.GetImage());
- SetData(cell.GetData());
- SetState(cell.GetState());
- SetFormat(cell.GetFormat());
- SetTextClr(cell.GetTextClr());
- SetBackClr(cell.GetBackClr());
- SetFont(cell.IsDefaultFont() ? NULL : cell.GetFont());
- SetMargin(cell.GetMargin());
- }
- CGridCellBase* CGridCellBase::GetDefaultCell() const
- {
- if (GetGrid())
- return GetGrid()->GetDefaultCell(IsFixedRow(), IsFixedCol());
- return NULL;
- }
- BOOL CGridCellBase::Draw(CDC* pDC, int nRow, int nCol, CRect rect, BOOL bEraseBkgnd )
- {
-
-
- CGridCtrl* pGrid = GetGrid();
- ASSERT(pGrid);
- if (!pGrid || !pDC)
- return FALSE;
- if (rect.Width() <= 0 || rect.Height() <= 0)
- return FALSE;
-
- int nSavedDC = pDC->SaveDC();
- pDC->SetBkMode(TRANSPARENT);
-
-
- CGridDefaultCell *pDefaultCell = (CGridDefaultCell*)GetDefaultCell();
- if (!pDefaultCell)
- return FALSE;
-
- COLORREF TextClr, TextBkClr;
- TextClr = (GetTextClr() == CLR_DEFAULT) ? pDefaultCell->GetTextClr() : GetTextClr();
- if (GetBackClr() == CLR_DEFAULT)
- TextBkClr = pDefaultCell->GetBackClr();
- else
- {
- bEraseBkgnd = TRUE;
- TextBkClr = GetBackClr();
- }
-
- if (IsFocused() || IsDropHighlighted())
- {
-
-
- if (GetState() & GVIS_SELECTED)
- {
- TextBkClr = ::GetSysColor(COLOR_HIGHLIGHT);
- TextClr = ::GetSysColor(COLOR_HIGHLIGHTTEXT);
- bEraseBkgnd = TRUE;
- }
- rect.right++; rect.bottom++;
- if (bEraseBkgnd)
- {
- TRY
- {
- CBrush brush(TextBkClr);
- pDC->FillRect(rect, &brush);
- }
- CATCH(CResourceException, e)
- {
-
- }
- END_CATCH
- }
-
-
- if (pGrid->GetGridLines() != GVL_NONE)
- {
- rect.right--;
- rect.bottom--;
- }
- if (pGrid->GetFrameFocusCell())
- {
-
-
- TRY
- {
- CBrush brush(TextClr);
- pDC->FrameRect(rect, &brush);
- }
- CATCH(CResourceException, e)
- {
-
- }
- END_CATCH
- }
- pDC->SetTextColor(TextClr);
-
- if (pGrid->GetGridLines() == GVL_NONE)
- {
- rect.right--;
- rect.bottom--;
- }
- rect.DeflateRect(1, 1);
- }
- else if ((GetState() & GVIS_SELECTED))
- {
- rect.right++; rect.bottom++;
- pDC->FillSolidRect(rect, ::GetSysColor(COLOR_HIGHLIGHT));
- rect.right--; rect.bottom--;
- pDC->SetTextColor(::GetSysColor(COLOR_HIGHLIGHTTEXT));
- }
- else
- {
- if (bEraseBkgnd)
- {
- rect.right++; rect.bottom++;
- CBrush brush(TextBkClr);
- pDC->FillRect(rect, &brush);
- rect.right--; rect.bottom--;
- }
- pDC->SetTextColor(TextClr);
- }
-
- if (IsFixed() && pGrid->GetGridLines() != GVL_NONE)
- {
- CCellID FocusCell = pGrid->GetFocusCell();
-
-
- BOOL bHiliteFixed = pGrid->GetTrackFocusCell() && pGrid->IsValid(FocusCell) &&
- (FocusCell.row == nRow || FocusCell.col == nCol);
-
-
- if (bHiliteFixed)
- {
- rect.right++; rect.bottom++;
- pDC->DrawEdge(rect, BDR_SUNKENINNER , BF_RECT);
- rect.DeflateRect(1, 1);
- }
- else
- {
- CPen lightpen(PS_SOLID, 1, ::GetSysColor(COLOR_3DHIGHLIGHT)),
- darkpen(PS_SOLID, 1, ::GetSysColor(COLOR_3DDKSHADOW)),
- *pOldPen = pDC->GetCurrentPen();
- pDC->SelectObject(&lightpen);
- pDC->MoveTo(rect.right, rect.top);
- pDC->LineTo(rect.left, rect.top);
- pDC->LineTo(rect.left, rect.bottom);
- pDC->SelectObject(&darkpen);
- pDC->MoveTo(rect.right, rect.top);
- pDC->LineTo(rect.right, rect.bottom);
- pDC->LineTo(rect.left, rect.bottom);
- pDC->SelectObject(pOldPen);
- rect.DeflateRect(1, 1);
- }
- }
-
- if (!pDC->m_bPrinting)
- {
- CFont *pFont = GetFontObject();
- if (pFont)
- pDC->SelectObject(pFont);
- }
- rect.DeflateRect(GetMargin(), 0);
- if (pGrid->GetImageList() && GetImage() >= 0)
- {
- IMAGEINFO Info;
- if (pGrid->GetImageList()->GetImageInfo(GetImage(), &Info))
- {
-
-
-
-
-
-
-
-
- int nImageWidth = Info.rcImage.right - Info.rcImage.left + 1;
- int nImageHeight = Info.rcImage.bottom - Info.rcImage.top + 1;
- if (nImageWidth + rect.left <= rect.right + (int)(2 * GetMargin())
- && nImageHeight + rect.top <= rect.bottom + (int)(2 * GetMargin()))
- {
- pGrid->GetImageList()->Draw(pDC, GetImage(), rect.TopLeft(), ILD_NORMAL);
- }
-
- }
- }
-
- if (pGrid->GetSortColumn() == nCol && nRow == 0)
- {
- CSize size = pDC->GetTextExtent(_T("M"));
- int nOffset = 2;
-
-
-
- size.cy -= (nOffset * 2);
- if (size.cy >= rect.Height())
- size.cy = rect.Height() - (nOffset * 2);
- size.cx = size.cy;
-
- BOOL bVertical = (GetFont()->lfEscapement == 900);
-
- if (size.cx + rect.left < rect.right + (int)(2 * GetMargin()))
- {
- int nTriangleBase = rect.bottom - nOffset - size.cy;
-
-
-
-
-
- int nTriangleLeft;
- if (bVertical)
- nTriangleLeft = (rect.right + rect.left - size.cx) / 2;
- else
- nTriangleLeft = rect.right - size.cx;
- CPen penShadow(PS_SOLID, 0, ::GetSysColor(COLOR_3DSHADOW));
- CPen penLight(PS_SOLID, 0, ::GetSysColor(COLOR_3DHILIGHT));
- if (pGrid->GetSortAscending())
- {
-
- CPen *pOldPen = (CPen*)pDC->SelectObject(&penLight);
- pDC->MoveTo(nTriangleLeft + 1, nTriangleBase + size.cy + 1);
- pDC->LineTo(nTriangleLeft + (size.cx / 2) + 1, nTriangleBase + 1);
- pDC->LineTo(nTriangleLeft + size.cx + 1, nTriangleBase + size.cy + 1);
- pDC->LineTo(nTriangleLeft + 1, nTriangleBase + size.cy + 1);
- pDC->SelectObject(&penShadow);
- pDC->MoveTo(nTriangleLeft, nTriangleBase + size.cy);
- pDC->LineTo(nTriangleLeft + (size.cx / 2), nTriangleBase);
- pDC->LineTo(nTriangleLeft + size.cx, nTriangleBase + size.cy);
- pDC->LineTo(nTriangleLeft, nTriangleBase + size.cy);
- pDC->SelectObject(pOldPen);
- }
- else
- {
-
- CPen *pOldPen = (CPen*)pDC->SelectObject(&penLight);
- pDC->MoveTo(nTriangleLeft + 1, nTriangleBase + 1);
- pDC->LineTo(nTriangleLeft + (size.cx / 2) + 1, nTriangleBase + size.cy + 1);
- pDC->LineTo(nTriangleLeft + size.cx + 1, nTriangleBase + 1);
- pDC->LineTo(nTriangleLeft + 1, nTriangleBase + 1);
- pDC->SelectObject(&penShadow);
- pDC->MoveTo(nTriangleLeft, nTriangleBase);
- pDC->LineTo(nTriangleLeft + (size.cx / 2), nTriangleBase + size.cy);
- pDC->LineTo(nTriangleLeft + size.cx, nTriangleBase);
- pDC->LineTo(nTriangleLeft, nTriangleBase);
- pDC->SelectObject(pOldPen);
- }
- if (!bVertical)
- rect.right -= size.cy;
- }
- }
-
- GetTextRect(rect);
- DrawText(pDC->m_hDC, GetText(), -1, rect, GetFormat() | DT_NOPREFIX);
- pDC->RestoreDC(nSavedDC);
- return TRUE;
- }
- void CGridCellBase::OnMouseEnter()
- {
-
- }
- void CGridCellBase::OnMouseOver()
- {
-
- }
- void CGridCellBase::OnMouseLeave()
- {
-
- }
- void CGridCellBase::OnClick(CPoint PointCellRelative)
- {
- UNUSED_ALWAYS(PointCellRelative);
-
- }
- void CGridCellBase::OnClickDown(CPoint PointCellRelative)
- {
- UNUSED_ALWAYS(PointCellRelative);
-
- }
- void CGridCellBase::OnRClick(CPoint PointCellRelative)
- {
- UNUSED_ALWAYS(PointCellRelative);
-
- }
- void CGridCellBase::OnDblClick(CPoint PointCellRelative)
- {
- UNUSED_ALWAYS(PointCellRelative);
-
- }
- BOOL CGridCellBase::OnSetCursor()
- {
- #ifndef _WIN32_WCE_NO_CURSOR
- SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
- #endif
- return TRUE;
- }
- void CGridCellBase::OnEndEdit()
- {
- ASSERT(FALSE);
- }
- BOOL CGridCellBase::ValidateEdit(LPCTSTR str)
- {
- UNUSED_ALWAYS(str);
- return TRUE;
- }
- BOOL CGridCellBase::GetTextRect(LPRECT pRect)
- {
- if (GetImage() >= 0)
- {
- IMAGEINFO Info;
- CGridCtrl* pGrid = GetGrid();
- CImageList* pImageList = pGrid->GetImageList();
- if (pImageList && pImageList->GetImageInfo(GetImage(), &Info))
- {
- int nImageWidth = Info.rcImage.right - Info.rcImage.left + 1;
- pRect->left += nImageWidth + GetMargin();
- }
- }
- return TRUE;
- }
- CSize CGridCellBase::GetTextExtent(LPCTSTR szText, CDC* pDC )
- {
- CGridCtrl* pGrid = GetGrid();
- ASSERT(pGrid);
- BOOL bReleaseDC = FALSE;
- if (pDC == NULL || szText == NULL)
- {
- if (szText)
- pDC = pGrid->GetDC();
- if (pDC == NULL || szText == NULL)
- {
- CGridDefaultCell* pDefCell = (CGridDefaultCell*)GetDefaultCell();
- ASSERT(pDefCell);
- return CSize(pDefCell->GetWidth(), pDefCell->GetHeight());
- }
- bReleaseDC = TRUE;
- }
- CFont *pOldFont = NULL,
- *pFont = GetFontObject();
- if (pFont)
- pOldFont = pDC->SelectObject(pFont);
- CSize size;
- int nFormat = GetFormat();
-
-
- if ((nFormat & DT_WORDBREAK) && !(nFormat & DT_SINGLELINE))
- {
- CString str = szText;
- int nMaxWidth = 0;
- while (TRUE)
- {
- int nPos = str.Find(_T('\n'));
- CString TempStr = (nPos < 0) ? str : str.Left(nPos);
- int nTempWidth = pDC->GetTextExtent(TempStr).cx;
- if (nTempWidth > nMaxWidth)
- nMaxWidth = nTempWidth;
- if (nPos < 0)
- break;
- str = str.Mid(nPos + 1);
- }
- CRect rect;
- rect.SetRect(0, 0, nMaxWidth + 1, 0);
- pDC->DrawText(szText, -1, rect, nFormat | DT_CALCRECT);
- size = rect.Size();
- }
- else
- size = pDC->GetTextExtent(szText, _tcslen(szText));
- TEXTMETRIC tm;
- pDC->GetTextMetrics(&tm);
- size.cx += (tm.tmOverhang);
- if (pOldFont)
- pDC->SelectObject(pOldFont);
- size += CSize(4 * GetMargin(), 2 * GetMargin());
-
- LOGFONT *pLF = GetFont();
- if (pLF->lfEscapement == 900 || pLF->lfEscapement == -900)
- {
- int nTemp = size.cx;
- size.cx = size.cy;
- size.cy = nTemp;
- size += CSize(0, 4 * GetMargin());
- }
- if (bReleaseDC)
- pGrid->ReleaseDC(pDC);
- return size;
- }
- CSize CGridCellBase::GetCellExtent(CDC* pDC)
- {
- CSize size = GetTextExtent(GetText(), pDC);
- CSize ImageSize(0, 0);
- int nImage = GetImage();
- if (nImage >= 0)
- {
- CGridCtrl* pGrid = GetGrid();
- ASSERT(pGrid);
- if (pGrid->GetImageList())
- {
- IMAGEINFO Info;
- if (pGrid->GetImageList()->GetImageInfo(nImage, &Info))
- ImageSize = CSize(Info.rcImage.right - Info.rcImage.left + 1,
- Info.rcImage.bottom - Info.rcImage.top + 1);
- }
- }
- return CSize(size.cx + ImageSize.cx, max(size.cy, ImageSize.cy));
- }
- BOOL CGridCellBase::PrintCell(CDC* pDC, int , int , CRect rect)
- {
- #if defined(_WIN32_WCE_NO_PRINTING) || defined(GRIDCONTROL_NO_PRINTING)
- return FALSE;
- #else
- COLORREF crFG, crBG;
- GV_ITEM Item;
- CGridCtrl* pGrid = GetGrid();
- if (!pGrid || !pDC)
- return FALSE;
- if (rect.Width() <= 0
- || rect.Height() <= 0)
- return FALSE;
- int nSavedDC = pDC->SaveDC();
- pDC->SetBkMode(TRANSPARENT);
- if (pGrid->GetShadedPrintOut())
- {
-
-
- CGridDefaultCell *pDefaultCell = (CGridDefaultCell*)GetDefaultCell();
- if (!pDefaultCell)
- return FALSE;
-
-
- if (IsFixed())
- crBG = (GetBackClr() != CLR_DEFAULT) ? GetBackClr() : pDefaultCell->GetBackClr();
- else
- crBG = (GetBackClr() != CLR_DEFAULT && GetBackClr() != pDefaultCell->GetBackClr()) ?
- GetBackClr() : CLR_DEFAULT;
-
-
-
- if (IsFixed())
- crFG = (GetBackClr() != CLR_DEFAULT) ? GetTextClr() : pDefaultCell->GetTextClr();
- else
- crFG = (GetBackClr() != CLR_DEFAULT ||
- (GetTextClr() != CLR_DEFAULT && GetTextClr() != pDefaultCell->GetTextClr())) ?
- GetTextClr() : RGB(0, 0, 0);
-
-
-
-
-
- if (pDC->GetDeviceCaps(NUMCOLORS) == 2 && crBG == CLR_DEFAULT)
- crFG = RGB(GetRValue(crFG) * 0.30, GetGValue(crFG) * 0.59,
- GetBValue(crFG) * 0.11);
-
-
- if (crBG != CLR_DEFAULT)
- {
- CBrush brush(crBG);
- rect.right++; rect.bottom++;
- pDC->FillRect(rect, &brush);
- rect.right--; rect.bottom--;
- }
- }
- else
- {
- crBG = CLR_DEFAULT;
- crFG = RGB(0, 0, 0);
- }
- pDC->SetTextColor(crFG);
- CFont *pFont = GetFontObject();
- if (pFont)
- pDC->SelectObject(pFont);
-
-
-
- if (pGrid->GetGridLines() != GVL_NONE && IsFixed())
- {
- CPen lightpen(PS_SOLID, 1, ::GetSysColor(COLOR_3DHIGHLIGHT)),
- darkpen(PS_SOLID, 1, ::GetSysColor(COLOR_3DDKSHADOW)),
- *pOldPen = pDC->GetCurrentPen();
- pDC->SelectObject(&lightpen);
- pDC->MoveTo(rect.right, rect.top);
- pDC->LineTo(rect.left, rect.top);
- pDC->LineTo(rect.left, rect.bottom);
- pDC->SelectObject(&darkpen);
- pDC->MoveTo(rect.right, rect.top);
- pDC->LineTo(rect.right, rect.bottom);
- pDC->LineTo(rect.left, rect.bottom);
- rect.DeflateRect(1, 1);
- pDC->SelectObject(pOldPen);
- }
- rect.DeflateRect(GetMargin(), 0);
- if (pGrid->GetImageList() && GetImage() >= 0)
- {
-
-
-
-
-
- IMAGEINFO Info;
- if (pGrid->GetImageList()->GetImageInfo(GetImage(), &Info))
- {
- int nImageWidth = Info.rcImage.right - Info.rcImage.left;
- pGrid->GetImageList()->Draw(pDC, GetImage(), rect.TopLeft(), ILD_NORMAL);
- rect.left += nImageWidth + GetMargin();
- }
- }
-
-
- DrawText(pDC->m_hDC, GetText(), -1, rect,
- GetFormat() | DT_NOPREFIX);
- pDC->RestoreDC(nSavedDC);
- return TRUE;
- #endif
- }
- LRESULT CGridCellBase::SendMessageToParent(int nRow, int nCol, int nMessage)
- {
- CGridCtrl* pGrid = GetGrid();
- if (pGrid)
- return pGrid->SendMessageToParent(nRow, nCol, nMessage);
- else
- return 0;
- }
|