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.

377 lines
9.5 KiB

  1. // Compont.cpp : Implementation of CComponent
  2. #include "stdafx.h"
  3. #include "BOMSnap.h"
  4. #include "ScopeNode.h"
  5. #include "CompData.h"
  6. #include "Compont.h"
  7. #include "qryitem.h"
  8. #include <atlgdi.h>
  9. #include <algorithm>
  10. // Toolbar button data (must match bitmap order in res\toolbar.bmp)
  11. static struct
  12. {
  13. int iMenuID;
  14. int iTextID;
  15. int iTipTextID;
  16. } ToolbarBtns[] =
  17. {
  18. { MID_EDITQUERY, BTN_EDITQUERY, TIP_EDITQUERY },
  19. { MID_STOPQUERY, BTN_STOPQUERY, TIP_STOPQUERY }
  20. };
  21. /////////////////////////////////////////////////////////////////////////////
  22. // CComponent
  23. STDMETHODIMP
  24. CComponent::Initialize(LPCONSOLE lpConsole)
  25. {
  26. VALIDATE_POINTER(lpConsole);
  27. ASSERT(lpConsole != NULL);
  28. if (lpConsole == NULL) return E_INVALIDARG;
  29. m_spConsole = lpConsole;
  30. m_spResultData = lpConsole;
  31. m_spHeaderCtrl = lpConsole;
  32. ASSERT(m_spHeaderCtrl != NULL);
  33. ASSERT(m_spResultData != NULL);
  34. return S_OK;
  35. }
  36. STDMETHODIMP
  37. CComponent::Notify(LPDATAOBJECT lpDataObject, MMC_NOTIFY_TYPE event, LPARAM arg, LPARAM param)
  38. {
  39. IBOMObjectPtr spObj = lpDataObject;
  40. if (spObj == NULL)
  41. {
  42. // until special notifications handled
  43. return S_FALSE;
  44. }
  45. HRESULT hr = S_OK;
  46. if (event == MMCN_SHOW)
  47. {
  48. // if selecting node
  49. if (arg)
  50. {
  51. m_spCurScopeNode = static_cast<CScopeNode*>((IBOMObject*)spObj);
  52. hr = m_spCurScopeNode->AttachComponent(this);
  53. }
  54. else
  55. {
  56. if (m_spCurScopeNode != NULL)
  57. {
  58. m_spCurScopeNode->DetachComponent(this);
  59. m_spCurScopeNode = NULL;
  60. }
  61. m_vRowItems.clear();
  62. }
  63. }
  64. else
  65. {
  66. hr = spObj->Notify(m_spConsole, event, arg, param);
  67. }
  68. return hr;
  69. }
  70. HRESULT
  71. CComponent::AddMenuItems(LPDATAOBJECT pDataObject, LPCONTEXTMENUCALLBACK pCallback, long* plAllowed)
  72. {
  73. IBOMObjectPtr spObj = pDataObject;
  74. if (spObj == NULL) return E_INVALIDARG;
  75. return spObj->AddMenuItems(pCallback, plAllowed);
  76. }
  77. HRESULT
  78. CComponent::Command(long lCommand, LPDATAOBJECT pDataObject)
  79. {
  80. IBOMObjectPtr spObj = pDataObject;
  81. if (spObj == NULL) return E_INVALIDARG;
  82. return spObj->MenuCommand(m_spConsole, lCommand);
  83. }
  84. HRESULT
  85. CComponent::QueryPagesFor(LPDATAOBJECT pDataObject)
  86. {
  87. IBOMObjectPtr spObj = pDataObject;
  88. if (spObj == NULL) return E_INVALIDARG;
  89. return spObj->QueryPagesFor();
  90. }
  91. HRESULT
  92. CComponent::CreatePropertyPages(LPPROPERTYSHEETCALLBACK pProvider, LONG_PTR handle, LPDATAOBJECT pDataObject)
  93. {
  94. IBOMObjectPtr spObj = pDataObject;
  95. if (spObj == NULL) return E_INVALIDARG;
  96. return spObj->CreatePropertyPages(pProvider, handle);
  97. }
  98. HRESULT
  99. CComponent::GetWatermarks(LPDATAOBJECT pDataObject, HBITMAP* phWatermark, HBITMAP* phHeader,
  100. HPALETTE* phPalette, BOOL* bStreach)
  101. {
  102. IBOMObjectPtr spObj = pDataObject;
  103. if (spObj == NULL) return E_INVALIDARG;
  104. return spObj->GetWatermarks(phWatermark, phHeader, phPalette, bStreach);
  105. }
  106. HRESULT
  107. CComponent::SetControlbar(LPCONTROLBAR pControlbar)
  108. {
  109. HRESULT hr;
  110. if (pControlbar != NULL)
  111. {
  112. m_spControlbar = pControlbar;
  113. if (m_spToolbar == NULL)
  114. {
  115. hr = pControlbar->Create(TOOLBAR, this, (LPUNKNOWN*)&m_spToolbar);
  116. ASSERT(SUCCEEDED(hr));
  117. if (m_spToolbar != NULL)
  118. {
  119. CBitmap bmpToolbar;
  120. bmpToolbar.LoadBitmap(IDB_TOOLBAR);
  121. ASSERT(bmpToolbar);
  122. if (bmpToolbar)
  123. {
  124. hr = m_spToolbar->AddBitmap(lengthof(ToolbarBtns), bmpToolbar, 16, 16, RGB(255,0,255));
  125. ASSERT(SUCCEEDED(hr));
  126. if (SUCCEEDED(hr))
  127. {
  128. MMCBUTTON btn;
  129. btn.fsState = 0;
  130. btn.fsType = TBSTYLE_BUTTON;
  131. for (int iBtn=0; iBtn < lengthof(ToolbarBtns); iBtn++)
  132. {
  133. CString strBtnText;
  134. strBtnText.LoadString(ToolbarBtns[iBtn].iTextID);
  135. CString strTipText;
  136. strTipText.LoadString(ToolbarBtns[iBtn].iTipTextID);
  137. btn.nBitmap = iBtn;
  138. btn.idCommand = ToolbarBtns[iBtn].iMenuID;
  139. btn.lpButtonText = const_cast<LPWSTR>((LPCWSTR)strBtnText);
  140. btn.lpTooltipText = const_cast<LPWSTR>((LPCWSTR)strTipText);
  141. hr = m_spToolbar->InsertButton(iBtn, &btn);
  142. ASSERT(SUCCEEDED(hr));
  143. }
  144. }
  145. }
  146. }
  147. }
  148. }
  149. else
  150. {
  151. if (m_spControlbar != NULL && m_spToolbar != NULL)
  152. m_spControlbar->Detach(m_spToolbar);
  153. }
  154. return S_OK;
  155. }
  156. HRESULT
  157. CComponent::ControlbarNotify (MMC_NOTIFY_TYPE event, LPARAM arg, LPARAM param)
  158. {
  159. HRESULT hr = S_FALSE;
  160. switch(event)
  161. {
  162. case MMCN_SELECT:
  163. // if selecting, update toolbar
  164. if (HIWORD(arg))
  165. {
  166. IBOMObjectPtr spObj = reinterpret_cast<LPDATAOBJECT>(param);
  167. if (spObj == NULL)
  168. break;
  169. ASSERT(m_spControlbar != NULL && m_spToolbar != NULL);
  170. // Let selected object set buttons
  171. // Show/hide toolbar based on return value
  172. m_spControlbar->Attach(TOOLBAR, m_spToolbar);
  173. if (spObj->SetToolButtons(m_spToolbar) != S_OK)
  174. m_spControlbar->Detach(m_spToolbar);
  175. }
  176. hr = S_OK;
  177. break;
  178. case MMCN_BTN_CLICK:
  179. IBOMObjectPtr spObj = reinterpret_cast<LPDATAOBJECT>(arg);
  180. if (spObj == NULL)
  181. break;
  182. // treat button click as corresponding menu item
  183. hr = spObj->MenuCommand(m_spConsole, param);
  184. break;
  185. }
  186. return hr;
  187. }
  188. HRESULT
  189. CComponent::Destroy(MMC_COOKIE cookie)
  190. {
  191. m_spConsole = NULL;
  192. m_spResultData = NULL;
  193. m_spControlbar = NULL;
  194. m_spHeaderCtrl = NULL;
  195. m_spToolbar = NULL;
  196. m_spCurScopeNode = NULL;
  197. return S_OK;
  198. }
  199. STDMETHODIMP
  200. CComponent::GetResultViewType(MMC_COOKIE cookie, LPOLESTR* ppViewType, long* pViewOptions)
  201. {
  202. CScopeNode* pNode = m_pCompData->CookieToScopeNode(cookie);
  203. if (pNode == NULL) return E_INVALIDARG;
  204. return pNode->GetResultViewType(ppViewType, pViewOptions);
  205. }
  206. STDMETHODIMP
  207. CComponent::QueryDataObject(MMC_COOKIE cookie, DATA_OBJECT_TYPES type, LPDATAOBJECT* ppDataObject)
  208. {
  209. if (type == CCT_RESULT)
  210. {
  211. if ( (m_spCurScopeNode == NULL) ||
  212. (cookie < 0) || (cookie >= m_vRowItems.size()) )
  213. {
  214. return E_INVALIDARG;
  215. }
  216. CQueryableNode* pQNode = dynamic_cast<CQueryableNode*>(m_spCurScopeNode.p);
  217. if (pQNode == NULL) return E_UNEXPECTED;
  218. CComObject<CQueryItem>* pItem;
  219. HRESULT hr = CComObject<CQueryItem>::CreateInstance(&pItem);
  220. if( SUCCEEDED(hr) )
  221. {
  222. hr = pItem->Initialize(pQNode, &m_vRowItems[cookie]);
  223. }
  224. if( SUCCEEDED(hr) )
  225. {
  226. hr = pItem->QueryInterface(IID_IDataObject, (void**)ppDataObject);
  227. }
  228. if( FAILED(hr) )
  229. {
  230. delete pItem;
  231. }
  232. return hr;
  233. }
  234. else
  235. {
  236. ASSERT(m_pCompData != NULL);
  237. return m_pCompData->QueryDataObject(cookie, type, ppDataObject);
  238. }
  239. }
  240. STDMETHODIMP CComponent::GetDisplayInfo(RESULTDATAITEM* pRDI)
  241. {
  242. VALIDATE_POINTER( pRDI );
  243. HRESULT hr = S_OK;
  244. if (pRDI->bScopeItem)
  245. {
  246. CScopeNode* pnode = reinterpret_cast<CScopeNode*>(pRDI->lParam);
  247. hr = pnode->GetDisplayInfo(pRDI);
  248. }
  249. else
  250. {
  251. if (pRDI->nIndex >= 0 && pRDI->nIndex < m_vRowItems.size())
  252. {
  253. if (pRDI->mask & RDI_STR)
  254. pRDI->str = const_cast<LPWSTR>(m_vRowItems[pRDI->nIndex][pRDI->nCol]);
  255. if (pRDI->mask & RDI_IMAGE)
  256. pRDI->nImage = m_vRowItems[pRDI->nIndex].GetIconIndex();
  257. }
  258. else
  259. {
  260. hr = E_INVALIDARG;
  261. }
  262. }
  263. return hr;
  264. }
  265. STDMETHODIMP
  266. CComponent::CompareObjects(LPDATAOBJECT lpDataObjectA, LPDATAOBJECT lpDataObjectB)
  267. {
  268. IUnknownPtr pUnkA= lpDataObjectA;
  269. IUnknownPtr pUnkB = lpDataObjectB;
  270. return (pUnkA == pUnkB) ? S_OK : S_FALSE;
  271. }
  272. STDMETHODIMP
  273. CComponent::SortItems(int nColumn, DWORD dwSortOptions, LPARAM lUserParam)
  274. {
  275. if( m_vRowItems.empty() ) return S_FALSE;
  276. CRowCompare rc(nColumn, dwSortOptions & RSI_DESCENDING);
  277. std::sort(m_vRowItems.begin(), m_vRowItems.end(), rc);
  278. return S_OK;
  279. }
  280. void
  281. CComponent::ClearRowItems()
  282. {
  283. m_vRowItems.clear();
  284. m_spResultData->SetItemCount(0,0);
  285. }
  286. void
  287. CComponent::AddRowItems(RowItemVector& vRowItems)
  288. {
  289. m_vRowItems.insert(m_vRowItems.end(), vRowItems.begin(), vRowItems.end());
  290. CRowCompare rc(0, 0);
  291. std::sort(m_vRowItems.begin(), m_vRowItems.end(), rc);
  292. m_spResultData->SetItemCount(m_vRowItems.size(), 0);
  293. }