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.

253 lines
8.0 KiB

  1. /*******************************************************************************
  2. * SPUIHelp.h *
  3. *------------*
  4. * Description:
  5. * This is the header file for user-interface helper functions. Note that
  6. * unlike SpHelper.H, this file requires the use of ATL.
  7. *-------------------------------------------------------------------------------
  8. * Copyright (c) Microsoft Corporation. All rights reserved.
  9. *******************************************************************************/
  10. #ifndef SPUIHelp_h
  11. #define SPUIHelp_h
  12. #ifndef __sapi_h__
  13. #include <sapi.h>
  14. #endif
  15. #ifndef SPError_h
  16. #include <SPError.h>
  17. #endif
  18. #ifndef SPDebug_h
  19. #include <SPDebug.h>
  20. #endif
  21. #ifndef SPHelper_h
  22. #include <SPHelper.h>
  23. #endif
  24. #ifndef __ATLBASE_H__
  25. #include <ATLBASE.h>
  26. #endif
  27. #ifndef __ATLCONV_H__
  28. #include <ATLCONV.H>
  29. #endif
  30. /****************************************************************************
  31. *
  32. *
  33. *
  34. ********************************************************************* RAL ***/
  35. //
  36. // Dont call this function directly. Use SpInitTokenComboBox or SpInitTokenListBox.
  37. //
  38. inline HRESULT SpInitTokenList(UINT MsgAddString, UINT MsgSetItemData, UINT MsgSetCurSel,
  39. HWND hwnd, const WCHAR * pszCatName,
  40. const WCHAR * pszRequiredAttrib, const WCHAR * pszOptionalAttrib)
  41. {
  42. HRESULT hr;
  43. ISpObjectToken * pToken; // NOTE: Not a CComPtr! Be Careful.
  44. CComPtr<IEnumSpObjectTokens> cpEnum;
  45. hr = SpEnumTokens(pszCatName, pszRequiredAttrib, pszOptionalAttrib, &cpEnum);
  46. if (hr == S_OK)
  47. {
  48. bool fSetDefault = false;
  49. while (cpEnum->Next(1, &pToken, NULL) == S_OK)
  50. {
  51. CSpDynamicString dstrDesc;
  52. hr = SpGetDescription(pToken, &dstrDesc);
  53. if (SUCCEEDED(hr))
  54. {
  55. USES_CONVERSION;
  56. LRESULT i = ::SendMessage(hwnd, MsgAddString, 0, (LPARAM)W2T(dstrDesc));
  57. if (i == CB_ERR || i == CB_ERRSPACE) // Note: CB_ and LB_ errors are identical values...
  58. {
  59. hr = E_OUTOFMEMORY;
  60. }
  61. else
  62. {
  63. ::SendMessage(hwnd, MsgSetItemData, i, (LPARAM)pToken);
  64. if (!fSetDefault)
  65. {
  66. ::SendMessage(hwnd, MsgSetCurSel, i, 0);
  67. fSetDefault = true;
  68. }
  69. }
  70. }
  71. if (FAILED(hr))
  72. {
  73. pToken->Release();
  74. }
  75. }
  76. }
  77. else
  78. {
  79. hr = SPERR_NO_MORE_ITEMS;
  80. }
  81. return hr;
  82. }
  83. inline HRESULT SpInitTokenComboBox(HWND hwnd, const WCHAR * pszCatName,
  84. const WCHAR * pszRequiredAttrib = NULL, const WCHAR * pszOptionalAttrib = NULL)
  85. {
  86. return SpInitTokenList(CB_ADDSTRING, CB_SETITEMDATA, CB_SETCURSEL, hwnd, pszCatName, pszRequiredAttrib, pszOptionalAttrib);
  87. }
  88. inline HRESULT SpInitTokenListBox(HWND hwnd, const WCHAR * pszCatName,
  89. const WCHAR * pszRequiredAttrib = NULL, const WCHAR * pszOptionalAttrib = NULL)
  90. {
  91. return SpInitTokenList(LB_ADDSTRING, LB_SETITEMDATA, LB_SETCURSEL, hwnd, pszCatName, pszRequiredAttrib, pszOptionalAttrib);
  92. }
  93. //
  94. // Dont call this function directly. Use SpDestoyTokenComboBox or SpDestroyTokenListBox.
  95. //
  96. inline void SpDestroyTokenList(UINT MsgGetCount, UINT MsgGetItemData, HWND hwnd)
  97. {
  98. LRESULT c = ::SendMessage(hwnd, MsgGetCount, 0, 0);
  99. for (LRESULT i = 0; i < c; i++)
  100. {
  101. IUnknown * pUnkObj = (IUnknown *)::SendMessage(hwnd, MsgGetItemData, i, 0);
  102. if (pUnkObj)
  103. {
  104. pUnkObj->Release();
  105. }
  106. }
  107. }
  108. inline void SpDestroyTokenComboBox(HWND hwnd)
  109. {
  110. SpDestroyTokenList(CB_GETCOUNT, CB_GETITEMDATA, hwnd);
  111. }
  112. inline void SpDestroyTokenListBox(HWND hwnd)
  113. {
  114. SpDestroyTokenList(LB_GETCOUNT, LB_GETITEMDATA, hwnd);
  115. }
  116. inline ISpObjectToken * SpGetComboBoxToken(HWND hwnd, WPARAM Index)
  117. {
  118. return (ISpObjectToken *)::SendMessage(hwnd, CB_GETITEMDATA, Index, 0);
  119. }
  120. inline ISpObjectToken * SpGetListBoxToken(HWND hwnd, WPARAM Index)
  121. {
  122. return (ISpObjectToken *)::SendMessage(hwnd, LB_GETITEMDATA, Index, 0);
  123. }
  124. inline ISpObjectToken * SpGetCurSelComboBoxToken(HWND hwnd)
  125. {
  126. LRESULT i = ::SendMessage(hwnd, CB_GETCURSEL, 0, 0);
  127. return (i == CB_ERR) ? NULL : SpGetComboBoxToken(hwnd, i);
  128. }
  129. inline ISpObjectToken * SpGetCurSelListBoxToken(HWND hwnd)
  130. {
  131. LRESULT i = ::SendMessage(hwnd, LB_GETCURSEL, 0, 0);
  132. return (i == LB_ERR) ? NULL : SpGetListBoxToken(hwnd, i);
  133. }
  134. //
  135. // Don't call this directly. Use SpUpdateCurSelComboBoxToken or SpUpdateCurSelListBoxToken
  136. //
  137. inline HRESULT SpUpdateCurSelToken(UINT MsgDelString, UINT MsgInsertString, UINT MsgGetItemData, UINT MsgSetItemData, UINT MsgGetCurSel, UINT MsgSetCurSel,
  138. HWND hwnd)
  139. {
  140. HRESULT hr = S_OK;
  141. LRESULT i = ::SendMessage(hwnd, MsgGetCurSel, 0, 0);
  142. if (i != CB_ERR)
  143. {
  144. ISpObjectToken * pToken = (ISpObjectToken *)::SendMessage(hwnd, MsgGetItemData, i, 0);
  145. CSpDynamicString dstrDesc;
  146. hr = SpGetDescription(pToken, &dstrDesc);
  147. if (SUCCEEDED(hr))
  148. {
  149. USES_CONVERSION;
  150. ::SendMessage(hwnd, MsgDelString, i, 0);
  151. ::SendMessage(hwnd, MsgInsertString, i, (LPARAM)W2T(dstrDesc));
  152. ::SendMessage(hwnd, MsgSetItemData, i, (LPARAM)pToken);
  153. ::SendMessage(hwnd, MsgSetCurSel, i, 0);
  154. }
  155. }
  156. return hr;
  157. }
  158. inline HRESULT SpUpdateCurSelComboBoxToken(HWND hwnd)
  159. {
  160. return SpUpdateCurSelToken(CB_DELETESTRING, CB_INSERTSTRING, CB_GETITEMDATA, CB_SETITEMDATA, CB_GETCURSEL, CB_SETCURSEL, hwnd);
  161. }
  162. inline HRESULT SpUpdateCurSelListBoxToken(HWND hwnd)
  163. {
  164. return SpUpdateCurSelToken(LB_DELETESTRING, LB_INSERTSTRING, LB_GETITEMDATA, LB_SETITEMDATA, LB_GETCURSEL, LB_SETCURSEL, hwnd);
  165. }
  166. inline HRESULT SpAddTokenToList(UINT MsgAddString, UINT MsgSetItemData, UINT MsgSetCurSel, HWND hwnd, ISpObjectToken * pToken)
  167. {
  168. CSpDynamicString dstrDesc;
  169. HRESULT hr = SpGetDescription(pToken, &dstrDesc);
  170. if (SUCCEEDED(hr))
  171. {
  172. USES_CONVERSION;
  173. LRESULT i = ::SendMessage(hwnd, MsgAddString, 0, (LPARAM)W2T(dstrDesc));
  174. if (i == CB_ERR || i == CB_ERRSPACE) // Note: CB_ and LB_ errors are identical values...
  175. {
  176. hr = E_OUTOFMEMORY;
  177. }
  178. else
  179. {
  180. ::SendMessage(hwnd, MsgSetItemData, i, (LPARAM)pToken);
  181. ::SendMessage(hwnd, MsgSetCurSel, i, 0);
  182. pToken->AddRef();
  183. }
  184. }
  185. return hr;
  186. }
  187. inline HRESULT SpAddTokenToComboBox(HWND hwnd, ISpObjectToken * pToken)
  188. {
  189. return SpAddTokenToList(CB_ADDSTRING, CB_SETITEMDATA, CB_SETCURSEL, hwnd, pToken);
  190. }
  191. inline HRESULT SpAddTokenToListBox(HWND hwnd, ISpObjectToken * pToken)
  192. {
  193. return SpAddTokenToList(LB_ADDSTRING, LB_SETITEMDATA, LB_SETCURSEL, hwnd, pToken);
  194. }
  195. inline HRESULT SpDeleteCurSelToken(UINT MsgGetCurSel, UINT MsgSetCurSel, UINT MsgGetItemData, UINT MsgDeleteString, HWND hwnd)
  196. {
  197. HRESULT hr = S_OK;
  198. LRESULT i = ::SendMessage(hwnd, MsgGetCurSel, 0, 0);
  199. if (i == CB_ERR)
  200. {
  201. hr = S_FALSE;
  202. }
  203. else
  204. {
  205. ISpObjectToken * pToken = (ISpObjectToken *)::SendMessage(hwnd, MsgGetItemData, i, 0);
  206. if (pToken)
  207. {
  208. pToken->Release();
  209. }
  210. ::SendMessage(hwnd, MsgDeleteString, i, 0);
  211. ::SendMessage(hwnd, MsgSetCurSel, i, 0);
  212. }
  213. return hr;
  214. }
  215. inline HRESULT SpDeleteCurSelComboBoxToken(HWND hwnd)
  216. {
  217. return SpDeleteCurSelToken(CB_GETCURSEL, CB_SETCURSEL, CB_GETITEMDATA, CB_DELETESTRING, hwnd);
  218. }
  219. inline HRESULT SpDeleteCurSelListBoxToken(HWND hwnd)
  220. {
  221. return SpDeleteCurSelToken(LB_GETCURSEL, CB_SETCURSEL, LB_GETITEMDATA, LB_DELETESTRING, hwnd);
  222. }
  223. #endif /* #ifndef SPUIHelp_h -- This must be the last line in the file */