Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3340910
  • 博文数量: 1450
  • 博客积分: 11163
  • 博客等级: 上将
  • 技术积分: 11101
  • 用 户 组: 普通用户
  • 注册时间: 2005-07-25 14:40
文章分类

全部博文(1450)

文章存档

2017年(5)

2014年(2)

2013年(3)

2012年(35)

2011年(39)

2010年(88)

2009年(395)

2008年(382)

2007年(241)

2006年(246)

2005年(14)

分类: C/C++

2009-08-14 14:14:42

今天再来做一个类似QQ好友列表控件:TreeView







效果图

需要如下几个类:
1.CTreeView (最外面的框,或者叫容器)
2.CTreeViewContainer(第二个容器)
3.CTreeNode(节点)
4.CTreeNodeItem(子节点)

其他:其他需要的就是我们在前面提到的文本显示控件和一个图片显示控件。后面的代码我会加上


/*
* TreeNode.h
*
*  Created on: 2009-7-15
*      Author: Administrator
*/

#ifndef TREENODE_H_
#define TREENODE_H_

#include "BaseControl.h"

class CTextLable;
class CImageLable;
class CFont;
class CTreeViewContainer;

class MOperateTreeNodeItem
        {
        virtual void RemoveControls(TInt aCount) = 0;
        virtual void AddControls(RPointerArray* aArrayControl) = 0;
        };
class CTreeNode : public CBaseControl
        {
public:
        static CTreeNode* NewL(const TDesC& aTitleStr,CFont* aFont,CTreeViewContainer* aParent = NULL);
        ~CTreeNode();
        
public:
        //form base class
        virtual void Draw(CBitmapContext& gc) const;
        virtual void SetRect(const TRect &aRect);
        virtual TInt GetHeight();
        virtual TInt GetWidth();
        
public:
        void AddNodeItem(CBaseControl* aControl);
        void RemoveNodeItem(TInt aIdx);
        
        TKeyResponse OfferKeyEventL(const TKeyEvent& aKeyEvent, TEventCode aType);
        
        void SetPic(CFbsBitmap* aBitmap,CFbsBitmap* aMaskBitmap = NULL);
        
        void SetSelect(TBool aVal);
        TBool GetSelect()
                {
                return isSelect;
                }
        
        RPointerArray iChildNodes;
private:
        void ConstructL(const TDesC& aTitleStr,CFont* aFont,CTreeViewContainer* aParent = NULL);
        CTreeNode();
        void SetControlsVisible(TBool aVal);
        TBool iConsVisible;
        
        CTextLable* iTitleTXT;
        CImageLable* iImage;
        
        
        
        CTreeViewContainer* iObserver;
        TBool isSelect;
        };

#endif /* TREENODE_H_ */
/*
* TreeNodeItem.h
*
*  Created on: 2009-7-15
*      Author: Administrator
*/

#ifndef TREENODEITEM_H_
#define TREENODEITEM_H_

#include "BaseControl.h"

class CTextLable;
class CFbsBitmap;

class CTreeNodeItem : public CBaseControl
        {
public:
        static CTreeNodeItem* NewL(CBaseControl* aParent = NULL);
        ~CTreeNodeItem();
        
        void SetString(TInt aRes,CFont* aFont);
        void SetString(const TDesC& aStr,CFont* aFont);
        void SetImage(TInt aRes);
        void SetImage(const CFbsBitmap* aRes);
        TKeyResponse OfferKeyEventL(const TKeyEvent& aKeyEvent, TEventCode /*aType*/);

public:
        //form base class
        virtual void Draw(CBitmapContext& gc) const;
        virtual void SetRect(const TRect &aRect);
        virtual TInt GetHeight();
        virtual TInt GetWidth();

public:
        void SetSelect(TBool aVal)
                {
                isSelect = aVal;
                }
        TBool GetSelect()
                {
                return isSelect;
                }
private:
        CTreeNodeItem();
        void ConstructL(CBaseControl* aParent = NULL);
        
private:
        //data
        CTextLable* iTextLable;
        TBool isSelect;
        };
#endif /* TREENODEITEM_H_ */
/*
* TreeView.h
*
*  Created on: 2009-7-15
*      Author: Administrator
*/

#ifndef TREEVIEW_H_
#define TREEVIEW_H_

#include "BaseControl.h"

class RWindowGroup;

class CFbsBitGc;
class CFbsBitmap;
class CFbsBitmapDevice;

class CBar;
class CArrows;
class CTreeViewContainer;
class CTreeNode;

class MTreeNodeItemSelect
        {
public:
        virtual void NotifyTreeNodeItemSelect() = 0;
        };

class CTreeView : public CBaseControl
        {
public:
        static CTreeView* NewL(MTreeNodeItemSelect& aObserver);
        CTreeView(MTreeNodeItemSelect& aObserver);
        ~CTreeView();
public:
        TKeyResponse OfferKeyEventL(const TKeyEvent& aKeyEvent, TEventCode aType);
        virtual void Draw(CBitmapContext& gc) const;
        virtual void SetRect(const TRect &aRect);
        virtual TInt GetHeight();
        virtual TInt GetWidth();
        virtual void HandleResourceChange();
public:
        void AddTreeViewNode(CBaseControl* aControl);
        void RemoveTreeViewNode(TInt aTreeViewItemIdx);
        TInt GetItemFocusIdx();
        void SetItemFocusIdx(TInt aIdx);
        TInt GetSelectNodeIdx();
        TInt GetSelectNodeItemIdx();
        CTreeViewContainer* iListContainer;
        enum
                {
                ENUM_DYNAMIC,
                ENUM_STATIC
                };
        
private:
        void ConstructL();
        void InitDrawBuffer();
        void SetArrowsRect();
        CBar* iBar;
        CArrows* iArrows;
        MTreeNodeItemSelect& iObserver;
        TInt iScrollType;
        TBool isSelect;
        };
#endif /* TREEVIEW_H_ */
/*
* TreeViewContainer.h
*
*  Created on: 2009-7-15
*      Author: Administrator
*/

#ifndef TREEVIEWCONTAINER_H_
#define TREEVIEWCONTAINER_H_

#include "BaseControl.h"

class CTreeViewContainer : public CBaseControl
        {
public:
        CTreeViewContainer();
        virtual ~CTreeViewContainer();

        virtual void Draw(CBitmapContext& gc) const;
        virtual void SetRect(const TRect &aRect);
        virtual TKeyResponse OfferKeyEventL(const TKeyEvent& aKeyEvent, TEventCode aType);

        virtual void AddControls(CBaseControl*);
        virtual void RemoveControls(TInt aControlIdx);

        virtual void ScrollPrev();
        virtual void ScrollNext();

        TInt GetFocusIdx();
        void SetFocusIdx(TInt aIdx);
        
        virtual void SetNextRect();
        virtual void SetPrvRect();
        
        void SetParentRect(const TRect& aRect);
        
        TInt GetControlsCount()
                {
                return iArrayAllControl.Count();
                }
        
public:
        CBaseControl* GetCurrentControl();
        void AddNodeItem(RPointerArray* aArrayControl);
        void RemoveNodeItem(TInt aCount);
        void RemoveAllNodeItem();
        
public:
        RPointerArray iArrayFocusControl;
        RPointerArray iArrayAllControl;
        TInt iFocusControlIdx;
        TRect iParentRect;
        
        };

#endif /* TREEVIEWCONTAINER_H_ */
/*
* TreeNode.cpp
*
*  Created on: 2009-7-15
*      Author: Administrator
*/
#include "TreeNode.h"
#include "TreeNodeItem.h"
#include "TextLable.h"
#include "ImageLable.h"
#include "TreeViewContainer.h"

#include

#include

CTreeNode* CTreeNode::NewL(const TDesC& aTitleStr, CFont* aFont,CTreeViewContainer* aParent)
        {
        CTreeNode* self = new (ELeave) CTreeNode();
        CleanupStack::PushL(self);
        self->ConstructL(aTitleStr, aFont,aParent);
        CleanupStack::Pop(self);
        return self;
        }
CTreeNode::~CTreeNode()
        {
        delete iTitleTXT;
        delete iImage;
        iChildNodes.ResetAndDestroy();
        iChildNodes.Close();
        }
CTreeNode::CTreeNode()
        {
        }
void CTreeNode::ConstructL(const TDesC& aTitleStr, CFont* aFont,CTreeViewContainer* aParent)
        {
        iControlType = ENUM_CTreeNode;
        iTitleTXT = CTextLable::NewL(aTitleStr, aFont);
        
        if (aParent)
                iObserver = aParent;
        }
void CTreeNode::SetPic(CFbsBitmap* aBitmap,CFbsBitmap* aMaskBitmap)
        {
        iImage = new (ELeave) CImageLable(aBitmap);
        iImage->SetMask(aMaskBitmap);
        }

//form base class
void CTreeNode::Draw(CBitmapContext& gc) const
        {
        if (iFocus)
                DrawFocus(gc);
        
        if (iTitleTXT)
                iTitleTXT->Draw(gc);
        
        if (iImage)
                iImage->Draw(gc);
        }
void CTreeNode::SetRect(const TRect &aRect)
        {
        CBaseControl::SetRect(aRect);
        if (iImage)
                {
                TInt tx = aRect.iTl.iX + 10;
                TInt ty = (aRect.Height() - iImage->GetHeight()) / 2 + aRect.iTl.iY;
                TInt bx = tx + 20;
                TInt by = ty + iImage->GetHeight();
                iImage->SetRect(TRect(tx, ty, bx, by));
                }
        if (iTitleTXT)
                {
                TInt tx = aRect.iTl.iX + 30;
                TInt ty = (aRect.Height() - iTitleTXT->GetTextHeight(aRect.Width() - 20)) / 2 + aRect.iTl.iY;
                TInt bx = aRect.iBr.iX - 10;
                TInt by = ty + iTitleTXT->GetTextHeight(aRect.Width() - 20);
                iTitleTXT->SetRect(TRect(tx, ty, bx, by));
                }
        
        }
TInt CTreeNode::GetHeight()
        {
        return 20;
        }
TInt CTreeNode::GetWidth()
        {
        return Rect().Width();
        }

void CTreeNode::AddNodeItem(CBaseControl* aControl)
        {
        if (!aControl)
                return;

        iChildNodes.Append(aControl);

        }
void CTreeNode::RemoveNodeItem(TInt aIdx)
        {
        if (aIdx >= 0 && aIdx < iChildNodes.Count())
                {
                //TODO
                iChildNodes.Remove(aIdx);
                }
        }
TKeyResponse CTreeNode::OfferKeyEventL(const TKeyEvent& aKeyEvent, TEventCode /*aType*/)
        {
        switch (aKeyEvent.iCode)
                        {
                        case EKeyOK:
                                {
                                SetControlsVisible(!iConsVisible);
                                return EKeyWasConsumed;
                                }
                        }
        return EKeyWasNotConsumed;
        }
void CTreeNode::SetControlsVisible(TBool aVal)
        {
        iConsVisible = !iConsVisible;
        for (TInt i = 0; i < iChildNodes.Count();i++)
                {
                iChildNodes->SetVisible(aVal);
                }
        if (!iObserver)
                return;
        if (aVal)
                {
                iObserver->AddNodeItem(&iChildNodes);
                }
        else
                {
                iObserver->RemoveNodeItem(iChildNodes.Count());
                }
        }
void CTreeNode::SetSelect(TBool aVal)
        {
        isSelect = aVal;
        if (!aVal)
                {
                for (TInt i = 0; i < iChildNodes.Count(); i++)
                        {
                        CTreeNodeItem* nodeItem = static_cast(iChildNodes);
                        if (nodeItem)
                                nodeItem->SetSelect(aVal);
                        }
                }
        }


/*
* TreeNodeItem.cpp
*
*  Created on: 2009-7-15
*      Author: Administrator
*/

#include "TreeNodeItem.h"
#include "TreeNode.h"
#include "TextLable.h"

#include
#include

CTreeNodeItem* CTreeNodeItem::NewL(CBaseControl* aParent)
        {
        CTreeNodeItem* self = new (ELeave) CTreeNodeItem();
        CleanupStack::PushL(self);
        self->ConstructL(aParent);
        CleanupStack::Pop(self);
        return self;
        }
CTreeNodeItem::~CTreeNodeItem()
        {
        delete iTextLable;
        }
CTreeNodeItem::CTreeNodeItem()
        {
        }
void CTreeNodeItem::ConstructL(CBaseControl* aParent)
        {
        iControlType = ENUM_CTreeNodeItem;
        if (aParent)
                iParentControl = aParent;
        }
void CTreeNodeItem::Draw(CBitmapContext& gc) const
        {
        if (iFocus)
                {
                DrawFocus(gc);
                }
        if (iTextLable)
                {
                iTextLable->Draw(gc);
                }
        }
void CTreeNodeItem::SetRect(const TRect &aRect)
        {
        CBaseControl::SetRect(aRect);
        if (iTextLable)
                {
                TInt tx = aRect.iTl.iX + 5;
                TInt ty = (aRect.Height() - iTextLable->GetHeight()) / 2 + aRect.iTl.iY;
                TInt bx = aRect.iBr.iX - 5;
                TInt by = ty + iTextLable->GetHeight();

                iTextLable->SetRect(TRect(tx, ty, bx, by));
                }
        }
TInt CTreeNodeItem::GetHeight()
        {
        return 20;

        TInt height = -1;
        if (iTextLable)
                height = iTextLable->GetHeight();

        return height;

        }
TInt CTreeNodeItem::GetWidth()
        {
        return Rect().Width();
        }
void CTreeNodeItem::SetString(TInt aRes, CFont* aFont)
        {
        HBufC* strBuf = StringLoader::LoadL(aRes);
        if (strBuf)
                {
                delete iTextLable;
                iTextLable = CTextLable::NewL(strBuf->Des(), aFont);
                }
        delete strBuf;
        }
void CTreeNodeItem::SetString(const TDesC& aStr, CFont* aFont)
        {
        HBufC* strBuf = aStr.Alloc();
        if (strBuf)
                {
                delete iTextLable;
                iTextLable = CTextLable::NewL(strBuf->Des(), aFont);
                }
        delete strBuf;
        }
void CTreeNodeItem::SetImage(TInt /*aRes*/)
        {
        }
void CTreeNodeItem::SetImage(const CFbsBitmap* /*aRes*/)
        {
        }
TKeyResponse CTreeNodeItem::OfferKeyEventL(const TKeyEvent& aKeyEvent, TEventCode /*aType*/)
        {
        switch (aKeyEvent.iCode)
                {
                case EKeyOK:
                        {
                        SetSelect(ETrue);
                        static_cast(iParentControl)->SetSelect(ETrue);
                        return EKeyWasConsumed;
                        }
                }
        return EKeyWasNotConsumed;
        }

/*
* TreeView.cpp
*
*  Created on: 2009-7-15
*      Author: Administrator
*/
#include "TreeView.h"
#include "UIConsts.h"
#include "TextLable.h"
#include "Bar.h"
#include "Arrows.h"
#include "TreeViewContainer.h"
#include "TreeNode.h"
#include "TreeNodeItem.h"
#include "BaseControl.h"

#include
#include
#include
#include
#include
#include

CTreeView* CTreeView::NewL(MTreeNodeItemSelect& aObserver)
        {
        CTreeView* self = new (ELeave) CTreeView(aObserver);
        CleanupStack::PushL(self);
        self->ConstructL();
        CleanupStack::Pop(self);
        return self;
        }

CTreeView::CTreeView(MTreeNodeItemSelect& aObserver) :
        iObserver(aObserver)
        {
        }
void CTreeView::ConstructL()
        {
        iBar = new (ELeave) CBar();
        iArrows = new (ELeave) CArrows();
        iListContainer = new (ELeave) CTreeViewContainer();
        }
CTreeView::~CTreeView()
        {
        delete iBar;
        delete iArrows;
        delete iListContainer;
        }
TKeyResponse CTreeView::OfferKeyEventL(const TKeyEvent& aKeyEvent, TEventCode aType)
        {
        switch (aKeyEvent.iCode)
                {
                case EKeyUpArrow:
                case EKeyDownArrow:
                        {
                        if (iListContainer)
                                {
                                TKeyResponse res = iListContainer->OfferKeyEventL(aKeyEvent, aType);
                                SetArrowsRect();
                                return res;
                                }
                        break;
                        }
                case EKeyOK:
                        {
                        if (iListContainer)
                                {
                                CBaseControl* control = iListContainer->GetCurrentControl();
                                if (control)
                                        {
                                        switch (control->iControlType)
                                                {
                                                case CBaseControl::ENUM_CTreeNode:
                                                        {
                                                        static_cast (control)->OfferKeyEventL(aKeyEvent, aType);
                                                        break;
                                                        }
                                                case CBaseControl::ENUM_CTreeNodeItem:
                                                        {
                                                        for (TInt i = 0; i < iListContainer->iArrayAllControl.Count(); i++)
                                                                {
                                                                if (iListContainer->iArrayAllControl->iControlType == CBaseControl::ENUM_CTreeNode)
                                                                        {
                                                                        static_cast (iListContainer->iArrayAllControl)->SetSelect(EFalse);
                                                                        }
                                                                }
                                                        static_cast (control)->OfferKeyEventL(aKeyEvent, aType);
                                                        iObserver.NotifyTreeNodeItemSelect();
                                                        break;
                                                        }
                                                }
                                        }
                                }
                        return EKeyWasConsumed;
                        }
                }
        return EKeyWasNotConsumed;
        }
void CTreeView::Draw(CBitmapContext& gc) const
        {
        if (iBar)
                iBar->Draw(gc);

        if (iArrows)
                iArrows->Draw(gc);

        if (iListContainer)
                iListContainer->Draw(gc);
        gc.SetBrushStyle(CGraphicsContext::ENullBrush);
        }
void CTreeView::SetRect(const TRect &aRect)
        {
        CBaseControl::SetRect(aRect);

        if (iListContainer)
                {
                TRect rect = TRect(aRect.iTl.iX + 5, aRect.iTl.iY, aRect.iBr.iX - 5, aRect.iBr.iY);
                iListContainer->SetRect(rect);
                iListContainer->SetParentRect(rect);
                }
        if (aRect.Height() < iListContainer->Rect().Height())
                {
                if (iBar)
                        iBar->SetRect(TRect(aRect.iBr.iX - 5, aRect.iTl.iY, aRect.iBr.iX, aRect.iBr.iY));
                if (iArrows)
                        SetArrowsRect();
                }
        }
void CTreeView::HandleResourceChange()
        {
        if (iListContainer)
                iListContainer->SetPrvRect();
        }
void CTreeView::AddTreeViewNode(CBaseControl* aControl)
        {
        if (aControl == NULL)
                return;
        if (iListContainer == NULL)
                {
                delete aControl;
                return;
                }
        iListContainer->AddControls(aControl);

        iListContainer->SetFocusIdx(0);
        }
TInt CTreeView::GetItemFocusIdx()
        {
        if (iListContainer == NULL)
                return -1;
        return iListContainer->GetFocusIdx();
        }
void CTreeView::SetItemFocusIdx(TInt aIdx)
        {
        if (iListContainer == NULL)
                return;
        iListContainer->SetFocusIdx(aIdx);
        }
void CTreeView::SetArrowsRect()
        {
        //set arrows rect
        if (!iArrows || !iBar)
                return;

        iArrows->SetRect(iBar->Rect());
        if (!iListContainer)
                return;

        if (iListContainer->GetControlsCount() > 0)
                {
                TRect barRect = iBar->Rect();
                TRect containerRect = iListContainer->Rect();

                TRect arrowsRect;
                if (iScrollType == ENUM_DYNAMIC)
                        {
                        TInt iNum = iListContainer->iFocusControlIdx * barRect.Height() / iListContainer->iArrayAllControl.Count() + barRect.iTl.iY;

                        arrowsRect = TRect(barRect.iTl.iX, iNum, barRect.iBr.iX, iNum + 10);
                        }
                else
                        {
                        arrowsRect = TRect(barRect.iTl.iX, barRect.iTl.iY, barRect.iBr.iX, barRect.iBr.iY);
                        }
                iArrows->SetRect(arrowsRect);
                }
        }
TInt CTreeView::GetHeight()
        {
        return Rect().Height();
        }
TInt CTreeView::GetWidth()
        {
        return Rect().Width();
        }
TInt CTreeView::GetSelectNodeIdx()
        {
        TInt idx = 0;
        for (TInt i = 0; i < iListContainer->iArrayAllControl.Count(); i++)
                {
                if (iListContainer->iArrayAllControl->iControlType == CBaseControl::ENUM_CTreeNode)
                        {
                        idx++;
                        if (static_cast (iListContainer->iArrayAllControl)->GetSelect())
                                {
                                break;
                                }
                        }
                }
        return idx - 1;
        }
TInt CTreeView::GetSelectNodeItemIdx()
        {
        TInt idx = 0;
        for (TInt i = 0; i < iListContainer->iArrayAllControl.Count(); i++)
                {
                if (iListContainer->iArrayAllControl->iControlType == CBaseControl::ENUM_CTreeNode)
                        {
                        CTreeNode* node = static_cast (iListContainer->iArrayAllControl);
                        if (node->GetSelect())
                                {
                                for (TInt j = 0; j < node->iChildNodes.Count(); j++)
                                        {
                                        idx = j;
                                        CTreeNodeItem* nodeItem = static_cast (node->iChildNodes[j]);
                                        if (nodeItem->GetSelect())
                                                {
                                                break;
                                                }
                                        }
                                }
                        }
                }
        return idx;
        }
/*
* TreeViewContainer.cpp
*
*  Created on: 2009-7-15
*      Author: Administrator
*/

#include "TreeViewContainer.h"
#include "TreeNode.h"
#include "TreeNodeItem.h"

#include

CTreeViewContainer::CTreeViewContainer()
        {
        }
CTreeViewContainer::~CTreeViewContainer()
        {
        RemoveAllNodeItem();
        iArrayAllControl.ResetAndDestroy();
        iArrayAllControl.Close();
        iArrayFocusControl.Close();
        }
void CTreeViewContainer::SetRect(const TRect &aRect)
        {
        TInt TLy = aRect.iTl.iY;
        TInt space = 0;
        TInt BRy = TLy;

        for (TInt i = 0; i < iArrayFocusControl.Count(); i++)
                {
                CBaseControl* iCmp = iArrayFocusControl;
                TInt height = iCmp->GetHeight();

                BRy += height;
                TInt tx = aRect.iTl.iX;
                if (iCmp->iControlType == ENUM_CTreeNodeItem)
                        tx += 20;

                TRect rect(tx, TLy, aRect.iTl.iX + aRect.Width() - UIConsts::BAR_WEIGHT, BRy);
                iCmp->SetRect(rect);
                TLy += (height + space);
                BRy = TLy;
                }
        CBaseControl::SetRect(TRect(aRect.iTl.iX, aRect.iTl.iY, aRect.iBr.iX, BRy));
        }
TKeyResponse CTreeViewContainer::OfferKeyEventL(const TKeyEvent& aKeyEvent, TEventCode aType)
        {
        switch (aKeyEvent.iCode)
                {
                case EKeyUpArrow:
                        {
                        ScrollPrev();
                        return EKeyWasConsumed;
                        }
                case EKeyDownArrow:
                        {
                        ScrollNext();
                        return EKeyWasConsumed;
                        }
                case EKeyOK:
                        {
                        //TODO
                        CBaseControl* control = iArrayAllControl[iFocusControlIdx];
                        if (control)
                                {
                                switch (control->iControlType)
                                        {
                                        case ENUM_CTreeNode:
                                                {
                                                static_cast (control)->OfferKeyEventL(aKeyEvent, aType);
                                                break;
                                                }
                                        case ENUM_CTreeNodeItem:
                                                {
                                                static_cast (control)->OfferKeyEventL(aKeyEvent, aType);
                                                break;
                                                }
                                        }
                                }
                        return EKeyWasConsumed;
                        }
                default:
                        return EKeyWasNotConsumed;
                }
        return EKeyWasNotConsumed;
        }
CBaseControl* CTreeViewContainer::GetCurrentControl()
        {
        return iArrayAllControl[iFocusControlIdx];
        }
void CTreeViewContainer::AddNodeItem(RPointerArray* aArrayControl)
        {
        TInt index = iFocusControlIdx + 1;
        for (TInt i = 0; i < (*aArrayControl).Count(); i++)
                {
                iArrayAllControl.Insert((*aArrayControl), index);
                iArrayFocusControl.Insert((*aArrayControl), index);
                index++;
                }
        SetRect(Rect());
        }
void CTreeViewContainer::RemoveNodeItem(TInt aCount)
        {
        TInt index = iFocusControlIdx + 1;
        for (TInt i = 0; i < aCount; i++)
                {
                iArrayAllControl.Remove(index);
                iArrayFocusControl.Remove(index);
                }
        SetRect(Rect());
        }

void CTreeViewContainer::Draw(CBitmapContext& gc) const
        {
        TRect aRect = Rect();
        gc.SetBrushStyle(CGraphicsContext::ENullBrush);
        gc.SetPenStyle(CGraphicsContext::ENullPen);
        //draw group
        for (TInt i = 0; i < iArrayAllControl.Count(); i++)
                {
                CBaseControl* control = iArrayAllControl;
                TRect itemRect = control->Rect();
                if (itemRect.iBr.iY <= iParentRect.iBr.iY && itemRect.iTl.iY >= iParentRect.iTl.iY)
                        {
                        control->Draw(gc);
                        }
                }
        gc.SetBrushStyle(CGraphicsContext::ENullBrush);
        gc.SetPenStyle(CGraphicsContext::ENullPen);
        }

void CTreeViewContainer::AddControls(CBaseControl* aControl)
        {
        if (aControl)
                {
                iArrayAllControl.Append(aControl);
                if (aControl->GetEnable())
                        iArrayFocusControl.Append(aControl);

                }
        }
void CTreeViewContainer::RemoveControls(TInt aControlIdx)
        {
        if (aControlIdx < iArrayAllControl.Count() && aControlIdx >= 0)
                {
                CBaseControl* iControl = iArrayAllControl[aControlIdx];
                iArrayAllControl.Remove(aControlIdx);
                delete iControl;
                iControl = NULL;
                }
        }

void CTreeViewContainer::ScrollPrev()
        {
        if (iArrayFocusControl.Count() > 0)
                {
                if (iFocusControlIdx > 0)
                        {
                        SetFocusIdx(iFocusControlIdx -= 1);
                        SetPrvRect();
                        }
                else
                        {
                        SetFocusIdx(iArrayFocusControl.Count() - 1);
                        SetNextRect();
                        }
                SetFocusIdx(iFocusControlIdx);
                }
        }
void CTreeViewContainer::ScrollNext()
        {
        if (iArrayFocusControl.Count() > 0)
                {
                if (iFocusControlIdx < iArrayFocusControl.Count() - 1)
                        {
                        SetFocusIdx(iFocusControlIdx += 1);
                        SetNextRect();
                        }
                else
                        {
                        SetFocusIdx(0);
                        SetPrvRect();
                        }
                }
        }
TInt CTreeViewContainer::GetFocusIdx()
        {
        return iFocusControlIdx;
        }
void CTreeViewContainer::SetFocusIdx(TInt aIdx)
        {
        if (aIdx < iArrayFocusControl.Count() && aIdx >= 0)
                {
                for (TInt i = 0; i < iArrayFocusControl.Count(); i++)
                        {
                        iArrayFocusControl->SetFocus(EFalse);
                        }
                iArrayFocusControl[aIdx]->SetFocus(ETrue);
                iFocusControlIdx = aIdx;
                }
        }
void CTreeViewContainer::SetNextRect()
        {
        //get focus item rect
        TRect itemRect(iArrayFocusControl[iFocusControlIdx]->Rect());
        //get container rect
        TRect containerRect = iParentRect;

        if (itemRect.iBr.iY > containerRect.iBr.iY)
                {
                TRect rect = Rect();
                rect.iTl.iY -= itemRect.iBr.iY - containerRect.iBr.iY;
                //                containerRect.iTl.iY -= (itemRect.iTl.iY - 320) + itemRect.Height();
                SetRect(rect);
                }
        }
void CTreeViewContainer::SetPrvRect()
        {
        //get focus item rect
        TRect itemRect(iArrayFocusControl[iFocusControlIdx]->Rect());
        //get container rect
        TRect containerRect = iParentRect;

        if (iFocusControlIdx == 0)
                {
                SetRect(containerRect);
                return;
                }
        
        if (itemRect.iTl.iY < containerRect.iTl.iY)
                {
                TRect rect = Rect();
                rect.iTl.iY -= (itemRect.iTl.iY - containerRect.iTl.iY);
                SetRect(rect);
                }
        else if (itemRect.iTl.iY < containerRect.iTl.iY)
                {
                TRect rect = Rect();
                rect.iTl.iY += itemRect.Height() ;
                SetRect(rect);
                }
        }
void CTreeViewContainer::SetParentRect(const TRect& aRect)
        {
        iParentRect = aRect;
        }
void CTreeViewContainer::RemoveAllNodeItem()
        {
        TInt idx = 0;
        while (idx < iArrayAllControl.Count())
                {
                if (iArrayAllControl[idx]->iControlType == CBaseControl::ENUM_CTreeNodeItem)
                        {
                        iArrayAllControl.Remove(idx);
                        iArrayFocusControl.Remove(idx);
                        }
                else
                        {
                        idx++;
                        }
                }
        }

转自 : www.devdiv.net/thread-12008-1-1.html
阅读(726) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~