⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 treecolumn.cpp

📁 是一个GridTree的控件
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*****************************************************************************
    COPYRIGHT (C) 2000, Ken Bertelson <kenbertelson@hotmail.com>


*****************************************************************************/
#include "stdafx.h"
#include "..\Include\TreeColumn.h"

#include "..\Include\GridTreeCell.H"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CTreeColumn::CTreeColumn()
{
    // tree cell data initialization
    m_nFormat = DT_LEFT|DT_VCENTER|DT_SINGLELINE|DT_END_ELLIPSIS;
    m_crBkClr = CLR_DEFAULT;
    m_crFgClr = CLR_DEFAULT;
    m_lParam  = 0;


#ifndef _WIN32_WCE
    // Initially use the system message font for the GridCtrl font
    NONCLIENTMETRICS ncm;
    ncm.cbSize = sizeof(NONCLIENTMETRICS);
    VERIFY(SystemParametersInfo(SPI_GETNONCLIENTMETRICS, sizeof(NONCLIENTMETRICS), &ncm, 0));
    memcpy(&m_lfFont, &(ncm.lfMessageFont), sizeof(LOGFONT));
#else
    LOGFONT lf;
    GetObject(GetStockObject(SYSTEM_FONT), sizeof(LOGFONT), &lf);
    memcpy(&m_lfFont, &lf, sizeof(LOGFONT));
#endif

    // tree properties initialization
    m_pGrid = NULL;
    m_iColumnWithTree = -1; // no column draws the tree
    m_bTreeUsesImages = FALSE;
    m_bShowTreeLines = TRUE;

    m_iFixedRowCount = -1;
    m_iRowCount = -1;
    m_bAllowDraw = FALSE;   // prevents crash during reset
    m_crTreeLineColour = ::GetSysColor(COLOR_3DSHADOW);
    m_nDefTreeIndent = 10;
    m_pEditWnd = NULL;
}

CTreeColumn::~CTreeColumn()
{

}

/*****************************************************************************
Sets up a column to be drawn with + - tree controls w/ indenting.  Text
is automatically indented.  Rows expand and collapse as appropriate

*****************************************************************************/
BOOL CTreeColumn::TreeSetup(  CGridCtrl* apGridCtrl,  // tree acts on a column in this grid
                            int aiColumn,       // which column has tree
                            int aiTotalRows,    // total number of rows if tree totally expanded
                            int aiFixedRowCount,// Set fixed row count now, too
                            const unsigned char* apucTreeLevelAry,    // Tree Level data array --
                                                            //  must have aiTotalRows of entries
                            BOOL abShowTreeLines,   // T=show tree (not grid) lines; F=no tree lines
                            BOOL abUseImages,   // T=use 1st 3 images from already set image list
                                                //  to display folder graphics
                            CRuntimeClass* apRuntimeClass)   // can use your special
                                                //  CGridTreeCellBase-derived class
// returns:  success / fail
{
    ASSERT( apGridCtrl != NULL);
    ASSERT( aiColumn >= apGridCtrl->GetFixedColumnCount()
            && aiColumn < apGridCtrl->GetColumnCount());

    if( apRuntimeClass == NULL)  // default can hold data
       apRuntimeClass = RUNTIME_CLASS(CGridTreeCell);
    if (!apRuntimeClass->IsDerivedFrom(RUNTIME_CLASS(CGridTreeCellBase)))
    {
        ASSERT( FALSE);
        return FALSE;
    }

    m_pGrid = apGridCtrl;
    m_bAllowDraw = FALSE;   // prevents crash during reset
    BOOL bRC = TRUE;

    m_iFixedRowCount = aiFixedRowCount;
    m_iRowCount = aiTotalRows + aiFixedRowCount;
    m_iColumnWithTree = aiColumn;

    m_pGrid->SetFixedRowCount( m_iFixedRowCount);
    m_pGrid->SetRowCount( m_iRowCount);

    if( aiTotalRows > 0)
    {
        ASSERT( apucTreeLevelAry != NULL);

        // retain old cell properties:  store one time in CTreeColumn
        CGridTreeCell GridCellCopy;
        GridCellCopy.SetTreeColumn( this);
        CGridCellBase* pCurrCell = m_pGrid->GetCell( m_iFixedRowCount, m_iColumnWithTree);
        if (pCurrCell)
            GridCellCopy = *pCurrCell;

        // copy all data while replacing cells to tree cell type
        for( int i1=0; i1 < aiTotalRows; i1++)
        {
            int iCellRow = m_iFixedRowCount + i1;


            if( !m_pGrid->SetCellType(   iCellRow,
                                m_iColumnWithTree,
                                apRuntimeClass ) )
            {
                bRC = FALSE;
                break;
            }

            CGridTreeCell* pGridTreeCell = (CGridTreeCell*)m_pGrid->GetCell( iCellRow, m_iColumnWithTree);
            if( pGridTreeCell != NULL)
            {
                pGridTreeCell->SetTreeColumn( this);
                pGridTreeCell->SetLevelAndHide( *apucTreeLevelAry );
            }

            apucTreeLevelAry++;
        }

        SetTreeUsesImages( abUseImages);   // T=use images
        SetTreeLines( abShowTreeLines);    // T=show tree lines
    }

    m_bAllowDraw = TRUE;
    m_pGrid->Invalidate();
    return bRC;
}

void CTreeColumn::SetTreeUsesImages( BOOL abUseImages)   // T=use images
{
#ifdef _DEBUG
    ASSERT( m_iColumnWithTree >= 0);
    ASSERT( m_pGrid != NULL);

    if( abUseImages
        && m_pGrid->GetImageList() == NULL)
        ASSERT( FALSE);     // must call SetImageList() first
#endif

    m_bTreeUsesImages = abUseImages;

    if( abUseImages)
    {
        // make sure that items have an image associated with it for
        //  autosize column width calculations
        ASSERT( m_iRowCount > 0); // call SetRowCount() before this
        ASSERT( m_iColumnWithTree >= 0);
        for( int i1=m_pGrid->GetFixedColumnCount(); i1 < m_iRowCount; i1++)
        {
            m_pGrid->SetItemImage( i1, m_iColumnWithTree, 0);
        }
    }
}

void CTreeColumn::SetTreeLines( BOOL abShowTreeLines)   // T=show tree lines
{
    ASSERT( m_iColumnWithTree >= 0);
    m_bShowTreeLines = abShowTreeLines;
}

unsigned char CTreeColumn::GetTreeLevel(  int aiRow)  // row
// returns:  tree level, =0 if invalid input
{
    ASSERT( m_pGrid != NULL);
    ASSERT( aiRow >= 0
            && aiRow < m_iRowCount );
    ASSERT( m_iColumnWithTree >= 0);

    CGridTreeCell* pGridTreeCell = (CGridTreeCell*)m_pGrid->GetCell( aiRow, m_iColumnWithTree);
    if( pGridTreeCell != NULL)
        return pGridTreeCell->GetLevel();

    return 0;
}

BOOL CTreeColumn::IsTreeRowDisplayed(  int aiRow)  // row
// returns:  T=tree row is displayed
{
    ASSERT( m_pGrid != NULL);
    ASSERT( aiRow >= 0
            && aiRow < m_iRowCount );
    ASSERT( m_iColumnWithTree >= 0);

    CGridTreeCell* pGridTreeCell = (CGridTreeCell*)m_pGrid->GetCell( aiRow, m_iColumnWithTree);
    if( pGridTreeCell != NULL)
        return pGridTreeCell->IsViewable();

    return FALSE;
}

unsigned char CTreeColumn::GetLevelAndHide( int aiRow)
// returns:  tree level, =0 if invalid input
{
    ASSERT( m_pGrid != NULL);
    ASSERT( aiRow >= 0
            && aiRow < m_iRowCount );
    ASSERT( m_iColumnWithTree >= 0);

    CGridTreeCell* pGridTreeCell = (CGridTreeCell*)m_pGrid->GetCell( aiRow, m_iColumnWithTree);
    if( pGridTreeCell != NULL)
        return pGridTreeCell->GetLevelAndHide();

    return 0;
}

/*****************************************************************************
Examines saved array show / hide bit to determine if a row should display.
There is a one-for-one mapping of these bits to total rows

*****************************************************************************/
void CTreeColumn::TreeRefreshRows()
{
    ASSERT( m_pGrid != NULL);
    ASSERT( m_iColumnWithTree >= 0);

    // hide / show each row
    for( int i1=m_iFixedRowCount; i1 < m_iRowCount; i1++)
    {
        CGridTreeCell* pGridTreeCell = (CGridTreeCell*)m_pGrid->GetCell( i1, m_iColumnWithTree);
        if( pGridTreeCell == NULL)
            return;

        if( pGridTreeCell->IsViewable() )
        {
            if( m_pGrid->GetRowHeight( i1) < m_pGrid->GetDefCellHeight() )
                m_pGrid->SetRowHeight( i1, m_pGrid->GetDefCellHeight() );
        }
        else
        {
            if( m_pGrid->GetRowHeight( i1) > 0 )
                m_pGrid->SetRowHeight( i1, 0);
        }

    }

    if (::IsWindow( m_pGrid->m_hWnd) && m_bAllowDraw)
    {
        m_pGrid->Invalidate();
    }

}
/*****************************************************************************
Callable by class consumer to prepare one's own data array of tree info to
display something reasonable.   High order Bits of the tree display data are
modified.

*****************************************************************************/
void CTreeColumn::TreeDataPrepOutline(unsigned char aucLevel, // level to display >= 0x80 displays all
                                    int aiIndex,            // Index to tree display data to modify
                                    int aiNbrElements)      // nbr of elements in tree display data
{

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -