Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

581 lines
14 KiB

  1. // BrowseView.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "viewex.h"
  5. #include "bwsview.h"
  6. #include "schclss.h"
  7. #ifdef _DEBUG
  8. #define new DEBUG_NEW
  9. #undef THIS_FILE
  10. static char THIS_FILE[] = __FILE__;
  11. #endif
  12. #define BUFF_SIZE 0xFFFFL
  13. //#define BUFF_SIZE 0x1000L
  14. /////////////////////////////////////////////////////////////////////////////
  15. // CBrowseView
  16. IMPLEMENT_DYNCREATE(CBrowseView, CTreeView)
  17. /***********************************************************
  18. Function:
  19. Arguments:
  20. Return:
  21. Purpose:
  22. Author(s):
  23. Revision:
  24. Date:
  25. ***********************************************************/
  26. CBrowseView::CBrowseView()
  27. {
  28. BOOL bOK;
  29. DWORD dwObjectType;
  30. CBitmap* pBitmap;
  31. UINT imageID;
  32. m_pImageList = new CImageList( );
  33. if( NULL != m_pImageList )
  34. {
  35. bOK = m_pImageList->Create( 18, 18, FALSE, 20, 20 );
  36. if( bOK )
  37. {
  38. for( dwObjectType = FIRST; dwObjectType < LIMIT ; dwObjectType++)
  39. {
  40. pBitmap = new CBitmap;
  41. imageID = GetBitmapImageId ( dwObjectType );
  42. pBitmap->LoadBitmap( imageID );
  43. m_pImageList->Add( pBitmap, (COLORREF)0L );
  44. pBitmap->DeleteObject( );
  45. delete pBitmap;
  46. }
  47. }
  48. }
  49. }
  50. /***********************************************************
  51. Function:
  52. Arguments:
  53. Return:
  54. Purpose:
  55. Author(s):
  56. Revision:
  57. Date:
  58. ***********************************************************/
  59. void CBrowseView::OnInitialUpdate()
  60. {
  61. HTREEITEM hItem;
  62. CMainDoc* pDoc;
  63. DWORD dwStyle;
  64. BOOL bRez;
  65. COleDsObject* pObject;
  66. m_bDoNotUpdate = TRUE;
  67. pDoc = (CMainDoc*) GetDocument( );
  68. dwStyle = TVS_HASLINES | TVS_LINESATROOT | TVS_HASBUTTONS;
  69. bRez = GetTreeCtrl( ).ModifyStyle( 0L, dwStyle );
  70. GetTreeCtrl( ).SetImageList( m_pImageList, TVSIL_NORMAL );
  71. GetTreeCtrl( ).DeleteAllItems( );
  72. GetTreeCtrl( ).SetIndent( 20 );
  73. pObject = pDoc->GetCurrentObject( );
  74. hItem = GetTreeCtrl( ).InsertItem( pObject->GetItemName( ) );
  75. GetTreeCtrl( ).SetItemData( hItem, pDoc->GetToken( &pObject ) );
  76. GetTreeCtrl( ).SetItemImage( hItem, pObject->GetType( ), pObject->GetType( ) );
  77. m_bDoNotUpdate = FALSE;
  78. CTreeView::OnInitialUpdate( );
  79. }
  80. /***********************************************************
  81. Function:
  82. Arguments:
  83. Return:
  84. Purpose:
  85. Author(s):
  86. Revision:
  87. Date:
  88. ***********************************************************/
  89. CBrowseView::~CBrowseView()
  90. {
  91. delete m_pImageList;
  92. }
  93. BEGIN_MESSAGE_MAP(CBrowseView, CTreeView)
  94. //{{AFX_MSG_MAP(CBrowseView)
  95. ON_NOTIFY_REFLECT(TVN_SELCHANGED, OnSelChanged)
  96. ON_NOTIFY_REFLECT(TVN_ITEMEXPANDED, OnItemExpanded)
  97. ON_COMMAND(IDM_ADD, OnAddItem)
  98. ON_COMMAND(IDM_DELETE, OnDeleteItem)
  99. ON_COMMAND(IDM_MOVEITEM, OnMoveItem)
  100. ON_COMMAND(IDM_COPYITEM, OnCopyItem)
  101. ON_COMMAND(IDM_REFRESH, OnRefresh)
  102. //}}AFX_MSG_MAP
  103. END_MESSAGE_MAP()
  104. CMainDoc* sgpDoc;
  105. int __cdecl QSortCompare( const void* pVal1, const void* pVal2 )
  106. {
  107. COleDsObject* pObject1;
  108. COleDsObject* pObject2;
  109. CString* pString1;
  110. CString* pString2;
  111. int nDiff;
  112. pObject1 = sgpDoc->GetObject( (void*)pVal1 );
  113. pObject2 = sgpDoc->GetObject( (void*)pVal2 );
  114. nDiff = pObject1->GetType( ) - pObject2->GetType( );
  115. if( nDiff )
  116. return nDiff;
  117. pString1 = pObject1->PtrGetItemName( );
  118. pString2 = pObject2->PtrGetItemName( );
  119. return pString1->Compare( (LPCTSTR)( pString2->GetBuffer( 128 ) ) );
  120. }
  121. /***********************************************************
  122. Function:
  123. Arguments:
  124. Return:
  125. Purpose:
  126. Author(s):
  127. Revision:
  128. Date:
  129. ***********************************************************/
  130. void CBrowseView::SortChildItemList( DWORD* pChildTokens, DWORD dwCount )
  131. {
  132. sgpDoc = (CMainDoc*)GetDocument( );
  133. qsort( (void*)pChildTokens, dwCount, sizeof(DWORD), QSortCompare );
  134. }
  135. /////////////////////////////////////////////////////////////////////////////
  136. // CBrowseView diagnostics
  137. #ifdef _DEBUG
  138. /***********************************************************
  139. Function:
  140. Arguments:
  141. Return:
  142. Purpose:
  143. Author(s):
  144. Revision:
  145. Date:
  146. ***********************************************************/
  147. void CBrowseView::AssertValid() const
  148. {
  149. CTreeView::AssertValid();
  150. }
  151. /***********************************************************
  152. Function:
  153. Arguments:
  154. Return:
  155. Purpose:
  156. Author(s):
  157. Revision:
  158. Date:
  159. ***********************************************************/
  160. void CBrowseView::Dump(CDumpContext& dc) const
  161. {
  162. CTreeView::Dump(dc);
  163. }
  164. #endif //_DEBUG
  165. /////////////////////////////////////////////////////////////////////////////
  166. // CBrowseView message handlers
  167. /***********************************************************
  168. Function:
  169. Arguments:
  170. Return:
  171. Purpose:
  172. Author(s):
  173. Revision:
  174. Date:
  175. ***********************************************************/
  176. void CBrowseView::OnSelChanged(NMHDR* pNMHDR, LRESULT* pResult)
  177. {
  178. NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
  179. // TODO: Add your control notification handler code here
  180. HTREEITEM hTreeItem, hChildItem;;
  181. CString strItemName;
  182. CMainDoc* pDoc;
  183. DWORD dwIter;
  184. DWORD dwToken;
  185. DWORD* pTokens = NULL;
  186. DWORD dwNumItems = 0L;
  187. *pResult = 0;
  188. if( m_bDoNotUpdate )
  189. {
  190. return;
  191. }
  192. hTreeItem = GetTreeCtrl( ).GetSelectedItem( );
  193. hChildItem = TVI_LAST;
  194. if( NULL != hTreeItem )
  195. {
  196. dwToken = (DWORD)GetTreeCtrl( ).GetItemData( hTreeItem );
  197. pDoc = (CMainDoc*) GetDocument( );
  198. ASSERT( NULL != pDoc );
  199. if( NULL != pDoc )
  200. {
  201. HCURSOR oldCursor, newCursor;
  202. newCursor = LoadCursor( NULL, IDC_WAIT );
  203. oldCursor = SetCursor( newCursor );
  204. // the item has children support ???
  205. pTokens = (DWORD*) malloc( sizeof(DWORD) * BUFF_SIZE );
  206. if( !GetTreeCtrl( ).ItemHasChildren( hTreeItem ) )
  207. {
  208. dwNumItems = pDoc->GetChildItemList( dwToken, pTokens, BUFF_SIZE );
  209. if( dwNumItems )
  210. {
  211. // siaply children items
  212. SortChildItemList( pTokens, dwNumItems );
  213. for( dwIter = 0; dwIter < dwNumItems ; dwIter++ )
  214. {
  215. COleDsObject* pObject;
  216. CString* pName;
  217. DWORD dwType;
  218. TCHAR szName[ 256 ];
  219. pObject = pDoc->GetObject( &pTokens[ dwIter ] );
  220. dwType = pObject->GetType( );
  221. pName = pObject->PtrGetItemName( );
  222. _ultot( dwIter + 1, szName, 10 );
  223. _tcscat( szName, _T(") ") );
  224. _tcscat( szName, pName->GetBuffer( 128 ) );
  225. /*hChildItem = GetTreeCtrl( ).InsertItem( pName->GetBuffer( 128 ),
  226. hTreeItem, hChildItem ); */
  227. hChildItem = GetTreeCtrl( ).InsertItem( szName,
  228. hTreeItem,
  229. hChildItem );
  230. GetTreeCtrl( ).SetItemData( hChildItem, pTokens[ dwIter ] );
  231. GetTreeCtrl( ).SetItemImage( hChildItem, dwType, dwType );
  232. }
  233. }
  234. }
  235. if( NULL != pTokens )
  236. {
  237. free((void*)pTokens);
  238. }
  239. pDoc->SetCurrentItem( dwToken );
  240. SetCursor( oldCursor );
  241. }
  242. }
  243. }
  244. /***********************************************************
  245. Function:
  246. Arguments:
  247. Return:
  248. Purpose:
  249. Author(s):
  250. Revision:
  251. Date:
  252. ***********************************************************/
  253. void CBrowseView::OnItemExpanded(NMHDR* pNMHDR, LRESULT* pResult)
  254. {
  255. NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
  256. HTREEITEM hItem;
  257. // TODO: Add your control notification handler code here
  258. hItem = pNMTreeView->itemNew.hItem;
  259. if( !( pNMTreeView->itemNew.state & TVIS_EXPANDED ) )
  260. {
  261. // this should mean that the item was unexpanded.
  262. // We should delete its children.
  263. /*hChildItem = GetTreeCtrl( ).GetChildItem( hItem );
  264. while( NULL != hChildItem )
  265. {
  266. bRez = GetTreeCtrl( ).DeleteItem( hChildItem );
  267. ASSERT( bRez );
  268. hChildItem = GetTreeCtrl( ).GetChildItem( hItem );
  269. } */
  270. }
  271. else
  272. {
  273. /*dwItemData = GetTreeCtrl( ).GetItemData( hItem );
  274. GetItemPath( hItem, strItemName );
  275. pDoc = (CMainDoc*) GetDocument( );
  276. ASSERT( NULL != pDoc );
  277. if( NULL != pDoc )
  278. {
  279. HCURSOR oldCursor, newCursor;
  280. newCursor = LoadCursor( NULL, IDC_WAIT );
  281. oldCursor = SetCursor( newCursor );
  282. // the item has children support ???
  283. lpItemsName = (LPWSTR) malloc( 0x40000L );
  284. lpItemsType = (LPDWORD) malloc( 0x20000L );
  285. bRez = pDoc->GetChildItemList( strItemName, dwItemData,
  286. lpItemsName, lpItemsType,
  287. &dwNumItems, 0x40000L );
  288. if( bRez )
  289. {
  290. // siaply children items
  291. lpName = lpItemsName;
  292. for( nIter = 0; nIter < dwNumItems ; nIter++ )
  293. {
  294. hChildItem = GetTreeCtrl( ).InsertItem( lpName, hItem );
  295. GetTreeCtrl( ).SetItemData( hChildItem, lpItemsType[ nIter ] );
  296. lpName = lpName + ( _tcslen( lpName ) + 1 );
  297. }
  298. }
  299. if( NULL != lpItemsName )
  300. {
  301. free( lpItemsName );
  302. }
  303. if( NULL != lpItemsType )
  304. {
  305. free( lpItemsType );
  306. }
  307. //pDoc->SetItemName( strItemName, dwItemData );
  308. SetCursor( oldCursor );
  309. } */
  310. }
  311. *pResult = 0;
  312. }
  313. /***********************************************************
  314. Function:
  315. Arguments:
  316. Return:
  317. Purpose:
  318. Author(s):
  319. Revision:
  320. Date:
  321. ***********************************************************/
  322. void CBrowseView::OnAddItem()
  323. {
  324. // TODO: Add your command handler code here
  325. CMainDoc* pDoc;
  326. COleDsObject* pObject;
  327. HRESULT hResult;
  328. pDoc = (CMainDoc*) GetDocument( );
  329. pObject = pDoc->GetCurrentObject( );
  330. if( NULL == pObject )
  331. return;
  332. if( pObject->AddItemSuported( ) )
  333. {
  334. pObject->CreateTheObject( );
  335. hResult = pObject->AddItem( );
  336. pObject->ReleaseIfNotTransient( );
  337. if( FAILED( hResult ) )
  338. {
  339. AfxMessageBox( OleDsGetErrorText( hResult ) );
  340. }
  341. else
  342. {
  343. pDoc->DeleteAllItems( );
  344. OnInitialUpdate( );
  345. }
  346. }
  347. }
  348. /***********************************************************
  349. Function:
  350. Arguments:
  351. Return:
  352. Purpose:
  353. Author(s):
  354. Revision:
  355. Date:
  356. ***********************************************************/
  357. void CBrowseView::OnDeleteItem()
  358. {
  359. // TODO: Add your command handler code here
  360. CMainDoc* pDoc;
  361. COleDsObject* pObject;
  362. HRESULT hResult;
  363. pDoc = (CMainDoc*) GetDocument( );
  364. pObject = pDoc->GetCurrentObject( );
  365. if( NULL == pObject )
  366. return;
  367. if( pObject->DeleteItemSuported( ) )
  368. {
  369. pObject->CreateTheObject( );
  370. hResult = pObject->DeleteItem( );
  371. pObject->ReleaseIfNotTransient( );
  372. if( FAILED( hResult ) )
  373. {
  374. AfxMessageBox( OleDsGetErrorText( hResult ) );
  375. }
  376. else
  377. {
  378. pDoc->DeleteAllItems( );
  379. OnInitialUpdate( );
  380. }
  381. }
  382. }
  383. /***********************************************************
  384. Function:
  385. Arguments:
  386. Return:
  387. Purpose:
  388. Author(s):
  389. Revision:
  390. Date:
  391. ***********************************************************/
  392. void CBrowseView::OnMoveItem()
  393. {
  394. // TODO: Add your command handler code here
  395. CMainDoc* pDoc;
  396. COleDsObject* pObject;
  397. HRESULT hResult;
  398. pDoc = (CMainDoc*) GetDocument( );
  399. pObject = pDoc->GetCurrentObject( );
  400. if( NULL == pObject )
  401. return;
  402. if( pObject->MoveItemSupported( ) )
  403. {
  404. hResult = pObject->MoveItem( );
  405. if( FAILED( hResult ) )
  406. {
  407. AfxMessageBox( OleDsGetErrorText( hResult ) );
  408. }
  409. else
  410. {
  411. OnRefresh( );
  412. }
  413. }
  414. }
  415. /***********************************************************
  416. Function:
  417. Arguments:
  418. Return:
  419. Purpose:
  420. Author(s):
  421. Revision:
  422. Date:
  423. ***********************************************************/
  424. void CBrowseView::OnCopyItem()
  425. {
  426. // TODO: Add your command handler code here
  427. CMainDoc* pDoc;
  428. COleDsObject* pObject;
  429. HRESULT hResult;
  430. pDoc = (CMainDoc*) GetDocument( );
  431. pObject = pDoc->GetCurrentObject( );
  432. if( NULL == pObject )
  433. return;
  434. if( pObject->CopyItemSupported( ) )
  435. {
  436. hResult = pObject->CopyItem( );
  437. if( FAILED( hResult ) )
  438. {
  439. AfxMessageBox( OleDsGetErrorText( hResult ) );
  440. }
  441. else
  442. {
  443. OnRefresh( );
  444. }
  445. }
  446. }
  447. /***********************************************************
  448. Function:
  449. Arguments:
  450. Return:
  451. Purpose:
  452. Author(s):
  453. Revision:
  454. Date:
  455. ***********************************************************/
  456. void CBrowseView::OnRefresh()
  457. {
  458. // TODO: Add your command handler code here
  459. CMainDoc* pDoc;
  460. pDoc = (CMainDoc*)GetDocument( );
  461. pDoc->DeleteAllItems( );
  462. OnInitialUpdate( );
  463. }
  464. /***********************************************************
  465. Function:
  466. Arguments:
  467. Return:
  468. Purpose:
  469. Author(s):
  470. Revision:
  471. Date:
  472. ***********************************************************/
  473. void CBrowseView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint)
  474. {
  475. HTREEITEM hTreeItem;
  476. // TODO: Add your specialized code here and/or call the base class
  477. CTreeView::OnUpdate( pSender, lHint, pHint );
  478. hTreeItem = GetTreeCtrl( ).GetSelectedItem( );
  479. if( NULL == hTreeItem )
  480. {
  481. hTreeItem = GetTreeCtrl( ).GetRootItem( );
  482. GetTreeCtrl( ).SelectItem( hTreeItem );
  483. }
  484. }