Leaked source code of windows server 2003
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.

4059 lines
114 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1998 - 1999
  6. //
  7. // File: compbas_.cpp
  8. //
  9. //--------------------------------------------------------------------------
  10. #include <strsafe.h>
  11. // initialize to the thread ID of the thread that loads the snapin
  12. // that is the main thread
  13. extern DWORD _MainThreadId = ::GetCurrentThreadId();
  14. const TCHAR NODE_TYPES_KEY[] = TEXT("Software\\Microsoft\\MMC\\NodeTypes");
  15. const TCHAR SNAPINS_KEY[] = TEXT("Software\\Microsoft\\MMC\\SnapIns");
  16. const TCHAR g_szNodeType[] = TEXT("NodeType");
  17. const TCHAR g_szNameString[] = TEXT("NameString");
  18. const TCHAR g_szNameStringIndirect[] = TEXT("NameStringIndirect");
  19. const TCHAR g_szStandaloneSnap[] = TEXT("Standalone");
  20. const TCHAR g_szExtensionSnap[] = TEXT("Extension");
  21. const TCHAR g_szNodeTypes[] = TEXT("NodeTypes");
  22. const TCHAR g_szExtensions[] = TEXT("Extensions");
  23. const TCHAR g_szDynamicExtensions[] = TEXT("Dynamic Extensions");
  24. const TCHAR g_szVersion[] = TEXT("Version");
  25. const TCHAR g_szProvider[] = _T("Provider");
  26. const TCHAR g_szAbout[] = _T("About");
  27. const unsigned int MAX_KEY_PATH_LENGTH = 2047; // not including null
  28. const unsigned int MAX_GUID_LENGTH = 127; // not including null
  29. HRESULT RegisterSnapin(const GUID* pSnapinCLSID,
  30. const GUID* pStaticNodeGUID,
  31. const GUID* pAboutGUID,
  32. LPCTSTR lpszNameString, LPCTSTR lpszVersion, LPCTSTR lpszProvider,
  33. BOOL bExtension, _NODE_TYPE_INFO_ENTRY* pNodeTypeInfoEntryArray,
  34. UINT nSnapinNameID)
  35. {
  36. OLECHAR szSnapinClassID[MAX_GUID_LENGTH + 1] = {0},
  37. szStaticNodeGuid[MAX_GUID_LENGTH + 1] = {0},
  38. szAboutGuid[MAX_GUID_LENGTH + 1] = {0};
  39. int numWritten;
  40. numWritten = ::StringFromGUID2(
  41. *pSnapinCLSID,
  42. szSnapinClassID,
  43. MAX_GUID_LENGTH + 1);
  44. if (numWritten <= 0)
  45. {
  46. ASSERT(false);
  47. return E_FAIL;
  48. }
  49. numWritten = ::StringFromGUID2(
  50. *pStaticNodeGUID,
  51. szStaticNodeGuid,
  52. MAX_GUID_LENGTH + 1);
  53. if (numWritten <= 0)
  54. {
  55. ASSERT(false);
  56. return E_FAIL;
  57. }
  58. numWritten = ::StringFromGUID2(
  59. *pAboutGUID,
  60. szAboutGuid,
  61. MAX_GUID_LENGTH + 1);
  62. if (numWritten <= 0)
  63. {
  64. ASSERT(false);
  65. return E_FAIL;
  66. }
  67. CRegKey regkeySnapins;
  68. LONG lRes = regkeySnapins.Open(HKEY_LOCAL_MACHINE, SNAPINS_KEY);
  69. ASSERT(lRes == ERROR_SUCCESS);
  70. if (lRes != ERROR_SUCCESS)
  71. return HRESULT_FROM_WIN32(lRes); // failed to open
  72. CRegKey regkeyThisSnapin;
  73. lRes = regkeyThisSnapin.Create(regkeySnapins, szSnapinClassID);
  74. ASSERT(lRes == ERROR_SUCCESS);
  75. if (lRes != ERROR_SUCCESS)
  76. return HRESULT_FROM_WIN32(lRes); // failed to create
  77. lRes = regkeyThisSnapin.SetValue(lpszNameString, g_szNameString);
  78. if (lRes != ERROR_SUCCESS)
  79. return HRESULT_FROM_WIN32(lRes);
  80. // JeffJon 6/12/00 100624: MUI: MMC: Shared Folders snap-in
  81. // stores its display information in the registry
  82. if (nSnapinNameID != 0)
  83. {
  84. CString str;
  85. // NOTICE-2002/04/08-artm Beware file name truncation.
  86. // Code iteratively grows the buffer size until either the buffer
  87. // gets too big or the module file name is read without truncation.
  88. //
  89. // See NTRAID#NTBUG9-540042 for more information and
  90. // NTRAID#NTBUG9-616513.
  91. #ifdef DBG
  92. // on chk builds, use a small buffer size so that our growth algorithm
  93. // gets exercised
  94. unsigned bufSizeInCharacters = 1;
  95. #else
  96. unsigned bufSizeInCharacters = _MAX_PATH;
  97. #endif
  98. PWSTR szModule = 0;
  99. HRESULT hr = S_OK;
  100. do
  101. {
  102. // +1 for extra null-termination paranoia
  103. szModule = new WCHAR[bufSizeInCharacters + 1];
  104. if (!szModule)
  105. {
  106. hr = E_OUTOFMEMORY;
  107. break;
  108. }
  109. ::ZeroMemory(szModule, (bufSizeInCharacters + 1) * sizeof WCHAR);
  110. // Pass the buffer size, in characters.
  111. DWORD result =
  112. ::GetModuleFileName(AfxGetInstanceHandle(), szModule, bufSizeInCharacters);
  113. if (!result)
  114. {
  115. DWORD err = GetLastError();
  116. hr = HRESULT_FROM_WIN32(err);
  117. ASSERT(FAILED(hr));
  118. break;
  119. }
  120. if (result == bufSizeInCharacters)
  121. {
  122. // buffer was too small, so the value was truncated. Resize the
  123. // buffer and try again.
  124. delete[] szModule;
  125. bufSizeInCharacters *= 2;
  126. if (bufSizeInCharacters > USHRT_MAX) // effectively ~32K max
  127. {
  128. // too big. way too big. Bail out.
  129. ASSERT(false);
  130. hr = E_FAIL;
  131. break;
  132. }
  133. continue;
  134. }
  135. // We should always have a null terminated string.
  136. ASSERT(szModule[result] == 0);
  137. break;
  138. }
  139. while (true);
  140. if (SUCCEEDED(hr))
  141. {
  142. str.Format(_T("@%s,-%d"), szModule, nSnapinNameID);
  143. }
  144. delete [] szModule;
  145. szModule = NULL;
  146. if (FAILED(hr))
  147. {
  148. return hr;
  149. }
  150. lRes = regkeyThisSnapin.SetValue(str, g_szNameStringIndirect);
  151. if (lRes != ERROR_SUCCESS)
  152. return HRESULT_FROM_WIN32(lRes);
  153. }
  154. lRes = regkeyThisSnapin.SetValue(szAboutGuid, g_szAbout);
  155. if (lRes != ERROR_SUCCESS)
  156. return HRESULT_FROM_WIN32(lRes);
  157. lRes = regkeyThisSnapin.SetValue(szStaticNodeGuid, g_szNodeType);
  158. if (lRes != ERROR_SUCCESS)
  159. return HRESULT_FROM_WIN32(lRes);
  160. lRes = regkeyThisSnapin.SetValue(lpszProvider, g_szProvider);
  161. if (lRes != ERROR_SUCCESS)
  162. return HRESULT_FROM_WIN32(lRes);
  163. lRes = regkeyThisSnapin.SetValue(lpszVersion, g_szVersion);
  164. if (lRes != ERROR_SUCCESS)
  165. return HRESULT_FROM_WIN32(lRes);
  166. CRegKey regKeyStandaloneorExtension;
  167. lRes = regKeyStandaloneorExtension.Create(regkeyThisSnapin,
  168. bExtension ? g_szExtensionSnap : g_szStandaloneSnap);
  169. if (lRes != ERROR_SUCCESS)
  170. return HRESULT_FROM_WIN32(lRes);
  171. CRegKey regKeyNodeTypes;
  172. lRes = regKeyNodeTypes.Create(regkeyThisSnapin, g_szNodeTypes);
  173. if (lRes != ERROR_SUCCESS)
  174. {
  175. return HRESULT_FROM_WIN32(lRes);
  176. }
  177. OLECHAR szNodeGUID[MAX_GUID_LENGTH + 1];
  178. for (_NODE_TYPE_INFO_ENTRY* pCurrEntry = pNodeTypeInfoEntryArray;
  179. pCurrEntry->m_pNodeGUID != NULL; pCurrEntry++)
  180. {
  181. numWritten = ::StringFromGUID2(
  182. *(pCurrEntry->m_pNodeGUID),
  183. szNodeGUID,
  184. MAX_GUID_LENGTH + 1);
  185. if (numWritten <= 0)
  186. {
  187. ASSERT(false);
  188. return E_FAIL;
  189. }
  190. CRegKey regKeyNode;
  191. lRes = regKeyNode.Create(regKeyNodeTypes, szNodeGUID);
  192. if (lRes != ERROR_SUCCESS)
  193. {
  194. return HRESULT_FROM_WIN32(lRes);
  195. }
  196. }
  197. return HRESULT_FROM_WIN32(lRes);
  198. }
  199. HRESULT UnregisterSnapin(const GUID* pSnapinCLSID)
  200. {
  201. OLECHAR szSnapinClassID[MAX_GUID_LENGTH + 1];
  202. int numWritten;
  203. numWritten = ::StringFromGUID2(
  204. *pSnapinCLSID,
  205. szSnapinClassID,
  206. MAX_GUID_LENGTH + 1);
  207. if (numWritten <= 0)
  208. {
  209. ASSERT(false);
  210. return E_FAIL;
  211. }
  212. CRegKey regkeySnapins;
  213. LONG lRes = regkeySnapins.Open(HKEY_LOCAL_MACHINE, SNAPINS_KEY);
  214. ASSERT(lRes == ERROR_SUCCESS);
  215. if (lRes != ERROR_SUCCESS)
  216. {
  217. return HRESULT_FROM_WIN32(lRes); // failed to open
  218. }
  219. lRes = regkeySnapins.RecurseDeleteKey(szSnapinClassID);
  220. ASSERT(lRes == ERROR_SUCCESS);
  221. return HRESULT_FROM_WIN32(lRes);
  222. }
  223. HRESULT RegisterNodeType(const GUID* pGuid, LPCTSTR lpszNodeDescription)
  224. {
  225. OLECHAR szNodeGuid[MAX_GUID_LENGTH + 1];
  226. int numWritten;
  227. numWritten = ::StringFromGUID2(
  228. *pGuid,
  229. szNodeGuid,
  230. MAX_GUID_LENGTH + 1);
  231. if (numWritten <= 0)
  232. {
  233. ASSERT(false);
  234. return E_FAIL;
  235. }
  236. CRegKey regkeyNodeTypes;
  237. LONG lRes = regkeyNodeTypes.Open(HKEY_LOCAL_MACHINE, NODE_TYPES_KEY);
  238. ASSERT(lRes == ERROR_SUCCESS);
  239. if (lRes != ERROR_SUCCESS)
  240. {
  241. return HRESULT_FROM_WIN32(lRes); // failed to open
  242. }
  243. CRegKey regkeyThisNodeType;
  244. lRes = regkeyThisNodeType.Create(regkeyNodeTypes, szNodeGuid);
  245. ASSERT(lRes == ERROR_SUCCESS);
  246. if (lRes != ERROR_SUCCESS)
  247. {
  248. return HRESULT_FROM_WIN32(lRes); // failed to create
  249. }
  250. lRes = regkeyThisNodeType.SetValue(lpszNodeDescription);
  251. ASSERT(lRes == ERROR_SUCCESS);
  252. return HRESULT_FROM_WIN32(lRes);
  253. }
  254. HRESULT UnregisterNodeType(const GUID* pGuid)
  255. {
  256. OLECHAR szNodeGuid[MAX_GUID_LENGTH + 1];
  257. int numWritten;
  258. numWritten = ::StringFromGUID2(
  259. *pGuid,
  260. szNodeGuid,
  261. MAX_GUID_LENGTH + 1);
  262. if (numWritten <= 0)
  263. {
  264. ASSERT(false);
  265. return E_FAIL;
  266. }
  267. CRegKey regkeyNodeTypes;
  268. LONG lRes = regkeyNodeTypes.Open(HKEY_LOCAL_MACHINE, NODE_TYPES_KEY);
  269. ASSERT(lRes == ERROR_SUCCESS);
  270. if (lRes != ERROR_SUCCESS)
  271. {
  272. return HRESULT_FROM_WIN32(lRes); // failed to open
  273. }
  274. lRes = regkeyNodeTypes.RecurseDeleteKey(szNodeGuid);
  275. ASSERT(lRes == ERROR_SUCCESS);
  276. return HRESULT_FROM_WIN32(lRes);
  277. }
  278. HRESULT RegisterNodeExtension(const GUID* pNodeGuid, LPCTSTR lpszExtensionType,
  279. const GUID* pExtensionSnapinCLSID, LPCTSTR lpszDescription,
  280. BOOL bDynamic)
  281. {
  282. OLECHAR szNodeGuid[MAX_GUID_LENGTH + 1], szExtensionSnapinCLSID[MAX_GUID_LENGTH + 1];
  283. int numWritten;
  284. numWritten = ::StringFromGUID2(
  285. *pNodeGuid,
  286. szNodeGuid,
  287. MAX_GUID_LENGTH + 1);
  288. if (numWritten <= 0)
  289. {
  290. ASSERT(false);
  291. return E_FAIL;
  292. }
  293. numWritten = ::StringFromGUID2(
  294. *pExtensionSnapinCLSID,
  295. szExtensionSnapinCLSID,
  296. MAX_GUID_LENGTH + 1);
  297. if (numWritten <= 0)
  298. {
  299. ASSERT(false);
  300. return E_FAIL;
  301. }
  302. //
  303. // compose full path of key up to the node GUID
  304. //
  305. WCHAR szKeyPath[MAX_KEY_PATH_LENGTH + 1];
  306. HRESULT hr;
  307. // NOTICE-2002/04/18-artm ntraid#ntbug9-540061
  308. // StringCchPrintf() guarantees that string will be null terminated
  309. // and will not overrun the buffer.
  310. hr = StringCchPrintf(
  311. szKeyPath,
  312. MAX_KEY_PATH_LENGTH + 1,
  313. L"%s\\%s",
  314. NODE_TYPES_KEY,
  315. szNodeGuid);
  316. if (FAILED(hr))
  317. {
  318. ASSERT(false);
  319. return hr;
  320. }
  321. CRegKey regkeyNodeTypesNode;
  322. LONG lRes = regkeyNodeTypesNode.Open(HKEY_LOCAL_MACHINE, szKeyPath);
  323. ASSERT(lRes == ERROR_SUCCESS);
  324. if (lRes != ERROR_SUCCESS)
  325. {
  326. return HRESULT_FROM_WIN32(lRes); // failed to open
  327. }
  328. CRegKey regkeyExtensions;
  329. lRes = regkeyExtensions.Create(regkeyNodeTypesNode, g_szExtensions);
  330. ASSERT(lRes == ERROR_SUCCESS);
  331. if (lRes != ERROR_SUCCESS)
  332. {
  333. return HRESULT_FROM_WIN32(lRes); // failed to create
  334. }
  335. CRegKey regkeyExtensionType;
  336. lRes = regkeyExtensionType.Create(regkeyExtensions, lpszExtensionType);
  337. ASSERT(lRes == ERROR_SUCCESS);
  338. if (lRes != ERROR_SUCCESS)
  339. {
  340. return HRESULT_FROM_WIN32(lRes); // failed to create
  341. }
  342. lRes = regkeyExtensionType.SetValue(lpszDescription, szExtensionSnapinCLSID);
  343. ASSERT(lRes == ERROR_SUCCESS);
  344. if (lRes != ERROR_SUCCESS)
  345. {
  346. return HRESULT_FROM_WIN32(lRes); // failed to set value
  347. }
  348. if (bDynamic)
  349. {
  350. // create a subkey under the node GUID
  351. CRegKey regkeyDynamicExtensions;
  352. lRes = regkeyDynamicExtensions.Create(regkeyNodeTypesNode, g_szDynamicExtensions);
  353. ASSERT(lRes == ERROR_SUCCESS);
  354. if (lRes != ERROR_SUCCESS)
  355. return HRESULT_FROM_WIN32(lRes); // failed to create
  356. // set value (same value as the extension type above)
  357. lRes = regkeyDynamicExtensions.SetValue(lpszDescription, szExtensionSnapinCLSID);
  358. ASSERT(lRes == ERROR_SUCCESS);
  359. if (lRes != ERROR_SUCCESS)
  360. {
  361. return HRESULT_FROM_WIN32(lRes); // failed to set value
  362. }
  363. }
  364. return HRESULT_FROM_WIN32(lRes);
  365. }
  366. HRESULT UnregisterNodeExtension(const GUID* pNodeGuid, LPCTSTR lpszExtensionType,
  367. const GUID* pExtensionSnapinCLSID, BOOL bDynamic)
  368. {
  369. OLECHAR szNodeGuid[MAX_GUID_LENGTH + 1], szExtensionSnapinCLSID[MAX_GUID_LENGTH + 1];
  370. int numWritten;
  371. numWritten = ::StringFromGUID2(
  372. *pNodeGuid,
  373. szNodeGuid,
  374. MAX_GUID_LENGTH + 1);
  375. if (numWritten <= 0)
  376. {
  377. ASSERT(false);
  378. return E_FAIL;
  379. }
  380. numWritten = ::StringFromGUID2(
  381. *pExtensionSnapinCLSID,
  382. szExtensionSnapinCLSID,
  383. MAX_GUID_LENGTH + 1);
  384. if (numWritten <= 0)
  385. {
  386. ASSERT(false);
  387. return E_FAIL;
  388. }
  389. //
  390. // compose full path of key up to the node GUID
  391. //
  392. WCHAR szKeyPath[MAX_KEY_PATH_LENGTH + 1];
  393. HRESULT hr;
  394. // NOTICE-2002/04/18-artm ntraid#ntbug9-540061
  395. // StringCchPrintf() guarantees that string will be null terminated
  396. // and will not overrun the buffer.
  397. hr = StringCchPrintf(
  398. szKeyPath,
  399. MAX_KEY_PATH_LENGTH + 1,
  400. L"%s\\%s",
  401. NODE_TYPES_KEY,
  402. szNodeGuid);
  403. if (FAILED(hr))
  404. {
  405. ASSERT(false);
  406. return hr;
  407. }
  408. CRegKey regkeyNodeTypesNode;
  409. LONG lRes = regkeyNodeTypesNode.Open(HKEY_LOCAL_MACHINE, szKeyPath);
  410. ASSERT(lRes == ERROR_SUCCESS);
  411. if (lRes != ERROR_SUCCESS)
  412. return HRESULT_FROM_WIN32(lRes); // failed to open
  413. lRes = ERROR_SUCCESS;
  414. // open the key for the Dynamic extensions
  415. if (bDynamic)
  416. {
  417. CRegKey regkeyDynamicExtensions;
  418. lRes = regkeyDynamicExtensions.Open(regkeyNodeTypesNode, g_szDynamicExtensions);
  419. if (lRes == ERROR_SUCCESS)
  420. {
  421. lRes = regkeyDynamicExtensions.DeleteValue(szExtensionSnapinCLSID);
  422. }
  423. }
  424. else
  425. {
  426. //
  427. // Open the extensions key
  428. //
  429. CRegKey regkeyExtensions;
  430. lRes = regkeyExtensions.Open(regkeyNodeTypesNode, g_szExtensions);
  431. if (lRes == ERROR_SUCCESS)
  432. {
  433. CRegKey regkeyExtensionType;
  434. lRes = regkeyExtensionType.Open(regkeyExtensions, lpszExtensionType);
  435. if (lRes == ERROR_SUCCESS)
  436. {
  437. lRes = regkeyExtensionType.DeleteValue(szExtensionSnapinCLSID);
  438. }
  439. }
  440. }
  441. lRes = ERROR_SUCCESS;
  442. return HRESULT_FROM_WIN32(lRes);
  443. }
  444. /////////////////////////////////////////////////////////////////////////////
  445. // CTimerThread
  446. BOOL CTimerThread::Start(HWND hWnd)
  447. {
  448. ASSERT(m_hWnd == NULL);
  449. ASSERT(::IsWindow(hWnd));
  450. m_hWnd = hWnd;
  451. return CreateThread();
  452. }
  453. BOOL CTimerThread::PostMessageToWnd(WPARAM wParam, LPARAM lParam)
  454. {
  455. ASSERT(::IsWindow(m_hWnd));
  456. return ::PostMessage(m_hWnd, CHiddenWnd::s_TimerThreadMessage, wParam, lParam);
  457. }
  458. /////////////////////////////////////////////////////////////////////////////
  459. // CWorkerThread
  460. CWorkerThread::CWorkerThread()
  461. {
  462. m_bAutoDelete = FALSE;
  463. m_bAbandoned = FALSE;
  464. m_hEventHandle = NULL;
  465. ExceptionPropagatingInitializeCriticalSection(&m_cs);
  466. m_hWnd = NULL;
  467. }
  468. CWorkerThread::~CWorkerThread()
  469. {
  470. ::DeleteCriticalSection(&m_cs);
  471. if (m_hEventHandle != NULL)
  472. {
  473. VERIFY(::CloseHandle(m_hEventHandle));
  474. m_hEventHandle = NULL;
  475. }
  476. }
  477. BOOL CWorkerThread::Start(HWND hWnd)
  478. {
  479. ASSERT(m_hWnd == NULL);
  480. ASSERT(::IsWindow(hWnd));
  481. m_hWnd = hWnd;
  482. // REVIEWED-2002/03/08-JeffJon-Squatting isn't an issue here because this is not a
  483. // named event
  484. ASSERT(m_hEventHandle == NULL); // cannot call start twice or reuse the same C++ object
  485. m_hEventHandle = ::CreateEvent(NULL,TRUE /*bManualReset*/,FALSE /*signalled*/, NULL);
  486. if (m_hEventHandle == NULL)
  487. {
  488. return FALSE;
  489. }
  490. return CreateThread();
  491. }
  492. void CWorkerThread::Abandon()
  493. {
  494. Lock();
  495. OnAbandon();
  496. m_bAutoDelete = TRUE;
  497. m_bAbandoned = TRUE;
  498. Unlock();
  499. }
  500. BOOL CWorkerThread::IsAbandoned()
  501. {
  502. Lock();
  503. BOOL b = m_bAbandoned;
  504. Unlock();
  505. return b;
  506. }
  507. BOOL CWorkerThread::PostMessageToWnd(UINT Msg, WPARAM wParam, LPARAM lParam)
  508. {
  509. BOOL b = IsAbandoned();
  510. if (b)
  511. {
  512. return TRUE; // no need to post
  513. }
  514. ASSERT(::IsWindow(m_hWnd));
  515. return ::PostMessage(m_hWnd, Msg, wParam, lParam);
  516. }
  517. void CWorkerThread::WaitForExitAcknowledge()
  518. {
  519. BOOL b = IsAbandoned();
  520. if (b)
  521. {
  522. return;
  523. }
  524. VERIFY(WAIT_OBJECT_0 == ::WaitForSingleObject(m_hEventHandle,INFINITE));
  525. }
  526. /////////////////////////////////////////////////////////////////////////////
  527. // CHiddenWnd
  528. const UINT CHiddenWnd::s_NodeThreadHaveDataNotificationMessage = WM_USER + 1;
  529. const UINT CHiddenWnd::s_NodeThreadErrorNotificationMessage = WM_USER + 2;
  530. const UINT CHiddenWnd::s_NodeThreadExitingNotificationMessage = WM_USER + 3;
  531. const UINT CHiddenWnd::s_NodePropertySheetCreateMessage = WM_USER + 4;
  532. const UINT CHiddenWnd::s_NodePropertySheetDeleteMessage = WM_USER + 5;
  533. const UINT CHiddenWnd::s_ExecCommandMessage = WM_USER + 6;
  534. const UINT CHiddenWnd::s_ForceEnumerationMessage = WM_USER + 7;
  535. const UINT CHiddenWnd::s_TimerThreadMessage = WM_USER + 8;
  536. CHiddenWnd::CHiddenWnd(CComponentDataObject* pComponentDataObject)
  537. {
  538. m_pComponentDataObject = pComponentDataObject;
  539. m_nTimerID = 0;
  540. }
  541. LRESULT CHiddenWnd::OnNodeThreadHaveDataNotification(UINT, WPARAM wParam, LPARAM, BOOL&)
  542. {
  543. //TRACE(_T("CHiddenWnd::OnNodeThreadHaveDataNotification()\n"));
  544. ASSERT(m_pComponentDataObject != NULL);
  545. // call into the CTreeNode code
  546. CMTContainerNode* pNode = reinterpret_cast<CMTContainerNode*>(wParam);
  547. ASSERT(pNode);
  548. ASSERT(pNode->IsContainer());
  549. pNode->OnThreadHaveDataNotification(m_pComponentDataObject);
  550. return 1;
  551. }
  552. LRESULT CHiddenWnd::OnNodeThreadExitingNotification(UINT, WPARAM wParam, LPARAM lParam, BOOL&)
  553. {
  554. //TRACE(_T("CHiddenWnd::OnNodeThreadExitingNotification()\n"));
  555. ASSERT(m_pComponentDataObject != NULL);
  556. // call into the CTreeNode code
  557. CMTContainerNode* pNode = reinterpret_cast<CMTContainerNode*>(wParam);
  558. ASSERT(pNode);
  559. ASSERT(pNode->IsContainer());
  560. pNode->OnThreadExitingNotification(m_pComponentDataObject);
  561. // notify anybody interested in this event
  562. m_pComponentDataObject->GetNotificationSinkTable()->Notify(
  563. CHiddenWnd::s_NodeThreadExitingNotificationMessage ,wParam,lParam);
  564. return 1;
  565. }
  566. LRESULT CHiddenWnd::OnNodeThreadErrorNotification(UINT, WPARAM wParam, LPARAM lParam, BOOL&)
  567. {
  568. ASSERT(m_pComponentDataObject != NULL);
  569. // call into the CTreeNode code
  570. CMTContainerNode* pNode = reinterpret_cast<CMTContainerNode*>(wParam);
  571. DWORD dwErr = static_cast<DWORD>(lParam);
  572. ASSERT(pNode);
  573. ASSERT(pNode->IsContainer());
  574. pNode->OnThreadErrorNotification(dwErr, m_pComponentDataObject);
  575. return 1;
  576. }
  577. LRESULT CHiddenWnd::OnNodePropertySheetCreate(UINT, WPARAM wParam, LPARAM lParam, BOOL&)
  578. {
  579. //TRACE(_T("CHiddenWnd::OnNodePropertySheetCreate()\n"));
  580. ASSERT(m_pComponentDataObject != NULL);
  581. CPropertyPageHolderBase* pPPHolder = reinterpret_cast<CPropertyPageHolderBase*>(wParam);
  582. ASSERT(pPPHolder != NULL);
  583. HWND hWnd = reinterpret_cast<HWND>(lParam);
  584. ASSERT(::IsWindow(hWnd));
  585. m_pComponentDataObject->GetPropertyPageHolderTable()->AddWindow(pPPHolder, hWnd);
  586. return 1;
  587. }
  588. LRESULT CHiddenWnd::OnNodePropertySheetDelete(UINT, WPARAM wParam, LPARAM lParam, BOOL&)
  589. {
  590. //TRACE(_T("CHiddenWnd::OnNodePropertySheetDestroy()\n"));
  591. ASSERT(m_pComponentDataObject != NULL);
  592. CPropertyPageHolderBase* pPPHolder = reinterpret_cast<CPropertyPageHolderBase*>(wParam);
  593. ASSERT(pPPHolder != NULL);
  594. CTreeNode* pNode = reinterpret_cast<CTreeNode*>(lParam);
  595. ASSERT(pNode != NULL);
  596. m_pComponentDataObject->GetPropertyPageHolderTable()->Remove(pPPHolder);
  597. pNode->OnDeleteSheet();
  598. //if Node is only for displaying the property sheet, delete the node
  599. //once the property sheet is deleted
  600. if(!pNode->HasSheet() && pNode->IsNodeForPropSheet())
  601. {
  602. delete pNode;
  603. }
  604. return 1;
  605. }
  606. LRESULT CHiddenWnd::OnExecCommand(UINT, WPARAM wParam, LPARAM lParam, BOOL&)
  607. {
  608. //TRACE(_T("CHiddenWnd::OnExecCommand()\n"));
  609. ASSERT(m_pComponentDataObject != NULL);
  610. CExecContext* pExec = reinterpret_cast<CExecContext*>(wParam);
  611. ASSERT(pExec != NULL);
  612. pExec->Execute((long)lParam); // execute code
  613. TRACE(_T("CHiddenWnd::BeforeDone()\n"));
  614. pExec->Done(); // let the secondary thread proceed
  615. return 1;
  616. }
  617. LRESULT CHiddenWnd::OnForceEnumeration(UINT, WPARAM wParam, LPARAM, BOOL&)
  618. {
  619. TRACE(_T("CHiddenWnd::OnForceEnumeration()\n"));
  620. ASSERT(m_pComponentDataObject != NULL);
  621. // call into the CTreeNode code
  622. CMTContainerNode* pNode = reinterpret_cast<CMTContainerNode*>(wParam);
  623. ASSERT(pNode);
  624. ASSERT(pNode->GetContainer() != NULL); // not the root!!!
  625. ASSERT(pNode->IsContainer());
  626. pNode->ForceEnumeration(m_pComponentDataObject);
  627. return 1;
  628. }
  629. LRESULT CHiddenWnd::OnTimerThread(UINT, WPARAM wParam, LPARAM lParam, BOOL&)
  630. {
  631. //TRACE(_T("CHiddenWnd::OnTimerThread()\n"));
  632. ASSERT(m_pComponentDataObject != NULL);
  633. // NULL arguments means that the thread acknowledge it is running properly
  634. // only to be called once
  635. if ((wParam == 0) && (lParam == 0))
  636. {
  637. ASSERT(!m_pComponentDataObject->m_bTimerThreadStarted);
  638. m_pComponentDataObject->m_bTimerThreadStarted = TRUE;
  639. }
  640. else
  641. {
  642. // got some object specific message
  643. m_pComponentDataObject->OnTimerThread(wParam, lParam);
  644. }
  645. return 1;
  646. }
  647. LRESULT CHiddenWnd::OnTimer(UINT, WPARAM, LPARAM, BOOL&)
  648. {
  649. ASSERT(m_pComponentDataObject != NULL);
  650. m_pComponentDataObject->OnTimer();
  651. return 1;
  652. }
  653. ////////////////////////////////////////////////////////////////////////////////////
  654. // CRunningThreadTable
  655. #define RUNNING_THREAD_ARRAY_DEF_SIZE (4)
  656. CRunningThreadTable::CRunningThreadTable(CComponentDataObject* pComponentData)
  657. {
  658. m_pComponentData = pComponentData;
  659. size_t arraySizeInBytes = sizeof(CMTContainerNode*) * RUNNING_THREAD_ARRAY_DEF_SIZE;
  660. m_pEntries = (CMTContainerNode**)malloc(arraySizeInBytes);
  661. if (m_pEntries != NULL)
  662. {
  663. // This is acceptable usage of memset
  664. memset(m_pEntries,NULL, arraySizeInBytes);
  665. }
  666. m_nSize = RUNNING_THREAD_ARRAY_DEF_SIZE;
  667. }
  668. CRunningThreadTable::~CRunningThreadTable()
  669. {
  670. #ifdef _DEBUG
  671. for (int k=0; k < m_nSize; k++)
  672. {
  673. ASSERT(m_pEntries[k] == NULL);
  674. }
  675. #endif
  676. free(m_pEntries);
  677. }
  678. void CRunningThreadTable::Add(CMTContainerNode* pNode)
  679. {
  680. ASSERT(pNode != NULL);
  681. for (int k=0; k < m_nSize; k++)
  682. {
  683. if (m_pEntries[k] == NULL) // get the first empty spot
  684. {
  685. pNode->IncrementThreadLockCount();
  686. m_pEntries[k] = pNode;
  687. return;
  688. }
  689. }
  690. // all full, need to grow the array
  691. int nAlloc = m_nSize*2;
  692. size_t arraySizeInBytes = sizeof(CMTContainerNode*)*nAlloc;
  693. CMTContainerNode** temp = (CMTContainerNode**)realloc(m_pEntries, arraySizeInBytes);
  694. if (temp)
  695. {
  696. m_pEntries = temp;
  697. }
  698. else
  699. {
  700. return;
  701. }
  702. // This is acceptable usage
  703. memset(&m_pEntries[m_nSize], NULL, sizeof(CMTContainerNode*)*m_nSize);
  704. pNode->IncrementThreadLockCount();
  705. m_pEntries[m_nSize] = pNode;
  706. m_nSize = nAlloc;
  707. }
  708. BOOL CRunningThreadTable::IsPresent(CMTContainerNode* pNode)
  709. {
  710. ASSERT(pNode != NULL);
  711. for (int k=0; k < m_nSize; k++)
  712. {
  713. if (m_pEntries[k] == pNode)
  714. {
  715. return TRUE;
  716. }
  717. }
  718. return FALSE;
  719. }
  720. void CRunningThreadTable::Remove(CMTContainerNode* pNode)
  721. {
  722. ASSERT(pNode != NULL);
  723. for (int k=0; k < m_nSize; k++)
  724. {
  725. if (m_pEntries[k] == pNode)
  726. {
  727. m_pEntries[k] = NULL;
  728. pNode->DecrementThreadLockCount();
  729. return; // assume no more that one holder entry
  730. }
  731. }
  732. }
  733. void CRunningThreadTable::RemoveAll()
  734. {
  735. for (int k=0; k < m_nSize; k++)
  736. {
  737. if (m_pEntries[k] != NULL)
  738. {
  739. m_pEntries[k]->AbandonThread(m_pComponentData);
  740. m_pEntries[k] = NULL;
  741. }
  742. }
  743. }
  744. ////////////////////////////////////////////////////////////////////////////////////
  745. // CExecContext
  746. CExecContext::CExecContext()
  747. {
  748. // REVIEWED-2002/03/08-JeffJon-Squatting isn't an issue here because this is not a
  749. // named event
  750. m_hEventHandle = ::CreateEvent(NULL,TRUE /*bManualReset*/,FALSE /*signalled*/, NULL);
  751. ASSERT(m_hEventHandle != NULL);
  752. }
  753. CExecContext::~CExecContext()
  754. {
  755. ASSERT(m_hEventHandle != NULL);
  756. VERIFY(::CloseHandle(m_hEventHandle));
  757. }
  758. void CExecContext::Done()
  759. {
  760. VERIFY(0 != ::SetEvent(m_hEventHandle));
  761. }
  762. void CExecContext::Wait()
  763. {
  764. ASSERT(m_hEventHandle != NULL);
  765. VERIFY(WAIT_OBJECT_0 == ::WaitForSingleObject(m_hEventHandle,INFINITE));
  766. }
  767. ////////////////////////////////////////////////////////////////////////////////////
  768. // CNotificationSinkEvent
  769. CNotificationSinkEvent::CNotificationSinkEvent()
  770. {
  771. // REVIEWED-2002/03/08-JeffJon-Squatting isn't an issue here because this is not a
  772. // named event
  773. m_hEventHandle = ::CreateEvent(NULL,TRUE /*bManualReset*/,FALSE /*signalled*/, NULL);
  774. ASSERT(m_hEventHandle != NULL);
  775. }
  776. CNotificationSinkEvent::~CNotificationSinkEvent()
  777. {
  778. ASSERT(m_hEventHandle != NULL);
  779. VERIFY(::CloseHandle(m_hEventHandle));
  780. }
  781. void CNotificationSinkEvent::OnNotify(DWORD, WPARAM, LPARAM)
  782. {
  783. TRACE(_T("CNotificationSinkEvent::OnNotify()\n"));
  784. VERIFY(0 != ::SetEvent(m_hEventHandle));
  785. }
  786. void CNotificationSinkEvent::Wait()
  787. {
  788. TRACE(_T("CNotificationSinkEvent::Wait()\n"));
  789. ASSERT(m_hEventHandle != NULL);
  790. VERIFY(WAIT_OBJECT_0 == ::WaitForSingleObject(m_hEventHandle,INFINITE));
  791. }
  792. ////////////////////////////////////////////////////////////////////////////////////
  793. // CNotificationSinkTable
  794. #define NOTIFICATION_SINK_ARRAY_DEF_SIZE (4)
  795. CNotificationSinkTable::CNotificationSinkTable()
  796. {
  797. ExceptionPropagatingInitializeCriticalSection(&m_cs);
  798. size_t arraySizeInBytes = sizeof(CNotificationSinkBase*) * NOTIFICATION_SINK_ARRAY_DEF_SIZE;
  799. m_pEntries = (CNotificationSinkBase**)malloc(arraySizeInBytes);
  800. if (m_pEntries != NULL)
  801. {
  802. // This is an acceptable usage
  803. memset(m_pEntries,NULL, arraySizeInBytes);
  804. }
  805. m_nSize = NOTIFICATION_SINK_ARRAY_DEF_SIZE;
  806. }
  807. CNotificationSinkTable::~CNotificationSinkTable()
  808. {
  809. free(m_pEntries);
  810. ::DeleteCriticalSection(&m_cs);
  811. }
  812. void CNotificationSinkTable::Advise(CNotificationSinkBase* p)
  813. {
  814. Lock();
  815. ASSERT(p != NULL);
  816. for (int k=0; k < m_nSize; k++)
  817. {
  818. if (m_pEntries[k] == NULL) // get the first empty spot
  819. {
  820. m_pEntries[k] = p;
  821. Unlock();
  822. return;
  823. }
  824. }
  825. // all full, need to grow the array
  826. int nAlloc = m_nSize*2;
  827. CNotificationSinkBase** temp = (CNotificationSinkBase**)realloc(m_pEntries, sizeof(CNotificationSinkBase*)*nAlloc);
  828. if (temp)
  829. {
  830. m_pEntries = temp;
  831. // This is an acceptable usage
  832. memset(&m_pEntries[m_nSize], NULL, sizeof(CNotificationSinkBase*)*m_nSize);
  833. m_pEntries[m_nSize] = p;
  834. m_nSize = nAlloc;
  835. }
  836. Unlock();
  837. }
  838. void CNotificationSinkTable::Unadvise(CNotificationSinkBase* p)
  839. {
  840. Lock();
  841. ASSERT(p != NULL);
  842. for (int k=0; k < m_nSize; k++)
  843. {
  844. if (m_pEntries[k] == p)
  845. {
  846. m_pEntries[k] = NULL;
  847. Unlock();
  848. return; // assume no more that one holder entry
  849. }
  850. }
  851. Unlock();
  852. }
  853. void CNotificationSinkTable::Notify(DWORD dwEvent, WPARAM dwArg1, LPARAM dwArg2)
  854. {
  855. Lock();
  856. for (int k=0; k < m_nSize; k++)
  857. {
  858. if (m_pEntries[k] != NULL)
  859. {
  860. m_pEntries[k]->OnNotify(dwEvent, dwArg1, dwArg2);
  861. }
  862. }
  863. Unlock();
  864. }
  865. ///////////////////////////////////////////////////////////////////////////////
  866. // CWatermarkInfoState (private class)
  867. class CWatermarkInfoState
  868. {
  869. public:
  870. CWatermarkInfoState()
  871. {
  872. m_pWatermarkInfo = NULL;
  873. m_hBanner = m_hWatermark = NULL;
  874. }
  875. ~CWatermarkInfoState()
  876. {
  877. DeleteBitmaps();
  878. if (m_pWatermarkInfo != NULL)
  879. {
  880. delete m_pWatermarkInfo;
  881. }
  882. }
  883. void DeleteBitmaps()
  884. {
  885. if (m_hBanner != NULL)
  886. {
  887. ::DeleteObject(m_hBanner);
  888. m_hBanner = NULL;
  889. }
  890. if (m_hWatermark != NULL)
  891. {
  892. ::DeleteObject(m_hWatermark);
  893. m_hWatermark = NULL;
  894. }
  895. }
  896. void LoadBitmaps()
  897. {
  898. ASSERT(m_pWatermarkInfo != NULL);
  899. if (m_hBanner == NULL)
  900. {
  901. m_hBanner = ::LoadBitmap(AfxGetInstanceHandle(), MAKEINTRESOURCE(m_pWatermarkInfo->m_nIDBanner));
  902. }
  903. if (m_hWatermark == NULL)
  904. {
  905. m_hWatermark = ::LoadBitmap(AfxGetInstanceHandle(), MAKEINTRESOURCE(m_pWatermarkInfo->m_nIDWatermark));
  906. }
  907. }
  908. CWatermarkInfo* m_pWatermarkInfo;
  909. HBITMAP m_hBanner;
  910. HBITMAP m_hWatermark;
  911. };
  912. ///////////////////////////////////////////////////////////////////////////////
  913. // CComponentDataObject implementation: helpers
  914. #ifdef _DEBUG_REFCOUNT
  915. unsigned int CComponentDataObject::m_nOustandingObjects = 0;
  916. #endif // _DEBUG_REFCOUNT
  917. CComponentDataObject::CComponentDataObject() :
  918. m_hiddenWnd((CComponentDataObject*)this), // initialize backpointer
  919. m_pTimerThreadObj(NULL),
  920. m_PPHTable(this), m_RTTable(this),
  921. m_pConsole(NULL), m_pConsoleNameSpace(NULL), m_pRootData(NULL), m_hWnd(NULL),
  922. m_nTimerThreadID(0x0), m_bTimerThreadStarted(FALSE), m_dwTimerInterval(1),
  923. m_dwTimerTime(0), m_pWatermarkInfoState(NULL), m_bExtensionSnapin(FALSE)
  924. {
  925. ExceptionPropagatingInitializeCriticalSection(&m_cs);
  926. #ifdef _DEBUG_REFCOUNT
  927. dbg_cRef = 0;
  928. ++m_nOustandingObjects;
  929. TRACE(_T("CComponentDataObject(), count = %d\n"),m_nOustandingObjects);
  930. #endif // _DEBUG_REFCOUNT
  931. }
  932. CComponentDataObject::~CComponentDataObject()
  933. {
  934. ::DeleteCriticalSection(&m_cs);
  935. #ifdef _DEBUG_REFCOUNT
  936. --m_nOustandingObjects;
  937. TRACE(_T("~CComponentDataObject(), count = %d\n"),m_nOustandingObjects);
  938. #endif // _DEBUG_REFCOUNT
  939. ASSERT(m_pConsole == NULL);
  940. ASSERT(m_pConsoleNameSpace == NULL);
  941. ASSERT(m_pRootData == NULL);
  942. }
  943. HRESULT CComponentDataObject::FinalConstruct()
  944. {
  945. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  946. if (!m_hiddenWnd.Create())
  947. {
  948. TRACE(_T("Failed to create hidden window\n"));
  949. return E_FAIL;
  950. }
  951. m_hWnd = m_hiddenWnd.m_hWnd;
  952. m_pRootData = OnCreateRootData();
  953. ASSERT(m_pRootData != NULL);
  954. return S_OK;
  955. }
  956. void CComponentDataObject::FinalRelease()
  957. {
  958. if (m_hiddenWnd.m_hWnd != NULL)
  959. {
  960. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  961. VERIFY(m_hiddenWnd.DestroyWindow());
  962. }
  963. // delete data
  964. if(m_pRootData != NULL)
  965. {
  966. delete m_pRootData;
  967. m_pRootData = NULL;
  968. }
  969. if (m_pWatermarkInfoState != NULL)
  970. {
  971. delete m_pWatermarkInfoState;
  972. }
  973. m_ColList.RemoveAndDeleteAllColumnSets();
  974. #if defined(_USE_MTFRMWK_LOGGING)
  975. if (log_instance != NULL)
  976. {
  977. log_instance->KillInstance();
  978. }
  979. #endif
  980. }
  981. ///////////////////////////////////////////////////////////////////////////////
  982. // CComponentDataObject::IComponentData members
  983. STDMETHODIMP CComponentDataObject::Initialize(LPUNKNOWN pUnknown)
  984. {
  985. ASSERT(m_pRootData != NULL);
  986. ASSERT(pUnknown != NULL);
  987. HRESULT hr = E_FAIL;
  988. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  989. // MMC should only call ::Initialize once!
  990. ASSERT(m_pConsole == NULL);
  991. ASSERT(m_pConsoleNameSpace == NULL);
  992. // get the pointers we need to hold on to
  993. hr = pUnknown->QueryInterface(IID_IConsoleNameSpace2, reinterpret_cast<void**>(&m_pConsoleNameSpace));
  994. ASSERT(hr == S_OK);
  995. ASSERT(m_pConsoleNameSpace != NULL);
  996. hr = pUnknown->QueryInterface(IID_IConsole2, reinterpret_cast<void**>(&m_pConsole));
  997. ASSERT(hr == S_OK);
  998. ASSERT(m_pConsole != NULL);
  999. // add the images for the scope tree
  1000. LPIMAGELIST lpScopeImage;
  1001. hr = m_pConsole->QueryScopeImageList(&lpScopeImage);
  1002. ASSERT(hr == S_OK);
  1003. // Set the images
  1004. hr = OnSetImages(lpScopeImage); // Load the bitmaps from the dll
  1005. ASSERT(hr == S_OK);
  1006. lpScopeImage->Release();
  1007. OnInitialize();
  1008. return S_OK;
  1009. }
  1010. STDMETHODIMP CComponentDataObject::Notify(LPDATAOBJECT lpDataObject, MMC_NOTIFY_TYPE event, LPARAM arg, LPARAM param)
  1011. {
  1012. ASSERT(m_pConsoleNameSpace != NULL);
  1013. HRESULT hr = S_OK;
  1014. // Since it's my folder it has an internal format.
  1015. // Design Note: for extension. I can use the fact, that the data object doesn't have
  1016. // my internal format and I should look at the node type and see how to extend it.
  1017. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  1018. if (event == MMCN_PROPERTY_CHANGE)
  1019. {
  1020. ASSERT(lpDataObject == NULL);
  1021. hr = OnPropertyChange(param, static_cast<long>(arg));
  1022. }
  1023. else
  1024. {
  1025. CInternalFormatCracker ifc;
  1026. ifc.Extract(lpDataObject);
  1027. if (ifc.GetCookieCount() == 0)
  1028. {
  1029. if ((event == MMCN_EXPAND) && (arg == TRUE) && IsExtensionSnapin())
  1030. {
  1031. return OnExtensionExpand(lpDataObject, param);
  1032. // this is a namespace extension, need to add
  1033. // the root of the snapin
  1034. CContainerNode* pContNode = GetRootData();
  1035. HSCOPEITEM pParent = param;
  1036. pContNode->SetScopeID(pParent);
  1037. pContNode->MarkExpanded();
  1038. return AddContainerNode(pContNode, pParent);
  1039. }
  1040. else if ((event == MMCN_REMOVE_CHILDREN) && IsExtensionSnapin())
  1041. {
  1042. hr = OnRemoveChildren(lpDataObject, arg);
  1043. }
  1044. return S_OK; // Extensions not supported
  1045. }
  1046. switch(event)
  1047. {
  1048. case MMCN_PASTE:
  1049. break;
  1050. case MMCN_DELETE:
  1051. hr = OnDeleteVerbHandler(ifc, NULL);
  1052. break;
  1053. case MMCN_REFRESH:
  1054. hr = OnRefreshVerbHandler(ifc);
  1055. break;
  1056. case MMCN_RENAME:
  1057. hr = OnRename(ifc, arg, param);
  1058. break;
  1059. case MMCN_EXPAND:
  1060. hr = OnExpand(ifc, arg, param);
  1061. break;
  1062. case MMCN_EXPANDSYNC:
  1063. hr = OnExpand(ifc, arg, param, FALSE);
  1064. break;
  1065. case MMCN_BTN_CLICK:
  1066. break;
  1067. case MMCN_SELECT:
  1068. hr = OnSelect(ifc, arg, param);
  1069. break;
  1070. default:
  1071. break;
  1072. } // switch
  1073. } // if
  1074. return hr;
  1075. }
  1076. STDMETHODIMP CComponentDataObject::Destroy()
  1077. {
  1078. InternalAddRef();
  1079. TRACE(_T("CComponentDataObject::Destroy()\n"));
  1080. OnDestroy();
  1081. SAFE_RELEASE(m_pConsoleNameSpace);
  1082. SAFE_RELEASE(m_pConsole);
  1083. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  1084. VERIFY(m_hiddenWnd.DestroyWindow());
  1085. InternalRelease();
  1086. return S_OK;
  1087. }
  1088. BOOL CComponentDataObject::PostExecMessage(CExecContext* pExec, LPARAM arg)
  1089. {
  1090. ASSERT(pExec != NULL);
  1091. ASSERT(::IsWindow(m_hWnd));
  1092. return ::PostMessage(m_hWnd, CHiddenWnd::s_ExecCommandMessage,
  1093. (WPARAM)pExec, (LPARAM)arg);
  1094. }
  1095. BOOL CComponentDataObject::PostForceEnumeration(CMTContainerNode* pContainerNode)
  1096. {
  1097. ASSERT(::IsWindow(m_hWnd));
  1098. return ::PostMessage(m_hWnd, CHiddenWnd::s_ForceEnumerationMessage,
  1099. (WPARAM)pContainerNode, (LPARAM)0);
  1100. }
  1101. BOOL CComponentDataObject::OnCreateSheet(CPropertyPageHolderBase* pPPHolder, HWND hWnd)
  1102. {
  1103. ASSERT(pPPHolder != NULL);
  1104. ASSERT(::IsWindow(hWnd));
  1105. ASSERT(::IsWindow(m_hWnd));
  1106. TRACE(_T("\nCComponentDataObject::OnCreateSheet()\n"));
  1107. return ::PostMessage(m_hWnd, CHiddenWnd::s_NodePropertySheetCreateMessage,
  1108. (WPARAM)pPPHolder, (LPARAM)hWnd);
  1109. }
  1110. BOOL CComponentDataObject::OnDeleteSheet(CPropertyPageHolderBase* pPPHolder, CTreeNode* pNode)
  1111. {
  1112. ASSERT(pPPHolder != NULL);
  1113. ASSERT(pNode != NULL);
  1114. ASSERT(::IsWindow(m_hWnd));
  1115. TRACE(_T("\nCComponentDataObject::OnDeleteSheet()\n"));
  1116. return ::PostMessage(m_hWnd, CHiddenWnd::s_NodePropertySheetDeleteMessage,
  1117. (WPARAM)pPPHolder, (LPARAM)pNode);
  1118. }
  1119. void CComponentDataObject::OnInitialize()
  1120. {
  1121. VERIFY(StartTimerThread());
  1122. }
  1123. void CComponentDataObject::OnDestroy()
  1124. {
  1125. // stop timer and worker thread
  1126. ShutDownTimerThread();
  1127. // detach all the threads that might be still running
  1128. GetRunningThreadTable()->RemoveAll();
  1129. // tell all the open property sheets to shut down
  1130. // shut down property sheets, if any
  1131. GetPropertyPageHolderTable()->WaitForAllToShutDown();
  1132. }
  1133. STDMETHODIMP CComponentDataObject::QueryDataObject(MMC_COOKIE cookie, DATA_OBJECT_TYPES type, LPDATAOBJECT* ppDataObject)
  1134. {
  1135. ASSERT(ppDataObject != NULL);
  1136. CComObject<CDataObject>* pObject;
  1137. CComObject<CDataObject>::CreateInstance(&pObject);
  1138. ASSERT(pObject != NULL);
  1139. if (!pObject)
  1140. {
  1141. // NTRAID#NTBUG9-657641-2002/07/11-sburns
  1142. return E_FAIL;
  1143. }
  1144. // Save cookie and type for delayed rendering
  1145. pObject->SetType(type);
  1146. CTreeNode* pNode = 0;
  1147. //
  1148. // -1 is an uninitialized data object, just ignore
  1149. //
  1150. if (cookie != -1)
  1151. {
  1152. if (cookie == NULL)
  1153. {
  1154. pNode = GetRootData();
  1155. }
  1156. else
  1157. {
  1158. pNode = reinterpret_cast<CTreeNode*>(cookie);
  1159. }
  1160. ASSERT(pNode != NULL);
  1161. pObject->AddCookie(pNode);
  1162. }
  1163. // save a pointer to "this"
  1164. IUnknown* pUnkComponentData = GetUnknown(); // no addref
  1165. ASSERT(pUnkComponentData != NULL);
  1166. pObject->SetComponentData(pUnkComponentData); // will addref it
  1167. return pObject->QueryInterface(IID_IDataObject,
  1168. reinterpret_cast<void**>(ppDataObject));
  1169. }
  1170. STDMETHODIMP CComponentDataObject::GetDisplayInfo(SCOPEDATAITEM* pScopeDataItem)
  1171. {
  1172. ASSERT(pScopeDataItem != NULL);
  1173. CTreeNode* pNode = reinterpret_cast<CTreeNode*>(pScopeDataItem->lParam);
  1174. ASSERT(pNode != NULL);
  1175. ASSERT(pNode->IsContainer());
  1176. ASSERT(pScopeDataItem->mask & SDI_STR);
  1177. pScopeDataItem->displayname = const_cast<LPWSTR>(pNode->GetDisplayName());
  1178. ASSERT(pScopeDataItem->displayname != NULL);
  1179. return S_OK;
  1180. }
  1181. STDMETHODIMP CComponentDataObject::CompareObjects(LPDATAOBJECT lpDataObjectA, LPDATAOBJECT lpDataObjectB)
  1182. {
  1183. ASSERT(lpDataObjectA != NULL);
  1184. ASSERT(lpDataObjectB != NULL);
  1185. CInternalFormatCracker ifcA, ifcB;
  1186. VERIFY(SUCCEEDED(ifcA.Extract(lpDataObjectA)));
  1187. VERIFY(SUCCEEDED(ifcB.Extract(lpDataObjectB)));
  1188. CTreeNode* pNodeA = ifcA.GetCookieAt(0);
  1189. CTreeNode* pNodeB = ifcB.GetCookieAt(0);
  1190. ASSERT(pNodeA != NULL);
  1191. ASSERT(pNodeB != NULL);
  1192. if ( (pNodeA == NULL) || (pNodeB == NULL) )
  1193. {
  1194. return E_FAIL;
  1195. }
  1196. return (pNodeA == pNodeB) ? S_OK : S_FALSE;
  1197. }
  1198. ///////////////////////////////////////////////////////////////////////////////
  1199. // Message handlers for CComponentDataObject::IComponentData::Notify()
  1200. HRESULT CComponentDataObject::OnAdd(CTreeNode*, LPARAM, LPARAM)
  1201. {
  1202. return E_UNEXPECTED;
  1203. }
  1204. HRESULT CComponentDataObject::OnRemoveChildren(LPDATAOBJECT lpDataObject, LPARAM)
  1205. {
  1206. CInternalFormatCracker ifc;
  1207. HRESULT hr = S_OK;
  1208. hr = ifc.Extract(lpDataObject);
  1209. if (SUCCEEDED(hr))
  1210. {
  1211. if (ifc.GetCookieCount() == 1)
  1212. {
  1213. CTreeNode* pNode = ifc.GetCookieAt(0);
  1214. if (pNode != NULL)
  1215. {
  1216. if (pNode->IsContainer())
  1217. {
  1218. CContainerNode* pContainerNode = dynamic_cast<CContainerNode*>(pNode);
  1219. if (pContainerNode != NULL)
  1220. {
  1221. pContainerNode->RemoveAllChildrenFromList();
  1222. }
  1223. }
  1224. }
  1225. }
  1226. else
  1227. {
  1228. ASSERT(FALSE);
  1229. }
  1230. }
  1231. return hr;
  1232. }
  1233. HRESULT CComponentDataObject::OnRename(CInternalFormatCracker& ifc, LPARAM, LPARAM param)
  1234. {
  1235. HRESULT hr = S_FALSE;
  1236. CTreeNode* pNode = ifc.GetCookieAt(0);
  1237. ASSERT(pNode != NULL);
  1238. hr = pNode->OnRename(this, (LPOLESTR)param);
  1239. if (hr == S_OK)
  1240. {
  1241. UpdateAllViewsHelper(reinterpret_cast<LONG_PTR>(pNode), CHANGE_RESULT_ITEM);
  1242. }
  1243. return hr;
  1244. }
  1245. HRESULT CComponentDataObject::OnExpand(CInternalFormatCracker& ifc,
  1246. LPARAM arg,
  1247. LPARAM param,
  1248. BOOL bAsync)
  1249. {
  1250. if (arg == TRUE)
  1251. {
  1252. // Did Initialize get called?
  1253. ASSERT(m_pConsoleNameSpace != NULL);
  1254. //
  1255. // I shouldn't have to deal with multiple select here...
  1256. //
  1257. ASSERT(ifc.GetCookieCount() == 1);
  1258. CTreeNode* pNode = ifc.GetCookieAt(0);
  1259. if (pNode == NULL)
  1260. {
  1261. ASSERT(pNode != NULL);
  1262. return S_FALSE;
  1263. }
  1264. EnumerateScopePane(pNode, param, bAsync);
  1265. }
  1266. else if (!bAsync)
  1267. {
  1268. ASSERT(m_pConsoleNameSpace != NULL);
  1269. //
  1270. // I shouldn't have to deal with multiple select here...
  1271. //
  1272. ASSERT(ifc.GetCookieCount() == 1);
  1273. CTreeNode* pNode = ifc.GetCookieAt(0);
  1274. ASSERT(pNode != NULL);
  1275. if (pNode && pNode->CanExpandSync())
  1276. {
  1277. MMC_EXPANDSYNC_STRUCT* pExpandStruct = reinterpret_cast<MMC_EXPANDSYNC_STRUCT*>(param);
  1278. if (pExpandStruct && pExpandStruct->bExpanding)
  1279. {
  1280. EnumerateScopePane(pNode, pExpandStruct->hItem, bAsync);
  1281. pExpandStruct->bHandled = TRUE;
  1282. }
  1283. }
  1284. else
  1285. {
  1286. return S_FALSE;
  1287. }
  1288. }
  1289. return S_OK;
  1290. }
  1291. HRESULT CComponentDataObject::OnSelect(CInternalFormatCracker&, LPARAM, LPARAM)
  1292. {
  1293. return E_UNEXPECTED;
  1294. }
  1295. HRESULT CComponentDataObject::OnContextMenu(CTreeNode*, LPARAM, LPARAM)
  1296. {
  1297. return S_OK;
  1298. }
  1299. HRESULT CComponentDataObject::OnPropertyChange(LPARAM param, long fScopePane)
  1300. {
  1301. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  1302. TRACE(_T("CComponentDataObject::OnPropertyChange()\n"));
  1303. ASSERT(param != NULL);
  1304. CPropertyPageHolderBase* pPPHolder = reinterpret_cast<CPropertyPageHolderBase*>(param);
  1305. ASSERT(pPPHolder != NULL);
  1306. CTreeNode* pNode = pPPHolder->GetTreeNode();
  1307. ASSERT(pNode != NULL);
  1308. // allow both types in the result pane, but only scope items in the scope pane
  1309. ASSERT(!fScopePane || (fScopePane && pNode->IsContainer()) );
  1310. long changeMask = CHANGE_RESULT_ITEM; // default, the holder can change it
  1311. BOOL bUpdate = pPPHolder->OnPropertyChange(fScopePane, &changeMask);
  1312. // fire event to let the property page thread proceed
  1313. pPPHolder->AcknowledgeNotify();
  1314. if (bUpdate)
  1315. {
  1316. pNode->OnPropertyChange(this, fScopePane, changeMask);
  1317. }
  1318. return S_OK;
  1319. }
  1320. /////////////////////////////////////////////////////////////////////////////
  1321. // CComponentDataObject::IExtendPropertySheet2 memebers
  1322. STDMETHODIMP CComponentDataObject::CreatePropertyPages(LPPROPERTYSHEETCALLBACK lpProvider,
  1323. LONG_PTR handle,
  1324. LPDATAOBJECT lpIDataObject)
  1325. {
  1326. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  1327. CInternalFormatCracker ifc;
  1328. HRESULT hr = ifc.Extract(lpIDataObject);
  1329. if (FAILED(hr))
  1330. {
  1331. return hr;
  1332. }
  1333. //
  1334. // this was an object created by the modal wizard, do nothing
  1335. //
  1336. if (ifc.GetCookieType() == CCT_UNINITIALIZED)
  1337. {
  1338. return hr;
  1339. }
  1340. if (ifc.GetCookieType() == CCT_SNAPIN_MANAGER)
  1341. {
  1342. return SnapinManagerCreatePropertyPages(lpProvider,handle);
  1343. }
  1344. CTreeNode* pNode = ifc.GetCookieAt(0);
  1345. if (!pNode)
  1346. {
  1347. // NTRAID#NTBUG9-657822-2002/07/11-sburns
  1348. return S_FALSE;
  1349. }
  1350. ASSERT(ifc.GetCookieType() == CCT_SCOPE || ifc.GetCookieType() == CCT_RESULT);
  1351. CNodeList nodeList;
  1352. ifc.GetCookieList(nodeList);
  1353. if (nodeList.GetCount() > 1) // multiple selection
  1354. {
  1355. //
  1356. // Delegate to the container
  1357. //
  1358. ASSERT(pNode->GetContainer() != NULL);
  1359. hr = pNode->GetContainer()->CreatePropertyPages(lpProvider, handle, &nodeList);
  1360. }
  1361. else if (nodeList.GetCount() == 1) // single selection
  1362. {
  1363. //
  1364. // Delegate to the node
  1365. //
  1366. ASSERT(pNode != NULL);
  1367. hr = pNode->CreatePropertyPages(lpProvider, handle, &nodeList);
  1368. }
  1369. else
  1370. {
  1371. hr = E_FAIL;
  1372. }
  1373. if (FAILED(hr))
  1374. {
  1375. // MMC is expecting S_FALSE if no pages were added
  1376. hr = S_FALSE;
  1377. }
  1378. return hr;
  1379. }
  1380. STDMETHODIMP CComponentDataObject::QueryPagesFor(LPDATAOBJECT lpDataObject)
  1381. {
  1382. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  1383. CTreeNode* pNode;
  1384. DATA_OBJECT_TYPES type;
  1385. CInternalFormatCracker ifc;
  1386. HRESULT hr = ifc.Extract(lpDataObject);
  1387. if (FAILED(hr))
  1388. {
  1389. return hr;
  1390. }
  1391. type = ifc.GetCookieType();
  1392. pNode = ifc.GetCookieAt(0);
  1393. //
  1394. // Retrieve node list and count
  1395. //
  1396. CNodeList nodeList;
  1397. ifc.GetCookieList(nodeList);
  1398. //
  1399. // this was an object created by the modal wizard, do nothing
  1400. //
  1401. if (type == CCT_UNINITIALIZED)
  1402. {
  1403. return hr;
  1404. }
  1405. if (type == CCT_SNAPIN_MANAGER)
  1406. {
  1407. return HasPropertyPages(type) ? S_OK : S_FALSE;
  1408. }
  1409. //
  1410. // we have a node, so delegate to it
  1411. //
  1412. ASSERT(pNode != NULL);
  1413. BOOL bDummy;
  1414. if (nodeList.GetCount() == 1) // single selection
  1415. {
  1416. ASSERT((type == CCT_SCOPE) || (type == CCT_RESULT));
  1417. if (pNode->GetSheetCount() > 0)
  1418. {
  1419. pNode->ShowPageForNode(this);
  1420. return S_FALSE;
  1421. }
  1422. else if (pNode->DelegatesPPToContainer() && pNode->GetContainer()->GetSheetCount() > 0)
  1423. {
  1424. //
  1425. // Find the page and bring it to foreground
  1426. //
  1427. pNode->ShowPageForNode(this);
  1428. return S_FALSE;
  1429. }
  1430. if (pNode->HasPropertyPages(type, &bDummy, &nodeList))
  1431. {
  1432. hr = S_OK;
  1433. }
  1434. else
  1435. {
  1436. hr = S_FALSE;
  1437. }
  1438. }
  1439. else if (nodeList.GetCount() > 1) // multiple selection
  1440. {
  1441. ASSERT(pNode->GetContainer() != NULL);
  1442. if (pNode->GetContainer()->HasPropertyPages(type, &bDummy, &nodeList))
  1443. {
  1444. hr = S_OK;
  1445. }
  1446. else
  1447. {
  1448. hr = S_FALSE;
  1449. }
  1450. }
  1451. return hr;
  1452. }
  1453. HRESULT CComponentDataObject::CreatePropertySheet(CTreeNode* pNode,
  1454. HWND hWndParent,
  1455. LPCWSTR lpszTitle)
  1456. {
  1457. HRESULT hr = S_OK;
  1458. HWND hWnd = hWndParent;
  1459. if (hWnd == NULL)
  1460. {
  1461. hr = m_pConsole->GetMainWindow(&hWnd);
  1462. if (FAILED(hr))
  1463. {
  1464. ASSERT(FALSE);
  1465. return hr;
  1466. }
  1467. }
  1468. //
  1469. // get an interface to a sheet provider
  1470. //
  1471. CComPtr<IPropertySheetProvider> spSheetProvider;
  1472. hr = m_pConsole->QueryInterface(IID_IPropertySheetProvider,(void**)&spSheetProvider);
  1473. ASSERT(SUCCEEDED(hr));
  1474. ASSERT(spSheetProvider != NULL);
  1475. //
  1476. // get an interface to a sheet callback
  1477. //
  1478. CComPtr<IPropertySheetCallback> spSheetCallback;
  1479. hr = m_pConsole->QueryInterface(IID_IPropertySheetCallback,(void**)&spSheetCallback);
  1480. ASSERT(SUCCEEDED(hr));
  1481. ASSERT(spSheetCallback != NULL);
  1482. //
  1483. // get a sheet
  1484. //
  1485. MMC_COOKIE cookie = reinterpret_cast<MMC_COOKIE>(pNode);
  1486. DATA_OBJECT_TYPES type = (pNode->IsContainer()) ? CCT_SCOPE : CCT_RESULT;
  1487. CComPtr<IDataObject> spDataObject;
  1488. hr = QueryDataObject(cookie, type, &spDataObject);
  1489. ASSERT(SUCCEEDED(hr));
  1490. ASSERT(spDataObject != NULL);
  1491. hr = spSheetProvider->CreatePropertySheet(lpszTitle, TRUE, cookie,
  1492. spDataObject, 0x0 /*dwOptions*/);
  1493. ASSERT(SUCCEEDED(hr));
  1494. hr = spSheetProvider->AddPrimaryPages(GetUnknown(),
  1495. TRUE /*bCreateHandle*/,
  1496. hWnd,
  1497. pNode->IsContainer() /* bScopePane*/);
  1498. hr = spSheetProvider->AddExtensionPages();
  1499. ASSERT(SUCCEEDED(hr));
  1500. hr = spSheetProvider->Show(reinterpret_cast<LONG_PTR>(hWnd), 0);
  1501. ASSERT(SUCCEEDED(hr));
  1502. return hr;
  1503. }
  1504. CWatermarkInfo* CComponentDataObject::SetWatermarkInfo(CWatermarkInfo* pWatermarkInfo)
  1505. {
  1506. if (m_pWatermarkInfoState == NULL)
  1507. {
  1508. m_pWatermarkInfoState = new CWatermarkInfoState;
  1509. }
  1510. CWatermarkInfo* pOldWatermarkInfo = m_pWatermarkInfoState->m_pWatermarkInfo;
  1511. m_pWatermarkInfoState->m_pWatermarkInfo = pWatermarkInfo;
  1512. // we changed info, so dump the old bitmap handles
  1513. m_pWatermarkInfoState->DeleteBitmaps();
  1514. return pOldWatermarkInfo;
  1515. }
  1516. STDMETHODIMP CComponentDataObject::GetWatermarks(LPDATAOBJECT,
  1517. HBITMAP* lphWatermark,
  1518. HBITMAP* lphHeader,
  1519. HPALETTE* lphPalette,
  1520. BOOL* pbStretch)
  1521. {
  1522. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  1523. *lphHeader = NULL;
  1524. *lphWatermark = NULL;
  1525. *lphPalette = NULL;
  1526. *pbStretch = TRUE;
  1527. if ((m_pWatermarkInfoState == NULL) || (m_pWatermarkInfoState->m_pWatermarkInfo == NULL))
  1528. {
  1529. return E_FAIL;
  1530. }
  1531. *pbStretch = m_pWatermarkInfoState->m_pWatermarkInfo->m_bStretch;
  1532. *lphPalette = m_pWatermarkInfoState->m_pWatermarkInfo->m_hPalette;
  1533. // load bitmaps if not loaded yet
  1534. m_pWatermarkInfoState->LoadBitmaps();
  1535. *lphHeader = m_pWatermarkInfoState->m_hBanner;
  1536. if (*lphHeader == NULL)
  1537. {
  1538. return E_FAIL;
  1539. }
  1540. *lphWatermark = m_pWatermarkInfoState->m_hWatermark;
  1541. if (*lphWatermark == NULL)
  1542. {
  1543. return E_FAIL;
  1544. }
  1545. return S_OK;
  1546. }
  1547. /////////////////////////////////////////////////////////////////////////////
  1548. // CComponentDataObject::IExtendContextMenu memebers
  1549. STDMETHODIMP CComponentDataObject::AddMenuItems(LPDATAOBJECT pDataObject,
  1550. LPCONTEXTMENUCALLBACK pContextMenuCallback,
  1551. long *pInsertionAllowed)
  1552. {
  1553. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  1554. HRESULT hr = S_OK;
  1555. CTreeNode* pNode;
  1556. DATA_OBJECT_TYPES type;
  1557. CInternalFormatCracker ifc;
  1558. hr = ifc.Extract(pDataObject);
  1559. if (FAILED(hr))
  1560. {
  1561. return hr;
  1562. }
  1563. type = ifc.GetCookieType();
  1564. pNode = ifc.GetCookieAt(0);
  1565. ASSERT(pNode != NULL);
  1566. if (pNode == NULL)
  1567. {
  1568. return hr;
  1569. }
  1570. CComPtr<IContextMenuCallback2> spContextMenuCallback2;
  1571. hr = pContextMenuCallback->QueryInterface(IID_IContextMenuCallback2, (PVOID*)&spContextMenuCallback2);
  1572. if (FAILED(hr))
  1573. {
  1574. return hr;
  1575. }
  1576. CNodeList nodeList;
  1577. ifc.GetCookieList(nodeList);
  1578. if (nodeList.GetCount() > 1) // multiple selection
  1579. {
  1580. ASSERT(pNode->GetContainer() != NULL);
  1581. hr = pNode->GetContainer()->OnAddMenuItems(spContextMenuCallback2,
  1582. type,
  1583. pInsertionAllowed,
  1584. &nodeList);
  1585. }
  1586. else if (nodeList.GetCount() == 1) // single selection
  1587. {
  1588. hr = pNode->OnAddMenuItems(spContextMenuCallback2,
  1589. type,
  1590. pInsertionAllowed,
  1591. &nodeList);
  1592. }
  1593. else
  1594. {
  1595. hr = E_FAIL;
  1596. }
  1597. return hr;
  1598. }
  1599. STDMETHODIMP CComponentDataObject::Command(long nCommandID, LPDATAOBJECT pDataObject)
  1600. {
  1601. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  1602. CInternalFormatCracker ifc;
  1603. HRESULT hr = ifc.Extract(pDataObject);
  1604. if (FAILED(hr))
  1605. {
  1606. return hr;
  1607. }
  1608. CTreeNode* pNode = ifc.GetCookieAt(0);
  1609. ASSERT(pNode != NULL);
  1610. //
  1611. // Retrieve node list and count
  1612. //
  1613. CNodeList nodeList;
  1614. ifc.GetCookieList(nodeList);
  1615. if (nodeList.GetCount() > 1) // multiple selection
  1616. {
  1617. //
  1618. // Delegate the command to the container
  1619. //
  1620. ASSERT(pNode->GetContainer() != NULL);
  1621. hr = pNode->GetContainer()->OnCommand(nCommandID,
  1622. ifc.GetCookieType(),
  1623. this,
  1624. &nodeList);
  1625. }
  1626. else if (nodeList.GetCount() == 1) // single selection
  1627. {
  1628. //
  1629. // Let the node take care of it
  1630. //
  1631. hr = pNode->OnCommand(nCommandID,
  1632. ifc.GetCookieType(),
  1633. this,
  1634. &nodeList);
  1635. }
  1636. else
  1637. {
  1638. hr = E_FAIL;
  1639. }
  1640. return hr;
  1641. }
  1642. /////////////////////////////////////////////////////////////////////////////
  1643. // CComponentDataObject::IPersistStream members
  1644. STDMETHODIMP CComponentDataObject::IsDirty()
  1645. {
  1646. // forward to the root of the tree
  1647. CRootData* pRootData = GetRootData();
  1648. ASSERT(pRootData != NULL);
  1649. return pRootData->IsDirty();
  1650. }
  1651. STDMETHODIMP CComponentDataObject::Load(IStream __RPC_FAR *pStm)
  1652. {
  1653. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  1654. // forward to the root of the tree
  1655. CRootData* pRootData = GetRootData();
  1656. ASSERT(pRootData != NULL);
  1657. return pRootData->Load(pStm);
  1658. }
  1659. STDMETHODIMP CComponentDataObject::Save(IStream __RPC_FAR *pStm, BOOL fClearDirty)
  1660. {
  1661. // forward to the root of the tree
  1662. CRootData* pRootData = GetRootData();
  1663. ASSERT(pRootData != NULL);
  1664. return pRootData->Save(pStm,fClearDirty);
  1665. }
  1666. /////////////////////////////////////////////////////////////////////////////
  1667. // CComponentDataObject::ISnapinHelp2 memebers
  1668. //
  1669. // Helper function for appending a help file name to the system directory.
  1670. //
  1671. // If returns S_OK, then helpFilePath will contain the full filename to the
  1672. // help file (including helpFileName).
  1673. //
  1674. HRESULT GetFullHelpFilePath(const CString& helpFileName, CString& helpFilePath)
  1675. {
  1676. UINT nLen;
  1677. helpFilePath.Empty();
  1678. // Determine how long the path to the system directory is.
  1679. // (does not include null)
  1680. nLen = ::GetSystemWindowsDirectory(NULL, 0);
  1681. if (nLen == 0)
  1682. {
  1683. ASSERT(false); // This should never happen.
  1684. return E_FAIL;
  1685. }
  1686. // Get a buffer big enough for system directory path, including null.
  1687. // We intentionally make it extra large in the hopes that later
  1688. // appending the name of the help file won't require allocating a larger
  1689. // buffer and performing a copy.
  1690. nLen = nLen < MAX_PATH ? MAX_PATH : nLen;
  1691. nLen = (2 * nLen) + 1;
  1692. LPWSTR lpszBuffer = helpFilePath.GetBuffer(nLen);
  1693. // Copy system directory path to our buffer (with null).
  1694. nLen = ::GetSystemWindowsDirectory(lpszBuffer, nLen);
  1695. if (nLen == 0)
  1696. {
  1697. return E_FAIL;
  1698. }
  1699. // Normally the system directory path does not end in a '\'. However,
  1700. // if the system is installed directly to the root of a drive (e.g. C:\)
  1701. // then the returned path does end with a '\'. We need to check for this and
  1702. // remove the '\' if it is there.
  1703. WCHAR slash[] = L"\\";
  1704. if (lpszBuffer[nLen - 1] == slash[0])
  1705. {
  1706. lpszBuffer[nLen - 1] = NULL;
  1707. }
  1708. // Release hold on buffer so string class can manage length and memory.
  1709. helpFilePath.ReleaseBuffer();
  1710. lpszBuffer = NULL;
  1711. // Append help file name to path.
  1712. helpFilePath += helpFileName;
  1713. return S_OK;
  1714. }
  1715. STDMETHODIMP CComponentDataObject::GetHelpTopic(LPOLESTR* lpCompiledHelpFile)
  1716. {
  1717. if (lpCompiledHelpFile == NULL)
  1718. {
  1719. return E_INVALIDARG;
  1720. }
  1721. LPCWSTR lpszHelpFileName = GetHTMLHelpFileName();
  1722. if (lpszHelpFileName == NULL)
  1723. {
  1724. *lpCompiledHelpFile = NULL;
  1725. return E_NOTIMPL;
  1726. }
  1727. //
  1728. // Get the full path to the help file by concatenating the help file name
  1729. // with the system directory.
  1730. //
  1731. CString szHelpFilePath;
  1732. HRESULT hr;
  1733. hr = GetFullHelpFilePath(lpszHelpFileName, szHelpFilePath);
  1734. if (FAILED(hr))
  1735. {
  1736. ASSERT(false); // Should never happen.
  1737. return hr;
  1738. }
  1739. UINT nBytes = (szHelpFilePath.GetLength()+1) * sizeof(WCHAR);
  1740. *lpCompiledHelpFile = (LPOLESTR)::CoTaskMemAlloc(nBytes);
  1741. if (*lpCompiledHelpFile != NULL)
  1742. {
  1743. memcpy(*lpCompiledHelpFile, (LPCWSTR)szHelpFilePath, nBytes);
  1744. }
  1745. else
  1746. {
  1747. return E_OUTOFMEMORY;
  1748. }
  1749. return S_OK;
  1750. }
  1751. HRESULT CComponentDataObject::GetLinkedTopics(LPOLESTR*)
  1752. {
  1753. return S_FALSE;
  1754. }
  1755. ///////////////////////////////////////////////////////////////////////////////
  1756. // CComponentDataObject Helpers
  1757. HRESULT CComponentDataObject::UpdateAllViewsHelper(LPARAM data, LONG_PTR hint)
  1758. {
  1759. ASSERT(m_pConsole != NULL);
  1760. CComObject<CDummyDataObject>* pObject;
  1761. CComObject<CDummyDataObject>::CreateInstance(&pObject);
  1762. ASSERT(pObject != NULL);
  1763. IDataObject* pDataObject;
  1764. HRESULT hr = pObject->QueryInterface(IID_IDataObject, reinterpret_cast<void**>(&pDataObject));
  1765. ASSERT(SUCCEEDED(hr));
  1766. ASSERT(pDataObject != NULL);
  1767. hr = m_pConsole->UpdateAllViews(pDataObject,data, hint);
  1768. pDataObject->Release();
  1769. return hr;
  1770. }
  1771. void CComponentDataObject::HandleStandardVerbsHelper(CComponentObject* pComponentObj,
  1772. LPCONSOLEVERB pConsoleVerb,
  1773. BOOL bScope, BOOL bSelect,
  1774. LPDATAOBJECT lpDataObject)
  1775. {
  1776. // You should crack the data object and enable/disable/hide standard
  1777. // commands appropriately. The standard commands are reset everytime you get
  1778. // called. So you must reset them back.
  1779. ASSERT(pConsoleVerb != NULL);
  1780. ASSERT(pComponentObj != NULL);
  1781. ASSERT(lpDataObject != NULL);
  1782. // reset the selection
  1783. pComponentObj->SetSelectedNode(NULL, CCT_UNINITIALIZED);
  1784. CInternalFormatCracker ifc;
  1785. VERIFY(SUCCEEDED(ifc.Extract(lpDataObject)));
  1786. CTreeNode* pNode = ifc.GetCookieAt(0);
  1787. if (pNode == NULL)
  1788. {
  1789. return;
  1790. }
  1791. //
  1792. // Retrieve node list and count
  1793. //
  1794. CNodeList nodeList;
  1795. ifc.GetCookieList(nodeList);
  1796. if (nodeList.GetCount() > 1) // multiple selection
  1797. {
  1798. //
  1799. // Delegate to the container
  1800. //
  1801. ASSERT(pNode->GetContainer() != NULL);
  1802. pNode->GetContainer()->OnSetVerbState(pConsoleVerb, ifc.GetCookieType(), &nodeList);
  1803. }
  1804. else if (nodeList.GetCount() == 1) // single selection
  1805. {
  1806. //
  1807. // set selection, if any
  1808. //
  1809. if (bSelect)
  1810. {
  1811. pComponentObj->SetSelectedNode(pNode, ifc.GetCookieType());
  1812. }
  1813. ASSERT((ifc.GetCookieType() == CCT_SCOPE) || (ifc.GetCookieType() == CCT_RESULT));
  1814. TRACE(_T("HandleStandardVerbsHelper: Node <%s> bScope = %d bSelect = %d, type = %s\n"),
  1815. pNode->GetDisplayName(), bScope, bSelect,
  1816. (ifc.GetCookieType() == CCT_SCOPE) ? _T("CCT_SCOPE") : _T("CCT_RESULT"));
  1817. pConsoleVerb->SetDefaultVerb(pNode->GetDefaultVerb(ifc.GetCookieType(), &nodeList));
  1818. pNode->OnSetVerbState(pConsoleVerb, ifc.GetCookieType(), &nodeList);
  1819. }
  1820. }
  1821. void CComponentDataObject::EnumerateScopePane(CTreeNode* cookie,
  1822. HSCOPEITEM pParent,
  1823. BOOL bAsync)
  1824. {
  1825. ASSERT(m_pConsoleNameSpace != NULL); // make sure we QI'ed for the interface
  1826. // find the node corresponding to the cookie
  1827. ASSERT(cookie != NULL);
  1828. ASSERT(cookie->IsContainer());
  1829. CContainerNode* pContNode = (CContainerNode*)cookie;
  1830. pContNode->MarkExpanded();
  1831. if (pContNode == GetRootData())
  1832. {
  1833. pContNode->SetScopeID(pParent);
  1834. }
  1835. // allow the node to enumerate its children, if not enumerated yet
  1836. if (!pContNode->IsEnumerated())
  1837. {
  1838. BOOL bAddChildrenNow = pContNode->OnEnumerate(this, bAsync);
  1839. pContNode->MarkEnumerated();
  1840. if (!bAddChildrenNow)
  1841. {
  1842. return;
  1843. }
  1844. }
  1845. // scan the list of children, looking for containers and add them
  1846. ASSERT(pParent != NULL);
  1847. CNodeList* pChildList = pContNode->GetContainerChildList();
  1848. ASSERT(pChildList != NULL);
  1849. POSITION pos;
  1850. for( pos = pChildList->GetHeadPosition(); pos != NULL; )
  1851. {
  1852. CContainerNode* pCurrChildNode = (CContainerNode*)pChildList->GetNext(pos);
  1853. ASSERT(pCurrChildNode != NULL);
  1854. if (pCurrChildNode->IsVisible())
  1855. {
  1856. AddContainerNode(pCurrChildNode, pParent);
  1857. }
  1858. }
  1859. }
  1860. HRESULT CComponentDataObject::OnDeleteVerbHandler(CInternalFormatCracker& ifc, CComponentObject*)
  1861. {
  1862. HRESULT hr = S_OK;
  1863. CTreeNode* pNode = ifc.GetCookieAt(0);
  1864. ASSERT(pNode != NULL);
  1865. //
  1866. // Retrieve the cookie list and count
  1867. //
  1868. CNodeList nodeList;
  1869. ifc.GetCookieList(nodeList);
  1870. if (nodeList.GetCount() > 1) // multiple selection
  1871. {
  1872. ASSERT(pNode->GetContainer() != NULL);
  1873. pNode->GetContainer()->OnDelete(this, &nodeList);
  1874. }
  1875. else if (nodeList.GetCount() == 1) // single selection
  1876. {
  1877. pNode->OnDelete(this, &nodeList);
  1878. }
  1879. else
  1880. {
  1881. hr = E_FAIL;
  1882. }
  1883. return hr;
  1884. }
  1885. HRESULT CComponentDataObject::OnRefreshVerbHandler(CInternalFormatCracker& ifc)
  1886. {
  1887. HRESULT hr = S_OK;
  1888. CTreeNode* pNode = ifc.GetCookieAt(0);
  1889. ASSERT(pNode != NULL);
  1890. //
  1891. // Retrieve the node list and the count
  1892. //
  1893. CNodeList nodeList;
  1894. ifc.GetCookieList(nodeList);
  1895. if (nodeList.GetCount() > 1) // multiple selection
  1896. {
  1897. ASSERT(pNode->GetContainer() != NULL);
  1898. pNode->GetContainer()->OnRefresh(this, &nodeList);
  1899. }
  1900. else if (nodeList.GetCount() == 1) // single selection
  1901. {
  1902. pNode->OnRefresh(this, &nodeList);
  1903. }
  1904. else
  1905. {
  1906. hr = E_FAIL;
  1907. }
  1908. return hr;
  1909. }
  1910. HRESULT CComponentDataObject::OnHelpHandler(CInternalFormatCracker& ifc, CComponentObject* pComponentObject)
  1911. {
  1912. //
  1913. // responding to MMCN_CONTEXTHELP
  1914. //
  1915. ASSERT(pComponentObject != NULL);
  1916. HRESULT hr = S_OK;
  1917. CTreeNode* pNode = ifc.GetCookieAt(0);
  1918. ASSERT(pNode != NULL);
  1919. //
  1920. // Retrieve the node list and count
  1921. //
  1922. CNodeList nodeList;
  1923. ifc.GetCookieList(nodeList);
  1924. if (nodeList.GetCount() > 1) // Multiple selection
  1925. {
  1926. ASSERT(pNode->GetContainer() != NULL);
  1927. OnNodeContextHelp(&nodeList);
  1928. }
  1929. else if (nodeList.GetCount() == 1) // Single selection
  1930. {
  1931. OnNodeContextHelp(pNode);
  1932. }
  1933. else
  1934. {
  1935. hr = E_FAIL;
  1936. }
  1937. return hr;
  1938. }
  1939. BOOL CComponentDataObject::WinHelp(LPCTSTR lpszHelpFileName, // file, no path
  1940. UINT uCommand, // type of Help
  1941. DWORD dwData // additional data
  1942. )
  1943. {
  1944. HWND hWnd;
  1945. GetConsole()->GetMainWindow(&hWnd);
  1946. //
  1947. // Get the full path to the help file by concatenating the help file name
  1948. // with the system directory.
  1949. //
  1950. CString szHelpFilePath;
  1951. HRESULT hr;
  1952. hr = GetFullHelpFilePath(lpszHelpFileName, szHelpFilePath);
  1953. if (FAILED(hr))
  1954. {
  1955. ASSERT(false); // Should never happen.
  1956. return FALSE;
  1957. }
  1958. return ::WinHelp(hWnd, szHelpFilePath, uCommand, dwData);
  1959. }
  1960. HRESULT CComponentDataObject::AddNode(CTreeNode* pNodeToAdd)
  1961. {
  1962. ASSERT(pNodeToAdd != NULL);
  1963. // if the node is hidden, just ignore
  1964. if (!pNodeToAdd->IsVisible())
  1965. return S_OK;
  1966. if (pNodeToAdd->IsContainer())
  1967. {
  1968. ASSERT(pNodeToAdd->GetContainer() != NULL);
  1969. HSCOPEITEM pParentScopeItem = pNodeToAdd->GetContainer()->GetScopeID();
  1970. ASSERT(pParentScopeItem != NULL);
  1971. return AddContainerNode((CContainerNode*)pNodeToAdd, pParentScopeItem);
  1972. }
  1973. return AddLeafNode((CLeafNode*)pNodeToAdd);
  1974. }
  1975. HRESULT CComponentDataObject::AddNodeSorted(CTreeNode* pNodeToAdd)
  1976. {
  1977. ASSERT(pNodeToAdd != NULL);
  1978. // if the node is hidden, just ignore
  1979. if (!pNodeToAdd->IsVisible())
  1980. {
  1981. return S_OK;
  1982. }
  1983. if (pNodeToAdd->IsContainer())
  1984. {
  1985. ASSERT(pNodeToAdd->GetContainer() != NULL);
  1986. HSCOPEITEM pParentScopeItem = pNodeToAdd->GetContainer()->GetScopeID();
  1987. ASSERT(pParentScopeItem != NULL);
  1988. return AddContainerNodeSorted((CContainerNode*)pNodeToAdd, pParentScopeItem);
  1989. }
  1990. return AddLeafNode((CLeafNode*)pNodeToAdd);
  1991. }
  1992. HRESULT CComponentDataObject::DeleteNode(CTreeNode* pNodeToDelete)
  1993. {
  1994. if (pNodeToDelete->IsContainer())
  1995. {
  1996. return DeleteContainerNode((CContainerNode*)pNodeToDelete);
  1997. }
  1998. return DeleteLeafNode((CLeafNode*)pNodeToDelete);
  1999. }
  2000. HRESULT CComponentDataObject::DeleteMultipleNodes(CNodeList* pNodeList)
  2001. {
  2002. HRESULT hr = S_OK;
  2003. POSITION pos = pNodeList->GetHeadPosition();
  2004. while (pos != NULL)
  2005. {
  2006. CTreeNode* pNode = pNodeList->GetNext(pos);
  2007. if (pNode->IsContainer())
  2008. {
  2009. DeleteContainerNode((CContainerNode*)pNode);
  2010. }
  2011. }
  2012. hr = UpdateAllViewsHelper(reinterpret_cast<LONG_PTR>(pNodeList), DELETE_MULTIPLE_RESULT_ITEMS);
  2013. return hr;
  2014. }
  2015. HRESULT CComponentDataObject::ChangeNode(CTreeNode* pNodeToChange, long changeMask)
  2016. {
  2017. if (!pNodeToChange->IsVisible())
  2018. {
  2019. return S_OK;
  2020. }
  2021. if (pNodeToChange->IsContainer())
  2022. {
  2023. CContainerNode* pContNode = (CContainerNode*)pNodeToChange;
  2024. //if (!pContNode->IsExpanded())
  2025. // return S_OK;
  2026. return ChangeContainerNode(pContNode, changeMask);
  2027. }
  2028. return ChangeLeafNode((CLeafNode*)pNodeToChange, changeMask);
  2029. }
  2030. HRESULT CComponentDataObject::RemoveAllChildren(CContainerNode* pNode)
  2031. {
  2032. // if the node is hidden or not expanded yet, just ignore
  2033. if (!pNode->IsVisible() || !pNode->IsExpanded())
  2034. {
  2035. return S_OK;
  2036. }
  2037. ASSERT(pNode != NULL);
  2038. HSCOPEITEM nID = pNode->GetScopeID();
  2039. ASSERT(nID != 0);
  2040. // remove the container itself
  2041. HRESULT hr = m_pConsoleNameSpace->DeleteItem(nID, /*fDeleteThis*/ FALSE);
  2042. ASSERT(SUCCEEDED(hr));
  2043. DeleteAllResultPaneItems(pNode);
  2044. // remove the result items from all the views (will do only if container selected)
  2045. ASSERT(SUCCEEDED(hr));
  2046. return hr;
  2047. }
  2048. HRESULT CComponentDataObject::RepaintSelectedFolderInResultPane()
  2049. {
  2050. return UpdateAllViewsHelper((long)NULL, REPAINT_RESULT_PANE);
  2051. }
  2052. HRESULT CComponentDataObject::RepaintResultPane(CContainerNode* pNode)
  2053. {
  2054. ASSERT(pNode != NULL);
  2055. return UpdateAllViewsHelper(reinterpret_cast<LONG_PTR>(pNode), REPAINT_RESULT_PANE);
  2056. }
  2057. HRESULT CComponentDataObject::DeleteAllResultPaneItems(CContainerNode* pNode)
  2058. {
  2059. ASSERT(pNode != NULL);
  2060. return UpdateAllViewsHelper(reinterpret_cast<LONG_PTR>(pNode), DELETE_ALL_RESULT_ITEMS);
  2061. }
  2062. HRESULT CComponentDataObject::AddContainerNode(CContainerNode* pNodeToInsert, HSCOPEITEM pParentScopeItem)
  2063. {
  2064. ASSERT(pNodeToInsert != NULL);
  2065. if ((pNodeToInsert != GetRootData()) && (!pNodeToInsert->GetContainer()->IsExpanded()))
  2066. {
  2067. return S_OK;
  2068. }
  2069. //ASSERT(pNodeToInsert->GetScopeID() == 0);
  2070. SCOPEDATAITEM scopeDataItem;
  2071. InitializeScopeDataItem(&scopeDataItem,
  2072. pParentScopeItem,
  2073. reinterpret_cast<LPARAM>(pNodeToInsert), // lParam, use the node pointer as cookie
  2074. pNodeToInsert->GetImageIndex(FALSE), // close image
  2075. pNodeToInsert->GetImageIndex(TRUE), // open image
  2076. pNodeToInsert->HasChildren());
  2077. HRESULT hr = m_pConsoleNameSpace->InsertItem(&scopeDataItem);
  2078. ASSERT(SUCCEEDED(hr));
  2079. if (FAILED(hr))
  2080. {
  2081. return hr;
  2082. }
  2083. // Note - On return, the ID member of 'scopeDataItem'
  2084. // contains the handle to the newly inserted item, so we have to save
  2085. ASSERT(scopeDataItem.ID != NULL);
  2086. pNodeToInsert->SetScopeID(scopeDataItem.ID);
  2087. return hr;
  2088. }
  2089. //
  2090. // Note : This should combined with the function above adding a third parameter that is a compare function,
  2091. // which is NULL by default. If it is NULL then we just skip the GetChildItem() and the while loop.
  2092. //
  2093. HRESULT CComponentDataObject::AddContainerNodeSorted(CContainerNode* pNodeToInsert, HSCOPEITEM pParentScopeItem)
  2094. {
  2095. ASSERT(pNodeToInsert != NULL);
  2096. if ((pNodeToInsert != GetRootData()) && (!pNodeToInsert->GetContainer()->IsExpanded()))
  2097. {
  2098. return S_OK;
  2099. }
  2100. SCOPEDATAITEM scopeDataItem;
  2101. InitializeScopeDataItem(&scopeDataItem,
  2102. pParentScopeItem,
  2103. reinterpret_cast<LPARAM>(pNodeToInsert), // lParam, use the node pointer as cookie
  2104. pNodeToInsert->GetImageIndex(FALSE), // close image
  2105. pNodeToInsert->GetImageIndex(TRUE), // open image
  2106. pNodeToInsert->HasChildren());
  2107. HSCOPEITEM pChildScopeItem;
  2108. CTreeNode* pChildNode = NULL;
  2109. // Enumerate through the scope node items and insert the new node in sorted order
  2110. HRESULT hr = m_pConsoleNameSpace->GetChildItem(pParentScopeItem, &pChildScopeItem, (MMC_COOKIE*)&pChildNode);
  2111. ASSERT(SUCCEEDED(hr));
  2112. if (FAILED(hr))
  2113. {
  2114. return hr;
  2115. }
  2116. while (pChildNode != NULL)
  2117. {
  2118. // REVIEW_JEFFJON : we should probably have a compare function as a parameter and use that here.
  2119. // NOTICE-2002/04/22-artm CTreeNode::GetDisplayName() will never return NULL,
  2120. // okay to use _wcsicoll() here. Underlying implementation is CString object.
  2121. if (_wcsicoll(pNodeToInsert->GetDisplayName(), pChildNode->GetDisplayName()) < 0)
  2122. {
  2123. // Insert the node before the node pointed to by pChildScopeItem
  2124. scopeDataItem.relativeID = pChildScopeItem;
  2125. scopeDataItem.mask |= SDI_NEXT;
  2126. break;
  2127. }
  2128. pChildNode = NULL;
  2129. hr = m_pConsoleNameSpace->GetNextItem(pChildScopeItem, &pChildScopeItem, (MMC_COOKIE*)&pChildNode);
  2130. ASSERT(SUCCEEDED(hr));
  2131. if (FAILED(hr))
  2132. {
  2133. return hr;
  2134. }
  2135. }
  2136. hr = m_pConsoleNameSpace->InsertItem(&scopeDataItem);
  2137. ASSERT(SUCCEEDED(hr));
  2138. if (FAILED(hr))
  2139. {
  2140. return hr;
  2141. }
  2142. // Note - On return, the ID member of 'scopeDataItem'
  2143. // contains the handle to the newly inserted item, so we have to save
  2144. ASSERT(scopeDataItem.ID != NULL);
  2145. pNodeToInsert->SetScopeID(scopeDataItem.ID);
  2146. return hr;
  2147. }
  2148. HRESULT CComponentDataObject::DeleteContainerNode(CContainerNode* pNodeToDelete)
  2149. {
  2150. ASSERT(pNodeToDelete != NULL);
  2151. ASSERT(pNodeToDelete->GetContainer() != NULL);
  2152. HSCOPEITEM nID = pNodeToDelete->GetScopeID();
  2153. ASSERT(nID != 0);
  2154. HRESULT hr = m_pConsoleNameSpace->DeleteItem(nID, /*fDeleteThis*/ TRUE);
  2155. pNodeToDelete->SetScopeID(0);
  2156. return hr;
  2157. }
  2158. HRESULT CComponentDataObject::ChangeContainerNode(CContainerNode* pNodeToChange, long changeMask)
  2159. {
  2160. ASSERT(pNodeToChange != NULL);
  2161. ASSERT(changeMask & CHANGE_RESULT_ITEM);
  2162. ASSERT(m_pConsoleNameSpace != NULL);
  2163. if (!pNodeToChange->AddedToScopePane())
  2164. {
  2165. return S_OK;
  2166. }
  2167. SCOPEDATAITEM scopeDataItem;
  2168. // This is an acceptable usage
  2169. memset(&scopeDataItem, 0, sizeof(SCOPEDATAITEM));
  2170. scopeDataItem.ID = pNodeToChange->GetScopeID();
  2171. ASSERT(scopeDataItem.ID != 0);
  2172. if (changeMask & CHANGE_RESULT_ITEM_DATA)
  2173. {
  2174. scopeDataItem.mask |= SDI_STR;
  2175. scopeDataItem.displayname = MMC_CALLBACK;
  2176. }
  2177. if (changeMask & CHANGE_RESULT_ITEM_ICON)
  2178. {
  2179. scopeDataItem.mask |= SDI_IMAGE;
  2180. scopeDataItem.nImage = pNodeToChange->GetImageIndex(FALSE);
  2181. scopeDataItem.mask |= SDI_OPENIMAGE;
  2182. scopeDataItem.nOpenImage = pNodeToChange->GetImageIndex(TRUE);
  2183. }
  2184. return m_pConsoleNameSpace->SetItem(&scopeDataItem);
  2185. }
  2186. HRESULT CComponentDataObject::AddLeafNode(CLeafNode* pNodeToAdd)
  2187. {
  2188. // will have to broadcast to all views
  2189. ASSERT(pNodeToAdd != NULL);
  2190. return UpdateAllViewsHelper(reinterpret_cast<LONG_PTR>(pNodeToAdd), ADD_RESULT_ITEM);
  2191. }
  2192. HRESULT CComponentDataObject::DeleteLeafNode(CLeafNode* pNodeToDelete)
  2193. {
  2194. // will have to broadcast to all views
  2195. ASSERT(pNodeToDelete != NULL);
  2196. return UpdateAllViewsHelper(reinterpret_cast<LONG_PTR>(pNodeToDelete), DELETE_RESULT_ITEM);
  2197. }
  2198. HRESULT CComponentDataObject::ChangeLeafNode(CLeafNode* pNodeToChange, long changeMask)
  2199. {
  2200. // will have to broadcast to all views
  2201. ASSERT(pNodeToChange != NULL);
  2202. return UpdateAllViewsHelper(reinterpret_cast<LONG_PTR>(pNodeToChange), changeMask);
  2203. }
  2204. HRESULT CComponentDataObject::UpdateVerbState(CTreeNode* pNodeToChange)
  2205. {
  2206. // will have to broadcast to all views
  2207. ASSERT(pNodeToChange != NULL);
  2208. return UpdateAllViewsHelper(reinterpret_cast<LONG_PTR>(pNodeToChange), UPDATE_VERB_STATE);
  2209. }
  2210. HRESULT CComponentDataObject::SetDescriptionBarText(CTreeNode* pTreeNode)
  2211. {
  2212. ASSERT(pTreeNode != NULL);
  2213. return UpdateAllViewsHelper(reinterpret_cast<LONG_PTR>(pTreeNode), UPDATE_DESCRIPTION_BAR);
  2214. }
  2215. HRESULT CComponentDataObject::SortResultPane(CContainerNode* pContainerNode)
  2216. {
  2217. ASSERT(pContainerNode != NULL);
  2218. return UpdateAllViewsHelper(reinterpret_cast<LONG_PTR>(pContainerNode), SORT_RESULT_PANE);
  2219. }
  2220. HRESULT CComponentDataObject::UpdateResultPaneView(CContainerNode* pContainerNode)
  2221. {
  2222. ASSERT(pContainerNode != NULL);
  2223. return UpdateAllViewsHelper(reinterpret_cast<LONG_PTR>(pContainerNode), UPDATE_RESULT_PANE_VIEW);
  2224. }
  2225. void CComponentDataObject::InitializeScopeDataItem(LPSCOPEDATAITEM pScopeDataItem,
  2226. HSCOPEITEM pParentScopeItem, LPARAM lParam,
  2227. int nImage, int nOpenImage, BOOL bHasChildren)
  2228. {
  2229. ASSERT(pScopeDataItem != NULL);
  2230. memset(pScopeDataItem, 0, sizeof(SCOPEDATAITEM));
  2231. // set parent scope item
  2232. pScopeDataItem->mask |= SDI_PARENT;
  2233. pScopeDataItem->relativeID = pParentScopeItem;
  2234. // Add node name, we implement callback
  2235. pScopeDataItem->mask |= SDI_STR;
  2236. pScopeDataItem->displayname = MMC_CALLBACK;
  2237. // Add the lParam
  2238. pScopeDataItem->mask |= SDI_PARAM;
  2239. pScopeDataItem->lParam = lParam;
  2240. // Add close image
  2241. if (nImage != -1)
  2242. {
  2243. pScopeDataItem->mask |= SDI_IMAGE;
  2244. pScopeDataItem->nImage = nImage;
  2245. }
  2246. // Add open image
  2247. if (nOpenImage != -1)
  2248. {
  2249. pScopeDataItem->mask |= SDI_OPENIMAGE;
  2250. pScopeDataItem->nOpenImage = nOpenImage;
  2251. }
  2252. // Add button to node if the folder has children
  2253. if (bHasChildren == TRUE)
  2254. {
  2255. pScopeDataItem->mask |= SDI_CHILDREN;
  2256. pScopeDataItem->cChildren = 1;
  2257. }
  2258. }
  2259. ///////////////////////////////////////////////////////////////////////////////
  2260. // Timer and Background Thread
  2261. BOOL CComponentDataObject::StartTimerThread()
  2262. {
  2263. ASSERT(::IsWindow(m_hWnd));
  2264. m_pTimerThreadObj = OnCreateTimerThread();
  2265. if (m_pTimerThreadObj == NULL)
  2266. {
  2267. return TRUE;
  2268. }
  2269. // start the the thread
  2270. if (!m_pTimerThreadObj->Start(m_hWnd))
  2271. {
  2272. return FALSE;
  2273. }
  2274. ASSERT(m_pTimerThreadObj->m_nThreadID != 0);
  2275. m_nTimerThreadID = m_pTimerThreadObj->m_nThreadID;
  2276. WaitForTimerThreadStartAck();
  2277. return SetTimer();
  2278. }
  2279. void CComponentDataObject::ShutDownTimerThread()
  2280. {
  2281. KillTimer();
  2282. PostMessageToTimerThread(WM_QUIT, 0,0);
  2283. //
  2284. // Wait for the thread to die or else we could AV since there may be more
  2285. // messages in the queue than just the WM_QUIT
  2286. //
  2287. if (m_pTimerThreadObj != NULL)
  2288. {
  2289. DWORD dwRetState = ::WaitForSingleObject(m_pTimerThreadObj->m_hThread,INFINITE);
  2290. ASSERT(dwRetState != WAIT_FAILED);
  2291. }
  2292. //
  2293. // Threads now gone, delete the thread object
  2294. //
  2295. delete m_pTimerThreadObj;
  2296. m_pTimerThreadObj = NULL;
  2297. }
  2298. BOOL CComponentDataObject::PostMessageToTimerThread(UINT Msg, WPARAM wParam, LPARAM lParam)
  2299. {
  2300. if (m_nTimerThreadID != 0)
  2301. {
  2302. return ::PostThreadMessage(m_nTimerThreadID, Msg, wParam, lParam);
  2303. }
  2304. return TRUE;
  2305. }
  2306. BOOL CComponentDataObject::SetTimer()
  2307. {
  2308. ASSERT(::IsWindow(m_hWnd));
  2309. ASSERT(m_hiddenWnd.m_nTimerID == 0);
  2310. m_dwTimerTime = 0;
  2311. DWORD dwTimerIntervalMillisec = m_dwTimerInterval*1000;
  2312. m_hiddenWnd.m_nTimerID = m_hiddenWnd.SetTimer(1, dwTimerIntervalMillisec);
  2313. return (m_hiddenWnd.m_nTimerID != 0);
  2314. }
  2315. void CComponentDataObject::KillTimer()
  2316. {
  2317. ASSERT(::IsWindow(m_hWnd));
  2318. if (m_hiddenWnd.m_nTimerID != 0)
  2319. {
  2320. VERIFY(m_hiddenWnd.KillTimer(static_cast<UINT>(m_hiddenWnd.m_nTimerID)));
  2321. m_hiddenWnd.m_nTimerID = 0;
  2322. }
  2323. }
  2324. void CComponentDataObject::WaitForTimerThreadStartAck()
  2325. {
  2326. MSG tempMSG;
  2327. ASSERT(!m_bTimerThreadStarted);
  2328. while(!m_bTimerThreadStarted)
  2329. {
  2330. if (::PeekMessage(&tempMSG,m_hWnd,CHiddenWnd::s_TimerThreadMessage,
  2331. CHiddenWnd::s_TimerThreadMessage,
  2332. PM_REMOVE))
  2333. {
  2334. DispatchMessage(&tempMSG);
  2335. }
  2336. }
  2337. }
  2338. void CComponentDataObject::WaitForThreadExitMessage(CMTContainerNode* pNode)
  2339. {
  2340. MSG tempMSG;
  2341. while(GetRunningThreadTable()->IsPresent(pNode))
  2342. {
  2343. if (::PeekMessage(&tempMSG,
  2344. m_hiddenWnd.m_hWnd,
  2345. CHiddenWnd::s_NodeThreadHaveDataNotificationMessage,
  2346. CHiddenWnd::s_NodeThreadExitingNotificationMessage,
  2347. PM_REMOVE))
  2348. {
  2349. DispatchMessage(&tempMSG);
  2350. }
  2351. } // while
  2352. }
  2353. ///////////////////////////////////////////////////////////////////////////////
  2354. // CComponentObject implementation
  2355. ///////////////////////////////////////////////////////////////////////////////
  2356. #ifdef _DEBUG_REFCOUNT
  2357. unsigned int CComponentObject::m_nOustandingObjects = 0;
  2358. #endif // _DEBUG_REFCOUNT
  2359. CComponentObject::CComponentObject()
  2360. {
  2361. #ifdef _DEBUG_REFCOUNT
  2362. dbg_cRef = 0;
  2363. ++m_nOustandingObjects;
  2364. TRACE(_T("CComponentObject(), count = %d\n"),m_nOustandingObjects);
  2365. #endif // _DEBUG_REFCOUNT
  2366. Construct();
  2367. }
  2368. CComponentObject::~CComponentObject()
  2369. {
  2370. #ifdef _DEBUG_REFCOUNT
  2371. --m_nOustandingObjects;
  2372. TRACE(_T("~CComponentObject(), count = %d\n"),m_nOustandingObjects);
  2373. #endif // _DEBUG_REFCOUNT
  2374. // Make sure the interfaces have been released
  2375. ASSERT(m_pConsole == NULL);
  2376. ASSERT(m_pHeader == NULL);
  2377. //SAFE_RELEASE(m_pComponentData); // QI'ed in IComponentDataImpl::CreateComponent
  2378. if (m_pComponentData != NULL)
  2379. {
  2380. m_pComponentData->Release();
  2381. m_pComponentData = NULL;
  2382. TRACE(_T("~CComponentObject() released m_pCompomentData\n"));
  2383. }
  2384. Construct();
  2385. }
  2386. void CComponentObject::Construct()
  2387. {
  2388. m_pConsole = NULL;
  2389. m_pHeader = NULL;
  2390. m_pResult = NULL;
  2391. m_pImageResult = NULL;
  2392. m_pComponentData = NULL;
  2393. m_pToolbar = NULL;
  2394. m_pControlbar = NULL;
  2395. m_pConsoleVerb = NULL;
  2396. m_pSelectedContainerNode = NULL;
  2397. m_pSelectedNode = NULL;
  2398. m_selectedType = CCT_UNINITIALIZED;
  2399. }
  2400. ///////////////////////////////////////////////////////////////////////////////
  2401. // CComponentObject::IComponent members
  2402. STDMETHODIMP CComponentObject::Initialize(LPCONSOLE lpConsole)
  2403. {
  2404. ASSERT(lpConsole != NULL);
  2405. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  2406. // Save the IConsole pointer
  2407. m_pConsole = lpConsole;
  2408. m_pConsole->AddRef();
  2409. // QI for a IHeaderCtrl
  2410. HRESULT hr = m_pConsole->QueryInterface(IID_IHeaderCtrl,
  2411. reinterpret_cast<void**>(&m_pHeader));
  2412. // Give the console the header control interface pointer
  2413. if (SUCCEEDED(hr))
  2414. {
  2415. m_pConsole->SetHeader(m_pHeader);
  2416. }
  2417. m_pConsole->QueryInterface(IID_IResultData,
  2418. reinterpret_cast<void**>(&m_pResult));
  2419. hr = m_pConsole->QueryResultImageList(&m_pImageResult);
  2420. ASSERT(hr == S_OK);
  2421. hr = m_pConsole->QueryConsoleVerb(&m_pConsoleVerb);
  2422. ASSERT(hr == S_OK);
  2423. return S_OK;
  2424. }
  2425. STDMETHODIMP CComponentObject::Notify(LPDATAOBJECT lpDataObject, MMC_NOTIFY_TYPE event, LPARAM arg, LPARAM param)
  2426. {
  2427. HRESULT hr = S_OK;
  2428. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  2429. if (event == MMCN_PROPERTY_CHANGE)
  2430. {
  2431. ASSERT(lpDataObject == NULL);
  2432. hr = OnPropertyChange(param, static_cast<ULONG>(arg));
  2433. }
  2434. else if (event == MMCN_VIEW_CHANGE)
  2435. {
  2436. hr = OnUpdateView(lpDataObject,arg,param);
  2437. }
  2438. else if (event == MMCN_DESELECT_ALL)
  2439. {
  2440. TRACE(_T("CComponentObject::Notify -> MMCN_DESELECT_ALL \n"));
  2441. }
  2442. else if (event == MMCN_COLUMN_CLICK)
  2443. {
  2444. OnColumnSortChanged(arg, param);
  2445. }
  2446. else if (event == MMCN_CUTORMOVE)
  2447. {
  2448. hr = S_FALSE;
  2449. }
  2450. else if (lpDataObject != NULL)
  2451. {
  2452. CInternalFormatCracker ifc;
  2453. ifc.Extract(lpDataObject);
  2454. if (ifc.GetCookieCount() < 1)
  2455. {
  2456. CComponentDataObject* pComponentDataObject = (CComponentDataObject*)m_pComponentData;
  2457. if ( (event == MMCN_ADD_IMAGES) && pComponentDataObject->IsExtensionSnapin() )
  2458. {
  2459. CTreeNode* pTreeNode = pComponentDataObject->GetRootData();
  2460. return InitializeBitmaps(pTreeNode); // cookie for the root
  2461. }
  2462. return S_OK;
  2463. }
  2464. switch(event)
  2465. {
  2466. case MMCN_ACTIVATE:
  2467. break;
  2468. case MMCN_CLICK:
  2469. OnResultItemClk(ifc, FALSE);
  2470. break;
  2471. case MMCN_DBLCLICK:
  2472. hr = S_FALSE;
  2473. break;
  2474. case MMCN_ADD_IMAGES:
  2475. OnAddImages(ifc, arg, param);
  2476. break;
  2477. case MMCN_SHOW:
  2478. hr = OnShow(ifc, arg, param);
  2479. break;
  2480. case MMCN_COLUMNS_CHANGED:
  2481. hr = OnColumnsChanged(ifc, arg, param);
  2482. break;
  2483. case MMCN_MINIMIZED:
  2484. hr = OnMinimize(ifc, arg, param);
  2485. break;
  2486. case MMCN_SELECT:
  2487. HandleStandardVerbs( (BOOL) LOWORD(arg)/*bScope*/,
  2488. (BOOL) HIWORD(arg)/*bSelect*/,lpDataObject);
  2489. break;
  2490. case MMCN_QUERY_PASTE:
  2491. hr = S_FALSE;
  2492. break;
  2493. case MMCN_PASTE:
  2494. AfxMessageBox(_T("CComponentObject::MMCN_PASTE"));
  2495. break;
  2496. case MMCN_DELETE:
  2497. // just delegate to the component data object
  2498. hr = ((CComponentDataObject*)m_pComponentData)->OnDeleteVerbHandler(
  2499. ifc, this);
  2500. break;
  2501. case MMCN_REFRESH:
  2502. // just delegate to the component data object
  2503. hr = ((CComponentDataObject*)m_pComponentData)->OnRefreshVerbHandler(
  2504. ifc);
  2505. //
  2506. // Once the refresh has begun, update the verbs associated with the
  2507. // object being refreshed.
  2508. //
  2509. HandleStandardVerbs( (BOOL) LOWORD(arg)/*bScope*/,
  2510. (BOOL) HIWORD(arg)/*bSelect*/,lpDataObject);
  2511. break;
  2512. case MMCN_RENAME:
  2513. // just delegate to the component data object
  2514. hr = ((CComponentDataObject*)m_pComponentData)->OnRename(ifc, arg, param);
  2515. break;
  2516. case MMCN_CONTEXTHELP:
  2517. // just delegate to the component data object
  2518. hr = ((CComponentDataObject*)m_pComponentData)->OnHelpHandler(ifc, this);
  2519. break;
  2520. default:
  2521. hr = E_UNEXPECTED;
  2522. break;
  2523. }
  2524. }
  2525. return hr;
  2526. }
  2527. STDMETHODIMP CComponentObject::Destroy(MMC_COOKIE)
  2528. {
  2529. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  2530. //
  2531. // Release the interfaces that we QI'ed
  2532. //
  2533. if (m_pConsole != NULL)
  2534. {
  2535. //
  2536. // Tell the console to release the header control interface
  2537. //
  2538. m_pConsole->SetHeader(NULL);
  2539. SAFE_RELEASE(m_pHeader);
  2540. SAFE_RELEASE(m_pToolbar);
  2541. SAFE_RELEASE(m_pControlbar);
  2542. SAFE_RELEASE(m_pResult);
  2543. SAFE_RELEASE(m_pImageResult);
  2544. SAFE_RELEASE(m_pConsoleVerb);
  2545. // Release the IConsole interface last
  2546. SAFE_RELEASE(m_pConsole);
  2547. }
  2548. return S_OK;
  2549. }
  2550. STDMETHODIMP CComponentObject::GetResultViewType(MMC_COOKIE cookie, LPOLESTR* ppViewType,
  2551. long* pViewOptions)
  2552. {
  2553. CTreeNode* pNode;
  2554. if (cookie == NULL)
  2555. {
  2556. pNode = ((CComponentDataObject*)m_pComponentData)->GetRootData();
  2557. }
  2558. else
  2559. {
  2560. pNode = reinterpret_cast<CTreeNode*>(cookie);
  2561. }
  2562. ASSERT(pNode != NULL);
  2563. if (pNode != NULL)
  2564. {
  2565. return pNode->GetResultViewType((CComponentDataObject*)m_pComponentData,
  2566. ppViewType,
  2567. pViewOptions);
  2568. }
  2569. // Use default view
  2570. if (((CComponentDataObject*)m_pComponentData)->IsMultiSelect())
  2571. {
  2572. *pViewOptions = MMC_VIEW_OPTIONS_MULTISELECT;
  2573. }
  2574. else
  2575. {
  2576. *pViewOptions = MMC_VIEW_OPTIONS_NONE;
  2577. }
  2578. *ppViewType = NULL;
  2579. return S_FALSE;
  2580. }
  2581. STDMETHODIMP CComponentObject::QueryDataObject(MMC_COOKIE cookie, DATA_OBJECT_TYPES type,
  2582. LPDATAOBJECT* ppDataObject)
  2583. {
  2584. HRESULT hr = S_OK;
  2585. ASSERT(ppDataObject != NULL);
  2586. CComObject<CDataObject>* pObject;
  2587. CComObject<CDataObject>::CreateInstance(&pObject);
  2588. ASSERT(pObject != NULL);
  2589. if (pObject != NULL)
  2590. {
  2591. CTreeNode* pNode = NULL;
  2592. if (cookie == MMC_MULTI_SELECT_COOKIE)
  2593. {
  2594. TRACE(_T("CDSEvent::GetDataObject() - multi-select.\n"));
  2595. RESULTDATAITEM rdi;
  2596. // This is an acceptable usage
  2597. ZeroMemory(&rdi, sizeof(rdi));
  2598. rdi.mask = RDI_STATE;
  2599. rdi.nIndex = -1;
  2600. rdi.nState = LVIS_SELECTED;
  2601. do
  2602. {
  2603. rdi.lParam = 0;
  2604. ASSERT(rdi.mask == RDI_STATE);
  2605. ASSERT(rdi.nState == LVIS_SELECTED);
  2606. hr = m_pResult->GetNextItem(&rdi);
  2607. if (hr != S_OK)
  2608. break;
  2609. pNode = reinterpret_cast<CTreeNode*>(rdi.lParam);
  2610. pObject->AddCookie(pNode);
  2611. } while (1);
  2612. // addref() the new pointer and return it.
  2613. pObject->AddRef();
  2614. *ppDataObject = pObject;
  2615. }
  2616. else
  2617. {
  2618. // Delegate it to the IComponentData implementation
  2619. ASSERT(m_pComponentData != NULL);
  2620. hr = m_pComponentData->QueryDataObject(cookie, type, ppDataObject);
  2621. }
  2622. }
  2623. return hr;
  2624. }
  2625. STDMETHODIMP CComponentObject::GetDisplayInfo(LPRESULTDATAITEM pResultDataItem)
  2626. {
  2627. ASSERT(pResultDataItem != NULL);
  2628. if (pResultDataItem)
  2629. {
  2630. CTreeNode* pNode = reinterpret_cast<CTreeNode*>(pResultDataItem->lParam);
  2631. ASSERT(pNode != NULL);
  2632. if (pNode)
  2633. {
  2634. ASSERT(pResultDataItem->bScopeItem == pNode->IsContainer());
  2635. if (pResultDataItem->mask & RDI_STR)
  2636. {
  2637. LPCWSTR lpszString = pNode->GetString(pResultDataItem->nCol);
  2638. if (lpszString != NULL)
  2639. {
  2640. pResultDataItem->str = (LPWSTR)lpszString;
  2641. }
  2642. }
  2643. if ((pResultDataItem->mask & RDI_IMAGE) && (pResultDataItem->nCol == 0))
  2644. {
  2645. pResultDataItem->nImage = pNode->GetImageIndex(FALSE);
  2646. }
  2647. }
  2648. }
  2649. return S_OK;
  2650. }
  2651. STDMETHODIMP CComponentObject::CompareObjects(LPDATAOBJECT lpDataObjectA, LPDATAOBJECT lpDataObjectB)
  2652. {
  2653. // Delegate it to the IComponentData implementation
  2654. ASSERT(m_pComponentData != NULL);
  2655. return m_pComponentData->CompareObjects(lpDataObjectA, lpDataObjectB);
  2656. }
  2657. ///////////////////////////////////////////////////////////////////////////////
  2658. // Message handlers for CComponentObject::IComponent::Notify()
  2659. HRESULT CComponentObject::OnFolder(CTreeNode*, LPARAM, LPARAM)
  2660. {
  2661. ASSERT(FALSE);
  2662. return S_OK;
  2663. }
  2664. HRESULT CComponentObject::OnShow(CInternalFormatCracker& ifc, LPARAM arg, LPARAM)
  2665. {
  2666. HRESULT hr = S_OK;
  2667. ASSERT(ifc.GetCookieCount() == 1);
  2668. //
  2669. // I shouldn't have to deal with multiple select here
  2670. //
  2671. CTreeNode* pNode = ifc.GetCookieAt(0);
  2672. ASSERT(pNode != NULL);
  2673. if (!pNode)
  2674. {
  2675. // NTRAID#NTBUG9-657633-2002/07/11-sburns
  2676. return E_FAIL;
  2677. }
  2678. ASSERT(pNode->IsContainer());
  2679. CContainerNode* pContainerNode = (CContainerNode*)pNode;
  2680. // Note - arg is TRUE when it is time to enumerate
  2681. if (arg == TRUE)
  2682. {
  2683. long lResultView;
  2684. LPOLESTR lpoleResultView = NULL;
  2685. pNode->GetResultViewType((CComponentDataObject*)m_pComponentData,
  2686. &lpoleResultView,
  2687. &lResultView);
  2688. if (lResultView == MMC_VIEW_OPTIONS_NONE || lResultView == MMC_VIEW_OPTIONS_MULTISELECT)
  2689. {
  2690. // Show the headers for this nodetype
  2691. InitializeHeaders(pContainerNode);
  2692. EnumerateResultPane(pContainerNode);
  2693. m_pSelectedContainerNode = pContainerNode;
  2694. SetDescriptionBarText(pContainerNode);
  2695. }
  2696. else
  2697. {
  2698. m_pSelectedContainerNode = pContainerNode;
  2699. hr = pNode->OnShow(m_pConsole);
  2700. }
  2701. }
  2702. else
  2703. {
  2704. // Removed by JEFFJON : new column header implementation
  2705. // if we want we can notify ourselves that the focus is being lost
  2706. // SaveHeadersInfo(pContainerNode);
  2707. m_pSelectedContainerNode = NULL;
  2708. // Free data associated with the result pane items, because
  2709. // your node is no longer being displayed.
  2710. // Note: The console will remove the items from the result pane
  2711. }
  2712. #ifdef _DEBUG
  2713. if (m_pSelectedContainerNode == NULL)
  2714. TRACE(_T("NULL selection\n"));
  2715. else
  2716. TRACE(_T("Node <%s> selected\n"), m_pSelectedContainerNode->GetDisplayName());
  2717. #endif
  2718. return hr;
  2719. }
  2720. HRESULT CComponentObject::OnColumnsChanged(CInternalFormatCracker& ifc, LPARAM, LPARAM param)
  2721. {
  2722. CTreeNode* pNode = ifc.GetCookieAt(0);
  2723. ASSERT(pNode != NULL);
  2724. ASSERT(pNode->IsContainer());
  2725. CContainerNode* pContainerNode = (CContainerNode*)pNode;
  2726. MMC_VISIBLE_COLUMNS* pVisibleCols = reinterpret_cast<MMC_VISIBLE_COLUMNS*>(param);
  2727. pContainerNode->OnColumnsChanged(pVisibleCols->rgVisibleCols, pVisibleCols->nVisibleColumns);
  2728. return S_OK;
  2729. }
  2730. HRESULT CComponentObject::OnColumnSortChanged(LPARAM, LPARAM)
  2731. {
  2732. return S_OK;
  2733. }
  2734. HRESULT CComponentObject::ForceSort(UINT iCol, DWORD dwDirection)
  2735. {
  2736. HRESULT hr = m_pResult->Sort(iCol, dwDirection, NULL);
  2737. return hr;
  2738. }
  2739. HRESULT CComponentObject::OnActivate(CTreeNode*, LPARAM, LPARAM)
  2740. {
  2741. ASSERT(FALSE);
  2742. return S_OK;
  2743. }
  2744. HRESULT CComponentObject::OnResultItemClk(CInternalFormatCracker&, BOOL)
  2745. {
  2746. return S_OK;
  2747. }
  2748. HRESULT CComponentObject::OnMinimize(CInternalFormatCracker&, LPARAM, LPARAM)
  2749. {
  2750. return S_OK;
  2751. }
  2752. HRESULT CComponentObject::OnPropertyChange(LPARAM param, long fScopePane)
  2753. {
  2754. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  2755. ASSERT(param != NULL);
  2756. #ifdef _DEBUG
  2757. TRACE(_T("CComponentObject::OnPropertyChange()\n"));
  2758. CPropertyPageHolderBase* pPPHolder = reinterpret_cast<CPropertyPageHolderBase*>(param);
  2759. ASSERT(pPPHolder != NULL);
  2760. CTreeNode* pNode = pPPHolder->GetTreeNode();
  2761. ASSERT(pNode != NULL);
  2762. // the item must be a result item and in the result pane
  2763. ASSERT(!fScopePane);
  2764. #endif
  2765. // we delegate the call to the IComponentData implementation
  2766. CComponentDataObject* pComponentDataObject = (CComponentDataObject*)m_pComponentData;
  2767. ASSERT(pComponentDataObject != NULL);
  2768. return pComponentDataObject->OnPropertyChange(param, fScopePane);
  2769. }
  2770. HRESULT CComponentObject::OnUpdateView(LPDATAOBJECT, LPARAM data, LONG_PTR hint)
  2771. {
  2772. if (m_pSelectedContainerNode == NULL)
  2773. {
  2774. return S_OK; // no selection for our IComponentData
  2775. }
  2776. if (hint == DELETE_ALL_RESULT_ITEMS)
  2777. {
  2778. // data contains the container whose result pane has to be refreshed
  2779. CContainerNode* pNode = reinterpret_cast<CContainerNode*>(data);
  2780. ASSERT(pNode != NULL);
  2781. // do it only if selected and we are using the standard list view,
  2782. // if not, reselecting will do a delete/enumeration
  2783. long lResultView;
  2784. LPOLESTR lpoleResultView = NULL;
  2785. pNode->GetResultViewType((CComponentDataObject*)m_pComponentData,
  2786. &lpoleResultView,
  2787. &lResultView);
  2788. if (m_pSelectedContainerNode == pNode &&
  2789. (lResultView == MMC_VIEW_OPTIONS_NONE || lResultView == MMC_VIEW_OPTIONS_MULTISELECT))
  2790. {
  2791. ASSERT(m_pResult != NULL);
  2792. VERIFY(SUCCEEDED(m_pResult->DeleteAllRsltItems()));
  2793. SetDescriptionBarText(pNode);
  2794. }
  2795. }
  2796. else if (hint == SORT_RESULT_PANE)
  2797. {
  2798. // data contains the container whose result pane has to be refreshed
  2799. CContainerNode* pNode = reinterpret_cast<CContainerNode*>(data);
  2800. ASSERT(pNode != NULL);
  2801. // do it only if selected, if not, reselecting will do a delete/enumeration
  2802. if (m_pSelectedContainerNode == pNode)
  2803. {
  2804. MMC_SORT_SET_DATA* pColumnSortData = NULL;
  2805. // build the column id
  2806. LPCWSTR lpszColumnID = pNode->GetColumnID();
  2807. // We are assuming the columnID is NULL terminated. Since this is usually
  2808. // hardcoded and there is no good way to verify that it is NULL terminated
  2809. // this usage should be fine.
  2810. size_t iLen = wcslen(lpszColumnID);
  2811. iLen += 1; // Include space for the null.
  2812. // allocate memory for the struct and add on enough to make the byte[1] into a string
  2813. // for the column id
  2814. // Add 1 to the length to include space for NULL.
  2815. size_t arraySizeInBytes = sizeof(SColumnSetID) + (iLen * sizeof(WCHAR));
  2816. SColumnSetID* pColumnID = (SColumnSetID*)malloc(arraySizeInBytes);
  2817. if (!pColumnID)
  2818. {
  2819. return S_OK;
  2820. }
  2821. // This is an acceptable usage
  2822. memset(pColumnID, 0, arraySizeInBytes);
  2823. pColumnID->cBytes = static_cast<DWORD>(iLen * sizeof(WCHAR));
  2824. // NOTICE-2002/04/18-artm Part of fix for ntraid#ntbug9-540061.
  2825. // Unlike wcscpy(), StringCchCopy() will ensure that the destination
  2826. // buffer is null terminated and report an error code if there was
  2827. // a truncation (won't overrun the destination buffer).
  2828. //
  2829. // Since we needed to use strsafe.h elsewhere in this file, I decided
  2830. // to replace these dangerous wcscpy() uses that were deprecated by
  2831. // strsafe.h.
  2832. HRESULT err;
  2833. err = StringCchCopyW(
  2834. reinterpret_cast<LPWSTR>(pColumnID->id), // destination string
  2835. iLen, // size of destination string (including null)
  2836. lpszColumnID); // source string
  2837. if (FAILED(err))
  2838. {
  2839. ASSERT(false); // This should never happen.
  2840. // Even though there was an error we return S_OK here since that
  2841. // is the behavior used above for out of memory failure.
  2842. free(pColumnID);
  2843. return S_OK;
  2844. }
  2845. // Get the sort column and direction
  2846. IColumnData* pColumnData = NULL;
  2847. HRESULT hr = m_pConsole->QueryInterface(IID_IColumnData, reinterpret_cast<void**>(&pColumnData));
  2848. if (pColumnData != NULL)
  2849. hr = pColumnData->GetColumnSortData(pColumnID, &pColumnSortData);
  2850. if (SUCCEEDED(hr))
  2851. {
  2852. if (pColumnSortData != NULL)
  2853. {
  2854. UINT iCurrentSortColumn = pColumnSortData->pSortData->nColIndex;
  2855. DWORD dwCurrentSortDirection = pColumnSortData->pSortData->dwSortOptions;
  2856. VERIFY(SUCCEEDED(ForceSort(iCurrentSortColumn, dwCurrentSortDirection)));
  2857. CoTaskMemFree(pColumnSortData);
  2858. }
  2859. }
  2860. if (pColumnData != NULL)
  2861. pColumnData->Release();
  2862. free(pColumnID);
  2863. }
  2864. }
  2865. else if (hint == REPAINT_RESULT_PANE)
  2866. {
  2867. // data contains the container whose result pane has to be refreshed
  2868. CContainerNode* pNode = reinterpret_cast<CContainerNode*>(data);
  2869. if (pNode == NULL)
  2870. pNode = m_pSelectedContainerNode; // passing NULL means apply to the current selection
  2871. // update all the leaf nodes in the result pane
  2872. CNodeList* pChildList = ((CContainerNode*)pNode)->GetLeafChildList();
  2873. for( POSITION pos = pChildList->GetHeadPosition(); pos != NULL; )
  2874. {
  2875. CLeafNode* pCurrentChild = (CLeafNode*)pChildList->GetNext(pos);
  2876. ChangeResultPaneItem(pCurrentChild,CHANGE_RESULT_ITEM);
  2877. }
  2878. }
  2879. else if ( hint == DELETE_MULTIPLE_RESULT_ITEMS)
  2880. {
  2881. CNodeList* pNodeList = reinterpret_cast<CNodeList*>(data);
  2882. ASSERT(pNodeList != NULL);
  2883. POSITION pos = pNodeList->GetHeadPosition();
  2884. while (pos != NULL)
  2885. {
  2886. CTreeNode* pNode = pNodeList->GetNext(pos);
  2887. ASSERT(pNode != NULL);
  2888. if (!pNode->IsContainer())
  2889. {
  2890. DeleteResultPaneItem(static_cast<CLeafNode*>(pNode));
  2891. }
  2892. }
  2893. SetDescriptionBarText(pNodeList->GetHead()->GetContainer());
  2894. }
  2895. else if ( (hint == ADD_RESULT_ITEM) || (hint == DELETE_RESULT_ITEM) || (hint & CHANGE_RESULT_ITEM))
  2896. {
  2897. // we deal with a leaf node
  2898. CLeafNode* pNode = reinterpret_cast<CLeafNode*>(data);
  2899. ASSERT(pNode != NULL);
  2900. // consider only if the parent is selected, otherwise will enumerate later when selected
  2901. if (m_pSelectedContainerNode == pNode->GetContainer())
  2902. {
  2903. if (hint & CHANGE_RESULT_ITEM)
  2904. {
  2905. ChangeResultPaneItem(pNode,hint);
  2906. }
  2907. else if ( hint == ADD_RESULT_ITEM)
  2908. {
  2909. AddResultPaneItem(pNode);
  2910. SetDescriptionBarText(pNode);
  2911. }
  2912. else if ( hint == DELETE_RESULT_ITEM)
  2913. {
  2914. DeleteResultPaneItem(pNode);
  2915. SetDescriptionBarText(pNode);
  2916. }
  2917. }
  2918. }
  2919. else if (hint == UPDATE_VERB_STATE)
  2920. {
  2921. CTreeNode* pTreeNode = reinterpret_cast<CTreeNode*>(data);
  2922. ASSERT(pTreeNode != NULL);
  2923. if (m_pSelectedNode == pTreeNode)
  2924. {
  2925. ASSERT(m_selectedType != CCT_UNINITIALIZED);
  2926. CNodeList nodeList;
  2927. nodeList.AddTail(pTreeNode);
  2928. m_pConsoleVerb->SetDefaultVerb(pTreeNode->GetDefaultVerb(m_selectedType, &nodeList));
  2929. pTreeNode->OnSetVerbState(m_pConsoleVerb, m_selectedType, &nodeList);
  2930. }
  2931. }
  2932. else if (hint == UPDATE_DESCRIPTION_BAR)
  2933. {
  2934. CTreeNode* pTreeNode = reinterpret_cast<CTreeNode*>(data);
  2935. ASSERT(pTreeNode != NULL);
  2936. SetDescriptionBarText(pTreeNode);
  2937. }
  2938. else if (hint == UPDATE_RESULT_PANE_VIEW)
  2939. {
  2940. CContainerNode* pNode = reinterpret_cast<CContainerNode*>(data);
  2941. ASSERT(pNode != NULL);
  2942. HSCOPEITEM hScopeID = pNode->GetScopeID();
  2943. if (hScopeID != 0)
  2944. {
  2945. m_pConsole->SelectScopeItem(hScopeID);
  2946. }
  2947. }
  2948. return S_OK;
  2949. }
  2950. HRESULT CComponentObject::SetDescriptionBarText(CTreeNode* pTreeNode)
  2951. {
  2952. ASSERT(pTreeNode != NULL);
  2953. HRESULT hr = S_OK;
  2954. if (m_pSelectedContainerNode == pTreeNode)
  2955. {
  2956. LPWSTR lpszText = pTreeNode->GetDescriptionBarText();
  2957. hr = m_pResult->SetDescBarText(lpszText);
  2958. }
  2959. else if (m_pSelectedContainerNode == pTreeNode->GetContainer())
  2960. {
  2961. LPWSTR lpszText = pTreeNode->GetContainer()->GetDescriptionBarText();
  2962. hr = m_pResult->SetDescBarText(lpszText);
  2963. }
  2964. return hr;
  2965. }
  2966. HRESULT CComponentObject::OnAddImages(CInternalFormatCracker& ifc, LPARAM, LPARAM)
  2967. {
  2968. CTreeNode* pNode = ifc.GetCookieAt(0);
  2969. ASSERT(pNode != NULL);
  2970. return InitializeBitmaps(pNode);
  2971. }
  2972. void CComponentObject::HandleStandardVerbs(BOOL bScope, BOOL bSelect, LPDATAOBJECT lpDataObject)
  2973. {
  2974. if (lpDataObject == NULL)
  2975. {
  2976. return;
  2977. }
  2978. ((CComponentDataObject*)m_pComponentData)->HandleStandardVerbsHelper(
  2979. this, m_pConsoleVerb, bScope, bSelect, lpDataObject);
  2980. }
  2981. void CComponentObject::EnumerateResultPane(CContainerNode* pContainerNode)
  2982. {
  2983. ASSERT(m_pResult != NULL); // make sure we QI'ed for the interfaces
  2984. ASSERT(m_pComponentData != NULL);
  2985. ASSERT(pContainerNode != NULL);
  2986. //
  2987. // get the list of children
  2988. // subfolders already added by console, add only the leaf nodes
  2989. //
  2990. CNodeList* pChildList = pContainerNode->GetLeafChildList();
  2991. ASSERT(pChildList != NULL);
  2992. POSITION pos;
  2993. for( pos = pChildList->GetHeadPosition(); pos != NULL; )
  2994. {
  2995. CTreeNode* pCurrChildNode = pChildList->GetNext(pos);
  2996. ASSERT(pCurrChildNode != NULL);
  2997. if(pCurrChildNode->IsVisible())
  2998. {
  2999. VERIFY(SUCCEEDED(AddResultPaneItem((CLeafNode*)pCurrChildNode)));
  3000. }
  3001. }
  3002. }
  3003. HRESULT CComponentObject::AddResultPaneItem(CLeafNode* pNodeToInsert)
  3004. {
  3005. ASSERT(m_pResult != NULL);
  3006. ASSERT(pNodeToInsert != NULL);
  3007. RESULTDATAITEM resultItem;
  3008. // This is an acceptable usage
  3009. memset(&resultItem, 0, sizeof(RESULTDATAITEM));
  3010. resultItem.mask = RDI_STR | RDI_IMAGE | RDI_PARAM;
  3011. resultItem.str = MMC_CALLBACK;
  3012. //use close image index on result pane
  3013. resultItem.nImage = pNodeToInsert->GetImageIndex(FALSE);
  3014. resultItem.lParam = reinterpret_cast<LPARAM>(pNodeToInsert);
  3015. return m_pResult->InsertItem(&resultItem);
  3016. }
  3017. HRESULT CComponentObject::DeleteResultPaneItem(CLeafNode* pNodeToDelete)
  3018. {
  3019. ASSERT(m_pResult != NULL);
  3020. ASSERT(pNodeToDelete != NULL);
  3021. RESULTDATAITEM resultItem;
  3022. // This is an acceptable usage
  3023. memset(&resultItem, 0, sizeof(RESULTDATAITEM));
  3024. HRESULTITEM itemID;
  3025. HRESULT hr = m_pResult->FindItemByLParam(reinterpret_cast<LPARAM>(pNodeToDelete), &itemID);
  3026. ASSERT(SUCCEEDED(hr));
  3027. if (FAILED(hr))
  3028. {
  3029. return hr;
  3030. }
  3031. return m_pResult->DeleteItem(itemID,0 /* all cols */);
  3032. }
  3033. HRESULT CComponentObject::ChangeResultPaneItem(CLeafNode* pNodeToChange, LONG_PTR changeMask)
  3034. {
  3035. ASSERT(changeMask & CHANGE_RESULT_ITEM);
  3036. ASSERT(m_pResult != NULL);
  3037. ASSERT(pNodeToChange != NULL);
  3038. HRESULTITEM itemID;
  3039. HRESULT hr = m_pResult->FindItemByLParam(reinterpret_cast<LPARAM>(pNodeToChange), &itemID);
  3040. ASSERT(SUCCEEDED(hr));
  3041. if (FAILED(hr))
  3042. {
  3043. return hr;
  3044. }
  3045. RESULTDATAITEM resultItem;
  3046. // This is an acceptable usage
  3047. memset(&resultItem, 0, sizeof(RESULTDATAITEM));
  3048. resultItem.itemID = itemID;
  3049. if (changeMask & CHANGE_RESULT_ITEM_DATA)
  3050. {
  3051. //
  3052. // UpdateItem() alone does not allow the
  3053. // item string buffer to grow and you get "foo..." when
  3054. // "foo" changes to "foobar" the first time (buffer grows)
  3055. //
  3056. resultItem.mask |= RDI_STR;
  3057. resultItem.str = MMC_CALLBACK;
  3058. //
  3059. // this line asserts, use the one above ask Tony
  3060. //
  3061. //resultItem.str = (LPWSTR)pNodeToChange->GetDisplayName();
  3062. }
  3063. if (changeMask & CHANGE_RESULT_ITEM_ICON)
  3064. {
  3065. resultItem.mask |= RDI_IMAGE;
  3066. resultItem.nImage = pNodeToChange->GetImageIndex(FALSE);
  3067. }
  3068. hr = m_pResult->SetItem(&resultItem);
  3069. ASSERT(SUCCEEDED(hr));
  3070. hr = m_pResult->UpdateItem(itemID);
  3071. ASSERT(SUCCEEDED(hr));
  3072. return hr;
  3073. }
  3074. HRESULT CComponentObject::FindResultPaneItemID(CLeafNode* pNode, HRESULTITEM*)
  3075. {
  3076. ASSERT(FALSE);
  3077. ASSERT(m_pResult != NULL);
  3078. RESULTDATAITEM resultItem;
  3079. // This is an acceptable usage
  3080. memset(&resultItem, 0, sizeof(RESULTDATAITEM));
  3081. resultItem.mask = SDI_PARAM;
  3082. resultItem.lParam = reinterpret_cast<LPARAM>(pNode);
  3083. HRESULT hr = m_pResult->GetItem(&resultItem);
  3084. ASSERT(SUCCEEDED(hr));
  3085. return E_FAIL;
  3086. }
  3087. ///////////////////////////////////////////////////////////////////////////////
  3088. // CComponentObject::IExtendPropertySheet2 members
  3089. STDMETHODIMP CComponentObject::CreatePropertyPages(LPPROPERTYSHEETCALLBACK lpProvider,
  3090. LONG_PTR handle,
  3091. LPDATAOBJECT lpIDataObject)
  3092. {
  3093. // Delegate it to the IComponentData implementation
  3094. ASSERT(m_pComponentData != NULL);
  3095. IExtendPropertySheet2* pIExtendPropertySheet2;
  3096. VERIFY(SUCCEEDED(m_pComponentData->QueryInterface(IID_IExtendPropertySheet2,
  3097. reinterpret_cast<void**>(&pIExtendPropertySheet2))));
  3098. ASSERT(pIExtendPropertySheet2 != NULL);
  3099. HRESULT hr = pIExtendPropertySheet2->CreatePropertyPages(lpProvider, handle, lpIDataObject);
  3100. pIExtendPropertySheet2->Release();
  3101. return hr;
  3102. }
  3103. STDMETHODIMP CComponentObject::QueryPagesFor(LPDATAOBJECT lpDataObject)
  3104. {
  3105. // Delegate it to the IComponentData implementation
  3106. ASSERT(m_pComponentData != NULL);
  3107. IExtendPropertySheet2* pIExtendPropertySheet2;
  3108. VERIFY(SUCCEEDED(m_pComponentData->QueryInterface(IID_IExtendPropertySheet2,
  3109. reinterpret_cast<void**>(&pIExtendPropertySheet2))));
  3110. ASSERT(pIExtendPropertySheet2 != NULL);
  3111. HRESULT hr = pIExtendPropertySheet2->QueryPagesFor(lpDataObject);
  3112. pIExtendPropertySheet2->Release();
  3113. return hr;
  3114. }
  3115. STDMETHODIMP CComponentObject::GetWatermarks(LPDATAOBJECT lpDataObject,
  3116. HBITMAP* lphWatermark,
  3117. HBITMAP* lphHeader,
  3118. HPALETTE* lphPalette,
  3119. BOOL* pbStretch)
  3120. {
  3121. // Delegate it to the IComponentData implementation
  3122. ASSERT(m_pComponentData != NULL);
  3123. IExtendPropertySheet2* pIExtendPropertySheet2;
  3124. VERIFY(SUCCEEDED(m_pComponentData->QueryInterface(IID_IExtendPropertySheet2,
  3125. reinterpret_cast<void**>(&pIExtendPropertySheet2))));
  3126. ASSERT(pIExtendPropertySheet2 != NULL);
  3127. HRESULT hr = pIExtendPropertySheet2->GetWatermarks(lpDataObject,
  3128. lphWatermark,
  3129. lphHeader,
  3130. lphPalette,
  3131. pbStretch);
  3132. pIExtendPropertySheet2->Release();
  3133. return hr;
  3134. }
  3135. ///////////////////////////////////////////////////////////////////////////////
  3136. // CComponentObject::IExtendContextMenu members
  3137. STDMETHODIMP CComponentObject::AddMenuItems(LPDATAOBJECT pDataObject,
  3138. LPCONTEXTMENUCALLBACK pContextMenuCallback,
  3139. long *pInsertionAllowed)
  3140. {
  3141. HRESULT hr = S_OK;
  3142. CComPtr<IContextMenuCallback2> spContextMenuCallback2;
  3143. hr = pContextMenuCallback->QueryInterface(IID_IContextMenuCallback2, (PVOID*)&spContextMenuCallback2);
  3144. if (FAILED(hr))
  3145. {
  3146. return hr;
  3147. }
  3148. if (pDataObject == DOBJ_CUSTOMOCX)
  3149. {
  3150. //
  3151. // A custom result pane is being used and we don't know what node it cooresponds to so we assume that it
  3152. // is the previously selected container.
  3153. //
  3154. ASSERT(m_pSelectedContainerNode != NULL);
  3155. CTreeNode* pNode = (CTreeNode*)m_pSelectedContainerNode;
  3156. CNodeList nodeList;
  3157. nodeList.AddTail(pNode);
  3158. hr = m_pSelectedContainerNode->OnAddMenuItems(spContextMenuCallback2,
  3159. CCT_UNINITIALIZED,
  3160. pInsertionAllowed,
  3161. &nodeList);
  3162. }
  3163. else
  3164. {
  3165. //
  3166. // Delegate it to the IComponentData implementation
  3167. //
  3168. ASSERT(m_pComponentData != NULL);
  3169. IExtendContextMenu* pIExtendContextMenu;
  3170. VERIFY(SUCCEEDED(m_pComponentData->QueryInterface(IID_IExtendContextMenu,
  3171. reinterpret_cast<void**>(&pIExtendContextMenu))));
  3172. ASSERT(pIExtendContextMenu != NULL);
  3173. hr = pIExtendContextMenu->AddMenuItems(pDataObject,
  3174. pContextMenuCallback,
  3175. pInsertionAllowed);
  3176. pIExtendContextMenu->Release();
  3177. }
  3178. return hr;
  3179. }
  3180. STDMETHODIMP CComponentObject::Command(long nCommandID, LPDATAOBJECT pDataObject)
  3181. {
  3182. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  3183. HRESULT hr = S_OK;
  3184. if (pDataObject == DOBJ_CUSTOMOCX)
  3185. {
  3186. //
  3187. // A custom result pane is being used and we don't know what node it cooresponds to so we assume that it
  3188. // is the previously selected container.
  3189. //
  3190. ASSERT(m_pSelectedContainerNode != NULL);
  3191. CTreeNode* pNode = (CTreeNode*)m_pSelectedContainerNode;
  3192. CNodeList nodeList;
  3193. nodeList.AddTail(pNode);
  3194. hr = m_pSelectedContainerNode->OnCommand(nCommandID,
  3195. CCT_UNINITIALIZED,
  3196. (CComponentDataObject*)m_pComponentData,
  3197. &nodeList);
  3198. }
  3199. else
  3200. {
  3201. // Delegate it to the IComponentData implementation
  3202. ASSERT(m_pComponentData != NULL);
  3203. IExtendContextMenu* pIExtendContextMenu;
  3204. VERIFY(SUCCEEDED(m_pComponentData->QueryInterface(IID_IExtendContextMenu,
  3205. reinterpret_cast<void**>(&pIExtendContextMenu))));
  3206. ASSERT(pIExtendContextMenu != NULL);
  3207. hr = pIExtendContextMenu->Command(nCommandID, pDataObject);
  3208. pIExtendContextMenu->Release();
  3209. }
  3210. return hr;
  3211. }
  3212. ///////////////////////////////////////////////////////////////////////////////
  3213. // CComponentObject::IExtendControlbar members
  3214. STDMETHODIMP CComponentObject::SetControlbar(LPCONTROLBAR pControlbar)
  3215. {
  3216. HRESULT hr = S_OK;
  3217. if (pControlbar == NULL)
  3218. {
  3219. //
  3220. // Detach the controls here
  3221. //
  3222. if (m_pControlbar != NULL && m_pToolbar != NULL)
  3223. {
  3224. hr = m_pControlbar->Detach((IUnknown *) m_pToolbar);
  3225. SAFE_RELEASE(m_pControlbar);
  3226. }
  3227. }
  3228. else
  3229. {
  3230. //
  3231. // Save the controlbar interface pointer
  3232. //
  3233. if (m_pControlbar == NULL)
  3234. {
  3235. m_pControlbar = pControlbar;
  3236. m_pControlbar->AddRef();
  3237. }
  3238. //
  3239. // Do something here that checks to see if we have toolbars
  3240. // already created and use those. If not then create one
  3241. // and load everything necessary for it.
  3242. //
  3243. //
  3244. // Create the toolbar
  3245. //
  3246. hr = m_pControlbar->Create (TOOLBAR,
  3247. this,
  3248. (IUnknown **) &m_pToolbar);
  3249. if (SUCCEEDED(hr))
  3250. {
  3251. //
  3252. // Load the toolbar
  3253. //
  3254. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  3255. hr = InitializeToolbar(m_pToolbar);
  3256. if (FAILED(hr))
  3257. {
  3258. hr = m_pControlbar->Detach((IUnknown*) m_pToolbar);
  3259. SAFE_RELEASE(m_pControlbar);
  3260. }
  3261. }
  3262. }
  3263. return hr;
  3264. }
  3265. STDMETHODIMP CComponentObject::ControlbarNotify(MMC_NOTIFY_TYPE event, LPARAM arg, LPARAM param)
  3266. {
  3267. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  3268. HRESULT hr = S_OK;
  3269. if (m_pControlbar == NULL)
  3270. {
  3271. return hr;
  3272. }
  3273. //
  3274. // MMC provides two events here MMCN_SELECT at the time a node is selected
  3275. // and MMCN_BTN_CLICK when a toolbar button is pressed
  3276. //
  3277. switch (event)
  3278. {
  3279. case MMCN_SELECT:
  3280. {
  3281. //
  3282. // Attach the toolbar to the controlbar
  3283. //
  3284. hr = m_pControlbar->Attach(TOOLBAR, (IUnknown *) m_pToolbar);
  3285. if (SUCCEEDED(hr))
  3286. {
  3287. ASSERT(m_pToolbar != NULL);
  3288. //
  3289. // bSelect is TRUE if the node was selected, FALSE if the node was deselected
  3290. // bScope is TRUE if the a scope node is selected, FALSE if a result node was selected
  3291. //
  3292. BOOL bSelect = HIWORD(arg);
  3293. if (bSelect)
  3294. {
  3295. CInternalFormatCracker ifc;
  3296. hr = ifc.Extract((LPDATAOBJECT)param);
  3297. if (SUCCEEDED(hr))
  3298. {
  3299. CTreeNode* pNode = ifc.GetCookieAt(0);
  3300. ASSERT(pNode != NULL);
  3301. CNodeList nodeList;
  3302. ifc.GetCookieList(nodeList);
  3303. if (ifc.GetCookieCount() > 1) // multiple selection
  3304. {
  3305. ASSERT(pNode->GetContainer() != NULL);
  3306. hr = pNode->GetContainer()->OnSetToolbarVerbState(m_pToolbar,
  3307. &nodeList);
  3308. }
  3309. else if (ifc.GetCookieCount() == 1) // single selection
  3310. {
  3311. hr = pNode->OnSetToolbarVerbState(m_pToolbar,
  3312. &nodeList);
  3313. }
  3314. }
  3315. }
  3316. }
  3317. break;
  3318. }
  3319. case MMCN_BTN_CLICK:
  3320. {
  3321. //
  3322. // The arg is -1 for custom views like MessageView
  3323. //
  3324. if (DOBJ_CUSTOMOCX == (LPDATAOBJECT)arg)
  3325. {
  3326. if (m_pSelectedContainerNode != NULL)
  3327. {
  3328. CNodeList nodeList;
  3329. nodeList.AddTail(m_pSelectedContainerNode);
  3330. hr = m_pSelectedContainerNode->ToolbarNotify(static_cast<int>(param),
  3331. (CComponentDataObject*)m_pComponentData,
  3332. &nodeList);
  3333. }
  3334. else
  3335. {
  3336. hr = S_FALSE;
  3337. }
  3338. }
  3339. else
  3340. {
  3341. CInternalFormatCracker ifc;
  3342. hr = ifc.Extract((LPDATAOBJECT)arg);
  3343. CTreeNode* pNode = ifc.GetCookieAt(0);
  3344. ASSERT(pNode != NULL);
  3345. CNodeList nodeList;
  3346. ifc.GetCookieList(nodeList);
  3347. if (ifc.GetCookieCount() > 1) // multiple selection
  3348. {
  3349. ASSERT(pNode->GetContainer() != NULL);
  3350. hr = pNode->GetContainer()->ToolbarNotify(static_cast<int>(param),
  3351. (CComponentDataObject*)m_pComponentData,
  3352. &nodeList);
  3353. }
  3354. else if (ifc.GetCookieCount() == 1) // single selection
  3355. {
  3356. hr = pNode->ToolbarNotify(static_cast<int>(param),
  3357. (CComponentDataObject*)m_pComponentData,
  3358. &nodeList);
  3359. }
  3360. else
  3361. {
  3362. hr = S_FALSE;
  3363. }
  3364. }
  3365. break;
  3366. }
  3367. default:
  3368. {
  3369. break;
  3370. }
  3371. }
  3372. return hr;
  3373. }
  3374. ///////////////////////////////////////////////////////////////////////////////
  3375. // CComponentObject::IResultDataCompareEx members
  3376. // This compare is used to sort the item's in the listview
  3377. //
  3378. // Note: Assum sort is ascending when comparing.
  3379. STDMETHODIMP CComponentObject::Compare(RDCOMPARE* prdc, int* pnResult)
  3380. {
  3381. if (pnResult == NULL)
  3382. {
  3383. ASSERT(FALSE);
  3384. return E_POINTER;
  3385. }
  3386. if (prdc == NULL)
  3387. {
  3388. ASSERT(FALSE);
  3389. return E_POINTER;
  3390. }
  3391. CTreeNode* pNodeA = reinterpret_cast<CTreeNode*>(prdc->prdch1->cookie);
  3392. CTreeNode* pNodeB = reinterpret_cast<CTreeNode*>(prdc->prdch2->cookie);
  3393. ASSERT(pNodeA != NULL);
  3394. ASSERT(pNodeB != NULL);
  3395. CContainerNode* pContNode = pNodeA->GetContainer();
  3396. ASSERT(pContNode != NULL);
  3397. // delegate the sorting to the container
  3398. int nCol = prdc->nColumn;
  3399. *pnResult = pContNode->Compare(pNodeA, pNodeB, nCol, prdc->lUserParam);
  3400. return S_OK;
  3401. }
  3402. ///////////////////////////////////////////////////////////////////////////////
  3403. // CComponentObject Helpers
  3404. // This wrapper function required to make prefast shut up when we are
  3405. // initializing a critical section in a constructor.
  3406. void
  3407. ExceptionPropagatingInitializeCriticalSection(LPCRITICAL_SECTION critsec)
  3408. {
  3409. __try
  3410. {
  3411. // REVIEWED-2002/03/08-JeffJon-We want to propogate the exception
  3412. ::InitializeCriticalSection(critsec);
  3413. }
  3414. //
  3415. // propagate the exception to our caller.
  3416. //
  3417. __except (EXCEPTION_CONTINUE_SEARCH)
  3418. {
  3419. }
  3420. }