VirtualTreeCtrl.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /* Copyright (c) 2002-2012 Croteam Ltd.
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of version 2 of the GNU General Public License as published by
  4. the Free Software Foundation
  5. This program is distributed in the hope that it will be useful,
  6. but WITHOUT ANY WARRANTY; without even the implied warranty of
  7. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8. GNU General Public License for more details.
  9. You should have received a copy of the GNU General Public License along
  10. with this program; if not, write to the Free Software Foundation, Inc.,
  11. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
  12. // VirtualTreeCtrl.cpp : implementation file
  13. //
  14. #include "stdafx.h"
  15. #include "WorldEditor.h"
  16. #include "VirtualTreeCtrl.h"
  17. #ifdef _DEBUG
  18. #undef new
  19. #define new DEBUG_NEW
  20. #undef THIS_FILE
  21. static char THIS_FILE[] = __FILE__;
  22. #endif
  23. /////////////////////////////////////////////////////////////////////////////
  24. // CVirtualTreeCtrl
  25. CVirtualTreeCtrl::CVirtualTreeCtrl()
  26. {
  27. m_bIsOpen = FALSE;
  28. }
  29. CVirtualTreeCtrl::~CVirtualTreeCtrl()
  30. {
  31. }
  32. void CVirtualTreeCtrl::SetBrowserPtr( CBrowser *pBrowser)
  33. {
  34. m_pBrowser = pBrowser;
  35. }
  36. BEGIN_MESSAGE_MAP(CVirtualTreeCtrl, CTreeCtrl)
  37. //{{AFX_MSG_MAP(CVirtualTreeCtrl)
  38. ON_WM_LBUTTONDBLCLK()
  39. ON_WM_KEYDOWN()
  40. ON_WM_LBUTTONDOWN()
  41. ON_WM_DROPFILES()
  42. //}}AFX_MSG_MAP
  43. END_MESSAGE_MAP()
  44. /////////////////////////////////////////////////////////////////////////////
  45. // CVirtualTreeCtrl message handlers
  46. void CVirtualTreeCtrl::CloseTreeCtrl(void)
  47. {
  48. if( m_bIsOpen)
  49. {
  50. m_pBrowser->m_TreeHeight = CLOSED_TREE;
  51. m_pBrowser->CalcDynamicLayout(0, LM_HORZDOCK);
  52. m_bIsOpen = FALSE;
  53. if( GetCount() != 0)
  54. {
  55. HTREEITEM pSelectedItem = GetSelectedItem();
  56. ASSERT( pSelectedItem != NULL);
  57. EnsureVisible( pSelectedItem);
  58. m_pBrowser->OpenSelectedDirectory();
  59. }
  60. }
  61. // Enable drag/drop open
  62. DragAcceptFiles();
  63. }
  64. void CVirtualTreeCtrl::OpenTreeCtrl(void)
  65. {
  66. if( !m_bIsOpen)
  67. {
  68. m_pBrowser->m_TreeHeight = OPEN_TREE;
  69. m_pBrowser->CalcDynamicLayout(0, LM_HORZDOCK);
  70. m_bIsOpen = TRUE;
  71. if( GetCount() != 0)
  72. {
  73. HTREEITEM pSelectedItem = GetSelectedItem();
  74. ASSERT( pSelectedItem != NULL);
  75. m_pBrowser->CloseSelectedDirectory();
  76. }
  77. }
  78. }
  79. void CVirtualTreeCtrl::OnLButtonDown(UINT nFlags, CPoint point)
  80. {
  81. CVirtualTreeNode *pVTN = ItemForCoordinate(point);
  82. // if is not null and not root
  83. if( pVTN!=NULL && pVTN->vnt_pvtnParent!=NULL)
  84. {
  85. CTString strAddr;
  86. strAddr.PrintF("VTN%d", pVTN);
  87. HGLOBAL hglobal = CreateHDrop( strAddr, FALSE);
  88. m_DataSource.CacheGlobalData( CF_HDROP, hglobal);
  89. m_DataSource.DoDragDrop( DROPEFFECT_COPY);
  90. }
  91. OpenTreeCtrl();
  92. CTreeCtrl::OnLButtonDown(nFlags, point);
  93. }
  94. CVirtualTreeNode *CVirtualTreeCtrl::ItemForCoordinate(CPoint pt)
  95. {
  96. UINT ulFlags;
  97. HTREEITEM pItem=HitTest(pt, &ulFlags);
  98. if( pItem==NULL || !(ulFlags&TVHT_ONITEMICON)) return NULL;
  99. CVirtualTreeNode *pVTN = (CVirtualTreeNode *)GetItemData( pItem);
  100. return pVTN;
  101. }
  102. void CVirtualTreeCtrl::OnLButtonDblClk(UINT nFlags, CPoint point)
  103. {
  104. CloseTreeCtrl();
  105. CTreeCtrl::OnLButtonDblClk(nFlags, point);
  106. }
  107. void CVirtualTreeCtrl::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
  108. {
  109. UWORD uwScanCode = nFlags & 255;
  110. BOOL bAlt = (nFlags & (1L<<13)) != 0;
  111. /*
  112. if( uwScanCode == 82) // Insert
  113. {
  114. m_pBrowser->OnCreateDirectory();
  115. }
  116. else if( uwScanCode == 83) // Delete
  117. {
  118. m_pBrowser->OnDeleteDirectory();
  119. }
  120. else if( bAlt && (uwScanCode == 19)) // Alt-R - rename
  121. {
  122. m_pBrowser->OnRenameDirectory();
  123. }
  124. else if( bAlt && (uwScanCode == 38)) // Alt+O - open
  125. {
  126. m_pBrowser->OnLoadVirtualTree();
  127. }
  128. else if( bAlt && (uwScanCode == 31)) // Alt+S - save
  129. {
  130. m_pBrowser->OnSaveVirtualTree();
  131. }
  132. */
  133. CTreeCtrl::OnKeyDown(nChar, nRepCnt, nFlags);
  134. }
  135. void CVirtualTreeCtrl::OnContextMenu( CPoint point)
  136. {
  137. CMenu menu;
  138. if( menu.LoadMenu(IDR_VTREEPOPUP))
  139. {
  140. CMenu* pPopup = menu.GetSubMenu(0);
  141. pPopup->TrackPopupMenu(TPM_LEFTBUTTON | TPM_RIGHTBUTTON | TPM_LEFTALIGN,
  142. point.x, point.y, m_pBrowser);
  143. }
  144. }
  145. void CVirtualTreeCtrl::OnDropFiles(HDROP hDropInfo)
  146. {
  147. CMainFrame* pMainFrame = STATIC_DOWNCAST(CMainFrame, AfxGetMainWnd());
  148. INDEX ctFiles = DragQueryFile( hDropInfo, 0xFFFFFFFF, NULL, 0);
  149. // get dropped coordinates
  150. CPoint point;
  151. DragQueryPoint( hDropInfo, &point);
  152. CVirtualTreeNode *pVTNDst = ItemForCoordinate(point);
  153. if( pVTNDst!=NULL)
  154. {
  155. for( INDEX i=0; i<ctFiles; i++)
  156. {
  157. wchar_t chrFile[ 256];
  158. DragQueryFile( hDropInfo, i, chrFile, 256);
  159. CTString strAddr = CTString(CStringA(chrFile));
  160. if( strAddr != "")
  161. {
  162. CVirtualTreeNode *pVTNSrc;
  163. strAddr.ScanF("VTN%d", &pVTNSrc);
  164. if(pVTNSrc==pVTNDst) return;
  165. pVTNSrc->MoveToDirectory( pVTNDst);
  166. // delete all items
  167. DeleteAllItems();
  168. m_pBrowser->AddDirectoryRecursiv( &m_pBrowser->m_VirtualTree, TVI_ROOT); // Fill CTreeCtrl using recursion
  169. SortChildren( NULL);
  170. SelectItem( (HTREEITEM) pVTNSrc->vtn_Handle);
  171. m_pBrowser->m_bVirtualTreeChanged = TRUE;
  172. m_pBrowser->OpenSelectedDirectory();
  173. }
  174. }
  175. }
  176. CTreeCtrl::OnDropFiles(hDropInfo);
  177. }