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.

562 lines
18 KiB

  1. //==============================================================;
  2. //
  3. // This source code is only intended as a supplement to
  4. // existing Microsoft documentation.
  5. //
  6. //
  7. //
  8. //
  9. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
  10. // KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  11. // IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
  12. // PURPOSE.
  13. //
  14. // Copyright (C) 1999 Microsoft Corporation. All Rights Reserved.
  15. //
  16. //
  17. //
  18. //==============================================================;
  19. #include <stdio.h>
  20. #include "DataObj.h"
  21. #include "Space.h"
  22. #include "Comp.h"
  23. const GUID CSpaceFolder::thisGuid = { 0x29743810, 0x4c4b, 0x11d2, { 0x89, 0xd8, 0x0, 0x0, 0x21, 0x47, 0x31, 0x28 } };
  24. const GUID CSpaceStation::thisGuid = { 0x62273a12, 0x1914, 0x11d3, { 0x9a, 0x38, 0x0, 0x80, 0xc7, 0x25, 0x80, 0x72 } };
  25. const GUID CRocket::thisGuid = { 0x29743811, 0x4c4b, 0x11d2, { 0x89, 0xd8, 0x0, 0x0, 0x21, 0x47, 0x31, 0x28 } };
  26. //==============================================================
  27. //
  28. // CSpaceFolder implementation
  29. //
  30. //
  31. CSpaceFolder::CSpaceFolder()
  32. {
  33. for (int n = 0; n < NUMBER_OF_CHILDREN; n++) {
  34. children[n] = new CSpaceStation();
  35. }
  36. }
  37. CSpaceFolder::~CSpaceFolder()
  38. {
  39. for (int n = 0; n < NUMBER_OF_CHILDREN; n++)
  40. if (children[n]) {
  41. delete children[n];
  42. }
  43. }
  44. HRESULT CSpaceFolder::OnExpand(IConsoleNameSpace *pConsoleNameSpace, IConsole *pConsole, HSCOPEITEM parent)
  45. {
  46. SCOPEDATAITEM sdi;
  47. if (!bExpanded) {
  48. // create the child nodes, then expand them
  49. for (int n = 0; n < NUMBER_OF_CHILDREN; n++) {
  50. ZeroMemory(&sdi, sizeof(SCOPEDATAITEM) );
  51. sdi.mask = SDI_STR | // Displayname is valid
  52. SDI_PARAM | // lParam is valid
  53. SDI_IMAGE | // nImage is valid
  54. SDI_OPENIMAGE | // nOpenImage is valid
  55. SDI_PARENT |
  56. SDI_CHILDREN;
  57. sdi.relativeID = (HSCOPEITEM)parent;
  58. sdi.nImage = children[n]->GetBitmapIndex();
  59. sdi.nOpenImage = INDEX_OPENFOLDER;
  60. sdi.displayname = MMC_CALLBACK;
  61. sdi.lParam = (LPARAM)children[n]; // The cookie
  62. sdi.cChildren = 0;
  63. HRESULT hr = pConsoleNameSpace->InsertItem( &sdi );
  64. _ASSERT( SUCCEEDED(hr) );
  65. children[n]->SetHandle((HANDLE)sdi.ID);
  66. }
  67. }
  68. return S_OK;
  69. }
  70. //==============================================================
  71. //
  72. // CSpaceStation implementation
  73. //
  74. //
  75. CSpaceStation::CSpaceStation() : m_cChildSpaceStations(0)
  76. {
  77. for (int n = 0; n < MAX_CHILDREN; n++) {
  78. children[n] = NULL;
  79. }
  80. for (n = 0; n < NUMBER_OF_CHILDREN; n++) {
  81. children[n] = new CRocket(this, _T("Rocket"), n, 350115, 320, 52300);
  82. }
  83. }
  84. CSpaceStation::~CSpaceStation()
  85. {
  86. for (int n = 0; n < MAX_CHILDREN; n++)
  87. if (children[n]) {
  88. delete children[n];
  89. }
  90. }
  91. HRESULT CSpaceStation::Expand(IConsoleNameSpace *pConsoleNameSpace)
  92. {
  93. //Expand the CSpaceStation if necessary.
  94. //This method is called by the object's OnPaste method during paste operations.
  95. HRESULT hr = S_FALSE;
  96. //First, need the IConsoleNameSpace2 interface to call Expand.
  97. IConsoleNameSpace2 *pConsoleNamespace2 = NULL;
  98. hr = pConsoleNameSpace->QueryInterface(IID_IConsoleNameSpace2, (void **)&pConsoleNamespace2);
  99. _ASSERT( SUCCEEDED(hr) );
  100. hr = pConsoleNamespace2->Expand((HSCOPEITEM)GetHandle());
  101. pConsoleNamespace2->Release();
  102. return hr;
  103. }
  104. HRESULT CSpaceStation::GetResultViewType(LPOLESTR *ppViewType, long *pViewOptions)
  105. {
  106. return S_FALSE;
  107. }
  108. HRESULT CSpaceStation::OnShow(IConsole *pConsole, BOOL bShow, HSCOPEITEM scopeitem)
  109. {
  110. HRESULT hr = S_OK;
  111. IHeaderCtrl *pHeaderCtrl = NULL;
  112. IResultData *pResultData = NULL;
  113. if (bShow) {
  114. hr = pConsole->QueryInterface(IID_IHeaderCtrl, (void **)&pHeaderCtrl);
  115. _ASSERT( SUCCEEDED(hr) );
  116. hr = pConsole->QueryInterface(IID_IResultData, (void **)&pResultData);
  117. _ASSERT( SUCCEEDED(hr) );
  118. // Set the column headers in the results pane
  119. hr = pHeaderCtrl->InsertColumn( 0, L"Rocket Class", 0, MMCLV_AUTO );
  120. _ASSERT( S_OK == hr );
  121. hr = pHeaderCtrl->InsertColumn( 1, L"Rocket Weight", 0, MMCLV_AUTO );
  122. _ASSERT( S_OK == hr );
  123. hr = pHeaderCtrl->InsertColumn( 2, L"Rocket Height", 0, MMCLV_AUTO );
  124. _ASSERT( S_OK == hr );
  125. hr = pHeaderCtrl->InsertColumn( 3, L"Rocket Payload", 0, MMCLV_AUTO );
  126. _ASSERT( S_OK == hr );
  127. hr = pHeaderCtrl->InsertColumn( 4, L"Status", 0, MMCLV_AUTO );
  128. _ASSERT( S_OK == hr );
  129. // insert items here
  130. RESULTDATAITEM rdi;
  131. hr = pResultData->DeleteAllRsltItems();
  132. _ASSERT( SUCCEEDED(hr) );
  133. if (!bExpanded) {
  134. // create the child nodes, then expand them
  135. for (int n = 0; n < MAX_CHILDREN; n++) {
  136. if (NULL == children[n])
  137. //No more children to insert, so exit for loop
  138. break;
  139. BOOL childDeleteStatus = children[n]->getDeletedStatus();
  140. if ( !childDeleteStatus) {
  141. ZeroMemory(&rdi, sizeof(RESULTDATAITEM) );
  142. rdi.mask = RDI_STR | // Displayname is valid
  143. RDI_IMAGE |
  144. RDI_PARAM; // nImage is valid
  145. rdi.nImage = children[n]->GetBitmapIndex();
  146. rdi.str = MMC_CALLBACK;
  147. rdi.nCol = 0;
  148. rdi.lParam = (LPARAM)children[n];
  149. hr = pResultData->InsertItem( &rdi );
  150. _ASSERT( SUCCEEDED(hr) );
  151. children[n]->SetHandle((HANDLE)rdi.itemID);
  152. }
  153. }
  154. }
  155. pHeaderCtrl->Release();
  156. pResultData->Release();
  157. }
  158. return hr;
  159. }
  160. HRESULT CSpaceStation::OnSelect(IConsole *pConsole, BOOL bScope, BOOL bSelect)
  161. {
  162. IConsoleVerb *pConsoleVerb;
  163. HRESULT hr = pConsole->QueryConsoleVerb(&pConsoleVerb);
  164. _ASSERT(SUCCEEDED(hr));
  165. hr = pConsoleVerb->SetVerbState(MMC_VERB_CUT, ENABLED, TRUE);
  166. hr = pConsoleVerb->SetVerbState(MMC_VERB_CUT, HIDDEN, FALSE);
  167. hr = pConsoleVerb->SetVerbState(MMC_VERB_COPY, ENABLED, TRUE);
  168. hr = pConsoleVerb->SetVerbState(MMC_VERB_COPY, HIDDEN, FALSE);
  169. hr = pConsoleVerb->SetVerbState(MMC_VERB_DELETE, ENABLED, TRUE);
  170. hr = pConsoleVerb->SetVerbState(MMC_VERB_DELETE, HIDDEN, FALSE);
  171. hr = pConsoleVerb->SetVerbState(MMC_VERB_PASTE, ENABLED, TRUE);
  172. hr = pConsoleVerb->SetVerbState(MMC_VERB_PASTE, HIDDEN, FALSE);
  173. pConsoleVerb->Release();
  174. return S_OK;
  175. }
  176. HRESULT CSpaceStation::OnPaste(IConsole *pConsole, CComponentData *pComponentData, CDelegationBase *pPasted)
  177. {
  178. CRocket *pRocket = dynamic_cast<CRocket *>(pPasted);
  179. HRESULT hr = S_OK;
  180. if (NULL == pRocket)
  181. {
  182. // See if this is CSpaceStation, if so paste it into this item.
  183. // This sample simply creates a new CSpaceStation
  184. // and inserts it as a child of the destination of the paste.
  185. // It does not paste the cut/copied CSpaceStation and the
  186. // current state of its result items.
  187. CSpaceStation* pSpaceStn = dynamic_cast<CSpaceStation*>(pPasted);
  188. if ( (NULL != pSpaceStn) &&
  189. (pSpaceStn != this) )
  190. {
  191. // Regardless of whether this item is expanded or not
  192. // always try to expand this scopeitem (so that paste can
  193. // succeed).
  194. hr = Expand(pComponentData->GetConsoleNameSpace());
  195. CSpaceStation* pNewStation = new CSpaceStation();
  196. SCOPEDATAITEM sdi;
  197. ZeroMemory(&sdi, sizeof(SCOPEDATAITEM) );
  198. sdi.mask = SDI_STR| // Displayname is valid
  199. SDI_PARAM | // lParam is valid
  200. SDI_IMAGE | // nImage is valid
  201. SDI_OPENIMAGE | // nOpenImage is valid
  202. SDI_PARENT |
  203. SDI_CHILDREN;
  204. sdi.relativeID = (HSCOPEITEM)GetHandle();
  205. sdi.nImage = pNewStation->GetBitmapIndex();
  206. sdi.nOpenImage = INDEX_OPENFOLDER;
  207. sdi.displayname = MMC_CALLBACK;
  208. sdi.lParam = (LPARAM)pNewStation; // The cookie
  209. sdi.cChildren = 0;
  210. hr = pComponentData->GetConsoleNameSpace()->InsertItem( &sdi );
  211. _ASSERT( SUCCEEDED(hr) );
  212. pNewStation->SetHandle((HANDLE)sdi.ID);
  213. }
  214. //increment count of child space stations
  215. m_cChildSpaceStations++;
  216. return hr;
  217. }
  218. if (pRocket->m_pSpaceStation == this)
  219. return S_FALSE;
  220. //Create a new CRocket for the destination CSpaceStation
  221. CRocket *myRocket = new CRocket(pRocket->m_pSpaceStation, pRocket->szName, pRocket->nId,
  222. pRocket->lWeight, pRocket->lHeight, pRocket->lPayload);
  223. for (int n = 0; n < MAX_CHILDREN; n++) {
  224. if (NULL == children[n]) {
  225. // put it here
  226. children[n] = myRocket;
  227. children[n]->isDeleted = FALSE;
  228. children[n]->nId = n;
  229. children[n]->m_pSpaceStation = this;
  230. return S_OK;
  231. }
  232. }
  233. return S_FALSE;
  234. }
  235. HRESULT CSpaceStation::OnQueryPaste(CDelegationBase *pPasted)
  236. {
  237. CRocket *pRocket = dynamic_cast<CRocket *>(pPasted);
  238. if (NULL == pRocket)
  239. {
  240. // See if this is CSpaceStation.
  241. CSpaceStation* pSpaceStn = dynamic_cast<CSpaceStation*>(pPasted);
  242. if ( (NULL != pSpaceStn) &&
  243. (pSpaceStn != this) )
  244. {
  245. return S_OK;
  246. }
  247. return S_FALSE;
  248. }
  249. if (pRocket->m_pSpaceStation != this)
  250. for (int n = 0; n < MAX_CHILDREN; n++) {
  251. if (NULL == children[n]) {
  252. return S_OK;
  253. }
  254. }
  255. return S_FALSE;
  256. }
  257. HRESULT CSpaceStation::OnUpdateItem(IConsole *pConsole, long item, ITEM_TYPE itemtype)
  258. {
  259. HRESULT hr = S_OK;
  260. _ASSERT(item);
  261. _ASSERT(SCOPE == itemtype);
  262. //refresh all result pane views
  263. hr = pConsole->SelectScopeItem( (HSCOPEITEM)item );
  264. _ASSERT( S_OK == hr);
  265. return hr;
  266. }
  267. HRESULT CSpaceStation::OnDeleteScopeItem (IConsoleNameSpace *pConsoleNameSpace)
  268. {
  269. HRESULT hr = S_FALSE;
  270. HSCOPEITEM hCutItem = (HSCOPEITEM)GetHandle();
  271. //Get handle and cookie of parent scope item. We need these to
  272. //remove the "+" sign if the parent's m_cChildSpaceStations goes to zero.
  273. HSCOPEITEM hParentItem;
  274. MMC_COOKIE cookieParentItem;
  275. HRESULT hr1 = pConsoleNameSpace->GetParentItem(hCutItem, &hParentItem,
  276. (long *)&cookieParentItem);
  277. //Delete the cut item
  278. hr = pConsoleNameSpace->DeleteItem(hCutItem, TRUE);
  279. _ASSERT(S_OK == hr);
  280. if (SUCCEEDED(hr1))
  281. {
  282. //Decrement parent's m_cChildSpaceStations count and
  283. //remove "+" sign if necessary
  284. CSpaceStation* pParentSpaceStn = reinterpret_cast<CSpaceStation*>(cookieParentItem);
  285. pParentSpaceStn->DecrementCountChildSpaceStations();
  286. if ( !pParentSpaceStn->GetCountChildSpaceStations() )
  287. {
  288. SCOPEDATAITEM sdi;
  289. ZeroMemory(&sdi, sizeof(SCOPEDATAITEM) );
  290. sdi.mask = SDI_CHILDREN; //cChildren is valid
  291. sdi.ID = (HSCOPEITEM)hParentItem;
  292. sdi.cChildren = 0;
  293. hr = pConsoleNameSpace->SetItem( &sdi );
  294. _ASSERT( SUCCEEDED(hr) );
  295. }
  296. }
  297. return hr;
  298. }
  299. //==============================================================
  300. //
  301. // CRocket implementation
  302. //
  303. //
  304. CRocket::CRocket(CSpaceStation *pSpaceStation, _TCHAR *szName, int id, LONG lWeight, LONG lHeight, LONG lPayload)
  305. : m_pSpaceStation(pSpaceStation), szName(NULL), lWeight(0), lHeight(0), lPayload(0), iStatus(STOPPED)
  306. {
  307. if (szName) {
  308. this->szName = new _TCHAR[(_tcslen(szName) + 1) * sizeof(_TCHAR)];
  309. _tcscpy(this->szName, szName);
  310. }
  311. this->nId = id;
  312. this->lWeight = lWeight;
  313. this->lHeight = lHeight;
  314. this->lPayload = lPayload;
  315. isDeleted = FALSE;
  316. }
  317. CRocket::~CRocket()
  318. {
  319. if (szName)
  320. delete [] szName;
  321. }
  322. const _TCHAR *CRocket::GetDisplayName(int nCol)
  323. {
  324. static _TCHAR buf[128];
  325. switch (nCol) {
  326. case 0:
  327. _stprintf(buf, _T("%s (#%d)"), szName ? szName : _T(""), nId);
  328. break;
  329. case 1:
  330. _stprintf(buf, _T("%ld metric tons"), lWeight);
  331. break;
  332. case 2:
  333. _stprintf(buf, _T("%ld meters"), lHeight);
  334. break;
  335. case 3:
  336. _stprintf(buf, _T("%ld kilos"), lPayload);
  337. break;
  338. case 4:
  339. _stprintf(buf, _T("%s"),
  340. iStatus == RUNNING ? _T("running") :
  341. iStatus == PAUSED ? _T("paused") :
  342. iStatus == STOPPED ? _T("stopped") : _T("unknown"));
  343. break;
  344. }
  345. return buf;
  346. }
  347. HRESULT CRocket::OnSelect(IConsole *pConsole, BOOL bScope, BOOL bSelect)
  348. {
  349. IConsoleVerb *pConsoleVerb;
  350. HRESULT hr = pConsole->QueryConsoleVerb(&pConsoleVerb);
  351. _ASSERT(SUCCEEDED(hr));
  352. hr = pConsoleVerb->SetVerbState(MMC_VERB_RENAME, ENABLED, TRUE);
  353. hr = pConsoleVerb->SetVerbState(MMC_VERB_CUT, ENABLED, TRUE);
  354. hr = pConsoleVerb->SetVerbState(MMC_VERB_COPY, ENABLED, TRUE);
  355. hr = pConsoleVerb->SetVerbState(MMC_VERB_DELETE, ENABLED, TRUE);
  356. pConsoleVerb->Release();
  357. return S_OK;
  358. }
  359. HRESULT CRocket::OnRefresh(IConsole *pConsole)
  360. {
  361. //Call IConsole::UpdateAllViews to redraw all views
  362. //owned by the parent scope item
  363. IDataObject *pDummy = NULL;
  364. HANDLE handle = m_pSpaceStation->GetHandle();
  365. HRESULT hr;
  366. hr = pConsole->UpdateAllViews(pDummy, (long)handle, UPDATE_SCOPEITEM);
  367. _ASSERT( S_OK == hr);
  368. return hr;
  369. }
  370. HRESULT CRocket::OnDelete(IConsole *pConsole)
  371. {
  372. _ASSERT( NULL != this );
  373. HRESULT hr = S_OK;
  374. //Delete the item. The IConsole that is passed into DeleteChild
  375. //is from the child result item, so we can use it to QI for IResultData
  376. IResultData *pResultData = NULL;
  377. hr = pConsole->QueryInterface(IID_IResultData, (void **)&pResultData);
  378. _ASSERT( SUCCEEDED(hr) );
  379. HRESULTITEM childresultitem;
  380. _ASSERT( NULL != &childresultitem );
  381. //lparam == this. See CSpaceStation::OnShow
  382. hr = pResultData->FindItemByLParam( (LPARAM)this, &childresultitem );
  383. _ASSERT( SUCCEEDED(hr) );
  384. hr = pResultData->DeleteItem( childresultitem, 0 );
  385. _ASSERT( SUCCEEDED(hr) );
  386. pResultData->Release();
  387. //Now set child's isDeleted member to true so that the parent doesn't try to
  388. //to insert it again in CSpaceVehicle::OnShow. Admittedly, a hack...
  389. isDeleted = TRUE;
  390. return hr;
  391. }
  392. HRESULT CRocket::OnRename(LPOLESTR pszNewName)
  393. {
  394. HRESULT hr = S_FALSE;
  395. if (szName) {
  396. delete [] szName;
  397. szName = NULL;
  398. }
  399. MAKE_TSTRPTR_FROMWIDE(ptrname, pszNewName);
  400. szName = new _TCHAR[(_tcslen(ptrname) + 1) * sizeof(_TCHAR)];
  401. _tcscpy(szName, ptrname);
  402. return hr;
  403. }
  404. HRESULT CRocket::OnUpdateItem(IConsole *pConsole, long item, ITEM_TYPE itemtype)
  405. {
  406. HRESULT hr = S_FALSE;
  407. _ASSERT(NULL != this || isDeleted || RESULT == itemtype);
  408. //redraw the item
  409. IResultData *pResultData = NULL;
  410. hr = pConsole->QueryInterface(IID_IResultData, (void **)&pResultData);
  411. _ASSERT( SUCCEEDED(hr) );
  412. HRESULTITEM myhresultitem;
  413. _ASSERT(NULL != &myhresultitem);
  414. //lparam == this. See CSpaceStation::OnShow
  415. hr = pResultData->FindItemByLParam( (LPARAM)this, &myhresultitem );
  416. if ( FAILED(hr) ) {
  417. //there is no HRESULTITEM for the item, because
  418. //the item is not inserted in the current view. Exit gracefully
  419. hr = S_FALSE;
  420. } else
  421. {
  422. hr = pResultData->UpdateItem( myhresultitem );
  423. _ASSERT( SUCCEEDED(hr) );
  424. }
  425. pResultData->Release();
  426. return hr;
  427. }