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.

250 lines
6.3 KiB

  1. //*********************************************************************
  2. //* Microsoft Windows **
  3. //* Copyright(c) Microsoft Corp., 1993 **
  4. //*********************************************************************
  5. #include "admincfg.h"
  6. UINT nFileShortcutItems=0;
  7. /*******************************************************************
  8. NAME: RestoreStateFromRegistry
  9. SYNOPSIS: Reads from registry and restores window
  10. placement
  11. ********************************************************************/
  12. BOOL RestoreStateFromRegistry(HWND hWnd)
  13. {
  14. HKEY hKeyMain;
  15. DWORD dwType,dwSize;
  16. UINT nIndex;
  17. CHAR szFilename[MAX_PATH+1];
  18. CHAR szValueName[24];
  19. DWORD dwAlloc = MEDIUMBUF;
  20. DWORD dwUsed = 0;
  21. if (dwCmdLineFlags & CLF_DIALOGMODE)
  22. return TRUE; // nothing to do
  23. if (RegCreateKey(HKEY_CURRENT_USER,szAPPREGKEY,&hKeyMain) !=
  24. ERROR_SUCCESS) return FALSE;
  25. // get template names (if not already specified on command line)
  26. if (!(dwCmdLineFlags & CLF_USETEMPLATENAME))
  27. {
  28. nIndex = 0;
  29. wsprintf(szValueName, szTemplate, nIndex);
  30. dwSize = sizeof(szFilename);
  31. while (RegQueryValueEx(hKeyMain,szValueName,NULL,&dwType,
  32. (CHAR *) &szFilename,&dwSize) == ERROR_SUCCESS)
  33. {
  34. if (!(pbufTemplates=ResizeBuffer(pbufTemplates,hBufTemplates,dwUsed+dwSize+4,&dwBufTemplates)))
  35. return ERROR_NOT_ENOUGH_MEMORY;
  36. lstrcpy(pbufTemplates+dwUsed,szFilename);
  37. dwUsed += dwSize;
  38. nIndex++;
  39. wsprintf(szValueName, szTemplate, nIndex);
  40. dwSize = sizeof(szFilename);
  41. }
  42. if (nIndex == 0)
  43. {
  44. dwUsed = GetDefaultTemplateFilename(hWnd,pbufTemplates,dwBufTemplates);
  45. }
  46. // doubly null-terminate the buffer... safe to do this because we
  47. // tacked on the extra "+4" in the ResizeBuffer calls above
  48. *(pbufTemplates+dwUsed) = '\0';
  49. dwUsed ++;
  50. }
  51. // get view information
  52. dwSize = sizeof(ViewInfo);
  53. if (RegQueryValueEx(hKeyMain,LoadSz(IDS_VIEW,szSmallBuf,
  54. sizeof(szSmallBuf)),NULL,&dwType,
  55. (CHAR *) &ViewInfo,&dwSize) != ERROR_SUCCESS) {
  56. ViewInfo.fStatusBar = ViewInfo.fToolbar = TRUE;
  57. ViewInfo.dwView = VT_LARGEICONS;
  58. }
  59. RegCloseKey(hKeyMain);
  60. return TRUE;
  61. }
  62. /*******************************************************************
  63. NAME: SaveStateToRegistry
  64. SYNOPSIS: Saves window placement, etc.
  65. ********************************************************************/
  66. BOOL SaveStateToRegistry(HWND hWnd)
  67. {
  68. HKEY hKeyMain;
  69. WINDOWPLACEMENT wp;
  70. UINT i;
  71. CHAR szValueName[24];
  72. DWORD dwFileLen;
  73. CHAR *p;
  74. if (dwCmdLineFlags & CLF_DIALOGMODE)
  75. return TRUE; // nothing to do
  76. if (RegCreateKey(HKEY_CURRENT_USER,szAPPREGKEY,&hKeyMain) !=
  77. ERROR_SUCCESS) return FALSE;
  78. // save windowplacement
  79. wp.length = sizeof(wp);
  80. if (GetWindowPlacement(hWnd,&wp)) {
  81. RegSetValueEx(hKeyMain,LoadSz(IDS_WP,szSmallBuf,
  82. sizeof(szSmallBuf)),0,REG_BINARY,(CHAR *) &wp,
  83. sizeof(WINDOWPLACEMENT));
  84. }
  85. // save list of template files
  86. p = pbufTemplates;
  87. i = 0;
  88. while (*p)
  89. {
  90. wsprintf(szValueName, szTemplate, i);
  91. dwFileLen = lstrlen(p)+1;
  92. RegSetValueEx(hKeyMain,szValueName,0,REG_SZ,p,dwFileLen);
  93. p += dwFileLen;
  94. i++;
  95. }
  96. // make sure there aren't more template file already listed in the registry.
  97. wsprintf(szValueName, szTemplate, i);
  98. RegDeleteValue(hKeyMain,szValueName);
  99. // save view infomation
  100. RegSetValueEx(hKeyMain,LoadSz(IDS_VIEW,szSmallBuf,sizeof(szSmallBuf)),
  101. 0,REG_BINARY,(CHAR *)&ViewInfo,sizeof(ViewInfo));
  102. RegCloseKey(hKeyMain);
  103. return TRUE;
  104. }
  105. VOID LoadFileMenuShortcuts(HMENU hMenu)
  106. {
  107. HKEY hKey;
  108. UINT nIndex,nMenuID = IDM_FILEHISTORY,uPos = GetMenuItemCount(hMenu);
  109. CHAR szValueName[24],szFileName[MAX_PATH+1];
  110. DWORD dwSize;
  111. MENUITEMINFO mii;
  112. if (RegOpenKey(HKEY_CURRENT_USER,szAPPREGKEY,&hKey) !=
  113. ERROR_SUCCESS) return;
  114. memset(&mii,0,sizeof(mii));
  115. mii.cbSize = sizeof(mii);
  116. mii.fMask = MIIM_ID | MIIM_TYPE | MIIM_STATE;
  117. mii.fType = MFT_STRING;
  118. mii.dwTypeData = szFileName;
  119. mii.fState = MFS_ENABLED;
  120. for (nIndex = 0; nIndex<FILEHISTORY_COUNT;nIndex ++) {
  121. wsprintf(szValueName,szLastFile,nIndex);
  122. dwSize = sizeof(szFileName);
  123. if (RegQueryValueEx(hKey,szValueName,NULL,NULL,szFileName,
  124. &dwSize) == ERROR_SUCCESS) {
  125. if (!nFileShortcutItems) {
  126. // if this is the first file shortcut, insert a separator
  127. // bar first
  128. MENUITEMINFO miiTmp;
  129. memset(&miiTmp,0,sizeof(miiTmp));
  130. miiTmp.cbSize = sizeof(miiTmp);
  131. miiTmp.fMask = MIIM_TYPE;
  132. miiTmp.fType = MFT_SEPARATOR;
  133. InsertMenuItem(hMenu,uPos,TRUE,&miiTmp);
  134. uPos++;
  135. }
  136. mii.wID = nMenuID;
  137. mii.cch = lstrlen(szFileName);
  138. // insert the file shortcut menu item
  139. InsertMenuItem(hMenu,uPos,TRUE,&mii);
  140. uPos++;
  141. nMenuID ++;
  142. nFileShortcutItems++;
  143. }
  144. }
  145. RegCloseKey(hKey);
  146. }
  147. VOID SaveFileMenuShortcuts(HMENU hMenu)
  148. {
  149. HKEY hKey;
  150. UINT nIndex,nMenuID = IDM_FILEHISTORY,nLen;
  151. CHAR szValueName[24],szFilename[MAX_PATH+1];
  152. MENUITEMINFO mii;
  153. if (RegCreateKey(HKEY_CURRENT_USER,szAPPREGKEY,&hKey) !=
  154. ERROR_SUCCESS) return;
  155. memset(&mii,0,sizeof(mii));
  156. mii.cbSize = sizeof(mii);
  157. mii.fMask = MIIM_TYPE;
  158. mii.dwTypeData = szFilename;
  159. // get the file shortcuts from menu and save them to registry
  160. for (nIndex = 0;nIndex<FILEHISTORY_COUNT;nIndex++) {
  161. mii.cch = sizeof(szFilename);
  162. if (GetMenuItemInfo(hMenu,nMenuID,FALSE,&mii) &&
  163. (nLen=lstrlen(szFilename))) {
  164. wsprintf(szValueName,szLastFile,nIndex);
  165. RegSetValueEx(hKey,szValueName,0,REG_SZ,szFilename,
  166. nLen+1);
  167. }
  168. nMenuID++;
  169. }
  170. RegCloseKey(hKey);
  171. }
  172. /*******************************************************************
  173. NAME: RestoreWindowPlacement
  174. SYNOPSIS: Restores window placement from registry, calls
  175. SetWindowPlacement()
  176. ********************************************************************/
  177. BOOL RestoreWindowPlacement( HWND hWnd,int nCmdShow)
  178. {
  179. WINDOWPLACEMENT wp;
  180. DWORD dwType,dwSize = sizeof(WINDOWPLACEMENT);
  181. HKEY hKeyMain;
  182. BOOL fRestored=FALSE;
  183. if (RegCreateKey(HKEY_CURRENT_USER,szAPPREGKEY,&hKeyMain) ==
  184. ERROR_SUCCESS) {
  185. // get info out of registry
  186. if (RegQueryValueEx(hKeyMain,LoadSz(IDS_WP,szSmallBuf,
  187. sizeof(szSmallBuf)),NULL,&dwType,
  188. (CHAR *) &wp,&dwSize) == ERROR_SUCCESS) {
  189. wp.showCmd = nCmdShow;
  190. SetWindowPlacement(hWnd,&wp);
  191. fRestored = TRUE;
  192. }
  193. RegCloseKey(hKeyMain);
  194. }
  195. if (!fRestored)
  196. ShowWindow(hWnd,nCmdShow);
  197. return TRUE;
  198. }