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.

440 lines
12 KiB

  1. /*++
  2. Copyright (c) 1990-1998, Microsoft Corporation All rights reserved.
  3. Module Name:
  4. filemru.cpp
  5. Abstract:
  6. This module contains the functions for implementing file mru
  7. in file open and file save dialog boxes
  8. Revision History:
  9. 01/22/98 arulk created
  10. --*/
  11. // precompiled headers
  12. #include "precomp.h"
  13. #pragma hdrstop
  14. #include "cdids.h"
  15. #include "filemru.h"
  16. #ifndef ASSERT
  17. #define ASSERT Assert
  18. #endif
  19. #define REGSTR_PATH_FILEMRU TEXT("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\ComDlg32\\OpenSaveMRU\\")
  20. #define REGSTR_PATH_LASTVISITED TEXT("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\ComDlg32\\LastVisitedMRU\\")
  21. HANDLE CreateMRU(LPCTSTR pszExt, int nMax)
  22. {
  23. TCHAR szRegPath[256];
  24. MRUINFO mi = {
  25. sizeof(MRUINFO),
  26. nMax,
  27. MRU_CACHEWRITE,
  28. HKEY_CURRENT_USER,
  29. szRegPath,
  30. NULL // NOTE: use default string compare
  31. };
  32. //Get the Registry path for the given file type MRU
  33. lstrcpy(szRegPath,REGSTR_PATH_FILEMRU);
  34. StrCatBuff(szRegPath, pszExt ? pszExt : TEXT("*"), ARRAYSIZE(szRegPath));
  35. //Call the comctl32 mru implementation to load the MRU from
  36. //the registry
  37. return CreateMRUList(&mi);
  38. }
  39. BOOL GetMRUEntry(HANDLE hMRU, int iIndex, LPTSTR lpString, UINT cbSize)
  40. {
  41. //Check for valid parameters
  42. if(!lpString || !cbSize || !hMRU)
  43. {
  44. return FALSE;
  45. }
  46. //Check for valid index
  47. if (iIndex < 0 || iIndex > EnumMRUList(hMRU, -1, NULL, 0))
  48. {
  49. return FALSE;
  50. }
  51. if ((EnumMRUList(hMRU, iIndex, lpString, cbSize) > 0 ))
  52. {
  53. return TRUE;
  54. }
  55. return FALSE;
  56. }
  57. typedef struct {
  58. HANDLE mru;
  59. LPTSTR psz;
  60. } EXTMRU, *PEXTMRU;
  61. STDAPI_(int) _FreeExtMru(void * pvItem, void * pvData)
  62. {
  63. PEXTMRU pem = (PEXTMRU) pvItem;
  64. if (pem)
  65. {
  66. ASSERT(pem->psz);
  67. ASSERT(pem->mru);
  68. LocalFree(pem->psz);
  69. FreeMRUList(pem->mru);
  70. LocalFree(pem);
  71. return TRUE;
  72. }
  73. return FALSE;
  74. }
  75. STDAPI_(int) _ExtMruFindExt(void * pvFind, void * pvItem, LPARAM pvParam)
  76. {
  77. ASSERT(pvFind && pvItem);
  78. return StrCmp(((PEXTMRU)pvItem)->psz, (LPCTSTR)pvFind);
  79. }
  80. PEXTMRU _AllocExtMru(LPCTSTR pszExt, int nMax)
  81. {
  82. PEXTMRU pem = (PEXTMRU) LocalAlloc(LPTR, SIZEOF(EXTMRU));
  83. if (pem)
  84. {
  85. pem->psz = StrDup(pszExt);
  86. pem->mru = CreateMRU (pszExt, nMax);
  87. if (pem->psz && pem->mru)
  88. return pem;
  89. _FreeExtMru(pem, NULL);
  90. }
  91. return NULL;
  92. }
  93. HDPA _CreateExtMruDpa(LPCTSTR pszFilter, int nMax, int *pcItems)
  94. {
  95. //Convert the filter string of form *.c;*.cpp;*.h into form
  96. // *.c\0*.cpp\0*.h\0. Also count the file types
  97. LPTSTR pszFree = StrDup(pszFilter);
  98. *pcItems = 0;
  99. if (pszFree)
  100. {
  101. HDPA hdpa = DPA_Create(4);
  102. if (hdpa)
  103. {
  104. LPTSTR pszNext = pszFree;
  105. int cItems = 0;
  106. LPTSTR pszSemi;
  107. do
  108. {
  109. pszSemi = StrChr(pszNext, CHAR_SEMICOLON);
  110. if (pszSemi)
  111. *pszSemi = CHAR_NULL;
  112. LPTSTR pszExt = PathFindExtension(pszNext);
  113. if (*pszExt)
  114. {
  115. // walk past the dot...
  116. pszExt++;
  117. // make sure this extension isnt already in the dpa
  118. if (-1 == DPA_Search(hdpa, pszExt, 0, _ExtMruFindExt, NULL, 0))
  119. {
  120. PEXTMRU pem = _AllocExtMru(pszExt, nMax);
  121. if (!pem)
  122. break;
  123. DPA_SetPtr(hdpa, cItems++, (void *)pem);
  124. }
  125. }
  126. // we only have a next if there was more than one...
  127. if (pszSemi)
  128. pszNext = pszSemi + 1;
  129. } while (pszSemi);
  130. *pcItems = cItems;
  131. }
  132. LocalFree(pszFree);
  133. return hdpa;
  134. }
  135. return NULL;
  136. }
  137. BOOL LoadMRU(LPCTSTR pszFilter, HWND hwndCombo, int nMax)
  138. {
  139. //Check if valid filter string is passed
  140. if (!pszFilter || !pszFilter[0] || nMax <= 0)
  141. {
  142. return FALSE;
  143. }
  144. //First reset the hwndCombo
  145. SendMessage(hwndCombo, CB_RESETCONTENT, (WPARAM)0L, (LPARAM)0L);
  146. int cDPAItems;
  147. HDPA hdpa = _CreateExtMruDpa(pszFilter, nMax, &cDPAItems);
  148. if (hdpa)
  149. {
  150. TCHAR szFile[MAX_PATH];
  151. //Set the comboboxex item values
  152. COMBOBOXEXITEM cbexItem = {0};
  153. cbexItem.mask = CBEIF_TEXT; // This combobox displays only text
  154. cbexItem.iItem = -1; // Always insert the item at the end
  155. cbexItem.pszText = szFile; // This buffer contains the string
  156. cbexItem.cchTextMax = ARRAYSIZE(szFile); // Size of the buffer
  157. //Now load the hwndcombo with file list from MRU.
  158. //We use a kind of round robin algorithm for filling
  159. //the mru. We start with first MRU and try to fill the combobox
  160. //with one string from each mru. Until we have filled the required
  161. //strings or we have exhausted all strings in the mrus
  162. for (int j = 0; nMax > 0; j++)
  163. {
  164. //Variable used for checking whether we are able to load atlease one string
  165. //during the loop
  166. BOOL fCouldLoadAtleastOne = FALSE;
  167. for (int i = 0; i < cDPAItems && nMax > 0; i++)
  168. {
  169. PEXTMRU pem = (PEXTMRU)DPA_FastGetPtr(hdpa, i);
  170. if (pem && GetMRUEntry(pem->mru, j, szFile, SIZECHARS(szFile)))
  171. {
  172. SendMessage(hwndCombo, CBEM_INSERTITEM, (WPARAM)0, (LPARAM)(void *)&cbexItem);
  173. nMax--;
  174. fCouldLoadAtleastOne = TRUE;
  175. }
  176. }
  177. //Check for possible infinite loop
  178. if(!fCouldLoadAtleastOne)
  179. {
  180. //We couldn't load string from any of the MRU's so there's no point
  181. //in continuing this loop further. This is the max number of strings
  182. // we can load for this user, for this filter type.
  183. break;
  184. }
  185. }
  186. DPA_DestroyCallback(hdpa, _FreeExtMru, NULL);
  187. }
  188. return TRUE;
  189. }
  190. //This function adds the selected file into the MRU of the appropriate file MRU's
  191. //This functions also takes care of MultiFile Select case in which the file selected
  192. //will c:\winnt\file1.c\0file2.c\0file3.c\0. Refer GetOpenFileName documentation for
  193. // how the multifile is returned.
  194. BOOL AddToMRU(LPOPENFILENAME lpOFN)
  195. {
  196. TCHAR szDir[MAX_PATH];
  197. TCHAR szFile[MAX_PATH];
  198. LPTSTR lpFile;
  199. LPTSTR lpExt;
  200. BOOL fAddToStar = TRUE;
  201. HANDLE hMRUStar;
  202. //Check if we have valid file name
  203. if (!lpOFN->lpstrFile)
  204. return FALSE;
  205. hMRUStar = CreateMRU(szStar, 10); //File MRU For *.* file extension
  206. //Copy the Directory for the selected file
  207. lstrcpyn(szDir, lpOFN->lpstrFile, lpOFN->nFileOffset);
  208. //point to the first file
  209. lpFile = lpOFN->lpstrFile + lpOFN->nFileOffset;
  210. do
  211. {
  212. // PERF: if there are multiple files of the same extension type,
  213. // don't keep re-creating the mru.
  214. lpExt = PathFindExtension(lpFile);
  215. if (lpExt && *lpExt)
  216. {
  217. lpExt += 1; // Remove dot
  218. }
  219. HANDLE hMRU = CreateMRU(lpExt, 10);
  220. if (hMRU)
  221. {
  222. PathCombine(szFile, szDir, lpFile);
  223. AddMRUString(hMRU, szFile);
  224. if((lstrcmpi(lpExt, szStar)) && hMRUStar)
  225. {
  226. //Add to the *.* file mru also
  227. AddMRUString(hMRUStar, szFile);
  228. }
  229. FreeMRUList(hMRU);
  230. }
  231. lpFile = lpFile + lstrlen(lpFile) + 1;
  232. } while (((lpOFN->Flags & OFN_ALLOWMULTISELECT)) && (*lpFile != CHAR_NULL));
  233. //Free the * file mru
  234. if (hMRUStar)
  235. {
  236. FreeMRUList(hMRUStar);
  237. }
  238. return TRUE;
  239. }
  240. ////////////////////////////////////////////////////////////////////////////
  241. //
  242. // Last Visited MRU Implementation
  243. // All Strings stored in the registry are stored in unicode format.
  244. //
  245. ////////////////////////////////////////////////////////////////////////////
  246. ////////////////////////////////////////////////////////////////////////////
  247. // CreateLastVisitedItem
  248. ////////////////////////////////////////////////////////////////////////////
  249. LPBYTE CreateLastVisitedItem(LPCWSTR wszModule, LPCWSTR wszPath, DWORD *pcbOut)
  250. {
  251. LPBYTE pitem = NULL;
  252. DWORD cbLen1, cbLen2;
  253. cbLen1 = CbFromCchW(lstrlenW(wszModule)+1);
  254. cbLen2 = CbFromCchW(lstrlenW(wszPath)+1);
  255. pitem = (LPBYTE) LocalAlloc(LPTR, cbLen1+cbLen2);
  256. if (pitem)
  257. {
  258. memcpy(pitem, wszModule, cbLen1);
  259. memcpy(pitem+cbLen1, wszPath, cbLen2);
  260. *pcbOut = cbLen1+cbLen2;
  261. }
  262. return pitem;
  263. }
  264. int cdecl LastVisitedCompareProc(const void *p1, const void *p2, size_t cb)
  265. {
  266. return StrCmpIW((LPWSTR)p1,(LPWSTR)p2);
  267. }
  268. ////////////////////////////////////////////////////////////////////////////
  269. // Store all the strings in the registry as unicode strings
  270. ////////////////////////////////////////////////////////////////////////////
  271. BOOL AddToLastVisitedMRU(LPCTSTR pszFile, int nFileOffset)
  272. {
  273. BOOL bRet = FALSE;
  274. if (!PathIsTemporary(pszFile))
  275. {
  276. MRUDATAINFO mi =
  277. {
  278. SIZEOF(MRUDATAINFO),
  279. MAX_MRU,
  280. MRU_BINARY | MRU_CACHEWRITE,
  281. HKEY_CURRENT_USER,
  282. REGSTR_PATH_LASTVISITED,
  283. LastVisitedCompareProc
  284. };
  285. HANDLE hMRU = CreateMRUList((MRUINFO *)&mi);
  286. if (hMRU)
  287. {
  288. WCHAR wszDir[MAX_PATH];
  289. WCHAR wszModulePath[MAX_PATH];
  290. //Get the module name
  291. GetModuleFileNameWrapW(GetModuleHandle(NULL), wszModulePath, ARRAYSIZE(wszModulePath));
  292. WCHAR* pszModuleName = PathFindFileNameW(wszModulePath);
  293. int i = FindMRUData(hMRU, (void *)pszModuleName, CbFromCchW(lstrlenW(pszModuleName)+1), NULL);
  294. if (i >= 0)
  295. {
  296. DelMRUString(hMRU, i);
  297. }
  298. //Get the Directoy from file.
  299. SHTCharToUnicode(pszFile, wszDir, ARRAYSIZE(wszDir));
  300. wszDir[nFileOffset - 1] = CHAR_NULL;
  301. DWORD cbSize;
  302. LPBYTE pitem = CreateLastVisitedItem(pszModuleName, wszDir, &cbSize);
  303. if (pitem)
  304. {
  305. AddMRUData(hMRU, pitem, cbSize);
  306. bRet = TRUE;
  307. LocalFree(pitem);
  308. }
  309. FreeMRUList(hMRU);
  310. }
  311. }
  312. return bRet;
  313. }
  314. BOOL GetPathFromLastVisitedMRU(LPTSTR pszDir, DWORD cchDir)
  315. {
  316. BOOL bRet = FALSE;
  317. MRUDATAINFO mi =
  318. {
  319. SIZEOF(MRUDATAINFO),
  320. MAX_MRU,
  321. MRU_BINARY | MRU_CACHEWRITE,
  322. HKEY_CURRENT_USER,
  323. REGSTR_PATH_LASTVISITED,
  324. LastVisitedCompareProc
  325. };
  326. pszDir[0] = 0;
  327. HANDLE hMRU = CreateMRUList((MRUINFO *)&mi);
  328. if (hMRU)
  329. {
  330. WCHAR wszModulePath[MAX_PATH];
  331. //Get the module name
  332. GetModuleFileNameWrapW(GetModuleHandle(NULL), wszModulePath, ARRAYSIZE(wszModulePath));
  333. WCHAR* pszModuleName = PathFindFileNameW(wszModulePath);
  334. int i = FindMRUData(hMRU, pszModuleName, CbFromCchW(lstrlenW(pszModuleName) + 1), NULL);
  335. if (i >= 0)
  336. {
  337. BYTE buf[CbFromCchW(2*MAX_PATH)];
  338. if (-1 != EnumMRUList(hMRU, i, buf, SIZEOF(buf)))
  339. {
  340. LPWSTR psz = (LPWSTR)((LPBYTE)buf + CbFromCchW(lstrlenW((LPWSTR)buf) +1));
  341. SHUnicodeToTChar(psz, pszDir, cchDir);
  342. bRet = TRUE;
  343. }
  344. }
  345. FreeMRUList(hMRU);
  346. }
  347. return bRet;
  348. }