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.

478 lines
13 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1999
  5. //
  6. // File: coldata.cpp
  7. //
  8. // Contents: Access Column Persistence data.
  9. //
  10. // History: 25-Jan-99 AnandhaG Created
  11. //
  12. //--------------------------------------------------------------------------
  13. #include "stdafx.h"
  14. #include "columninfo.h"
  15. #include "colwidth.h"
  16. CColumnData::CColumnData()
  17. {
  18. }
  19. CColumnData::~CColumnData()
  20. {
  21. }
  22. //+-------------------------------------------------------------------
  23. //
  24. // Member: GetColumnData
  25. //
  26. // Synopsis: Helper function to retrieve the column data for a
  27. // given column-id.
  28. //
  29. // Arguments: [pColID] - Column-Set identifier.
  30. // [columnSetData] - CColumnSetData, used to return the
  31. // persisted column information.
  32. //
  33. // Returns: S_OK - if data found else S_FALSE.
  34. //
  35. // History: 01-25-1999 AnandhaG Created
  36. // 05-04-1999 AnandhaG Changed first param to SColumnSetID.
  37. //
  38. //--------------------------------------------------------------------
  39. HRESULT CColumnData::GetColumnData(SColumnSetID* pColID, CColumnSetData& columnSetData)
  40. {
  41. MMC_TRY
  42. HRESULT hr = E_FAIL;
  43. do
  44. {
  45. CNodeInitObject* pNodeInit = dynamic_cast<CNodeInitObject*>(this);
  46. if (! pNodeInit)
  47. break;
  48. CViewData* pCV = pNodeInit->m_pNode->GetViewData();
  49. if (! pCV)
  50. break;
  51. CLSID clsidSnapin;
  52. hr = pNodeInit->GetSnapinCLSID(clsidSnapin);
  53. if (FAILED(hr))
  54. {
  55. ASSERT(FALSE);
  56. hr = E_FAIL;
  57. break;
  58. }
  59. // Get the persisted column data.
  60. BOOL bRet = pCV->RetrieveColumnData( clsidSnapin, *pColID, columnSetData);
  61. // No data.
  62. if (! bRet)
  63. {
  64. hr = S_FALSE;
  65. break;
  66. }
  67. hr = S_OK;
  68. } while ( FALSE );
  69. return hr;
  70. MMC_CATCH
  71. }
  72. //+-------------------------------------------------------------------
  73. //
  74. // Member: SetColumnData
  75. //
  76. // Synopsis: Helper function to set the column data for a
  77. // given column-id.
  78. //
  79. // Arguments: [pColID] - Column-Set identifier.
  80. // [columnSetData] - CColumnSetData, that should be
  81. // persisted.
  82. //
  83. // Returns: S_OK - if data is persisted else S_FALSE.
  84. //
  85. // History: 01-25-1999 AnandhaG Created
  86. // 05-04-1999 AnandhaG Changed first param to SColumnSetID.
  87. //
  88. //--------------------------------------------------------------------
  89. HRESULT CColumnData::SetColumnData(SColumnSetID* pColID, CColumnSetData& columnSetData)
  90. {
  91. MMC_TRY
  92. HRESULT hr = E_FAIL;
  93. do
  94. {
  95. CNodeInitObject* pNodeInit = dynamic_cast<CNodeInitObject*>(this);
  96. if (! pNodeInit)
  97. break;
  98. CViewData* pCV = pNodeInit->m_pNode->GetViewData();
  99. if (! pCV)
  100. break;
  101. CLSID clsidSnapin;
  102. hr = pNodeInit->GetSnapinCLSID(clsidSnapin);
  103. if (FAILED(hr))
  104. {
  105. ASSERT(FALSE);
  106. hr = E_FAIL;
  107. break;
  108. }
  109. // Copy the data into the internal data structures.
  110. BOOL bRet = pCV->SaveColumnData( clsidSnapin, *pColID, columnSetData);
  111. if (! bRet)
  112. {
  113. hr = E_FAIL;
  114. break;
  115. }
  116. hr = S_OK;
  117. } while ( FALSE );
  118. return hr;
  119. MMC_CATCH
  120. }
  121. //+-------------------------------------------------------------------
  122. //
  123. // Member: SetColumnConfigData
  124. //
  125. // Synopsis: Method snapin can call to set the column data for a
  126. // given column-id.
  127. // Any sort data that was persisted will be cleared by
  128. // this call.
  129. //
  130. // Arguments: [pColID] - Column-Set identifier.
  131. // [pcolSetData] - Column data that should be persisted.
  132. //
  133. // Returns: S_OK - if data is persisted else S_FALSE.
  134. //
  135. // History: 01-25-1999 AnandhaG Created
  136. // 05-04-1999 AnandhaG Changed first param to SColumnSetID.
  137. //
  138. //--------------------------------------------------------------------
  139. STDMETHODIMP CColumnData::SetColumnConfigData(SColumnSetID* pColID,
  140. MMC_COLUMN_SET_DATA* pColSetData)
  141. {
  142. DECLARE_SC_FOR_PUBLIC_INTERFACE(sc, _T("IColumnData::SetColumnConfigData"));
  143. if (NULL == pColID)
  144. {
  145. sc = E_INVALIDARG;
  146. TraceSnapinError(_T("NULL SColumnSetID ptr"), sc);
  147. return sc.ToHr();
  148. }
  149. if (NULL == pColSetData)
  150. {
  151. sc = E_INVALIDARG;
  152. TraceSnapinError(_T("NULL MMC_COLUMN_SET_DATA ptr"), sc);
  153. return sc.ToHr();
  154. }
  155. sc = S_FALSE;
  156. CColumnInfoList colInfoList;
  157. for (int i = 0; i < pColSetData->nNumCols; i++)
  158. {
  159. CColumnInfo colInfo;
  160. MMC_COLUMN_DATA* pColData = &(pColSetData->pColData[i]);
  161. colInfo.SetColWidth(pColData->nWidth);
  162. colInfo.SetColHidden( HDI_HIDDEN & pColData->dwFlags);
  163. colInfo.SetColIndex(pColData->nColIndex);
  164. if ( (colInfo.GetColIndex() == 0) && colInfo.IsColHidden() )
  165. return (sc = E_INVALIDARG).ToHr();
  166. // Add the CColumnInfo to the list.
  167. colInfoList.push_back(colInfo);
  168. }
  169. CColumnSetData columnSetData;
  170. columnSetData.set_ColumnInfoList(colInfoList);
  171. sc = SetColumnData(pColID, columnSetData);
  172. return sc.ToHr();
  173. }
  174. //+-------------------------------------------------------------------
  175. //
  176. // Member: GetColumnConfigData
  177. //
  178. // Synopsis: Method snapin can call to retrieve the column data for a
  179. // given column-id.
  180. //
  181. // Arguments: [pColID] - Column-Set identifier.
  182. // [ppcolSetData] - Persisted column-data that is returned.
  183. //
  184. // Returns: S_OK - if data is found else S_FALSE.
  185. //
  186. // History: 01-25-1999 AnandhaG Created
  187. // 05-04-1999 AnandhaG Changed first param to SColumnSetID.
  188. //
  189. //--------------------------------------------------------------------
  190. STDMETHODIMP CColumnData::GetColumnConfigData(SColumnSetID* pColID,
  191. MMC_COLUMN_SET_DATA** ppColSetData)
  192. {
  193. DECLARE_SC_FOR_PUBLIC_INTERFACE(sc, _T("IColumnData::GetColumnConfigData"));
  194. if (NULL == pColID)
  195. {
  196. sc = E_INVALIDARG;
  197. TraceSnapinError(_T("NULL SColumnSetID ptr"), sc);
  198. return sc.ToHr();
  199. }
  200. if (NULL == ppColSetData)
  201. {
  202. sc = E_INVALIDARG;
  203. TraceSnapinError(_T("NULL MMC_COLUMN_SET_DATA ptr"), sc);
  204. return sc.ToHr();
  205. }
  206. sc = S_FALSE;
  207. *ppColSetData = NULL;
  208. CColumnSetData columnSetData;
  209. sc = GetColumnData(pColID, columnSetData);
  210. if (S_OK != sc.ToHr())
  211. return sc.ToHr(); // data doesnt exist.
  212. CColumnInfoList* pColInfoList = columnSetData.get_ColumnInfoList();
  213. CColumnInfo colInfo;
  214. int nNumCols = pColInfoList->size();
  215. if (nNumCols <= 0)
  216. {
  217. sc = S_FALSE;
  218. return sc.ToHr();
  219. }
  220. // Allocate memory, copy & return the data.
  221. int cb = sizeof(MMC_COLUMN_SET_DATA) + sizeof(MMC_COLUMN_DATA) * nNumCols;
  222. BYTE* pb = (BYTE*)::CoTaskMemAlloc(cb);
  223. if (! pb)
  224. {
  225. sc = E_OUTOFMEMORY;
  226. return sc.ToHr();
  227. }
  228. *ppColSetData = (MMC_COLUMN_SET_DATA*)pb;
  229. (*ppColSetData)->cbSize = sizeof(MMC_COLUMN_SET_DATA);
  230. (*ppColSetData)->nNumCols = nNumCols;
  231. (*ppColSetData)->pColData = (MMC_COLUMN_DATA*)(pb + sizeof(MMC_COLUMN_SET_DATA));
  232. CColumnInfoList::iterator itColInfo;
  233. int i = 0;
  234. MMC_COLUMN_DATA* pColData = (*ppColSetData)->pColData;
  235. for (itColInfo = pColInfoList->begin();itColInfo != pColInfoList->end(); itColInfo++, i++)
  236. {
  237. pColData[i].nWidth = (*itColInfo).GetColWidth();
  238. pColData[i].dwFlags = (*itColInfo).IsColHidden() ? HDI_HIDDEN : 0;
  239. pColData[i].nColIndex = (*itColInfo).GetColIndex();
  240. }
  241. return sc.ToHr();
  242. }
  243. //+-------------------------------------------------------------------
  244. //
  245. // Member: SetColumnSortData
  246. //
  247. // Synopsis: Method snapin can call to set the sort data for a
  248. // given column-id.
  249. // Any column config data (width, order...) that was
  250. // persisted will not be affected by this call.
  251. //
  252. // Arguments: [pColID] - Column-Set identifier.
  253. // [pcolSorData] - Sort data that should be persisted.
  254. //
  255. // Returns: S_OK - if data is persisted else S_FALSE.
  256. //
  257. // History: 01-25-1999 AnandhaG Created
  258. // 05-04-1999 AnandhaG Changed first param to SColumnSetID.
  259. //
  260. //--------------------------------------------------------------------
  261. STDMETHODIMP CColumnData::SetColumnSortData(SColumnSetID* pColID,
  262. MMC_SORT_SET_DATA* pColSortData)
  263. {
  264. DECLARE_SC_FOR_PUBLIC_INTERFACE(sc, _T("IColumnData::SetColumnSortData"));
  265. if (NULL == pColID)
  266. {
  267. sc = E_INVALIDARG;
  268. TraceSnapinError(_T("NULL SColumnSetID ptr"), sc);
  269. return sc.ToHr();
  270. }
  271. if (NULL == pColSortData)
  272. {
  273. sc = E_INVALIDARG;
  274. TraceSnapinError(_T("NULL MMC_COLUMN_SET_DATA ptr"), sc);
  275. return sc.ToHr();
  276. }
  277. sc = S_FALSE;
  278. // First get old data. We need to preserve the width, view info.
  279. CColumnSetData columnSetData;
  280. sc = GetColumnData(pColID, columnSetData);
  281. if (sc)
  282. return sc.ToHr();
  283. CColumnSortList* pColSortList = columnSetData.get_ColumnSortList();
  284. pColSortList->clear();
  285. // For MMC version 1.2 we do only single column sorting.
  286. if (pColSortData->nNumItems > 1)
  287. {
  288. sc = S_FALSE;
  289. return sc.ToHr();
  290. }
  291. CColumnSortInfo colSortInfo;
  292. for (int i = 0; i < pColSortData->nNumItems; i++)
  293. {
  294. MMC_SORT_DATA* pSortData = &(pColSortData->pSortData[i]);
  295. ::ZeroMemory(&colSortInfo, sizeof(colSortInfo));
  296. colSortInfo.m_nCol = pSortData->nColIndex;
  297. colSortInfo.m_dwSortOptions = pSortData->dwSortOptions;
  298. colSortInfo.m_lpUserParam = pSortData->ulReserved;
  299. // Add the CColumnSortInfo to the list.
  300. pColSortList->push_back(colSortInfo);
  301. }
  302. sc = SetColumnData(pColID, columnSetData);
  303. return sc.ToHr();
  304. }
  305. //+-------------------------------------------------------------------
  306. //
  307. // Member: GetColumnSortData
  308. //
  309. // Synopsis: Method snapin can call to retrieve the column sort data
  310. // for a given column-id.
  311. //
  312. // Arguments: [pColID] - Column-Set identifier.
  313. // [ppcolSortData] - Persisted column-sort-data that is returned.
  314. //
  315. // Returns: S_OK - if data is found else S_FALSE.
  316. //
  317. // History: 01-25-1999 AnandhaG Created
  318. // 05-04-1999 AnandhaG Changed first param to SColumnSetID.
  319. //
  320. //--------------------------------------------------------------------
  321. STDMETHODIMP CColumnData::GetColumnSortData(SColumnSetID* pColID,
  322. MMC_SORT_SET_DATA** ppColSortData)
  323. {
  324. DECLARE_SC_FOR_PUBLIC_INTERFACE(sc, _T("IColumnData::SetColumnSortData"));
  325. if (NULL == pColID)
  326. {
  327. sc = E_INVALIDARG;
  328. TraceSnapinError(_T("NULL SColumnSetID ptr"), sc);
  329. return sc.ToHr();
  330. }
  331. if (NULL == ppColSortData)
  332. {
  333. sc = E_INVALIDARG;
  334. TraceSnapinError(_T("NULL MMC_COLUMN_SET_DATA ptr"), sc);
  335. return sc.ToHr();
  336. }
  337. sc = S_FALSE;
  338. *ppColSortData = NULL;
  339. CColumnSetData columnSetData;
  340. sc = GetColumnData(pColID, columnSetData);
  341. if (S_OK != sc.ToHr())
  342. return sc.ToHr();
  343. CColumnSortList* pColSortList = columnSetData.get_ColumnSortList();
  344. CColumnSortInfo colSortInfo;
  345. int nNumItems = pColSortList->size();
  346. if (nNumItems <= 0)
  347. {
  348. sc = S_FALSE;
  349. return sc.ToHr();
  350. }
  351. // For MMC 1.2 we sort on only one column.
  352. ASSERT(nNumItems == 1);
  353. int cb = sizeof(MMC_SORT_SET_DATA) + sizeof(MMC_SORT_DATA) * nNumItems;
  354. BYTE* pb = (BYTE*)::CoTaskMemAlloc(cb);
  355. if (! pb)
  356. {
  357. sc = E_OUTOFMEMORY;
  358. return sc.ToHr();
  359. }
  360. *ppColSortData = (MMC_SORT_SET_DATA*)pb;
  361. (*ppColSortData)->cbSize = sizeof(MMC_SORT_SET_DATA);
  362. (*ppColSortData)->nNumItems = nNumItems;
  363. (*ppColSortData)->pSortData = (MMC_SORT_DATA*)(pb + sizeof(MMC_SORT_SET_DATA));
  364. CColumnSortList::iterator itSortInfo;
  365. int i = 0;
  366. MMC_SORT_DATA* pSortData = (*ppColSortData)->pSortData;
  367. for (itSortInfo = pColSortList->begin();itSortInfo != pColSortList->end(); itSortInfo++, i++)
  368. {
  369. pSortData[i].nColIndex = (*itSortInfo).m_nCol;
  370. pSortData[i].dwSortOptions = (*itSortInfo).m_dwSortOptions;
  371. pSortData[i].ulReserved = (*itSortInfo).m_lpUserParam;
  372. }
  373. sc = S_OK;
  374. return sc.ToHr();
  375. }
  376. HRESULT WINAPI ColumnInterfaceFunc(void* pv, REFIID riid, LPVOID* ppv, DWORD_PTR dw)
  377. {
  378. *ppv = NULL;
  379. ASSERT(IID_IColumnData == riid);
  380. CColumnData* pColData = (CColumnData*)(pv);
  381. CNodeInitObject* pNodeInit = dynamic_cast<CNodeInitObject*>(pColData);
  382. if (pNodeInit && pNodeInit->GetComponent())
  383. {
  384. IColumnData* pIColData = dynamic_cast<IColumnData*>(pNodeInit);
  385. pIColData->AddRef();
  386. *ppv = static_cast<void*>(pIColData);
  387. return S_OK;
  388. }
  389. return E_NOINTERFACE;
  390. }