home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 23 / IOPROG_23.ISO / SOFT / VFORM.ZIP / Source / VTree.h < prev   
Encoding:
C/C++ Source or Header  |  1998-10-10  |  12.9 KB  |  328 lines

  1. // -------------------------------------------------------------------------
  2. // Copyright @ 1997 TCK Software, Incorporated
  3. // All Rights Reserved
  4. // -------------------------------------------------------------------------
  5. #ifndef __VTREE_H__
  6. #define __VTREE_H__
  7.  
  8. #ifdef VF_STATIC
  9.     #undef AFX_EXT_CLASS
  10.     #define AFX_EXT_CLASS
  11. #endif
  12.  
  13. #include "afxtempl.h"
  14.  
  15. #define VTREE_MAX_DEPTH    16
  16.  
  17. // --- Special VBranch pointer values to optimize memory requirements ---
  18. #define  VBRANCH_NONE        0                // No Branch - No Children
  19. #define  VBRANCH_UNKNOWN    1                // No Branch Yet - May have children
  20. #define  VBRANCH_UNFILLED    2                // No Branch Yet - DOES have children
  21. #define  VBRANCH_VALID        3                // Used to check for invalid value
  22.  
  23. #define  VTI_ROOT            -1                // Flag for ROOT Item
  24. #define  VTI_FIRSTCHILD        1                // Insert as First Child
  25. #define  VTI_LASTCHILD        2                // Insert as Last Child
  26. #define  VTI_SORTCHILD        3                // Insert child in sorted sequence
  27. #define  VTI_BEFORE            4                // Insert before (sibling)
  28. #define  VTI_AFTER            5                // Insert after (sibling)
  29.  
  30. // --- Forward Declarations ---
  31. class  VTreeItem;                // Base Tree Item Class
  32. class  VTreePtrItem;            // Specialized VTreeItem
  33. class  VBranch;                    // Branch of a tree
  34. struct VTreeInfo;                // Expanded Info on a tree item
  35. struct VBranchPos;                // Holds a branch position
  36. class  VTreePos;                // Holds a tree position
  37. class  VTree;                    // The Tree Object
  38.  
  39. // --- Prototype for VTree Sort Callback Function ---
  40. typedef int (* LPFN_VTISORT)(VTreeItem **, VTreeItem **);
  41. typedef int (* LPFN_QSORT)(const void *, const void *);
  42.  
  43. // -------------------------------------------------------------------------
  44. // Class VTreeItem - Item within a VTree
  45. // - To be used as a base class for users own items
  46. //
  47. // Please Note:
  48. //  The only overhead to this class is a branch pointer and the VTable pointer.
  49. //
  50. //  Since there may be many VTreeItems (nodes), we want to minimize memory
  51. //  requirements.  Therefore the branch (if one exists) maintains the pointer
  52. //  to its parent branch, its children count and its expanded status.  If
  53. //  the VTreeItem does not have any children (no branch), this information
  54. //  is not needed.
  55. //
  56. //  One complexity exists - the pointer to the parent branch must be
  57. //  correctly maintained.  The VTree will handle this - therefore all
  58. //  inserts of children should be handled through the VTree.
  59. //
  60. //  A specialized version of VTree and VTreeItem (copied to a new name - not
  61. //  derived) could be created to avoid the VBranch * on the leaf nodes.  This
  62. //  implementation is more flexible (but does incur extra overhead on known
  63. //  leaf nodes).
  64. // -------------------------------------------------------------------------
  65. class AFX_EXT_CLASS VTreeItem
  66. {
  67. protected:
  68.     VBranch        *m_pBranch;                            // Pointer to Branch
  69.  
  70. public:
  71.     VTreeItem()        { m_pBranch = VBRANCH_NONE; }    // Constructor
  72.     virtual ~VTreeItem();                            // Virtual Destructor
  73.  
  74.     VBranch*    GetBranch();                    // Returns branch pointer 
  75.  
  76.     BOOL        IsExpanded();                    // Returns TRUE if Expanded Branch
  77.     long        ChildCount();                    // Immediate Child Count
  78.     long        VisChildCount();                // Visible Child Count
  79.     long        TotChildCount();                // All Children Count
  80.  
  81.     void        SetBranchStatus(int nBranchStatus);
  82.     ULONG        GetBranchStatus();
  83.     void        Expand(BOOL bFlag=TRUE);        // Expands/Collapses Branch
  84.     void        ExpandAll(BOOL bFlag=TRUE);        // Recursively Expand/Collapse
  85.     void        Collapse()    { Expand(FALSE); }    // Collapses Branch
  86.     void        CollapseAll()    { ExpandAll(FALSE); }    // Collapses Recursively
  87.     void        CollapseReset(int nBranchStatus=VBRANCH_NONE);
  88.                                                 // Collapses and delete Branch
  89.  
  90.     BOOL        SortChildren(LPFN_VTISORT pSortFunc, BOOL bRecursive=FALSE);
  91.                                                 // Sort child items
  92.  
  93.     virtual    int        IsA()    { return 0; }        // override to indicate type        
  94.     virtual BOOL    OnExpand(BOOL bExpand)        // Called when Expanded/Collapsed
  95.                     { return TRUE; }
  96.  
  97.     // --- Used by VTree to Insert and Remove items ----
  98.     BOOL        InsertAt(long nIdx, VTreeItem *pItem, VBranch *pParentBranch);
  99.     BOOL        InsertLast(VTreeItem *pItem, VBranch *pParentBranch);
  100.     BOOL        InsertFirst(VTreeItem *pItem, VBranch *pParentBranch);
  101.  
  102.     VTreeItem*    RemoveAt(long nIdx);
  103.     VTreeItem*    RemoveFirst();
  104.     VTreeItem*    RemoveLast();
  105. };
  106.  
  107. typedef CArray<VTreeItem *, VTreeItem *> VTreeItemPtrArray;
  108.  
  109. // -------------------------------------------------------------------------
  110. // Class VTreePtrItem - example VTreeItem derived class
  111. // -------------------------------------------------------------------------
  112. class AFX_EXT_CLASS VTreePtrItem : public VTreeItem
  113. {
  114. protected:
  115.     void*    m_pData;                                // pointer to actual data
  116. public:
  117.     VTreePtrItem(void *p=0)        { m_pData = p;    }    // Constructor
  118.     virtual ~VTreePtrItem() {};                        // Virtual Destructor
  119.  
  120.     void    SetData(void *p)    { m_pData = p; }
  121.     void*    GetData()            { return m_pData; }
  122. };
  123.  
  124. // --- Utilize if we wish to keep a quick ref list of all branches ---
  125. // typedef CArray<VBranch *, VBranch *> VBranchPtrArray;
  126.  
  127. // -------------------------------------------------------------------------
  128. // Class VTreeItem - Item within a VTree
  129. // - To be used as a base class for users own items
  130. //
  131. // Notes:
  132. //        m_nChildCount includes immediate and deep children
  133. //        m_nVisChildCount includes immediate and deep children
  134. // -------------------------------------------------------------------------
  135. class AFX_EXT_CLASS VBranch
  136. {
  137. protected:
  138.     VBranch*    m_pParent;                // Parent Branch
  139.     BOOL        m_bExpanded;            // Expanded flag
  140.     long        m_nChildCount;            // Count of ALL child items
  141.     long        m_nVisChildCount;        // Count of ALL visible child items
  142.     VTreeItemPtrArray    m_item;            // Array of items
  143.     // VBranchPtrArray        m_child;        // Array of children branches
  144.  
  145. protected:
  146.     // ---- Utility Functions, used internally ----
  147.     void ChgVisChildCount(long nDelta);    // Change Vis Child Count
  148.     void ChgChildCount(long nDelta);    // Change Child Count
  149.  
  150. public:
  151.     VBranch(VBranch *pParent=0);        // Constructor
  152.     virtual ~VBranch();                    // Virtual Destructor
  153.  
  154.     // --- Key functions to providing a "Flat" look to the tree ---
  155.     BOOL    GetPosition(long nItem, short nLvl, VTreePos &pos);
  156.     BOOL    GetVisPosition(long nVisItem, short nLvl, VTreePos &pos);
  157.     // --- the following versions ALWAYS start from beginning ---
  158.     BOOL    SimpleGetPosition(long nItem, short nLvl, VTreePos &pos);
  159.     BOOL    SimpleGetVisPosition(long nVisItem, short nLvl, VTreePos &pos);
  160.  
  161.     // --- Get position of any item by its pointer value
  162.     BOOL    GetItemPosition(VTreeItem* pVTItem, short nLvl, VTreePos &pos);
  163.  
  164.     void    Expand(BOOL bExpand);        // Expand/Collapse branch
  165.     void    ExpandAll(BOOL bExpand);    // Expand/Collapse ALL children
  166.  
  167.     BOOL    IsEmpty()                { return ItemCount() == 0; }
  168.     BOOL    IsExpanded()            { return m_bExpanded; }
  169.     long    ItemCount()                { return m_item.GetSize(); }
  170.     long    ChildCount()            { return m_nChildCount; }
  171.     long    VisChildCount();            // Visible children count
  172.  
  173.     VTreeItemPtrArray*    GetArray()    { return &m_item; }
  174.     VTreeItem*    operator [](long x)    { return m_item[x]; }
  175.  
  176.     BOOL    InsertAt(long nIdx, VTreeItem* pItem);
  177.     BOOL    InsertLast(VTreeItem* pItem);
  178.     BOOL    InsertFirst(VTreeItem* pItem);
  179.  
  180.     VTreeItem*    RemoveAt(long nIdx);    // Removes Item at index
  181.     VTreeItem*    RemoveFirst();            // Removes First item
  182.     VTreeItem*    RemoveLast();            // Removes Last Item
  183.  
  184.     BOOL    SortChildren(LPFN_VTISORT pSortFunc, BOOL bRecursive=FALSE);    
  185.                                         // Sort branch items
  186.  
  187.     void    DeleteAllItems();            // Deletes all items from branch
  188. };
  189.  
  190. // -------------------------------------------------------------------------
  191. // VTreeInfo structure
  192. // -------------------------------------------------------------------------
  193. struct VTreeInfo
  194. {
  195.     VTreeItem    *m_pItem;            // Pointer to TreeItem
  196.     long        m_nItem;            // Item Id
  197.     long        m_nVisItem;            // Visible Item Id
  198. };
  199.  
  200. // -------------------------------------------------------------------------
  201. // VBranchPos - helper structure, to facilitate faster navigation of tree
  202. // -------------------------------------------------------------------------
  203. struct VBranchPos
  204. {
  205.     VBranch*    m_pBranch;            // Branch we are on
  206.     long        m_nArrIdx;            // Array index
  207.     long        m_nItem;            // Number of item
  208.     long        m_nVisItem;            // Number of Visible item
  209.     VTreeItem*    m_pTreeItem;        // Item we are pointing to
  210. };
  211.  
  212. // -------------------------------------------------------------------------
  213. // VTreePos - helper structure, to facilitate faster repeat navigation of tree
  214. //   - actually acts as a very simple index to a position
  215. //   - contains an index to each level (child branches) of the position
  216. // -------------------------------------------------------------------------
  217. class VTreePos
  218. {
  219.     friend class VTree;
  220.     friend class VBranch;
  221. protected:
  222.     VTreeItem*    m_pTreeItem;    // Item we are pointing to
  223.     VBranchPos    m_posBranch[VTREE_MAX_DEPTH]; // position on branch
  224.     long        m_nItem;        // Flat index of position
  225.     long        m_nVisItem;        // Flat visible index of position
  226.     short        m_nLevel;        // Level of position - starts at 1
  227.     BOOL        m_bValid;        // Is this position valid
  228. public:
  229.     VTreePos();
  230.  
  231.     BOOL    IsValid()        { return m_bValid; }
  232.     void    Valid(BOOL x)    { m_bValid = x; }
  233.  
  234.     VTreeItem* TreeItem()    { return m_pTreeItem; }
  235.     long    Item()            { return m_nItem; }
  236.     long    VisibleItem()    { return m_nVisItem; }
  237.     short    Level()            { return m_nLevel; }
  238.  
  239.     BOOL    Parent();        // Moves to current items parent
  240.     BOOL    Child();        // Moves to first child
  241.     BOOL    Next();            // Moves to current items next sibling
  242.     BOOL    Previous();        // Moves to current items previous sibling
  243.  
  244.     CString    GetTextPosition();        // used for debugging
  245.     void    MsgTextPosition();        // used for debugging
  246.  
  247.     VTreeItem*    GetParentItem(); // Returns parent item pointer (no movement)
  248.     VBranchPos* GetBranchPos(int nLevel=0);    // Returns pointer to bPos
  249. };
  250.  
  251. // -------------------------------------------------------------------------
  252. // Class VTree - Tree memory storage class
  253. // - provides functions for a "flat" view of the tree
  254. //   Concepts:
  255. //        Using a 'Flat' index, you may iterate through every tree member.
  256. //        Using a 'Flat Visible' index, you may iterate through every visible
  257. //        tree member.
  258. //    - VForm will only use the Visible index as its view    
  259. // -------------------------------------------------------------------------
  260. class AFX_EXT_CLASS VTree
  261. {
  262. protected:
  263.     VBranch        m_branch;        // Primary branch (root) of the tree
  264.     VTreePos    m_pos;            // holds current position
  265.     LPFN_VTISORT m_pSortFunc;    // Pointer to sort callback function
  266. public:
  267.     VTree();                    // Constructor
  268.     virtual ~VTree() {};        // Destructor
  269.  
  270.     void    SetSortCallback(LPFN_VTISORT pFunc)    { m_pSortFunc = pFunc; }
  271.     long    GetCount()            { return m_branch.ChildCount(); }
  272.     long    GetRootCount()        { return m_branch.ItemCount(); }
  273.     long    GetVisibleCount()    { return m_branch.VisChildCount(); }
  274.     void    GetPosition(VTreePos &pos)    { pos = m_pos; }
  275.     long    CurItem();                        // returns current item or -1
  276.     long    CurVisItem();                    // returns current visible item or -1
  277.  
  278.     BOOL    IsEmpty()            { return m_branch.IsEmpty(); }
  279.     BOOL    IsExpanded(long nItem);            // Is this item expanded
  280.     BOOL    Expand(long nItem, UINT nCode);    // Expands similar to TreeCtrl
  281.     BOOL    Expand(long nItem);                // Expands item at given index
  282.     BOOL    Collapse(long nItem);            // Collapse item at given index
  283.     BOOL    CollapseReset(long nItem);        // Collapse and delete below
  284.     BOOL    Toggle(long nItem);                // Toggles item at index
  285.     BOOL    ExpandAll(long nCurRow=0);        // Expands all
  286.     BOOL    CollapseAll(long nCurRow=0);    // Collapses all
  287.  
  288.     // --- The following reposition current position ---
  289.     BOOL Position(long nItem, VTreePos* pPos=0);
  290.     BOOL VisPosition(long nVisItem, VTreePos* pPos=0);
  291.  
  292.     // --- These always start at tree beginning
  293.     BOOL SimplePosition(long nItem, VTreePos* pPos=0);
  294.     BOOL SimpleVisPosition(long nVisItem, VTreePos* pPos=0);
  295.  
  296.     BOOL ItemPosition(VTreeItem *pItem, VTreePos* pPos=0);
  297.  
  298.     BOOL GetRoot(VTreeInfo &rInfo);                        // Root
  299.     BOOL GetParent(long nItem, VTreeInfo &rInfo);        // Parent
  300.     BOOL GetChild(long nItem, VTreeInfo &rInfo);        // First Child
  301.     BOOL GetNext(long nItem, VTreeInfo &rInfo);            // Next Sibling
  302.     BOOL GetPrevious(long nItem, VTreeInfo &rInfo);        // Previous Sibling
  303.     BOOL GetFirstVisible(VTreeInfo &rInfo);                // Of ALL items
  304.     BOOL GetNextVisible(long nVisItem, VTreeInfo &rInfo);        // Of ALL items
  305.     BOOL GetPreviousVisible(long nVisItem, VTreeInfo &rInfo);    // Of ALL items
  306.  
  307.     BOOL Get(long nItem, VTreeInfo &rInfo);            // Gets item at index
  308.     BOOL GetVisible(long nVisibleItem, VTreeInfo &rInfo);
  309.     VTreeItem* GetCurItem();    
  310.     VTreeItem* GetItem(long nItem);    
  311.     VTreeItem* GetVisItem(long nVisItem);    
  312.     long ItemToVisItem(long nItem);    
  313.     long VisItemToItem(long nVisItem);    
  314.     
  315.     // --- Action Functions ---
  316.     BOOL InsertItem(VTreeItem *pItem, long nAnchorItem, int nPositionFlag);
  317.     VTreeItem* RemoveItem(long nItem);
  318.     VTreeItem* RemoveVisItem(long nVisItem);
  319.     BOOL DeleteItem(long nItem);
  320.     BOOL DeleteVisItem(long nVisItem);
  321.     void DeleteAllItems();                        // Deletes all items
  322.     BOOL SortChildren(long nItem=VTI_ROOT, BOOL bRecursive=FALSE, LPFN_VTISORT=0);
  323.  
  324.     // Used for debugging
  325.     void    MsgCurPosition()    { m_pos.MsgTextPosition(); }
  326. };
  327.  
  328. #endif