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.

483 lines
11 KiB

  1. //+--------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1994 - 1998.
  5. //
  6. // File: password.cxx
  7. //
  8. // Contents: Implementation of class used to prompt user for credentials.
  9. //
  10. // Classes: CMessageDlg
  11. //
  12. // History: 06-28-1998 DavidMun Created
  13. //
  14. //---------------------------------------------------------------------------
  15. #include "headers.hxx"
  16. #pragma hdrstop
  17. inline LONG
  18. WindowRectWidth(
  19. const RECT &rc)
  20. {
  21. return rc.right - rc.left;
  22. }
  23. inline LONG
  24. WindowRectHeight(
  25. const RECT &rc)
  26. {
  27. return rc.bottom - rc.top;
  28. }
  29. //+--------------------------------------------------------------------------
  30. //
  31. // Function: PopupMessage
  32. //
  33. // Synopsis: Invoke a modal dialog to display a formatted message string.
  34. //
  35. // Arguments: [idsMessage] - resource id of printf style format string
  36. // [...] - arguments required for string
  37. //
  38. // History: 07-06-1998 DavidMun Created
  39. //
  40. //---------------------------------------------------------------------------
  41. void
  42. PopupMessage(
  43. ULONG idsMessage,
  44. ...)
  45. {
  46. va_list valArgs;
  47. va_start(valArgs, idsMessage);
  48. CMessageDlg MessageDlg;
  49. MessageDlg.DoModalDialog(NULL,
  50. IDI_ERROR,
  51. NULL,
  52. 0,
  53. 0,
  54. idsMessage,
  55. valArgs);
  56. va_end(valArgs);
  57. }
  58. //+--------------------------------------------------------------------------
  59. //
  60. // Function: PopupMessageEx
  61. //
  62. // Synopsis: Invoke a modal dialog to display a formatted message string.
  63. //
  64. // Arguments: [idIcon] - resource identifier of system icon
  65. // [idsMessage] - resource id of printf style format string
  66. // [...] - arguments required for string
  67. //
  68. // History: 07-06-1998 DavidMun Created
  69. //
  70. //---------------------------------------------------------------------------
  71. void
  72. PopupMessageEx(
  73. PCWSTR idIcon,
  74. ULONG idsMessage,
  75. ...)
  76. {
  77. va_list valArgs;
  78. va_start(valArgs, idsMessage);
  79. CMessageDlg MessageDlg;
  80. MessageDlg.DoModalDialog(NULL,
  81. idIcon,
  82. NULL,
  83. 0,
  84. 0,
  85. idsMessage,
  86. valArgs);
  87. va_end(valArgs);
  88. }
  89. //+--------------------------------------------------------------------------
  90. //
  91. // Function: PopupMessageAndCode
  92. //
  93. // Synopsis: Invoke a modal dialog to display a formatted message string
  94. // and error code.
  95. //
  96. // Arguments: [pwzFileName] - error code's file name
  97. // [ulLineNo] - error code's line number
  98. // [hr] - error code's HRESULT
  99. // [idsMessage] - resource id of printf style format string
  100. // [...] - arguments required for string
  101. //
  102. // History: 07-06-1998 DavidMun Created
  103. //
  104. //---------------------------------------------------------------------------
  105. void
  106. PopupMessageAndCode(
  107. PCWSTR pwzFileName,
  108. ULONG ulLineNo,
  109. HRESULT hr,
  110. ULONG idsMessage,
  111. ...)
  112. {
  113. va_list valArgs;
  114. va_start(valArgs, idsMessage);
  115. CMessageDlg MessageDlg;
  116. MessageDlg.DoModalDialog(NULL,
  117. IDI_ERROR,
  118. pwzFileName,
  119. ulLineNo,
  120. hr,
  121. idsMessage,
  122. valArgs);
  123. va_end(valArgs);
  124. }
  125. //+--------------------------------------------------------------------------
  126. //
  127. // Function: LoadStr
  128. //
  129. // Synopsis: Load string with resource id [ids] into buffer [wszBuf],
  130. // which is of size [cchBuf] characters.
  131. //
  132. // Arguments: [ids] - string to load
  133. // [wszBuf] - buffer for string
  134. // [cchBuf] - size of buffer
  135. // [wszDefault] - NULL or string to use if load fails
  136. //
  137. // Returns: S_OK or error from LoadString
  138. //
  139. // Modifies: *[wszBuf]
  140. //
  141. // History: 12-11-1996 DavidMun Created
  142. //
  143. // Notes: If the load fails and no default is supplied, [wszBuf] is
  144. // set to an empty string.
  145. //
  146. //---------------------------------------------------------------------------
  147. HRESULT
  148. LoadStr(
  149. ULONG ids,
  150. LPWSTR wszBuf,
  151. ULONG cchBuf,
  152. LPCWSTR wszDefault)
  153. {
  154. HRESULT hr = S_OK;
  155. ULONG cchLoaded;
  156. cchLoaded = LoadString(GetModuleHandle(NULL), ids, wszBuf, cchBuf);
  157. if (!cchLoaded)
  158. {
  159. hr = HRESULT_FROM_WIN32(GetLastError());
  160. if (wszDefault)
  161. {
  162. lstrcpyn(wszBuf, wszDefault, cchBuf);
  163. }
  164. else
  165. {
  166. *wszBuf = L'\0';
  167. }
  168. }
  169. return hr;
  170. }
  171. //+--------------------------------------------------------------------------
  172. //
  173. // Member: CMessageDlg::DoModalDialog
  174. //
  175. // Synopsis: Invoke a modal dialog to display a message.
  176. //
  177. // Arguments: [hinstIcon] - NULL or module handle where icon lives
  178. // [idIcon] - resource identifier of icon to display
  179. // [pwzFile] - error code's file name
  180. // [ulLine] - error code's line number
  181. // [ulErrorCode] - error code's error value
  182. // [idsMessage] - resource id of printf style format string
  183. // [valArgs] - arguments required for string
  184. //
  185. // Returns: HRESULT
  186. //
  187. // History: 07-06-1998 DavidMun Created
  188. //
  189. //---------------------------------------------------------------------------
  190. INT_PTR
  191. CMessageDlg::DoModalDialog(
  192. HINSTANCE hinstIcon,
  193. PCWSTR idIcon,
  194. PCWSTR pwzFile,
  195. ULONG ulLine,
  196. ULONG ulErrorCode,
  197. ULONG idsMessage,
  198. va_list valArgs)
  199. {
  200. m_hinstIcon = hinstIcon;
  201. m_idIcon = idIcon;
  202. if (pwzFile)
  203. {
  204. wsprintf(m_wzErrorCode, L"%ws %u %x", pwzFile, ulLine, ulErrorCode);
  205. }
  206. WCHAR wzMessageFmt[MAX_PATH];
  207. LoadStr(idsMessage, wzMessageFmt, MAX_PATH, L"");
  208. PWSTR pwzTemp = NULL;
  209. ULONG ulResult;
  210. ulResult = FormatMessageW(FORMAT_MESSAGE_FROM_STRING
  211. | FORMAT_MESSAGE_ALLOCATE_BUFFER,
  212. wzMessageFmt,
  213. 0,
  214. 0,
  215. (LPWSTR) &pwzTemp,
  216. 0,
  217. &valArgs);
  218. if (ulResult)
  219. {
  220. m_pwzMessage = new WCHAR[lstrlen(pwzTemp) + 1];
  221. lstrcpy(m_pwzMessage, pwzTemp);
  222. LocalFree(pwzTemp);
  223. }
  224. return _DoModalDlg(NULL, IDD_ERROR);
  225. }
  226. //+--------------------------------------------------------------------------
  227. //
  228. // Member: CMessageDlg::_OnInit
  229. //
  230. // Synopsis: Initialize dialog controls
  231. //
  232. // Arguments: [pfSetFocus] - unused
  233. //
  234. // Returns: S_OK
  235. //
  236. // History: 06-28-1998 DavidMun Created
  237. //
  238. //---------------------------------------------------------------------------
  239. HRESULT
  240. CMessageDlg::_OnInit(
  241. bool *pfSetFocus)
  242. {
  243. //
  244. // The error dialog has these components:
  245. //
  246. // 1. caption
  247. // 2. icon
  248. // 3. error message
  249. // 4. error code
  250. //
  251. // The last is optional.
  252. //
  253. //
  254. // Set the icon.
  255. //
  256. HICON hIcon = LoadIcon(m_hinstIcon, m_idIcon);
  257. if (hIcon)
  258. {
  259. SendDlgItemMessage(m_hwnd,
  260. IDC_POPUP_MSG_ICON,
  261. STM_SETICON,
  262. (WPARAM)hIcon,
  263. 0L);
  264. }
  265. //
  266. // Set the error message static
  267. //
  268. Static_SetText(_hCtrl(IDC_ERRORMSG), m_pwzMessage);
  269. //
  270. // If there is an error code string, set it, otherwise hide
  271. // the edit control and its label and resize the dialog to
  272. // eliminate the empty space.
  273. //
  274. if (*m_wzErrorCode)
  275. {
  276. Static_SetText(_hCtrl(IDC_ERROR_CODE_EDIT), m_wzErrorCode);
  277. }
  278. else
  279. {
  280. EnableWindow(_hCtrl(IDC_ERROR_CODE_LBL), false);
  281. EnableWindow(_hCtrl(IDC_ERROR_CODE_EDIT), false);
  282. ShowWindow(_hCtrl(IDC_ERROR_CODE_LBL), SW_HIDE);
  283. ShowWindow(_hCtrl(IDC_ERROR_CODE_EDIT), SW_HIDE);
  284. RECT rcClose;
  285. RECT rcEdit;
  286. _GetChildWindowRect(_hCtrl(IDCANCEL), &rcClose);
  287. _GetChildWindowRect(_hCtrl(IDC_ERROR_CODE_EDIT), &rcEdit);
  288. //
  289. // Move the Close button up on top of the hidden edit control
  290. //
  291. SetWindowPos(_hCtrl(IDCANCEL),
  292. NULL,
  293. rcClose.left,
  294. rcEdit.top,
  295. 0,
  296. 0,
  297. SWP_NOSIZE | SWP_NOZORDER);
  298. //
  299. // Shrink the dialog vertically by the amount the button was
  300. // moved up
  301. //
  302. LONG cyDelta = rcClose.top - rcEdit.top;
  303. RECT rcDialog;
  304. GetWindowRect(m_hwnd, &rcDialog);
  305. SetWindowPos(m_hwnd,
  306. NULL,
  307. 0,
  308. 0,
  309. WindowRectWidth(rcDialog),
  310. WindowRectHeight(rcDialog) - cyDelta,
  311. SWP_NOMOVE | SWP_NOZORDER);
  312. }
  313. return S_OK;
  314. }
  315. //+--------------------------------------------------------------------------
  316. //
  317. // Member: CMessageDlg::_OnCommand
  318. //
  319. // Synopsis: Handle user input.
  320. //
  321. // Arguments: standard windows
  322. //
  323. // Returns: standard windows
  324. //
  325. // History: 06-28-1998 DavidMun Created
  326. //
  327. //---------------------------------------------------------------------------
  328. bool
  329. CMessageDlg::_OnCommand(
  330. WPARAM wParam,
  331. LPARAM lParam)
  332. {
  333. bool fNotHandled = false;
  334. switch (LOWORD(wParam))
  335. {
  336. case IDCANCEL:
  337. EndDialog(m_hwnd, 0);
  338. break;
  339. default:
  340. fNotHandled = true;
  341. break;
  342. }
  343. return fNotHandled;
  344. }
  345. //+--------------------------------------------------------------------------
  346. //
  347. // Function: InvokeWinHelp
  348. //
  349. // Synopsis: Helper (ahem) function to invoke winhelp.
  350. //
  351. // Arguments: [message] - WM_CONTEXTMENU or WM_HELP
  352. // [wParam] - depends on [message]
  353. // [wszHelpFileName] - filename with or without path
  354. // [aulControlIdToHelpIdMap] - see WinHelp API
  355. //
  356. // History: 06-10-1997 DavidMun Created
  357. //
  358. //---------------------------------------------------------------------------
  359. void
  360. InvokeWinHelp(
  361. UINT message,
  362. WPARAM wParam,
  363. LPARAM lParam,
  364. PCWSTR wszHelpFileName,
  365. ULONG aulControlIdToHelpIdMap[])
  366. {
  367. switch (message)
  368. {
  369. case WM_CONTEXTMENU: // Right mouse click - "What's This" context menu
  370. WinHelp((HWND) wParam,
  371. wszHelpFileName,
  372. HELP_CONTEXTMENU,
  373. (DWORD_PTR) aulControlIdToHelpIdMap);
  374. break;
  375. case WM_HELP: // Help from the "?" dialog
  376. {
  377. const LPHELPINFO pHelpInfo = (LPHELPINFO) lParam;
  378. if (pHelpInfo && pHelpInfo->iContextType == HELPINFO_WINDOW)
  379. {
  380. WinHelp((HWND) pHelpInfo->hItemHandle,
  381. wszHelpFileName,
  382. HELP_WM_HELP,
  383. (DWORD_PTR) aulControlIdToHelpIdMap);
  384. }
  385. break;
  386. }
  387. default:
  388. break;
  389. }
  390. }