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.

285 lines
7.0 KiB

  1. //*********************************************************************
  2. //* Microsoft Windows **
  3. //* Copyright(c) Microsoft Corp., 1993 **
  4. //*********************************************************************
  5. #include "admincfg.h"
  6. BOOL fInfLoaded = FALSE;
  7. BOOL OpenTemplateDlg(HWND hWnd,TCHAR * szFilename,UINT cbFileName);
  8. BOOL GetATemplateFile(HWND hWnd)
  9. {
  10. TCHAR szTmpFilename[MAX_PATH+1];
  11. // user cancelled or didn't select a file
  12. if (!OpenTemplateDlg(hWnd,szTmpFilename,ARRAYSIZE(szTmpFilename)))
  13. return FALSE;
  14. SendDlgItemMessage(hWnd,IDD_TEMPLATELIST,LB_ADDSTRING,0,(LPARAM) szTmpFilename);
  15. return TRUE;
  16. }
  17. UINT LoadTemplatesFromDlg(HWND hWnd)
  18. {
  19. UINT uRet;
  20. TCHAR szFilename[MAX_PATH+1];
  21. UINT i=0;
  22. DWORD dwUsed, dwSize;
  23. UnloadTemplates(); // unload previous files, if any
  24. PrepareToLoadTemplates();
  25. while (LB_ERR != SendDlgItemMessage(hWnd,IDD_TEMPLATELIST,LB_GETTEXT,
  26. i,(LPARAM) szFilename))
  27. {
  28. uRet = LoadTemplateFile(hWnd,szFilename);
  29. if (uRet != ERROR_SUCCESS) {
  30. if (uRet != ERROR_ALREADY_DISPLAYED) {
  31. DisplayStandardError(hWnd,szFilename,IDS_ErrOPENTEMPLATE,uRet);
  32. }
  33. return uRet;
  34. }
  35. i++;
  36. }
  37. // Fill up the template file buffer.
  38. i=0;
  39. dwUsed = 0;
  40. while (LB_ERR != SendDlgItemMessage(hWnd,IDD_TEMPLATELIST,LB_GETTEXT,
  41. i,(LPARAM) szFilename))
  42. {
  43. dwSize = lstrlen(szFilename)+1;
  44. if (!(pbufTemplates=ResizeBuffer(pbufTemplates,hBufTemplates,dwUsed+dwSize+4,&dwBufTemplates)))
  45. return ERROR_NOT_ENOUGH_MEMORY;
  46. lstrcpy(pbufTemplates+dwUsed,szFilename);
  47. dwUsed += dwSize;
  48. i++;
  49. }
  50. // doubly null-terminate the buffer... safe to do this because we
  51. // tacked on the extra "+4" in the ResizeBuffer calls above
  52. *(pbufTemplates+dwUsed) = TEXT('\0');
  53. return ERROR_SUCCESS;
  54. }
  55. UINT LoadTemplates(HWND hWnd)
  56. {
  57. UINT uRet;
  58. TCHAR *szFilename;
  59. UINT i=0;
  60. if (!(*pbufTemplates))
  61. return ERROR_FILE_NOT_FOUND; //Nothing to load.
  62. UnloadTemplates(); // unload previous file, if any
  63. PrepareToLoadTemplates();
  64. szFilename = pbufTemplates;
  65. while (*szFilename)
  66. {
  67. uRet = LoadTemplateFile(hWnd,szFilename);
  68. if (uRet != ERROR_SUCCESS) {
  69. if (uRet != ERROR_ALREADY_DISPLAYED) {
  70. DisplayStandardError(hWnd,szFilename,IDS_ErrOPENTEMPLATE,uRet);
  71. }
  72. return uRet;
  73. }
  74. szFilename += lstrlen(szFilename)+1;
  75. }
  76. return ERROR_SUCCESS;
  77. }
  78. BOOL OpenTemplateDlg(HWND hWnd,TCHAR * szFilename,UINT cbFilename)
  79. {
  80. OPENFILENAME ofn;
  81. TCHAR szFilter[SMALLBUF];
  82. TCHAR szOpenInfTitle[SMALLBUF];
  83. lstrcpy(szFilename,szNull);
  84. LoadSz(IDS_InfOPENTITLE,szOpenInfTitle,ARRAYSIZE(szOpenInfTitle));
  85. // have to load the openfile filter in 2 stages, because the string
  86. // contains a terminating character and LoadString won't load the
  87. // whole thing in one go
  88. memset(szFilter,0,(ARRAYSIZE(szFilter) * sizeof(TCHAR)));
  89. LoadSz(IDS_InfOPENFILTER1,szFilter,ARRAYSIZE(szFilter));
  90. LoadSz(IDS_InfOPENFILTER2,szFilter+lstrlen(szFilter)+1,ARRAYSIZE(szFilter)-
  91. (lstrlen(szFilter)-1));
  92. memset(&ofn,0,sizeof(ofn));
  93. ofn.lStructSize = sizeof(ofn);
  94. ofn.hwndOwner = hWnd;
  95. ofn.hInstance = ghInst;
  96. ofn.lpstrFilter = szFilter;
  97. ofn.lpstrFile = szFilename;
  98. ofn.nMaxFile = cbFilename;
  99. ofn.lpstrTitle = szOpenInfTitle;
  100. ofn.Flags = OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST
  101. | OFN_SHAREAWARE | OFN_HIDEREADONLY;
  102. return GetOpenFileName(&ofn);
  103. }
  104. UINT PrepareToLoadTemplates()
  105. {
  106. gClassList.nUserDataItems = 0;
  107. gClassList.nMachineDataItems = 0;
  108. if (!InitializeRootTables())
  109. return ERROR_NOT_ENOUGH_MEMORY;
  110. return ERROR_SUCCESS;
  111. }
  112. UINT LoadTemplateFile(HWND hWnd,TCHAR * szFilename)
  113. {
  114. HANDLE hFile;
  115. UINT uRet;
  116. TCHAR szStatusTextOld[255];
  117. TCHAR szStatusText[SMALLBUF+MAX_PATH+1];
  118. // open the specified file
  119. if ((hFile = CreateFile(szFilename,GENERIC_READ,FILE_SHARE_READ,
  120. NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL)) == INVALID_HANDLE_VALUE) {
  121. #ifdef DEBUG
  122. wsprintf(szDebugOut,TEXT("LoadTemplateFile: CreateFile returned %lu\r\n"),
  123. GetLastError());
  124. OutputDebugString(szDebugOut);
  125. #endif
  126. return (UINT) GetLastError();
  127. }
  128. // save old status bar text
  129. GetStatusText(szStatusTextOld,ARRAYSIZE(szStatusTextOld));
  130. // set status bar text a la "loading template file <name>..."
  131. wsprintf(szStatusText,LoadSz(IDS_LOADINGTEMPLATE,szSmallBuf,
  132. ARRAYSIZE(szSmallBuf)),szFilename);
  133. SetStatusText(szStatusText);
  134. uRet=ParseTemplateFile(hWnd,hFile,szFilename);
  135. SetStatusText(szStatusTextOld);
  136. CloseHandle(hFile);
  137. if (uRet != ERROR_SUCCESS)
  138. FreeRootTables();
  139. else {
  140. fInfLoaded = TRUE;
  141. dwAppState |= AS_CANHAVEDOCUMENT;
  142. }
  143. return uRet;
  144. }
  145. VOID UnloadTemplates(VOID)
  146. {
  147. if (fInfLoaded) {
  148. FreeRootTables();
  149. fInfLoaded = FALSE;
  150. dwAppState &= ~AS_CANHAVEDOCUMENT;
  151. }
  152. }
  153. VOID EnableMainMenuItems(HWND hwndApp,BOOL fEnable)
  154. {
  155. HMENU hMenu = GetMenu(hwndApp);
  156. UINT uFlags = MF_BYCOMMAND | (fEnable ? MF_ENABLED : MF_DISABLED);
  157. EnableMenuItem(GetMenu(hwndMain),IDM_NEW,uFlags);
  158. EnableMenuItem(GetMenu(hwndMain),IDM_OPEN,uFlags);
  159. }
  160. /*******************************************************************
  161. NAME: GetDefaultTemplateFilename
  162. SYNOPSIS: Builds a default .adm path and filename to look for
  163. NOTES: Fills szFilename with "<windows dir>\INF\ADMINCFG.ADF".
  164. Text is loaded from resources since file and directory
  165. names are localizable.
  166. ********************************************************************/
  167. DWORD GetDefaultTemplateFilename(HWND hWnd,TCHAR * pszFilename,UINT cbFileName)
  168. {
  169. TCHAR szFileName[MAX_PATH] = {0};
  170. UINT cchFileName;
  171. LPTSTR lpEnd;
  172. //
  173. // Load the common adm file
  174. //
  175. if (!GetWindowsDirectory(szFileName,MAX_PATH)) {
  176. return 0;
  177. }
  178. if (lstrlen (szFileName) > (MAX_PATH - 20)) {
  179. *pszFilename = TEXT('\0');
  180. return 0;
  181. }
  182. lstrcat(szFileName,szSlash);
  183. lstrcat(szFileName,LoadSz(IDS_INFDIR,szSmallBuf,ARRAYSIZE(szSmallBuf)));
  184. lstrcat(szFileName,szSlash);
  185. lstrcat(szFileName,LoadSz(IDS_DEF_COMMONNAME,szSmallBuf,ARRAYSIZE(szSmallBuf)));
  186. cchFileName = lstrlen (szFileName) + 1;
  187. if (cchFileName <= cbFileName) {
  188. lstrcpy (pszFilename, szFileName);
  189. lpEnd = pszFilename + lstrlen (pszFilename) + 1;
  190. } else {
  191. *pszFilename = TEXT('\0');
  192. return 0;
  193. }
  194. //
  195. // Add on the base adm filename
  196. //
  197. if (!GetWindowsDirectory(szFileName,MAX_PATH)) {
  198. return 0;
  199. }
  200. lstrcat(szFileName,szSlash);
  201. lstrcat(szFileName,LoadSz(IDS_INFDIR,szSmallBuf,ARRAYSIZE(szSmallBuf)));
  202. lstrcat(szFileName,szSlash);
  203. if (g_bWinnt) {
  204. lstrcat(szFileName,LoadSz(IDS_DEF_NT_INFNAME,szSmallBuf,ARRAYSIZE(szSmallBuf)));
  205. } else {
  206. lstrcat(szFileName,LoadSz(IDS_DEF_INFNAME,szSmallBuf,ARRAYSIZE(szSmallBuf)));
  207. }
  208. cchFileName += lstrlen (szFileName) + 1;
  209. if (cchFileName < cbFileName) {
  210. lstrcat (lpEnd, szFileName);
  211. }
  212. return (cchFileName);
  213. }