📄 gridtreecellbase.cpp
字号:
/*****************************************************************************
COPYRIGHT (C) 2000, Ken Bertelson <kenbertelson@hotmail.com>
*****************************************************************************/
#include "stdafx.h"
#include "..\Include\GridTreeCellBase.h"
#include "..\Include\GridCtrl.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
#define TREE_BOX_MARGIN 2 // for drawing "+" or "-" box associated with a Tree
IMPLEMENT_DYNCREATE(CGridTreeCellBase, CGridCellBase)
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CGridTreeCellBase::CGridTreeCellBase()
{
m_pTreeColumn = NULL;
Reset();
}
CGridTreeCellBase::~CGridTreeCellBase()
{
}
void CGridTreeCellBase::Reset()
{
m_iRow = -1;
m_iCol = -1;
m_nState = 0;
m_sImage = -1;
m_ucEditing = FALSE;
m_ucLevelAndHide = 0x80;
}
BOOL CGridTreeCellBase::Draw(CDC* pDC, int nRow, int nCol, CRect rect,
BOOL bEraseBkgnd /*=TRUE*/)
{
CRect RectCell( rect);
if( DrawTreeCell( pDC, nRow, nCol, &RectCell, bEraseBkgnd) )
{
// call base class to finish it off
return CGridCellBase::Draw( pDC, nRow, nCol, RectCell, bEraseBkgnd);
}
return FALSE;
}
/*****************************************************************************
Allows Tree + Btn object to call drawing member
*****************************************************************************/
BOOL CGridTreeCellBase::DrawTreeCell(CDC* pDC, int nRow, int nCol, CRect* prect,
BOOL /* bEraseBkgnd */)
{
ASSERT( m_pTreeColumn != NULL);
if( !m_pTreeColumn->GetAllowDraw())
return FALSE;
CGridCtrl* pGrid = GetGrid();
if (!pGrid || !pDC)
return FALSE;
if( m_pTreeColumn->GetFixedRowCount() != pGrid->GetFixedRowCount()
|| m_pTreeColumn->GetRowCount() > pGrid->GetRowCount() )
{
ASSERT( FALSE); // if ASSERT here, this means that you a tree in a column
// but you called CGridCtrl::SetFixedRowCount() or
// or CGridCtrl::SetRowCount() directly. You can't do this.
// To change the layout of rows when you are using the tree,
// you must call CTreeColumn::Setup().
return FALSE;
}
if( prect->Width() <= 0
|| prect->Height() <= 0) // prevents imagelist item from drawing even
return FALSE; // though cell is hidden
// tree drawing graphic logic begins here
int nSavedDC = pDC->SaveDC();
if( nCol == m_pTreeColumn->GetColumnWithTree())
{
// this column has a tree
// move text over to allow for level indentation
if( IsViewable() )
{
// if row is showing, draw the tree graphic
if( m_pTreeColumn->GetTreeLines())
{
TreeDrawGraphic( pDC, // current CDC
*prect); // coordinates of bounding cell rectangle
}
else if( m_pTreeColumn->GetTreeUsesImages())
{
TREE_IMAGE TreeImage = TREE_IMAGE_DOCUMENT;
// is it not the very last row?
if( nRow + m_pTreeColumn->GetFixedRowCount() < m_pTreeColumn->GetRowCount())
{
// is it a plus or minus?
BOOL bIsPlus;
BOOL bIsMinus;
BOOL bIsLastLeaf;
if( m_pTreeColumn->TreeCellHasPlusMinus( nRow, // row of Cell to check
&bIsPlus, // returns: T=Is a plus
&bIsMinus, // returns: T=Is a minus
&bIsLastLeaf) )// returns: T=Is Last Leaf
{
// returns: T=cell has a plus or minus; F=not
if( bIsPlus)
TreeImage = TREE_IMAGE_FOLDER_CLOSED;
else
TreeImage = TREE_IMAGE_FOLDER_OPEN;
}
}
SetImage( TreeImage);
}
}
prect->left += GetTreeIndent();
}
pDC->RestoreDC(nSavedDC);
return TRUE;
}
void CGridTreeCellBase::OnClick( CPoint /* PointCellRelative */)
{
}
/*****************************************************************************
Put click exapansion on down rather than up because OnClick() will not
be called if a TitleTip is showing
*****************************************************************************/
void CGridTreeCellBase::OnClickDown( CPoint PointCellRelative)
{
ASSERT( m_pTreeColumn != NULL);
if( TreeHitPlusMinus( PointCellRelative) ) // relative coordinates of mouse click
{
// returns: T=hit a plus or minus; F=Missed it or tree cell has no plus or minus
m_pTreeColumn->TreeExpandCollapseToggle( m_iRow); // Grid row of node to toggle
}
}
void CGridTreeCellBase::OnRClick( CPoint /* PointCellRelative */)
{
}
void CGridTreeCellBase::OnDblClick( CPoint PointCellRelative)
{
ASSERT( m_pTreeColumn != NULL);
if( GetState() & GVIS_READONLY
|| !m_pTreeColumn->GetGrid()->IsEditable() )
{
if( !TreeHitPlusMinus( PointCellRelative) ) // relative coordinates of mouse click
{
// returns: T=hit a plus or minus; F=Missed it or tree cell has no plus or minus
m_pTreeColumn->TreeExpandCollapseToggle( m_iRow); // Grid row of node to toggle
}
}
}
BOOL CGridTreeCellBase::GetTextRect( LPRECT pRect) // i/o: i=dims of cell rect; o=dims of text rect
{
// move text over to allow for level indentation
pRect->left += GetTreeIndent();
return CGridCellBase::GetTextRect( pRect);
}
CSize CGridTreeCellBase::GetCellExtent(CDC* pDC)
{
CSize sizeBase = CGridCellBase::GetCellExtent(pDC);
// move text over to allow for level indentation
sizeBase.cx += GetTreeIndent();
return sizeBase;
}
// Simplify by just using drawing logic for printing, too
BOOL CGridTreeCellBase::PrintCell(CDC* pDC, int nRow, int nCol, CRect rect)
{
return Draw( pDC, nRow, nCol, rect);
}
/*****************************************************************************
For mouse hit test want to know if user clicked on the "+" / "-" box. Also
for drawing the box
*****************************************************************************/
void CGridTreeCellBase::TreeGetBoxRelCoords(CRect* apRect) // returns: relative coordinates
{
ASSERT( apRect != NULL);
ASSERT( m_pTreeColumn != NULL);
int iMargin = GetMargin();
int iDefTreeIndent = m_pTreeColumn->GetDefTreeIndent();
unsigned char ucLevel = GetLevel();
if( ucLevel < 1)
return;
apRect->left = iMargin + (iDefTreeIndent * ( ucLevel - 1) ) + TREE_BOX_MARGIN;
apRect->right = apRect->left + iDefTreeIndent - TREE_BOX_MARGIN;
apRect->top = iMargin;
apRect->bottom = apRect->top + iDefTreeIndent - TREE_BOX_MARGIN;
}
int CGridTreeCellBase::GetTreeIndent()
// returns: device units to indent within a cell for a tree at this level
{
ASSERT( m_pTreeColumn != NULL);
CGridCtrl* pGridCtrl = GetGrid();
ASSERT( pGridCtrl != NULL);
unsigned char ucLevel = GetLevel();
if( ucLevel == 0)
return 0;
if( !m_pTreeColumn->GetTreeLines() )
ucLevel--;
return (m_pTreeColumn->GetDefTreeIndent() * ucLevel) + (pGridCtrl->GetDefCellMargin() * 2);
}
/*****************************************************************************
When user single clicked on a tree cell, did he happen to click on "+" or "-"
graphic in the tree?
*****************************************************************************/
BOOL CGridTreeCellBase::TreeHitPlusMinus( CPoint aPointMouse) // relative coordinates of mouse click
// returns: T=hit a plus or minus; F=Missed it or tree cell has no plus or minus
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -