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.

242 lines
5.2 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. return pfnCreateMRUList(pmi);
  61. }
  62. static void
  63. _FreeMRUList(HANDLE hMru)
  64. {
  65. static FREE_MRU_LIST pfnFreeMRUList;
  66. if (pfnFreeMRUList == NULL)
  67. {
  68. pfnFreeMRUList = (FREE_MRU_LIST)GetProcAddress(
  69. _GetComCtlInstance(), (LPCSTR)FREE_LIST_ORD);
  70. }
  71. ASSERT(pfnFreeMRUList != NULL);
  72. pfnFreeMRUList(hMru);
  73. }
  74. static int
  75. _AddMRUString(HANDLE hMru, LPCTSTR szString)
  76. {
  77. static ADD_MRU_STRING pfnAddMRUString;
  78. if (pfnAddMRUString == NULL)
  79. {
  80. pfnAddMRUString = (ADD_MRU_STRING)GetProcAddress(
  81. _GetComCtlInstance(), (LPCSTR)ADD_MRU_STRING_ORD);
  82. }
  83. ASSERT(pfnAddMRUString != NULL);
  84. return pfnAddMRUString(hMru, szString);
  85. }
  86. static int
  87. _DelMRUString(HANDLE hMru, int item)
  88. {
  89. static DEL_MRU_STRING pfnDelMRUString;
  90. if (pfnDelMRUString == NULL)
  91. {
  92. pfnDelMRUString = (DEL_MRU_STRING)GetProcAddress(
  93. _GetComCtlInstance(), (LPCSTR)DEL_MRU_STRING_ORD);
  94. }
  95. ASSERT(pfnDelMRUString != NULL);
  96. return pfnDelMRUString(hMru, item);
  97. }
  98. static int
  99. _EnumMRUList(HANDLE hMru, int nItem, LPVOID lpData, UINT uLen)
  100. {
  101. static ENUM_MRU_LIST pfnEnumMRUList;
  102. if (pfnEnumMRUList == NULL)
  103. {
  104. pfnEnumMRUList = (ENUM_MRU_LIST)GetProcAddress(
  105. _GetComCtlInstance(), (LPCSTR)ENUM_MRU_LIST_ORD);
  106. }
  107. ASSERT(pfnEnumMRUList != NULL);
  108. return pfnEnumMRUList(hMru, nItem, lpData, uLen);
  109. }
  110. #if 0
  111. static BOOL
  112. _DPA_SetPtr(HDPA hdpa, int i, LPVOID p)
  113. {
  114. static DPA_SET_PTR pfnDPASetPtr;
  115. if (pfnDPASetPtr == NULL)
  116. {
  117. pfnDPASetPtr = (DPA_SET_PTR)GetProcAddress(
  118. _GetComCtlInstance(), (LPCSTR)DPA_SET_PTR_ORD);
  119. }
  120. ASSERT(pfnDPASetPtr != NULL);
  121. return pfnDPASetPtr(hdpa, i, p);
  122. }
  123. #endif
  124. /****************************************************************************/
  125. HANDLE
  126. CreateMRU(const CString& mru_name, int nMax)
  127. {
  128. MRUINFO mi =
  129. {
  130. sizeof(MRUINFO),
  131. nMax,
  132. MRU_CACHEWRITE,
  133. theApp.RegOpenKeyWizard(),
  134. mru_name,
  135. NULL // NOTE: use default string compare
  136. };
  137. //Call the comctl32 mru implementation to load the MRU from
  138. //the registry
  139. HANDLE hMRU = _CreateMRUList(&mi);
  140. RegCloseKey(mi.hKey);
  141. return hMRU;
  142. }
  143. BOOL
  144. GetMRUEntry(HANDLE hMRU, int iIndex, CString& str)
  145. {
  146. BOOL bRes= FALSE;
  147. //Check for valid parameters
  148. ASSERT(hMRU != NULL);
  149. //Check for valid index
  150. if (iIndex >= 0 && iIndex <= _EnumMRUList(hMRU, -1, NULL, 0))
  151. {
  152. LPTSTR p = str.GetBuffer(MAX_PATH);
  153. bRes = (_EnumMRUList(hMRU, iIndex, p, MAX_PATH) > 0);
  154. str.ReleaseBuffer();
  155. }
  156. return bRes;
  157. }
  158. BOOL
  159. LoadMRU(LPCTSTR mru_name, CComboBox * pCombo, int nMax)
  160. {
  161. BOOL fRet = FALSE;
  162. HANDLE hMRU;
  163. ASSERT(mru_name != NULL && mru_name[0] != 0);
  164. ASSERT(nMax > 0);
  165. if (NULL != (hMRU = CreateMRU(mru_name, nMax)))
  166. {
  167. //First reset the hwndCombo
  168. pCombo->ResetContent();
  169. //Now load the hwndcombo with file list from MRU.
  170. int i = 0;
  171. while (nMax > 0)
  172. {
  173. CString name;
  174. if (GetMRUEntry(hMRU, i++, name))
  175. {
  176. pCombo->AddString(name);
  177. nMax--;
  178. fRet = TRUE;
  179. }
  180. else
  181. break;
  182. }
  183. _FreeMRUList(hMRU);
  184. }
  185. return fRet;
  186. }
  187. BOOL
  188. AddToMRU(LPCTSTR mru_name, CString& name)
  189. {
  190. HANDLE hMRU = CreateMRU(mru_name, 10);
  191. if (hMRU)
  192. {
  193. _AddMRUString(hMRU, name);
  194. _FreeMRUList(hMRU);
  195. return TRUE;
  196. }
  197. return FALSE;
  198. }
  199. BOOL
  200. LoadMRUToCombo(CWnd * pDlg, int id, LPCTSTR mru_name, LPCTSTR str, int mru_size)
  201. {
  202. ASSERT(mru_name != NULL);
  203. ASSERT(str != NULL);
  204. CComboBox * pCombo = (CComboBox *)CComboBox::FromHandle(pDlg->GetDlgItem(id)->m_hWnd);
  205. if (LoadMRU(mru_name, pCombo, mru_size))
  206. {
  207. // select LRU name in combobox
  208. if ( CB_ERR == pCombo->SelectString(-1, str)
  209. && !str[0] == 0
  210. )
  211. {
  212. // put our default to combobox edit
  213. pCombo->AddString(str);
  214. pCombo->SelectString(-1, str);
  215. }
  216. return TRUE;
  217. }
  218. return FALSE;
  219. }