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

1120 lines
25 KiB

  1. /*++
  2. Copyright (C) 1997-2001 Microsoft Corporation
  3. Module Name:
  4. Abstract:
  5. History:
  6. --*/
  7. // OpView.cpp : implementation of the COpView class
  8. //
  9. #include "stdafx.h"
  10. #include "WMITest.h"
  11. #include "MainFrm.h"
  12. #include "WMITestDoc.h"
  13. #include "OpView.h"
  14. #include "ObjVw.h"
  15. #include "DelDlg.h"
  16. #include "PropsPg.H"
  17. #include "PropQualsPg.h"
  18. #include "MethodsPg.h"
  19. #ifdef _DEBUG
  20. #define new DEBUG_NEW
  21. #undef THIS_FILE
  22. static char THIS_FILE[] = __FILE__;
  23. #endif
  24. /////////////////////////////////////////////////////////////////////////////
  25. // COpView
  26. IMPLEMENT_DYNCREATE(COpView, CTreeView)
  27. BEGIN_MESSAGE_MAP(COpView, CTreeView)
  28. //{{AFX_MSG_MAP(COpView)
  29. ON_WM_SIZE()
  30. ON_NOTIFY_REFLECT(NM_DBLCLK, OnDblclk)
  31. ON_NOTIFY_REFLECT(TVN_SELCHANGED, OnSelChanged)
  32. ON_NOTIFY_REFLECT(NM_RCLICK, OnRclick)
  33. ON_COMMAND(ID_DELETE, OnDelete)
  34. ON_UPDATE_COMMAND_UI(ID_DELETE, OnUpdateDelete)
  35. ON_COMMAND(ID_MODIFY, OnModify)
  36. ON_UPDATE_COMMAND_UI(ID_MODIFY, OnUpdateModify)
  37. ON_COMMAND(ID_EDIT_COPY, OnEditCopy)
  38. ON_WM_DESTROY()
  39. ON_UPDATE_COMMAND_UI(ID_EDIT_COPY, OnUpdateEditCopy)
  40. //}}AFX_MSG_MAP
  41. ON_MESSAGE(WM_OBJ_INDICATE, OnObjIndicate)
  42. ON_MESSAGE(WM_OP_STATUS, OnOpStatus)
  43. END_MESSAGE_MAP()
  44. // Global used by sinks.
  45. COpView *g_pOpView;
  46. /////////////////////////////////////////////////////////////////////////////
  47. // COpView construction/destruction
  48. COpView::COpView() :
  49. m_pTree(NULL),
  50. m_hitemRoot(NULL)
  51. {
  52. g_pOpView = this;
  53. }
  54. COpView::~COpView()
  55. {
  56. }
  57. BOOL COpView::PreCreateWindow(CREATESTRUCT& cs)
  58. {
  59. cs.style |= WS_CHILD | WS_VISIBLE | TVS_HASLINES | TVS_HASBUTTONS |
  60. TVS_SHOWSELALWAYS; //TVS_EDITLABELS |
  61. return CTreeView::PreCreateWindow(cs);
  62. }
  63. /////////////////////////////////////////////////////////////////////////////
  64. // COpView drawing
  65. void COpView::OnInitialUpdate()
  66. {
  67. CTreeView::OnInitialUpdate();
  68. m_pTree->SetImageList(&((CMainFrame *) GetParentFrame())->m_imageList,
  69. TVSIL_NORMAL);
  70. if (!m_hitemRoot)
  71. m_hitemRoot = m_pTree->InsertItem(_T(""), IMAGE_ROOT, IMAGE_ROOT);
  72. GetDocument()->m_pOpView = this;
  73. UpdateRootText();
  74. }
  75. /////////////////////////////////////////////////////////////////////////////
  76. // COpView diagnostics
  77. #ifdef _DEBUG
  78. void COpView::AssertValid() const
  79. {
  80. CTreeView::AssertValid();
  81. }
  82. void COpView::Dump(CDumpContext& dc) const
  83. {
  84. CTreeView::Dump(dc);
  85. }
  86. CWMITestDoc* COpView::GetDocument() // non-debug version is inline
  87. {
  88. ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CWMITestDoc)));
  89. return (CWMITestDoc*)m_pDocument;
  90. }
  91. #endif //_DEBUG
  92. /////////////////////////////////////////////////////////////////////////////
  93. // COpView message handlers
  94. void COpView::OnSize(UINT nType, int cx, int cy)
  95. {
  96. CTreeView::OnSize(nType, cx, cy);
  97. if (!m_pTree)
  98. m_pTree = &GetTreeCtrl();
  99. }
  100. LRESULT COpView::OnObjIndicate(WPARAM wParam, LPARAM lParam)
  101. {
  102. COpWrap *pWrap = (COpWrap*) wParam;
  103. CObjInfo *pInfo = (CObjInfo*) lParam;
  104. if (!pWrap->IsObject())
  105. {
  106. AddOpItemChild(
  107. pInfo,
  108. pWrap->m_item,
  109. pInfo->GetImage(),
  110. pInfo->GetObjText());
  111. }
  112. else
  113. {
  114. // This just simulates a selection since the object has already been
  115. // added.
  116. if (pWrap->m_item == m_pTree->GetSelectedItem())
  117. GetDocument()->UpdateAllViews(this, HINT_OP_SEL,
  118. (CObject*) pWrap->m_item);
  119. }
  120. if (pWrap->m_item == m_pTree->GetSelectedItem())
  121. UpdateStatusText();
  122. return 0;
  123. }
  124. LRESULT COpView::OnOpStatus(WPARAM wParam, LPARAM lParam)
  125. {
  126. COpWrap *pWrap = (COpWrap*) wParam;
  127. int iImage = pWrap->GetImage();
  128. GetDocument()->DecBusyOps();
  129. // Update in case it's changed.
  130. m_pTree->SetItemImage(pWrap->m_item, iImage, iImage);
  131. if (pWrap->m_item == m_pTree->GetSelectedItem())
  132. UpdateStatusText();
  133. return 0;
  134. }
  135. void COpView::AddOpItem(WMI_OP_TYPE type, LPCTSTR szText, BOOL bOption)
  136. {
  137. AddOpItem(new COpWrap(type, szText, bOption));
  138. }
  139. void COpView::AddOpItem(COpWrap *pWrap)
  140. {
  141. CString strCaption = pWrap->GetCaption();
  142. int iImage = pWrap->GetImage();
  143. BOOL bExpand = m_pTree->GetChildItem(m_hitemRoot) == NULL;
  144. pWrap->m_item = m_pTree->InsertItem(strCaption, iImage, iImage,
  145. m_hitemRoot);
  146. m_pTree->SetItemData(pWrap->m_item, (DWORD_PTR) pWrap);
  147. RefreshItem(pWrap);
  148. if (bExpand)
  149. m_pTree->Expand(m_hitemRoot, TVE_EXPAND);
  150. m_pTree->SelectItem(pWrap->m_item);
  151. GetDocument()->SetModifiedFlag(TRUE);
  152. }
  153. void COpView::AddOpItemChild(
  154. CObjInfo *pInfo,
  155. HTREEITEM hParent,
  156. int iImage,
  157. LPCTSTR szText)
  158. {
  159. BOOL bUpdateCaption = !m_pTree->ItemHasChildren(hParent);
  160. HTREEITEM hChild = m_pTree->InsertItem(szText, iImage, iImage, hParent);
  161. // If this is the first child added see if the caption has been
  162. // updated.
  163. if (bUpdateCaption)
  164. {
  165. COpWrap *pWrap = (COpWrap*) m_pTree->GetItemData(hParent);
  166. m_pTree->SetItemText(pWrap->m_item, pWrap->GetCaption());
  167. }
  168. if (hParent == m_pTree->GetSelectedItem())
  169. UpdateStatusText();
  170. m_pTree->SetItemData(hChild, (DWORD_PTR) pInfo);
  171. GetDocument()->UpdateAllViews(this, HINT_NEW_CHILD, (CObject*) hChild);
  172. }
  173. void COpView::FlushItems()
  174. {
  175. HTREEITEM hitemParent;
  176. if (!m_hitemRoot)
  177. m_hitemRoot = m_pTree->InsertItem(_T(""), IMAGE_ROOT, IMAGE_ROOT);
  178. while ((hitemParent = m_pTree->GetChildItem(m_hitemRoot)) != NULL)
  179. {
  180. RemoveItemFromTree(hitemParent, TRUE);
  181. }
  182. }
  183. void COpView::RemoveChildrenFromTree(HTREEITEM item, BOOL bDoRemove)
  184. {
  185. COpWrap *pWrap = GetCurrentOp();
  186. if (pWrap && pWrap->m_item == item)
  187. GetDocument()->m_pObjView->Flush();
  188. HTREEITEM hitemChild;
  189. while ((hitemChild = m_pTree->GetChildItem(item)) != NULL)
  190. {
  191. RemoveNonOpObjectFromTree(hitemChild);
  192. }
  193. }
  194. void COpView::RemoveNonOpObjectFromTree(HTREEITEM hitem)
  195. {
  196. ASSERT(!IsOp(hitem));
  197. CObjInfo *pInfo = (CObjInfo*) m_pTree->GetItemData(hitem);
  198. ASSERT(pInfo != NULL);
  199. m_pTree->DeleteItem(hitem);
  200. delete pInfo;
  201. }
  202. void COpView::RemoveItemFromTree(HTREEITEM item, BOOL bNoWMIRemove)
  203. {
  204. if (!bNoWMIRemove && IsObjDeleteable(item))
  205. {
  206. CDelDlg dlg;
  207. dlg.m_bDelFromWMI = theApp.m_bDelFromWMI;
  208. if (dlg.DoModal() == IDOK)
  209. {
  210. theApp.m_bDelFromWMI = dlg.m_bDelFromWMI;
  211. if (dlg.m_bDelFromWMI)
  212. {
  213. CObjInfo *pObj = GetObjInfo(item);
  214. HRESULT hr;
  215. IWbemCallResultPtr pResult;
  216. if (pObj->IsInstance())
  217. {
  218. CString strPath;
  219. strPath = pObj->GetStringPropValue(L"__RELPATH");
  220. hr =
  221. GetDocument()->m_pNamespace->DeleteInstance(
  222. _bstr_t(strPath),
  223. 0,
  224. NULL,
  225. &pResult);
  226. }
  227. else
  228. {
  229. CString strClass;
  230. strClass = pObj->GetStringPropValue(L"__CLASS");
  231. hr =
  232. GetDocument()->m_pNamespace->DeleteClass(
  233. _bstr_t(strClass),
  234. 0,
  235. NULL,
  236. &pResult);
  237. }
  238. if (FAILED(hr))
  239. {
  240. //CWMITestDoc::DisplayWMIErrorBox(hr, pResult);
  241. CWMITestDoc::DisplayWMIErrorBox(hr);
  242. return;
  243. }
  244. }
  245. }
  246. else
  247. // Get out if the user canceled the dialog.
  248. return;
  249. }
  250. if (IsOp(item))
  251. {
  252. RemoveChildrenFromTree(item, TRUE);
  253. COpWrap *pWrap = (COpWrap*) m_pTree->GetItemData(item);
  254. pWrap->CancelOp(GetDocument()->m_pNamespace);
  255. m_pTree->DeleteItem(item);
  256. delete pWrap;
  257. }
  258. else
  259. {
  260. HTREEITEM itemParent = m_pTree->GetParentItem(item);
  261. RemoveNonOpObjectFromTree(item);
  262. UpdateItem(itemParent);
  263. }
  264. }
  265. void COpView::UpdateRootText()
  266. {
  267. CString strText;
  268. if (GetDocument()->m_pNamespace)
  269. strText = GetDocument()->m_strNamespace;
  270. else
  271. strText.LoadString(IDS_NOT_CONNECTED);
  272. m_pTree->SetItemText(m_hitemRoot, strText);
  273. UpdateStatusText();
  274. }
  275. void COpView::OnDblclk(NMHDR* pNMHDR, LRESULT* pResult)
  276. {
  277. CPoint curPoint;
  278. GetCursorPos(&curPoint);
  279. ScreenToClient(&curPoint);
  280. HTREEITEM hItem = m_pTree->HitTest(curPoint);
  281. *pResult = 0;
  282. // Only do something if we really selected an item.
  283. if (hItem)
  284. {
  285. m_pTree->SelectItem(hItem);
  286. if (hItem == m_hitemRoot)
  287. {
  288. GetDocument()->DoConnectDlg();
  289. *pResult = 1;
  290. }
  291. else
  292. OnModify();
  293. }
  294. }
  295. void COpView::UpdateCurrentItem()
  296. {
  297. HTREEITEM hitem = m_pTree->GetSelectedItem();
  298. if (hitem)
  299. UpdateItem(hitem);
  300. }
  301. void COpView::UpdateItem(HTREEITEM hitem)
  302. {
  303. long iHint;
  304. if (IsRoot(hitem))
  305. iHint = HINT_ROOT_SEL;
  306. else if (IsOp(hitem))
  307. iHint = HINT_OP_SEL;
  308. else
  309. iHint = HINT_OBJ_SEL;
  310. GetDocument()->UpdateAllViews(this, iHint, (CObject*) hitem);
  311. }
  312. void COpView::OnSelChanged(NMHDR* pNMHDR, LRESULT* pResult)
  313. {
  314. NM_TREEVIEW *pNMTreeView = (NM_TREEVIEW*)pNMHDR;
  315. if ((pNMTreeView->itemNew.state & TVIS_SELECTED))
  316. {
  317. UpdateItem(pNMTreeView->itemNew.hItem);
  318. }
  319. UpdateStatusText();
  320. *pResult = 0;
  321. }
  322. void COpView::RefreshObject(CObjInfo *pInfo)
  323. {
  324. }
  325. void COpView::RefreshItem(HTREEITEM hitem)
  326. {
  327. RefreshItem((COpWrap*) m_pTree->GetItemData(hitem));
  328. }
  329. void COpView::RefreshItem(COpWrap *pWrap)
  330. {
  331. pWrap->CancelOp(GetDocument()->m_pNamespace);
  332. pWrap->m_listObj.RemoveAll();
  333. RemoveChildrenFromTree(pWrap->m_item, TRUE);
  334. GetDocument()->IncBusyOps();
  335. pWrap->Execute(GetDocument()->m_pNamespace);
  336. int iImage = pWrap->GetImage();
  337. m_pTree->SetItemImage(pWrap->m_item, iImage, iImage);
  338. }
  339. void COpView::RefreshItems()
  340. {
  341. for (HTREEITEM hitemOp = m_pTree->GetChildItem(m_hitemRoot);
  342. hitemOp != NULL;
  343. hitemOp = m_pTree->GetNextSiblingItem(hitemOp))
  344. {
  345. RefreshItem(hitemOp);
  346. }
  347. }
  348. COpWrap *COpView::GetCurrentOp()
  349. {
  350. HTREEITEM hitemCurrent = m_pTree->GetSelectedItem();
  351. COpWrap *pWrap = NULL;
  352. if (hitemCurrent && (IsOp(hitemCurrent) ||
  353. IsOp(hitemCurrent = m_pTree->GetParentItem(hitemCurrent))))
  354. pWrap = (COpWrap*) m_pTree->GetItemData(hitemCurrent);
  355. return pWrap;
  356. }
  357. CObjInfo *COpView::GetCurrentObj()
  358. {
  359. HTREEITEM hitemCurrent = m_pTree->GetSelectedItem();
  360. CObjInfo *pObj = NULL;
  361. if (hitemCurrent && !IsRoot(hitemCurrent))
  362. {
  363. if (IsOp(hitemCurrent))
  364. {
  365. COpWrap *pWrap;
  366. pWrap = (COpWrap*) m_pTree->GetItemData(hitemCurrent);
  367. if (pWrap->IsObject())
  368. pObj = pWrap->GetObjInfo();
  369. }
  370. else
  371. pObj = (CObjInfo*) m_pTree->GetItemData(hitemCurrent);
  372. }
  373. return pObj;
  374. }
  375. void COpView::DoPopupMenu(UINT nMenuID)
  376. {
  377. CMenu popMenu;
  378. popMenu.LoadMenu(nMenuID);
  379. CPoint posMouse;
  380. GetCursorPos(&posMouse);
  381. CWnd* pWndPopupOwner = this;
  382. while (pWndPopupOwner->GetStyle() & WS_CHILD)
  383. pWndPopupOwner = pWndPopupOwner->GetParent();
  384. popMenu.GetSubMenu(0)->TrackPopupMenu(0,posMouse.x,posMouse.y,pWndPopupOwner);
  385. }
  386. void COpView::OnRclick(NMHDR* pNMHDR, LRESULT* pResult)
  387. {
  388. CPoint curPoint;
  389. GetCursorPos(&curPoint);
  390. ScreenToClient(&curPoint);
  391. HTREEITEM hItem = m_pTree->HitTest(curPoint);
  392. // Only do something if we really selected an item.
  393. if (hItem)
  394. {
  395. m_pTree->SelectItem(hItem);
  396. DoContextMenuForItem(hItem);
  397. }
  398. *pResult = 0;
  399. }
  400. void COpView::DoContextMenuForItem(HTREEITEM hItem)
  401. {
  402. if (hItem == m_hitemRoot)
  403. DoPopupMenu(IDR_NAMESPACE);
  404. else if (IsOp(hItem))
  405. {
  406. COpWrap *pWrap = (COpWrap*) m_pTree->GetItemData(hItem);
  407. if (FAILED(pWrap->m_hr))
  408. DoPopupMenu(IDR_BAD_OP);
  409. else
  410. {
  411. if (!pWrap->IsObject())
  412. DoPopupMenu(IDR_OP);
  413. else
  414. {
  415. if (pWrap->HoldsObjects())
  416. DoPopupMenu(IDR_INST);
  417. else
  418. DoPopupMenu(IDR_CLASS);
  419. }
  420. }
  421. }
  422. else
  423. {
  424. HTREEITEM hitemParent = m_pTree->GetParentItem(hItem);
  425. COpWrap *pWrap = (COpWrap*) m_pTree->GetItemData(hitemParent);
  426. if (pWrap->HoldsObjects())
  427. DoPopupMenu(IDR_INST);
  428. else
  429. DoPopupMenu(IDR_CLASS);
  430. }
  431. }
  432. int COpView::GetChildCount(HTREEITEM hitem)
  433. {
  434. int nCount = 0;
  435. for (HTREEITEM hitemOp = m_pTree->GetChildItem(m_hitemRoot);
  436. hitemOp != NULL;
  437. hitemOp = m_pTree->GetNextSiblingItem(hitemOp))
  438. {
  439. nCount++;
  440. }
  441. return nCount;
  442. }
  443. int COpView::GetOpCount()
  444. {
  445. return GetChildCount(m_hitemRoot);
  446. }
  447. BOOL COpView::IsObj(HTREEITEM hitem)
  448. {
  449. return GetObjInfo(hitem) != NULL;
  450. }
  451. CObjInfo *COpView::GetObjInfo(HTREEITEM hitem)
  452. {
  453. if (IsRoot(hitem))
  454. return NULL;
  455. BOOL bIsOp = IsOp(hitem);
  456. if (bIsOp)
  457. {
  458. COpWrap *pWrap = (COpWrap*) m_pTree->GetItemData(hitem);
  459. if (pWrap->IsObject())
  460. return pWrap->GetObjInfo();
  461. else
  462. return NULL;
  463. }
  464. else
  465. return (CObjInfo*) m_pTree->GetItemData(hitem);
  466. }
  467. void COpView::UpdateCurrentObject(BOOL bReloadProps)
  468. {
  469. HTREEITEM hitem = m_pTree->GetSelectedItem();
  470. if (!IsObj(hitem))
  471. hitem = GetDocument()->m_pObjView->GetSelectedItem();
  472. if (IsObj(hitem))
  473. {
  474. if (bReloadProps)
  475. {
  476. CObjInfo *pObj = (CObjInfo*) GetObjInfo(hitem);
  477. COpWrap *pOp = (COpWrap*) GetCurrentOp();
  478. pOp->RefreshPropInfo(pObj);
  479. // Update the icon.
  480. int iImage = pObj->GetImage();
  481. m_pTree->SetItemImage(hitem, iImage, iImage);
  482. // Update the text.
  483. m_pTree->SetItemText(hitem,
  484. pObj == &pOp->m_objInfo ? pOp->GetCaption() : pObj->GetObjText());
  485. }
  486. }
  487. UpdateItem(m_pTree->GetSelectedItem());
  488. }
  489. void COpView::UpdateStatusText()
  490. {
  491. HTREEITEM hitem = m_pTree->GetSelectedItem();
  492. CString strText;
  493. if (IsRoot(hitem))
  494. {
  495. if (GetDocument()->m_pNamespace)
  496. strText.Format(IDS_ROOT_STATUS, (LPCTSTR) GetDocument()->m_strNamespace);
  497. else
  498. strText.LoadString(IDS_NOT_CONNECTED);
  499. }
  500. else if (IsOp(hitem))
  501. {
  502. COpWrap *pWrap = (COpWrap*) m_pTree->GetItemData(hitem);
  503. if (SUCCEEDED(pWrap->m_hr))
  504. {
  505. strText.Format(
  506. pWrap->HoldsObjects() ? IDS_OBJ_COUNT : IDS_CLASS_COUNT,
  507. pWrap->GetObjCount());
  508. }
  509. else
  510. strText = pWrap->m_strErrorText;
  511. }
  512. ((CMainFrame *) GetParentFrame())->SetStatusText(strText);
  513. }
  514. BOOL COpView::GetSelectedObjPath(CString &strPath)
  515. {
  516. BOOL bRet = FALSE;
  517. CObjInfo *pInfo = GetCurrentObj();
  518. if (pInfo)
  519. {
  520. strPath = pInfo->GetStringPropValue(L"__RELPATH");
  521. bRet = !strPath.IsEmpty();
  522. }
  523. return bRet;
  524. }
  525. BOOL COpView::GetSelectedClass(CString &strClass)
  526. {
  527. BOOL bRet = FALSE;
  528. CObjInfo *pInfo = GetCurrentObj();
  529. if (pInfo)
  530. {
  531. strClass = pInfo->GetStringPropValue(L"__CLASS");
  532. bRet = TRUE;
  533. }
  534. return bRet;
  535. }
  536. void COpView::OnDelete()
  537. {
  538. HTREEITEM hitem = m_pTree->GetSelectedItem();
  539. if (hitem != NULL && !IsRoot(hitem))
  540. {
  541. RemoveItemFromTree(hitem, FALSE);
  542. GetDocument()->SetModifiedFlag(TRUE);
  543. }
  544. }
  545. void COpView::OnUpdateDelete(CCmdUI* pCmdUI)
  546. {
  547. HTREEITEM hitem = m_pTree->GetSelectedItem();
  548. pCmdUI->Enable(hitem != NULL && !IsRoot(hitem));
  549. }
  550. BOOL COpView::IsObjDeleteable(HTREEITEM hitem)
  551. {
  552. BOOL bRet = FALSE;
  553. if (IsObj(hitem))
  554. {
  555. if (IsOp(hitem))
  556. {
  557. COpWrap *pWrap = (COpWrap*) m_pTree->GetItemData(hitem);
  558. bRet = SUCCEEDED(pWrap->m_hr) &&
  559. pWrap->m_type != WMI_CREATE_CLASS &&
  560. pWrap->m_type != WMI_CREATE_OBJ;
  561. }
  562. else
  563. bRet = TRUE;
  564. }
  565. return bRet;
  566. }
  567. void COpView::EditObj(CObjInfo *pInfo)
  568. {
  569. CPropertySheet sheet(pInfo->IsInstance() ? IDS_EDIT_INST : IDS_EDIT_CLASS);
  570. CPropsPg pgProps;
  571. CPropQualsPg pgQuals;
  572. CMethodsPg pgMethods;
  573. IWbemClassObjectPtr
  574. pOrigObj;
  575. pInfo->m_pObj->Clone(&pOrigObj);
  576. pgProps.m_pNamespace = GetDocument()->m_pNamespace;
  577. pgProps.m_pObj = pInfo;
  578. pgQuals.m_pObj = pInfo->m_pObj;
  579. pgQuals.m_bIsInstance = pInfo->IsInstance();
  580. pgQuals.m_mode = CPropQualsPg::QMODE_CLASS;
  581. sheet.AddPage(&pgProps);
  582. sheet.AddPage(&pgQuals);
  583. if (!pInfo->IsInstance())
  584. {
  585. pgMethods.m_pObj = pInfo->m_pObj;
  586. sheet.AddPage(&pgMethods);
  587. }
  588. sheet.DoModal();
  589. if (pInfo->m_pObj->CompareTo(WBEM_COMPARISON_INCLUDE_ALL, pOrigObj) ==
  590. WBEM_S_DIFFERENT)
  591. {
  592. pInfo->SetModified(TRUE);
  593. UpdateCurrentObject(TRUE);
  594. }
  595. }
  596. void COpView::OnModify()
  597. {
  598. CObjInfo *pInfo = GetCurrentObj();
  599. if (pInfo && pInfo->m_pObj != NULL)
  600. EditObj(pInfo);
  601. else
  602. {
  603. COpWrap *pOp = GetCurrentOp();
  604. if (pOp && pOp->m_pErrorObj != NULL)
  605. CWMITestDoc::DisplayWMIErrorDetails(pOp->m_pErrorObj);
  606. }
  607. }
  608. void COpView::OnUpdateModify(CCmdUI* pCmdUI)
  609. {
  610. pCmdUI->Enable(GetCurrentObj() != NULL);
  611. }
  612. void COpView::ExportItemToFile(LPCTSTR szFilename, HTREEITEM hitem,
  613. BOOL bShowSystem, BOOL bTranslate)
  614. {
  615. // Open the file. If this fails, an exception will fire that MFC will
  616. // handle for us.
  617. CStdioFile file(szFilename,
  618. CFile::modeCreate | CFile::modeWrite | CFile::typeText);
  619. // Add the namespace.
  620. CString strNamespace;
  621. strNamespace.Format("<%s>\n", GetDocument()->m_strNamespace);
  622. file.WriteString(strNamespace);
  623. ExportItem(&file, hitem, bShowSystem, bTranslate);
  624. }
  625. void COpView::ExportItem(CStdioFile *pFile, HTREEITEM hitem,
  626. BOOL bShowSystem, BOOL bTranslate)
  627. {
  628. CObjInfo *pObj = GetObjInfo(hitem);
  629. if (pObj)
  630. pObj->Export(pFile, bShowSystem, bTranslate, 0);
  631. else
  632. {
  633. for (HTREEITEM hitemChild = m_pTree->GetChildItem(hitem);
  634. hitemChild != NULL;
  635. hitemChild = m_pTree->GetNextSiblingItem(hitemChild))
  636. {
  637. ExportItem(pFile, hitemChild, bShowSystem, bTranslate);
  638. }
  639. }
  640. }
  641. /*
  642. DWORD WINAPI SetClipboardThreadProc(COleDataSource *pSrc)
  643. {
  644. OleInitialize(NULL);
  645. pSrc->SetClipboard();
  646. BOOL bRet;
  647. bRet = IsClipboardFormatAvailable(CF_TEXT);
  648. bRet = IsClipboardFormatAvailable(CF_UNICODETEXT);
  649. OpenClipboard(NULL);
  650. // HANDLE hRet = GetClipboardData(CF_TEXT);
  651. // LPSTR szText = (LPSTR) GlobalLock((HGLOBAL) hRet);
  652. // GlobalUnlock((HGLOBAL) hRet);
  653. CloseClipboard();
  654. OleUninitialize();
  655. return 0;
  656. }
  657. DWORD WINAPI COpView::CopyThreadProc(COpView *pThis)
  658. {
  659. OleInitialize(NULL);
  660. pThis->CopyToClipboard();
  661. OleUninitialize();
  662. return 0;
  663. }
  664. void COpView::DoThreadedCopy()
  665. {
  666. HANDLE hThread;
  667. DWORD dwID;
  668. hThread =
  669. CreateThread(
  670. NULL,
  671. 0,
  672. (LPTHREAD_START_ROUTINE) CopyThreadProc,
  673. this,
  674. 0,
  675. &dwID);
  676. //WaitForSingleObject(hThread, INFINITE);
  677. CloseHandle(hThread);
  678. }
  679. */
  680. void COpView::OnEditCopy()
  681. {
  682. CopyToClipboard();
  683. //DoThreadedCopy();
  684. }
  685. #define NO_DATA_SRC
  686. void COpView::CopyToClipboard()
  687. {
  688. COpList list;
  689. BuildSelectedOpList(list);
  690. if (list.GetCount())
  691. {
  692. // Ole will kill this variable when it's done.
  693. //COleDataSource *pSrc = new COleDataSource;
  694. //DoCopy(pSrc, list);
  695. DoCopy(list);
  696. #ifndef NO_DATA_SRC
  697. #if 1
  698. pSrc->SetClipboard();
  699. #else
  700. HANDLE hThread;
  701. DWORD dwID;
  702. hThread =
  703. CreateThread(
  704. NULL,
  705. 0,
  706. (LPTHREAD_START_ROUTINE) SetClipboardThreadProc,
  707. pSrc,
  708. 0,
  709. &dwID);
  710. WaitForSingleObject(hThread, INFINITE);
  711. CloseHandle(hThread);
  712. #endif
  713. #endif
  714. }
  715. }
  716. void COpView::BuildSelectedOpList(COpList &list)
  717. {
  718. HTREEITEM hitem = m_pTree->GetSelectedItem();
  719. list.RemoveAll();
  720. if (hitem == m_hitemRoot)
  721. {
  722. for (HTREEITEM hitemOp = m_pTree->GetChildItem(m_hitemRoot);
  723. hitemOp != NULL;
  724. hitemOp = m_pTree->GetNextSiblingItem(hitemOp))
  725. {
  726. COpWrap *pWrap = (COpWrap*) m_pTree->GetItemData(hitemOp);
  727. list.AddTail(*pWrap);
  728. }
  729. }
  730. else if (IsOp(hitem))
  731. {
  732. COpWrap *pWrap = (COpWrap*) m_pTree->GetItemData(hitem);
  733. list.AddTail(*pWrap);
  734. }
  735. else
  736. {
  737. ASSERT(IsObj(hitem));
  738. CObjInfo *pObj = GetObjInfo(hitem);
  739. CString strRelpath = pObj->GetStringPropValue(L"__RELPATH");
  740. if (!strRelpath.IsEmpty())
  741. {
  742. BOOL bIsClass = strRelpath.Find('=') == -1;
  743. COpWrap wrap(bIsClass ? WMI_GET_CLASS : WMI_GET_OBJ, strRelpath);
  744. list.AddTail(wrap);
  745. }
  746. }
  747. }
  748. void COpView::DoCopy(COpList &list)
  749. {
  750. CMemFile memNative,
  751. memText;
  752. CArchive arcNative(&memNative, CArchive::store),
  753. arcText(&memText, CArchive::store);
  754. POSITION pos = list.GetHeadPosition();
  755. BOOL bFirst = TRUE;
  756. // Save off the count into the archive.
  757. arcNative << (int) list.GetCount();
  758. while (pos)
  759. {
  760. COpWrap &op = list.GetNext(pos);
  761. // Save off the option wrapper into the native archive.
  762. op.Serialize(arcNative);
  763. // Add a new-line if we've already done an item.
  764. if (bFirst)
  765. bFirst = FALSE;
  766. else
  767. arcText.Write("\n", sizeof("\n") - 1);
  768. // Add the option text into the text archive.
  769. arcText.Write((LPCTSTR) op.m_strOpText, op.m_strOpText.GetLength());
  770. }
  771. // Add the null character.
  772. arcText.Write("", 1);
  773. arcNative.Close();
  774. arcText.Close();
  775. #ifdef NO_DATA_SRC
  776. BOOL bRet = ::OpenClipboard(NULL);
  777. #endif
  778. // Save off the native archive contents.
  779. DWORD dwSize = memNative.GetLength();
  780. HGLOBAL pGlobBuffer = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, dwSize),
  781. pArcBuffer = memNative.Detach();
  782. memcpy(GlobalLock(pGlobBuffer), pArcBuffer, dwSize);
  783. GlobalUnlock(pGlobBuffer);
  784. free(pArcBuffer);
  785. #ifdef NO_DATA_SRC
  786. SetClipboardData(GetDocument()->m_cfOps, (HGLOBAL) pGlobBuffer);
  787. #else
  788. pSrc->CacheGlobalData(GetDocument()->m_cfOps, (HGLOBAL) pGlobBuffer);
  789. #endif
  790. // Save off the text archive contents.
  791. dwSize = memText.GetLength();
  792. pGlobBuffer = (LPBYTE) GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, dwSize);
  793. pArcBuffer = memText.Detach();
  794. // Turn the text into Unicode.
  795. _bstr_t strText = (LPSTR) pArcBuffer;
  796. memcpy(GlobalLock(pGlobBuffer), pArcBuffer, dwSize);
  797. GlobalUnlock(pGlobBuffer);
  798. free(pArcBuffer);
  799. #ifdef NO_DATA_SRC
  800. SetClipboardData(CF_TEXT, (HGLOBAL) pGlobBuffer);
  801. #else
  802. pSrc->CacheGlobalData(CF_TEXT, (HGLOBAL) pGlobBuffer);
  803. #endif
  804. pGlobBuffer = (LPBYTE) GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, dwSize * 2);
  805. LPBYTE pBuffer = (LPBYTE) GlobalLock(pGlobBuffer);
  806. memcpy(pBuffer, (LPCWSTR) strText, dwSize * 2);
  807. GlobalUnlock(pGlobBuffer);
  808. #ifdef NO_DATA_SRC
  809. SetClipboardData(CF_UNICODETEXT, (HGLOBAL) pGlobBuffer);
  810. #else
  811. pSrc->CacheGlobalData(CF_UNICODETEXT, (HGLOBAL) pGlobBuffer);
  812. #endif
  813. #ifdef NO_DATA_SRC
  814. ::CloseClipboard();
  815. #endif
  816. }
  817. /*
  818. void COpView::SerializeForClipboard(CArchive &arcNative, CArchive &arcText,
  819. CLIPFORMAT *pFormat)
  820. {
  821. HTREEITEM hitem = m_pTree->GetSelectedItem();
  822. if (hitem == m_hitemRoot)
  823. {
  824. int nCount = GetOpCount();
  825. archive << nCount;
  826. for (HTREEITEM hitemOp = m_pTree->GetChildItem(m_hitemRoot);
  827. hitemOp != NULL;
  828. hitemOp = m_pTree->GetNextSiblingItem(hitemOp))
  829. {
  830. COpWrap *pWrap = (COpWrap*) m_pTree->GetItemData(hitemOp);
  831. m_pWrap->Serialize(arcNative);
  832. }
  833. *pFormat = GetDocument()->m_cfOps;
  834. }
  835. else if (IsOp(hitem))
  836. {
  837. int nCount = 1;
  838. COpWrap *pWrap = (COpWrap*) m_pTree->GetItemData(hitem);
  839. archive << nCount;
  840. pWrap->Serialize(arcNative);
  841. *pFormat = GetDocument()->m_cfOps;
  842. }
  843. else
  844. {
  845. ASSERT(IsObj(hitem));
  846. }
  847. }
  848. void COpView::SerializeTreeItem(HTREEITEM item, CArchive &arcNative, CArchive &arcText)
  849. {
  850. }
  851. */
  852. BOOL COpView::CanCopy()
  853. {
  854. return m_pTree->GetSelectedItem() != NULL;
  855. }
  856. void COpView::OnUpdateEditCopy(CCmdUI* pCmdUI)
  857. {
  858. pCmdUI->Enable(CanCopy());
  859. }