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.

331 lines
7.7 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1997 - 1999
  6. //
  7. // File: msgbox.cpp
  8. //
  9. //--------------------------------------------------------------------------
  10. #include "pch.h"
  11. #pragma hdrstop
  12. #include "msgbox.h"
  13. //
  14. // Helper to report system errors.
  15. // Merely ensures a little consistency with respect to message box
  16. // flags.
  17. //
  18. // Example:
  19. //
  20. // CscWin32Message(hwndMain,
  21. // ERROR_NOT_ENOUGH_MEMORY,
  22. // CSCUI::SEV_ERROR);
  23. //
  24. INT
  25. CscWin32Message(
  26. HWND hwndParent,
  27. DWORD dwError, // From GetLastError().
  28. CSCUI::Severity severity
  29. )
  30. {
  31. UINT uType = MB_OK;
  32. switch(severity)
  33. {
  34. case CSCUI::SEV_ERROR:
  35. uType |= MB_ICONERROR;
  36. break;
  37. case CSCUI::SEV_WARNING:
  38. uType |= MB_ICONWARNING;
  39. break;
  40. case CSCUI::SEV_INFORMATION:
  41. uType |= MB_ICONINFORMATION;
  42. break;
  43. default: break;
  44. }
  45. return CscMessageBox(hwndParent, uType, Win32Error(dwError));
  46. }
  47. INT
  48. CscMBLoadString(
  49. HINSTANCE hInstance,
  50. UINT idStr,
  51. LPTSTR *ppszOut
  52. )
  53. {
  54. int cch = 0;
  55. TCHAR szBuffer[MAX_PATH];
  56. int cchResource = SizeofStringResource(hInstance, idStr);
  57. if (0 < cchResource)
  58. {
  59. cchResource++; // Add for nul term.
  60. *ppszOut = (LPTSTR)LocalAlloc(LMEM_FIXED, cchResource * sizeof(TCHAR));
  61. if (NULL != *ppszOut)
  62. {
  63. cch = LoadString(hInstance, idStr, *ppszOut, cchResource);
  64. if (0 == cch)
  65. {
  66. LocalFree(*ppszOut);
  67. *ppszOut = NULL;
  68. }
  69. }
  70. }
  71. return cch;
  72. }
  73. INT
  74. CscMBFormatMessage(
  75. HINSTANCE hInstance,
  76. UINT idStr,
  77. LPTSTR *ppszOut,
  78. va_list *pargs
  79. )
  80. {
  81. INT iResult = -1;
  82. LPTSTR pszFmt = NULL;
  83. int cchLoaded = CscMBLoadString(hInstance, idStr, &pszFmt);
  84. if (0 == cchLoaded)
  85. {
  86. TraceMsg("String resource error");
  87. throw "resource error"; // Don't want to return a value.
  88. }
  89. cchLoaded = ::FormatMessage(FORMAT_MESSAGE_FROM_STRING |
  90. FORMAT_MESSAGE_ALLOCATE_BUFFER,
  91. pszFmt,
  92. 0,
  93. 0,
  94. (LPTSTR)ppszOut,
  95. 1,
  96. pargs);
  97. LocalFree(pszFmt);
  98. return cchLoaded;
  99. }
  100. //
  101. // Display a system error message in a message box.
  102. // The Win32Error class was created to eliminate any signature ambiguities
  103. // with the other versions of CscMessageBox.
  104. //
  105. // Example:
  106. //
  107. // CscMessageBox(hwndMain,
  108. // MB_OK | MB_ICONERROR,
  109. // Win32Error(ERROR_NOT_ENOUGH_MEMORY));
  110. //
  111. INT
  112. CscMessageBox(
  113. HWND hwndParent,
  114. UINT uType,
  115. const Win32Error& error
  116. )
  117. {
  118. INT iResult = -1;
  119. LPTSTR pszBuffer = NULL;
  120. INT cchLoaded = ::FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM |
  121. FORMAT_MESSAGE_ALLOCATE_BUFFER,
  122. NULL,
  123. error.Code(),
  124. 0,
  125. (LPTSTR)&pszBuffer,
  126. 1,
  127. NULL);
  128. if (0 == cchLoaded)
  129. {
  130. TraceMsg("String resource error");
  131. throw "resource error"; // Don't want to return a value.
  132. }
  133. if (NULL != pszBuffer)
  134. {
  135. iResult = CscMessageBox(hwndParent, uType, pszBuffer);
  136. LocalFree(pszBuffer);
  137. }
  138. return iResult;
  139. }
  140. //
  141. // Display a system error message in a message box with additional
  142. // text.
  143. //
  144. // Example:
  145. //
  146. // CscMessageBox(hwndMain,
  147. // MB_OK | MB_ICONERROR,
  148. // Win32Error(ERROR_NOT_ENOUGH_MEMORY),
  149. // IDS_FMT_LOADINGFILE,
  150. // pszFile);
  151. //
  152. INT
  153. CscMessageBox(
  154. HWND hwndParent,
  155. UINT uType,
  156. const Win32Error& error,
  157. LPCTSTR pszMsgText
  158. )
  159. {
  160. INT iResult = -1;
  161. int cchMsg = lstrlen(pszMsgText);
  162. LPTSTR pszBuffer = (LPTSTR)LocalAlloc(LMEM_FIXED, (cchMsg + MAX_PATH) * sizeof(TCHAR));
  163. if (NULL != pszBuffer)
  164. {
  165. lstrcpy(pszBuffer, pszMsgText);
  166. *(pszBuffer + cchMsg++) = TEXT('\n');
  167. *(pszBuffer + cchMsg++) = TEXT('\n');
  168. int cchLoaded = ::FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,
  169. NULL,
  170. error.Code(),
  171. 0,
  172. pszBuffer + cchMsg,
  173. MAX_PATH - 2, // 2 for newlines.
  174. NULL);
  175. if (0 != cchLoaded)
  176. {
  177. iResult = CscMessageBox(hwndParent, uType, pszBuffer);
  178. }
  179. LocalFree(pszBuffer);
  180. }
  181. return iResult;
  182. }
  183. INT
  184. CscMessageBox(
  185. HWND hwndParent,
  186. UINT uType,
  187. const Win32Error& error,
  188. HINSTANCE hInstance,
  189. UINT idMsgText,
  190. va_list *pargs
  191. )
  192. {
  193. INT iResult = -1;
  194. LPTSTR pszBuffer = NULL;
  195. int cchLoaded = CscMBFormatMessage(hInstance,
  196. idMsgText,
  197. &pszBuffer,
  198. pargs);
  199. if (0 == cchLoaded)
  200. {
  201. TraceMsg("String resource error");
  202. throw "resource error"; // Don't want to return a value.
  203. }
  204. iResult = CscMessageBox(hwndParent, uType, error, pszBuffer);
  205. LocalFree(pszBuffer);
  206. return iResult;
  207. }
  208. INT
  209. CscMessageBox(
  210. HWND hwndParent,
  211. UINT uType,
  212. const Win32Error& error,
  213. HINSTANCE hInstance,
  214. UINT idMsgText,
  215. ...
  216. )
  217. {
  218. INT iResult = -1;
  219. va_list args;
  220. va_start(args, idMsgText);
  221. iResult = CscMessageBox(hwndParent, uType, error, hInstance, idMsgText, &args);
  222. va_end(args);
  223. return iResult;
  224. }
  225. //
  226. // Example:
  227. //
  228. // CscMessageBox(hwndMain,
  229. // MB_OK | MB_ICONWARNING,
  230. // TEXT("File %1 could not be deleted"), pszFilename);
  231. //
  232. INT
  233. CscMessageBox(
  234. HWND hwndParent,
  235. UINT uType,
  236. HINSTANCE hInstance,
  237. UINT idMsgText,
  238. va_list *pargs
  239. )
  240. {
  241. INT iResult = -1;
  242. LPTSTR pszFmt = NULL;
  243. int cchLoaded = CscMBLoadString(hInstance, idMsgText, &pszFmt);
  244. if (0 == cchLoaded)
  245. {
  246. TraceMsg("String resource error");
  247. throw "resource error"; // Don't want to return a value.
  248. }
  249. LPTSTR pszMsg = NULL;
  250. cchLoaded = ::FormatMessage(FORMAT_MESSAGE_FROM_STRING |
  251. FORMAT_MESSAGE_ALLOCATE_BUFFER,
  252. pszFmt,
  253. 0,
  254. 0,
  255. (LPTSTR)&pszMsg,
  256. 1,
  257. pargs);
  258. LocalFree(pszFmt);
  259. if (0 == cchLoaded)
  260. {
  261. TraceMsg("String resource error");
  262. throw "resource error"; // Don't want to return a value.
  263. }
  264. iResult = CscMessageBox(hwndParent, uType, pszMsg);
  265. LocalFree(pszMsg);
  266. return iResult;
  267. }
  268. INT
  269. CscMessageBox(
  270. HWND hwndParent,
  271. UINT uType,
  272. HINSTANCE hInstance,
  273. UINT idMsgText,
  274. ...
  275. )
  276. {
  277. INT iResult = -1;
  278. va_list args;
  279. va_start(args, idMsgText);
  280. iResult = CscMessageBox(hwndParent, uType, hInstance, idMsgText, &args);
  281. va_end(args);
  282. return iResult;
  283. }
  284. //
  285. // All of the other variations of CscMessageBox() end up calling this one.
  286. //
  287. INT
  288. CscMessageBox(
  289. HWND hwndParent,
  290. UINT uType,
  291. LPCTSTR pszMsgText
  292. )
  293. {
  294. TCHAR szCaption[80] = { TEXT('\0') };
  295. LoadString(g_hInstance, IDS_APPLICATION, szCaption, ARRAYSIZE(szCaption));
  296. return MessageBox(hwndParent, pszMsgText, szCaption, uType);
  297. }