home *** CD-ROM | disk | FTP | other *** search
/ Xentax forum attachments archive / xentax.7z / 8090 / ModelEdit.7z / ltwintreemgr.h < prev    next >
Encoding:
C/C++ Source or Header  |  2006-03-08  |  6.6 KB  |  215 lines

  1. ////////////////////////////////////////////////////////////////
  2. //
  3. // ltwintreemgr.h
  4. //
  5. // This is the tree manager, that handles the management of
  6. // the tree data structure as well as the management of the
  7. // windows tree control. 
  8. //
  9. // Author: John O'Rorke
  10. // Created: 7/21/00
  11. // Modification History:
  12. //
  13. ////////////////////////////////////////////////////////////////
  14. #ifndef __LTWINTREEMGR_H__
  15. #define __LTWINTREEMGR_H__
  16.  
  17. #include "stdafx.h"
  18.  
  19. #ifndef __LTWINTREEKEY_H__
  20. #    include "ltwintreekey.h"
  21. #endif
  22.  
  23. #ifndef __LTWINTREEICON_H__
  24. #    include "ltwintreeicon.h"
  25. #endif
  26.  
  27.  
  28. // messages to be sent to the parent window on certain events
  29. #define WM_LTTREE_SELCHANGED    (WM_USER+471)    // the selection has changed
  30. #define WM_LTTREE_EDITTEXT        (WM_USER+472)    // text has been successfully edited
  31.  
  32.  
  33. class CLTWinTreeItem;
  34. class CLTWinTreeItemIter;
  35.  
  36.  
  37. class CLTWinTreeMgr :
  38.     public CTreeCtrl
  39. {
  40. public:
  41.  
  42.     //the callback prototype used for visiting each node
  43.     typedef BOOL (*VisitCallback)(CLTWinTreeItem*, CLTWinTreeMgr*, DWORD);
  44.  
  45.     //callback that can be registered for when the selection changes
  46.     typedef void (*ChangeSelCallback)(CLTWinTreeItem*, CLTWinTreeMgr*, DWORD);
  47.  
  48.  
  49.     //default constructor
  50.     CLTWinTreeMgr();
  51.  
  52.     //default destructor
  53.     virtual ~CLTWinTreeMgr();
  54.  
  55.     //adds a newly created item to the tree after belonging to the specified parent
  56.     // If the parent is NULLITEM it is added as a sibling to the root. It will fail
  57.     // if it cannot find the appropriate parent. This object is then orphaned, and
  58.     // it is the tree manager's responsibility to release the memory. Due to this
  59.     // all the items MUST be created on the heap using new. The return value is the
  60.     // key for the new item, which uniquely identifies it. Keys CANNOT be
  61.     // transferred between trees
  62.     virtual CLTWinTreeKey    AddItem(CLTWinTreeItem* pNewItem, 
  63.                                     CLTWinTreeKey Parent = NULLITEM,
  64.                                     BOOL bSortChildren = TRUE);
  65.  
  66.     //deletes a specified item from the tree as well as its children. Returns
  67.     //true if it successfully found and deleted the item, false otherwise
  68.     virtual BOOL            DeleteItem(CLTWinTreeKey Item);
  69.  
  70.     //creates an iterator that begins iterating through the root
  71.     CLTWinTreeItemIter*        CreateIter();
  72.  
  73.     //sets the text of an item given a key
  74.     virtual BOOL            SetItemText(CLTWinTreeKey Item, const char* pszText);
  75.  
  76.     //sets the icon of an item given a key
  77.     BOOL                    SetItemIcon(CLTWinTreeKey Item, CLTWinTreeIcon Icon);
  78.  
  79.     //converts a key to an item
  80.     CLTWinTreeItem*    KeyToItem(CLTWinTreeKey Key);
  81.  
  82.     //converts an item to a key
  83.     CLTWinTreeKey    ItemToKey(CLTWinTreeItem* pItem);
  84.  
  85.     //this will visit each node in turn, and call the passed calback
  86.     //in once per item. It will continue iteration as long as the
  87.     //function returns true and there are items left to be iterated.
  88.     //it performs a depth first search to get each item. In addition,
  89.     //the user data will be passed to the function each time it is
  90.     //called. It will begin at the passed in node and visit all of the
  91.     //children of that node. If it is null, it will start at the root
  92.     void            VisitEachItem(    VisitCallback CallbackFunc, 
  93.                                     DWORD UserData,
  94.                                     CLTWinTreeKey Start = NULLITEM);
  95.  
  96.     //adds an icon to the available list, and returns the index that
  97.     //will refer to the icon
  98.     CLTWinTreeIcon    AddIcon(LPCTSTR pszIcon);
  99.  
  100.     //deletes the entire tree
  101.     virtual void    DeleteTree();
  102.  
  103.     //clears the selection, setting all item's flags for set to false
  104.     void ClearSelection();
  105.  
  106.     //expand all items in the tree
  107.     void ExpandAll();
  108.  
  109.     // collapse all items in the tree
  110.     void CollapseAll();
  111.  
  112.     //enables the dragging and dropping of items
  113.     void    EnableDragDrop(BOOL bEnable = TRUE);
  114.  
  115.     //enables the editing of text in the control
  116.     void    EnableEditText(BOOL bEnable = TRUE);
  117.  
  118.     //enables multiple selection of nodes
  119.     void    EnableMultiSelect(BOOL bEnable = TRUE);
  120.  
  121.     //registers a callback for when selections change
  122.     void    RegisterCallback(ChangeSelCallback Callback, DWORD nUserData);
  123.  
  124.     //unregisters the associated callback
  125.     void    UnregisterCallback();
  126.  
  127. private:
  128.  
  129.     //NOTIFY CALLBACKS
  130.     afx_msg void OnBeginEditText(NMHDR*, LRESULT*);
  131.     afx_msg void OnEndEditText(NMHDR*, LRESULT*);
  132.     afx_msg void OnBeginDrag(NMHDR*, LRESULT*);
  133.     afx_msg void OnSelChanged(NMHDR*, LRESULT*);
  134.  
  135.     //WINDOWS MESSAGE FOR DRAGGING OPERATIONS
  136.     afx_msg void OnMouseMove(UINT nFlags, CPoint pt);
  137.     afx_msg void OnLButtonUp(UINT nFlags, CPoint pt);
  138.     afx_msg void OnLButtonDown(UINT nFlags, CPoint pt);
  139.  
  140.     //unlinks the item from all surrounding siblings
  141.     void    UnlinkFromSiblings(CLTWinTreeItem* pItem);
  142.  
  143.     //handles the deletion of a tree, it starts on the specified node,
  144.     // and recursively deletes all children of that node
  145.     void    RecurseDeleteChildren(CLTWinTreeItem* pItem);
  146.  
  147.     //returns a pointer to a valid drop target if one exists, otherwise
  148.     //NULL
  149.     CLTWinTreeItem*    GetDropTarget(CPoint pt);
  150.  
  151.     //recursively builds a dragged tree as a parent to the passed in
  152.     //item.
  153.     void RebuildDraggedTree(CLTWinTreeItem* pItem, CLTWinTreeItem* pParent);
  154.  
  155.     //selects a specified node in multiple selection mode
  156.     void MultiSelectItem(HTREEITEM hItem, BOOL bSelect);
  157.  
  158.     //callback function to visit each node and clear the selection flag
  159.     static BOOL ClearSelectCallback(CLTWinTreeItem*, CLTWinTreeMgr*, DWORD);
  160.  
  161.     //calback function to visit each node and expand or collapse it
  162.     static BOOL ExpandCollapseCallback(CLTWinTreeItem* item, CLTWinTreeMgr* mgr, DWORD collapse);
  163.  
  164.     //callback function to visit each node and update their drawing method
  165.     static BOOL UpdateItemCallback(CLTWinTreeItem*, CLTWinTreeMgr*, DWORD);
  166.  
  167.     //triggers the change selection callback
  168.     void    TriggerChangeSel(CLTWinTreeItem* pItem);
  169.  
  170.     //determines if an item is currently being dragged
  171.     BOOL    IsInDrag() const;
  172.  
  173.  
  174.     //do not allow copying of trees
  175.     CLTWinTreeMgr(const CLTWinTreeMgr&)    {}
  176.  
  177.     //handle to the first selected node for multiple selection
  178.     CLTWinTreeItem*    m_pFirstSelected;
  179.  
  180.     //the root item
  181.     CLTWinTreeItem*    m_pRoot;
  182.  
  183.     //the image list for the tree control
  184.     CImageList        m_ImageList;
  185.  
  186.     //determines if dragging and dropping is supported
  187.     BOOL            m_bDragDrop;
  188.  
  189.     //determines if editing text is supported
  190.     BOOL            m_bEditText;
  191.  
  192.     //determine if we are currently in a drag mode
  193.     BOOL            m_bDragging;
  194.  
  195.     //determines if multiple nodes can be selected
  196.     BOOL            m_bMultiSelect;
  197.  
  198.     //the item that is currently being dragged
  199.     CLTWinTreeItem*    m_pDragItem;
  200.  
  201.     //image list for dragged item
  202.     CImageList*        m_pDragImage;
  203.  
  204.     //the callback to call whenever the selection changes
  205.     ChangeSelCallback    m_pfnChangeSel;
  206.  
  207.     //user data for the change selection callback
  208.     DWORD                m_nChangeSelUserData;
  209.  
  210.     DECLARE_MESSAGE_MAP();
  211.  
  212. };
  213.  
  214. #endif
  215.