home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / Basic / Visual Basic.60 / COMMON / TOOLS / VCM / VCM.MDB / VcmComponentContainer / 02_Cabinet / MTREECTL.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1998-05-18  |  5.2 KB  |  207 lines

  1. // MyTreeCtrl.cpp : implementation file
  2. //
  3.  
  4. // This is a part of the Microsoft Foundation Classes C++ library.
  5. // Copyright (C) 1992-1998 Microsoft Corporation
  6. // All rights reserved.
  7. //
  8. // This source code is only intended as a supplement to the
  9. // Microsoft Foundation Classes Reference and related
  10. // electronic documentation provided with the library.
  11. // See these sources for detailed information regarding the
  12. // Microsoft Foundation Classes product.
  13.  
  14. #include "stdafx.h"
  15. #include "CmnCtrl1.h"
  16. #include "mtreectl.h"
  17. #include "treecpg.h"
  18.  
  19. #ifdef _DEBUG
  20. #define new DEBUG_NEW
  21. #undef THIS_FILE
  22. static char THIS_FILE[] = __FILE__;
  23. #endif
  24.  
  25. /////////////////////////////////////////////////////////////////////////////
  26. // CMyTreeCtrl
  27.  
  28. CMyTreeCtrl::CMyTreeCtrl()
  29. {
  30.     m_bDragging = FALSE;
  31.     m_pimagelist = NULL;
  32. }
  33.  
  34. CMyTreeCtrl::~CMyTreeCtrl()
  35. {
  36. }
  37.  
  38.  
  39. BEGIN_MESSAGE_MAP(CMyTreeCtrl, CTreeCtrl)
  40.     //{{AFX_MSG_MAP(CMyTreeCtrl)
  41.     ON_NOTIFY_REFLECT(TVN_ENDLABELEDIT, OnEndLabelEdit)
  42.     ON_NOTIFY_REFLECT(TVN_BEGINDRAG, OnBeginDrag)
  43.     ON_NOTIFY_REFLECT(TVN_BEGINRDRAG, OnBeginDrag)
  44.     ON_WM_MOUSEMOVE()
  45.     ON_WM_DESTROY()
  46.     ON_WM_LBUTTONUP()
  47.     ON_WM_RBUTTONUP()
  48.     //}}AFX_MSG_MAP
  49. END_MESSAGE_MAP()
  50.  
  51. /////////////////////////////////////////////////////////////////////////////
  52. // CMyTreeCtrl message handlers
  53. void CMyTreeCtrl::OnDestroy()
  54. {
  55.     CImageList  *pimagelist;
  56.  
  57.     pimagelist = GetImageList(TVSIL_NORMAL);
  58.     pimagelist->DeleteImageList();
  59.     delete pimagelist;
  60. }
  61.  
  62. void CMyTreeCtrl::SetNewStyle(long lStyleMask, BOOL bSetBits)
  63. {
  64.     long        lStyleOld;
  65.  
  66.     lStyleOld = GetWindowLong(m_hWnd, GWL_STYLE);
  67.     lStyleOld &= ~lStyleMask;
  68.     if (bSetBits)
  69.         lStyleOld |= lStyleMask;
  70.  
  71.     SetWindowLong(m_hWnd, GWL_STYLE, lStyleOld);
  72.     SetWindowPos(NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER);
  73. }
  74.  
  75. void CMyTreeCtrl::OnEndLabelEdit(LPNMHDR pnmhdr, LRESULT *pLResult)
  76. {
  77.     TV_DISPINFO     *ptvinfo;
  78.  
  79.     ((CTreeCtrlPage *)GetParent())->ShowNotification(pnmhdr, pLResult);
  80.     ptvinfo = (TV_DISPINFO *)pnmhdr;
  81.     if (ptvinfo->item.pszText != NULL)
  82.     {
  83.         ptvinfo->item.mask = TVIF_TEXT;
  84.         SetItem(&ptvinfo->item);
  85.     }
  86.     *pLResult = TRUE;
  87. }
  88.  
  89. void CMyTreeCtrl::OnMouseMove(UINT nFlags, CPoint point)
  90. {
  91.     HTREEITEM           hitem;
  92.     UINT                flags;
  93.  
  94.     if (m_bDragging)
  95.     {
  96.         ASSERT(m_pimagelist != NULL);
  97.         m_pimagelist->DragMove(point);
  98.         if ((hitem = HitTest(point, &flags)) != NULL)
  99.         {
  100.             m_pimagelist->DragLeave(this);
  101.             SelectDropTarget(hitem);
  102.             m_hitemDrop = hitem;
  103.             m_pimagelist->DragEnter(this, point);
  104.         }
  105.     }
  106.  
  107.     CTreeCtrl::OnMouseMove(nFlags, point);
  108. }
  109.  
  110. BOOL CMyTreeCtrl::IsChildNodeOf(HTREEITEM hitemChild, HTREEITEM hitemSuspectedParent)
  111. {
  112.     do
  113.     {
  114.         if (hitemChild == hitemSuspectedParent)
  115.             break;
  116.     }
  117.     while ((hitemChild = GetParentItem(hitemChild)) != NULL);
  118.  
  119.     return (hitemChild != NULL);
  120. }
  121.  
  122.  
  123. BOOL CMyTreeCtrl::TransferItem(HTREEITEM hitemDrag, HTREEITEM hitemDrop)
  124. {
  125.     TV_INSERTSTRUCT     tvstruct;
  126.     TCHAR               sztBuffer[50];
  127.     HTREEITEM           hNewItem, hFirstChild;
  128.  
  129.         // avoid an infinite recursion situation
  130.     tvstruct.item.hItem = hitemDrag;
  131.     tvstruct.item.cchTextMax = 49;
  132.     tvstruct.item.pszText = sztBuffer;
  133.     tvstruct.item.mask = TVIF_CHILDREN | TVIF_HANDLE | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_TEXT;
  134.     GetItem(&tvstruct.item);  // get information of the dragged element
  135.     tvstruct.hParent = hitemDrop;
  136.     tvstruct.hInsertAfter = TVI_SORT;
  137.     tvstruct.item.mask = TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_TEXT;
  138.     hNewItem = InsertItem(&tvstruct);
  139.  
  140.     while ((hFirstChild = GetChildItem(hitemDrag)) != NULL)
  141.     {
  142.         TransferItem(hFirstChild, hNewItem);  // recursively transfer all the items
  143.         DeleteItem(hFirstChild);        // delete the first child and all its children
  144.     }
  145.     return TRUE;
  146. }
  147.  
  148. void CMyTreeCtrl::OnButtonUp()
  149. {
  150.     if (m_bDragging)
  151.     {
  152.         ASSERT(m_pimagelist != NULL);
  153.         m_pimagelist->DragLeave(this);
  154.         m_pimagelist->EndDrag();
  155.         delete m_pimagelist;
  156.         m_pimagelist = NULL;
  157.  
  158.         if (m_hitemDrag != m_hitemDrop && !IsChildNodeOf(m_hitemDrop, m_hitemDrag) &&
  159.                                                             GetParentItem(m_hitemDrag) != m_hitemDrop)
  160.         {
  161.             TransferItem(m_hitemDrag, m_hitemDrop);
  162.             DeleteItem(m_hitemDrag);
  163.         }
  164.         else
  165.             MessageBeep(0);
  166.  
  167.         ReleaseCapture();
  168.         m_bDragging = FALSE;
  169.         SelectDropTarget(NULL);
  170.     }
  171. }
  172.  
  173. void CMyTreeCtrl::OnLButtonUp(UINT nFlags, CPoint point)
  174. {
  175.     OnButtonUp();
  176.     CTreeCtrl::OnLButtonUp(nFlags, point);
  177. }
  178.  
  179. void CMyTreeCtrl::OnRButtonUp(UINT nFlags, CPoint point)
  180. {
  181.     OnButtonUp();
  182.     CTreeCtrl::OnRButtonUp(nFlags, point);
  183. }
  184.  
  185. void CMyTreeCtrl::OnBeginDrag(LPNMHDR pnmhdr, LRESULT *pLResult)
  186. {
  187.     CPoint      ptAction;
  188.     UINT        nFlags;
  189.  
  190.     GetCursorPos(&ptAction);
  191.     ScreenToClient(&ptAction);
  192.     ((CTreeCtrlPage *)GetParent())->ShowNotification(pnmhdr, pLResult);
  193.     ASSERT(!m_bDragging);
  194.     m_bDragging = TRUE;
  195.     m_hitemDrag = HitTest(ptAction, &nFlags);
  196.     m_hitemDrop = NULL;
  197.  
  198.     ASSERT(m_pimagelist == NULL);
  199.     m_pimagelist = CreateDragImage(m_hitemDrag);  // get the image list for dragging
  200.     m_pimagelist->DragShowNolock(TRUE);
  201.     m_pimagelist->SetDragCursorImage(0, CPoint(0, 0));
  202.     m_pimagelist->BeginDrag(0, CPoint(0,0));
  203.     m_pimagelist->DragMove(ptAction);
  204.     m_pimagelist->DragEnter(this, ptAction);
  205.     SetCapture();
  206. }
  207.