home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / BC_502 / CMNCTRL.PAK / MTREECTL.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  5.0 KB  |  200 lines

  1. // MyTreeCtrl.cpp : implementation file
  2. //
  3.  
  4. // This is a part of the Microsoft Foundation Classes C++ library.
  5. // Copyright (C) 1992-1995 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 "ctrldemo.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.         m_pimagelist->DragMove(point);
  97.         if ((hitem = HitTest(point, &flags)) != NULL)
  98.         {
  99.             m_pimagelist->DragLeave(this);
  100.             SelectDropTarget(hitem);
  101.             m_hitemDrop = hitem;
  102.             m_pimagelist->DragEnter(this, point);
  103.         }
  104.     }
  105.  
  106.     CTreeCtrl::OnMouseMove(nFlags, point);
  107. }
  108.  
  109. BOOL CMyTreeCtrl::IsChildNodeOf(HTREEITEM hitemChild, HTREEITEM hitemSuspectedParent)
  110. {
  111.     do
  112.     {
  113.         if (hitemChild == hitemSuspectedParent)
  114.             break;
  115.     }
  116.     while ((hitemChild = GetParentItem(hitemChild)) != NULL);
  117.  
  118.     return (hitemChild != NULL);
  119. }
  120.  
  121.  
  122. BOOL CMyTreeCtrl::TransferItem(HTREEITEM hitemDrag, HTREEITEM hitemDrop)
  123. {
  124.     TV_INSERTSTRUCT        tvstruct;
  125.     TCHAR                sztBuffer[50];
  126.     HTREEITEM            hNewItem, hFirstChild;
  127.  
  128.         // avoid an infinite recursion situation
  129.     tvstruct.item.hItem = hitemDrag;
  130.     tvstruct.item.cchTextMax = 49;
  131.     tvstruct.item.pszText = sztBuffer;
  132.     tvstruct.item.mask = TVIF_CHILDREN | TVIF_HANDLE | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_TEXT;
  133.     GetItem(&tvstruct.item);  // get information of the dragged element
  134.     tvstruct.hParent = hitemDrop;
  135.     tvstruct.hInsertAfter = TVI_SORT;
  136.     tvstruct.item.mask = TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_TEXT;
  137.     hNewItem = InsertItem(&tvstruct);
  138.  
  139.     while ((hFirstChild = GetChildItem(hitemDrag)) != NULL)
  140.     {
  141.         TransferItem(hFirstChild, hNewItem);  // recursively transfer all the items
  142.         DeleteItem(hFirstChild);        // delete the first child and all its children
  143.     }
  144.     return TRUE;
  145. }
  146.  
  147. void CMyTreeCtrl::OnButtonUp()
  148. {
  149.     if (m_bDragging)
  150.     {
  151.         m_pimagelist->DragLeave(this);
  152.         m_pimagelist->EndDrag();
  153.         if (m_hitemDrag != m_hitemDrop && !IsChildNodeOf(m_hitemDrop, m_hitemDrag) && 
  154.                                                             GetParentItem(m_hitemDrag) != m_hitemDrop)
  155.         {
  156.             TransferItem(m_hitemDrag, m_hitemDrop);
  157.             DeleteItem(m_hitemDrag);
  158.         }
  159.         else
  160.             MessageBeep(0);
  161.  
  162.         ReleaseCapture();
  163.         m_bDragging = FALSE;
  164.         SelectDropTarget(NULL);
  165.     }
  166. }
  167.  
  168. void CMyTreeCtrl::OnLButtonUp(UINT nFlags, CPoint point)
  169. {
  170.     OnButtonUp();
  171.     CTreeCtrl::OnLButtonUp(nFlags, point);
  172. }
  173.  
  174. void CMyTreeCtrl::OnRButtonUp(UINT nFlags, CPoint point)
  175. {
  176.     OnButtonUp();
  177.     CTreeCtrl::OnRButtonUp(nFlags, point);
  178. }
  179.  
  180. void CMyTreeCtrl::OnBeginDrag(LPNMHDR pnmhdr, LRESULT *pLResult)
  181. {
  182.     CPoint        ptAction;
  183.     UINT        nFlags;
  184.  
  185.     GetCursorPos(&ptAction);
  186.     ScreenToClient(&ptAction);
  187.     ((CTreeCtrlPage *)GetParent())->ShowNotification(pnmhdr, pLResult);
  188.     ASSERT(!m_bDragging);
  189.     m_bDragging = TRUE;
  190.     m_hitemDrag = HitTest(ptAction, &nFlags);
  191.     m_hitemDrop = NULL;
  192.     m_pimagelist = CreateDragImage(m_hitemDrag);  // get the image list for dragging
  193.     m_pimagelist->DragShowNolock(TRUE);
  194.     m_pimagelist->SetDragCursorImage(0, CPoint(0, 0));
  195.     m_pimagelist->BeginDrag(0, CPoint(0,0));
  196.     m_pimagelist->DragMove(ptAction);
  197.     m_pimagelist->DragEnter(this, ptAction);
  198.     SetCapture();
  199. }
  200.