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.

263 lines
5.8 KiB

  1. //
  2. // mru.cpp
  3. //
  4. #include "stdafx.h"
  5. #include "CertWiz.h"
  6. #include <commctrl.h>
  7. #include <comctrlp.h>
  8. #include <shlwapi.h>
  9. #include "mru.h"
  10. extern CCertWizApp NEAR theApp;
  11. /***********************************************************************
  12. Thunks to internal functions in ComCtl32
  13. */
  14. const TCHAR szComCtl32[] = _T("comctl32.dll");
  15. #ifdef UNICODE
  16. #define CREATE_LIST_ORD 400
  17. #define ADD_MRU_STRING_ORD 401
  18. #define ENUM_MRU_LIST_ORD 403
  19. #define FIND_MRU_STRING_ORD 402
  20. #else
  21. #define CREATE_LIST_ORD 151
  22. #define ADD_MRU_STRING_ORD 153
  23. #define ENUM_MRU_LIST_ORD 154
  24. #define FIND_MRU_STRING_ORD 155
  25. #endif
  26. #define FREE_LIST_ORD 152
  27. #define DEL_MRU_STRING_ORD 156
  28. #define DPA_SET_PTR_ORD 335
  29. typedef HANDLE (CALLBACK *CREATE_MRU_LIST)(LPMRUINFO);
  30. typedef void (CALLBACK *FREE_MRU_LIST)(HANDLE);
  31. typedef int (CALLBACK *ADD_MRU_STRING)(HANDLE, LPCTSTR);
  32. typedef int (CALLBACK *DEL_MRU_STRING)(HANDLE, int);
  33. typedef int (CALLBACK *FIND_MRU_STRING)(HANDLE, LPCTSTR, LPINT);
  34. typedef int (CALLBACK *ENUM_MRU_LIST)(HANDLE, int, LPVOID, UINT);
  35. typedef struct _DPA * HDPA;
  36. typedef BOOL (CALLBACK *DPA_SET_PTR)(HDPA hdpa, int i, LPVOID p);
  37. static HINSTANCE
  38. _GetComCtlInstance()
  39. {
  40. static HANDLE g_hInst;
  41. if (g_hInst == NULL)
  42. {
  43. g_hInst = GetModuleHandle(szComCtl32);
  44. if (NULL == g_hInst)
  45. g_hInst = LoadLibrary(szComCtl32);
  46. ASSERT(NULL != g_hInst);
  47. }
  48. return (HINSTANCE)g_hInst;
  49. }
  50. static HANDLE
  51. _CreateMRUList(LPMRUINFO pmi)
  52. {
  53. static CREATE_MRU_LIST pfnCreateMRUList;
  54. if (pfnCreateMRUList == NULL)
  55. {
  56. pfnCreateMRUList = (CREATE_MRU_LIST)GetProcAddress(
  57. _GetComCtlInstance(), (LPCSTR)CREATE_LIST_ORD);
  58. }
  59. ASSERT(pfnCreateMRUList != NULL);
  60. if (pfnCreateMRUList)
  61. {
  62. return pfnCreateMRUList(pmi);
  63. }
  64. else
  65. {
  66. return NULL;
  67. }
  68. }
  69. static void
  70. _FreeMRUList(HANDLE hMru)
  71. {
  72. static FREE_MRU_LIST pfnFreeMRUList;
  73. if (pfnFreeMRUList == NULL)
  74. {
  75. pfnFreeMRUList = (FREE_MRU_LIST)GetProcAddress(
  76. _GetComCtlInstance(), (LPCSTR)FREE_LIST_ORD);
  77. }
  78. ASSERT(pfnFreeMRUList != NULL);
  79. if (pfnFreeMRUList){pfnFreeMRUList(hMru);}
  80. }
  81. static int
  82. _AddMRUString(HANDLE hMru, LPCTSTR szString)
  83. {
  84. static ADD_MRU_STRING pfnAddMRUString;
  85. if (pfnAddMRUString == NULL)
  86. {
  87. pfnAddMRUString = (ADD_MRU_STRING)GetProcAddress(
  88. _GetComCtlInstance(), (LPCSTR)ADD_MRU_STRING_ORD);
  89. }
  90. ASSERT(pfnAddMRUString != NULL);
  91. if (pfnAddMRUString)
  92. {
  93. return pfnAddMRUString(hMru, szString);
  94. }
  95. else
  96. {
  97. return 0;
  98. }
  99. }
  100. static int
  101. _DelMRUString(HANDLE hMru, int item)
  102. {
  103. static DEL_MRU_STRING pfnDelMRUString;
  104. if (pfnDelMRUString == NULL)
  105. {
  106. pfnDelMRUString = (DEL_MRU_STRING)GetProcAddress(
  107. _GetComCtlInstance(), (LPCSTR)DEL_MRU_STRING_ORD);
  108. }
  109. ASSERT(pfnDelMRUString != NULL);
  110. return pfnDelMRUString(hMru, item);
  111. }
  112. static int
  113. _EnumMRUList(HANDLE hMru, int nItem, LPVOID lpData, UINT uLen)
  114. {
  115. static ENUM_MRU_LIST pfnEnumMRUList;
  116. if (pfnEnumMRUList == NULL)
  117. {
  118. pfnEnumMRUList = (ENUM_MRU_LIST)GetProcAddress(
  119. _GetComCtlInstance(), (LPCSTR)ENUM_MRU_LIST_ORD);
  120. }
  121. ASSERT(pfnEnumMRUList != NULL);
  122. if (pfnEnumMRUList)
  123. {
  124. return pfnEnumMRUList(hMru, nItem, lpData, uLen);
  125. }
  126. else
  127. {
  128. return 0;
  129. }
  130. }
  131. #if 0
  132. static BOOL
  133. _DPA_SetPtr(HDPA hdpa, int i, LPVOID p)
  134. {
  135. static DPA_SET_PTR pfnDPASetPtr;
  136. if (pfnDPASetPtr == NULL)
  137. {
  138. pfnDPASetPtr = (DPA_SET_PTR)GetProcAddress(
  139. _GetComCtlInstance(), (LPCSTR)DPA_SET_PTR_ORD);
  140. }
  141. ASSERT(pfnDPASetPtr != NULL);
  142. return pfnDPASetPtr(hdpa, i, p);
  143. }
  144. #endif
  145. /****************************************************************************/
  146. HANDLE
  147. CreateMRU(const CString& mru_name, int nMax)
  148. {
  149. MRUINFO mi =
  150. {
  151. sizeof(MRUINFO),
  152. nMax,
  153. MRU_CACHEWRITE,
  154. theApp.RegOpenKeyWizard(),
  155. mru_name,
  156. NULL // NOTE: use default string compare
  157. };
  158. //Call the comctl32 mru implementation to load the MRU from
  159. //the registry
  160. HANDLE hMRU = _CreateMRUList(&mi);
  161. RegCloseKey(mi.hKey);
  162. return hMRU;
  163. }
  164. BOOL
  165. GetMRUEntry(HANDLE hMRU, int iIndex, CString& str)
  166. {
  167. BOOL bRes= FALSE;
  168. //Check for valid parameters
  169. ASSERT(hMRU != NULL);
  170. //Check for valid index
  171. if (iIndex >= 0 && iIndex <= _EnumMRUList(hMRU, -1, NULL, 0))
  172. {
  173. LPTSTR p = str.GetBuffer(MAX_PATH);
  174. bRes = (_EnumMRUList(hMRU, iIndex, p, MAX_PATH) > 0);
  175. str.ReleaseBuffer();
  176. }
  177. return bRes;
  178. }
  179. BOOL
  180. LoadMRU(LPCTSTR mru_name, CComboBox * pCombo, int nMax)
  181. {
  182. BOOL fRet = FALSE;
  183. HANDLE hMRU;
  184. ASSERT(mru_name != NULL && mru_name[0] != 0);
  185. ASSERT(nMax > 0);
  186. if (NULL != (hMRU = CreateMRU(mru_name, nMax)))
  187. {
  188. //First reset the hwndCombo
  189. pCombo->ResetContent();
  190. //Now load the hwndcombo with file list from MRU.
  191. int i = 0;
  192. while (nMax > 0)
  193. {
  194. CString name;
  195. if (GetMRUEntry(hMRU, i++, name))
  196. {
  197. pCombo->AddString(name);
  198. nMax--;
  199. fRet = TRUE;
  200. }
  201. else
  202. break;
  203. }
  204. _FreeMRUList(hMRU);
  205. }
  206. return fRet;
  207. }
  208. BOOL
  209. AddToMRU(LPCTSTR mru_name, CString& name)
  210. {
  211. HANDLE hMRU = CreateMRU(mru_name, 10);
  212. if (hMRU)
  213. {
  214. _AddMRUString(hMRU, name);
  215. _FreeMRUList(hMRU);
  216. return TRUE;
  217. }
  218. return FALSE;
  219. }
  220. BOOL
  221. LoadMRUToCombo(CWnd * pDlg, int id, LPCTSTR mru_name, LPCTSTR str, int mru_size)
  222. {
  223. ASSERT(mru_name != NULL);
  224. ASSERT(str != NULL);
  225. CComboBox * pCombo = (CComboBox *)CComboBox::FromHandle(pDlg->GetDlgItem(id)->m_hWnd);
  226. if (LoadMRU(mru_name, pCombo, mru_size))
  227. {
  228. // select LRU name in combobox
  229. if ( CB_ERR == pCombo->SelectString(-1, str)
  230. && !str[0] == 0
  231. )
  232. {
  233. // put our default to combobox edit
  234. pCombo->AddString(str);
  235. pCombo->SelectString(-1, str);
  236. }
  237. return TRUE;
  238. }
  239. return FALSE;
  240. }