home *** CD-ROM | disk | FTP | other *** search
- // -------------------------------------------------------------------------
- // Copyright @ 1997 TCK Software, Incorporated
- // All Rights Reserved
- // -------------------------------------------------------------------------
- #ifndef __VTREE_H__
- #define __VTREE_H__
-
- #ifdef VF_STATIC
- #undef AFX_EXT_CLASS
- #define AFX_EXT_CLASS
- #endif
-
- #include "afxtempl.h"
-
- #define VTREE_MAX_DEPTH 16
-
- // --- Special VBranch pointer values to optimize memory requirements ---
- #define VBRANCH_NONE 0 // No Branch - No Children
- #define VBRANCH_UNKNOWN 1 // No Branch Yet - May have children
- #define VBRANCH_UNFILLED 2 // No Branch Yet - DOES have children
- #define VBRANCH_VALID 3 // Used to check for invalid value
-
- #define VTI_ROOT -1 // Flag for ROOT Item
- #define VTI_FIRSTCHILD 1 // Insert as First Child
- #define VTI_LASTCHILD 2 // Insert as Last Child
- #define VTI_SORTCHILD 3 // Insert child in sorted sequence
- #define VTI_BEFORE 4 // Insert before (sibling)
- #define VTI_AFTER 5 // Insert after (sibling)
-
- // --- Forward Declarations ---
- class VTreeItem; // Base Tree Item Class
- class VTreePtrItem; // Specialized VTreeItem
- class VBranch; // Branch of a tree
- struct VTreeInfo; // Expanded Info on a tree item
- struct VBranchPos; // Holds a branch position
- class VTreePos; // Holds a tree position
- class VTree; // The Tree Object
-
- // --- Prototype for VTree Sort Callback Function ---
- typedef int (* LPFN_VTISORT)(VTreeItem **, VTreeItem **);
- typedef int (* LPFN_QSORT)(const void *, const void *);
-
- // -------------------------------------------------------------------------
- // Class VTreeItem - Item within a VTree
- // - To be used as a base class for users own items
- //
- // Please Note:
- // The only overhead to this class is a branch pointer and the VTable pointer.
- //
- // Since there may be many VTreeItems (nodes), we want to minimize memory
- // requirements. Therefore the branch (if one exists) maintains the pointer
- // to its parent branch, its children count and its expanded status. If
- // the VTreeItem does not have any children (no branch), this information
- // is not needed.
- //
- // One complexity exists - the pointer to the parent branch must be
- // correctly maintained. The VTree will handle this - therefore all
- // inserts of children should be handled through the VTree.
- //
- // A specialized version of VTree and VTreeItem (copied to a new name - not
- // derived) could be created to avoid the VBranch * on the leaf nodes. This
- // implementation is more flexible (but does incur extra overhead on known
- // leaf nodes).
- // -------------------------------------------------------------------------
- class AFX_EXT_CLASS VTreeItem
- {
- protected:
- VBranch *m_pBranch; // Pointer to Branch
-
- public:
- VTreeItem() { m_pBranch = VBRANCH_NONE; } // Constructor
- virtual ~VTreeItem(); // Virtual Destructor
-
- VBranch* GetBranch(); // Returns branch pointer
-
- BOOL IsExpanded(); // Returns TRUE if Expanded Branch
- long ChildCount(); // Immediate Child Count
- long VisChildCount(); // Visible Child Count
- long TotChildCount(); // All Children Count
-
- void SetBranchStatus(int nBranchStatus);
- ULONG GetBranchStatus();
- void Expand(BOOL bFlag=TRUE); // Expands/Collapses Branch
- void ExpandAll(BOOL bFlag=TRUE); // Recursively Expand/Collapse
- void Collapse() { Expand(FALSE); } // Collapses Branch
- void CollapseAll() { ExpandAll(FALSE); } // Collapses Recursively
- void CollapseReset(int nBranchStatus=VBRANCH_NONE);
- // Collapses and delete Branch
-
- BOOL SortChildren(LPFN_VTISORT pSortFunc, BOOL bRecursive=FALSE);
- // Sort child items
-
- virtual int IsA() { return 0; } // override to indicate type
- virtual BOOL OnExpand(BOOL bExpand) // Called when Expanded/Collapsed
- { return TRUE; }
-
- // --- Used by VTree to Insert and Remove items ----
- BOOL InsertAt(long nIdx, VTreeItem *pItem, VBranch *pParentBranch);
- BOOL InsertLast(VTreeItem *pItem, VBranch *pParentBranch);
- BOOL InsertFirst(VTreeItem *pItem, VBranch *pParentBranch);
-
- VTreeItem* RemoveAt(long nIdx);
- VTreeItem* RemoveFirst();
- VTreeItem* RemoveLast();
- };
-
- typedef CArray<VTreeItem *, VTreeItem *> VTreeItemPtrArray;
-
- // -------------------------------------------------------------------------
- // Class VTreePtrItem - example VTreeItem derived class
- // -------------------------------------------------------------------------
- class AFX_EXT_CLASS VTreePtrItem : public VTreeItem
- {
- protected:
- void* m_pData; // pointer to actual data
- public:
- VTreePtrItem(void *p=0) { m_pData = p; } // Constructor
- virtual ~VTreePtrItem() {}; // Virtual Destructor
-
- void SetData(void *p) { m_pData = p; }
- void* GetData() { return m_pData; }
- };
-
- // --- Utilize if we wish to keep a quick ref list of all branches ---
- // typedef CArray<VBranch *, VBranch *> VBranchPtrArray;
-
- // -------------------------------------------------------------------------
- // Class VTreeItem - Item within a VTree
- // - To be used as a base class for users own items
- //
- // Notes:
- // m_nChildCount includes immediate and deep children
- // m_nVisChildCount includes immediate and deep children
- // -------------------------------------------------------------------------
- class AFX_EXT_CLASS VBranch
- {
- protected:
- VBranch* m_pParent; // Parent Branch
- BOOL m_bExpanded; // Expanded flag
- long m_nChildCount; // Count of ALL child items
- long m_nVisChildCount; // Count of ALL visible child items
- VTreeItemPtrArray m_item; // Array of items
- // VBranchPtrArray m_child; // Array of children branches
-
- protected:
- // ---- Utility Functions, used internally ----
- void ChgVisChildCount(long nDelta); // Change Vis Child Count
- void ChgChildCount(long nDelta); // Change Child Count
-
- public:
- VBranch(VBranch *pParent=0); // Constructor
- virtual ~VBranch(); // Virtual Destructor
-
- // --- Key functions to providing a "Flat" look to the tree ---
- BOOL GetPosition(long nItem, short nLvl, VTreePos &pos);
- BOOL GetVisPosition(long nVisItem, short nLvl, VTreePos &pos);
- // --- the following versions ALWAYS start from beginning ---
- BOOL SimpleGetPosition(long nItem, short nLvl, VTreePos &pos);
- BOOL SimpleGetVisPosition(long nVisItem, short nLvl, VTreePos &pos);
-
- // --- Get position of any item by its pointer value
- BOOL GetItemPosition(VTreeItem* pVTItem, short nLvl, VTreePos &pos);
-
- void Expand(BOOL bExpand); // Expand/Collapse branch
- void ExpandAll(BOOL bExpand); // Expand/Collapse ALL children
-
- BOOL IsEmpty() { return ItemCount() == 0; }
- BOOL IsExpanded() { return m_bExpanded; }
- long ItemCount() { return m_item.GetSize(); }
- long ChildCount() { return m_nChildCount; }
- long VisChildCount(); // Visible children count
-
- VTreeItemPtrArray* GetArray() { return &m_item; }
- VTreeItem* operator [](long x) { return m_item[x]; }
-
- BOOL InsertAt(long nIdx, VTreeItem* pItem);
- BOOL InsertLast(VTreeItem* pItem);
- BOOL InsertFirst(VTreeItem* pItem);
-
- VTreeItem* RemoveAt(long nIdx); // Removes Item at index
- VTreeItem* RemoveFirst(); // Removes First item
- VTreeItem* RemoveLast(); // Removes Last Item
-
- BOOL SortChildren(LPFN_VTISORT pSortFunc, BOOL bRecursive=FALSE);
- // Sort branch items
-
- void DeleteAllItems(); // Deletes all items from branch
- };
-
- // -------------------------------------------------------------------------
- // VTreeInfo structure
- // -------------------------------------------------------------------------
- struct VTreeInfo
- {
- VTreeItem *m_pItem; // Pointer to TreeItem
- long m_nItem; // Item Id
- long m_nVisItem; // Visible Item Id
- };
-
- // -------------------------------------------------------------------------
- // VBranchPos - helper structure, to facilitate faster navigation of tree
- // -------------------------------------------------------------------------
- struct VBranchPos
- {
- VBranch* m_pBranch; // Branch we are on
- long m_nArrIdx; // Array index
- long m_nItem; // Number of item
- long m_nVisItem; // Number of Visible item
- VTreeItem* m_pTreeItem; // Item we are pointing to
- };
-
- // -------------------------------------------------------------------------
- // VTreePos - helper structure, to facilitate faster repeat navigation of tree
- // - actually acts as a very simple index to a position
- // - contains an index to each level (child branches) of the position
- // -------------------------------------------------------------------------
- class VTreePos
- {
- friend class VTree;
- friend class VBranch;
- protected:
- VTreeItem* m_pTreeItem; // Item we are pointing to
- VBranchPos m_posBranch[VTREE_MAX_DEPTH]; // position on branch
- long m_nItem; // Flat index of position
- long m_nVisItem; // Flat visible index of position
- short m_nLevel; // Level of position - starts at 1
- BOOL m_bValid; // Is this position valid
- public:
- VTreePos();
-
- BOOL IsValid() { return m_bValid; }
- void Valid(BOOL x) { m_bValid = x; }
-
- VTreeItem* TreeItem() { return m_pTreeItem; }
- long Item() { return m_nItem; }
- long VisibleItem() { return m_nVisItem; }
- short Level() { return m_nLevel; }
-
- BOOL Parent(); // Moves to current items parent
- BOOL Child(); // Moves to first child
- BOOL Next(); // Moves to current items next sibling
- BOOL Previous(); // Moves to current items previous sibling
-
- CString GetTextPosition(); // used for debugging
- void MsgTextPosition(); // used for debugging
-
- VTreeItem* GetParentItem(); // Returns parent item pointer (no movement)
- VBranchPos* GetBranchPos(int nLevel=0); // Returns pointer to bPos
- };
-
- // -------------------------------------------------------------------------
- // Class VTree - Tree memory storage class
- // - provides functions for a "flat" view of the tree
- // Concepts:
- // Using a 'Flat' index, you may iterate through every tree member.
- // Using a 'Flat Visible' index, you may iterate through every visible
- // tree member.
- // - VForm will only use the Visible index as its view
- // -------------------------------------------------------------------------
- class AFX_EXT_CLASS VTree
- {
- protected:
- VBranch m_branch; // Primary branch (root) of the tree
- VTreePos m_pos; // holds current position
- LPFN_VTISORT m_pSortFunc; // Pointer to sort callback function
- public:
- VTree(); // Constructor
- virtual ~VTree() {}; // Destructor
-
- void SetSortCallback(LPFN_VTISORT pFunc) { m_pSortFunc = pFunc; }
- long GetCount() { return m_branch.ChildCount(); }
- long GetRootCount() { return m_branch.ItemCount(); }
- long GetVisibleCount() { return m_branch.VisChildCount(); }
- void GetPosition(VTreePos &pos) { pos = m_pos; }
- long CurItem(); // returns current item or -1
- long CurVisItem(); // returns current visible item or -1
-
- BOOL IsEmpty() { return m_branch.IsEmpty(); }
- BOOL IsExpanded(long nItem); // Is this item expanded
- BOOL Expand(long nItem, UINT nCode); // Expands similar to TreeCtrl
- BOOL Expand(long nItem); // Expands item at given index
- BOOL Collapse(long nItem); // Collapse item at given index
- BOOL CollapseReset(long nItem); // Collapse and delete below
- BOOL Toggle(long nItem); // Toggles item at index
- BOOL ExpandAll(long nCurRow=0); // Expands all
- BOOL CollapseAll(long nCurRow=0); // Collapses all
-
- // --- The following reposition current position ---
- BOOL Position(long nItem, VTreePos* pPos=0);
- BOOL VisPosition(long nVisItem, VTreePos* pPos=0);
-
- // --- These always start at tree beginning
- BOOL SimplePosition(long nItem, VTreePos* pPos=0);
- BOOL SimpleVisPosition(long nVisItem, VTreePos* pPos=0);
-
- BOOL ItemPosition(VTreeItem *pItem, VTreePos* pPos=0);
-
- BOOL GetRoot(VTreeInfo &rInfo); // Root
- BOOL GetParent(long nItem, VTreeInfo &rInfo); // Parent
- BOOL GetChild(long nItem, VTreeInfo &rInfo); // First Child
- BOOL GetNext(long nItem, VTreeInfo &rInfo); // Next Sibling
- BOOL GetPrevious(long nItem, VTreeInfo &rInfo); // Previous Sibling
- BOOL GetFirstVisible(VTreeInfo &rInfo); // Of ALL items
- BOOL GetNextVisible(long nVisItem, VTreeInfo &rInfo); // Of ALL items
- BOOL GetPreviousVisible(long nVisItem, VTreeInfo &rInfo); // Of ALL items
-
- BOOL Get(long nItem, VTreeInfo &rInfo); // Gets item at index
- BOOL GetVisible(long nVisibleItem, VTreeInfo &rInfo);
- VTreeItem* GetCurItem();
- VTreeItem* GetItem(long nItem);
- VTreeItem* GetVisItem(long nVisItem);
- long ItemToVisItem(long nItem);
- long VisItemToItem(long nVisItem);
-
- // --- Action Functions ---
- BOOL InsertItem(VTreeItem *pItem, long nAnchorItem, int nPositionFlag);
- VTreeItem* RemoveItem(long nItem);
- VTreeItem* RemoveVisItem(long nVisItem);
- BOOL DeleteItem(long nItem);
- BOOL DeleteVisItem(long nVisItem);
- void DeleteAllItems(); // Deletes all items
- BOOL SortChildren(long nItem=VTI_ROOT, BOOL bRecursive=FALSE, LPFN_VTISORT=0);
-
- // Used for debugging
- void MsgCurPosition() { m_pos.MsgTextPosition(); }
- };
-
- #endif