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.

444 lines
11 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1997.
  5. //
  6. // File: N C U I . C P P
  7. //
  8. // Contents: Common user interface routines.
  9. //
  10. // Notes:
  11. //
  12. // Author: shaunco 24 Mar 1997
  13. //
  14. //----------------------------------------------------------------------------
  15. #include <pch.h>
  16. #pragma hdrstop
  17. #include "prsht.h"
  18. #include "ncstring.h"
  19. #include "ncui.h"
  20. //+---------------------------------------------------------------------------
  21. //
  22. // Function: EnableOrDisableDialogControls
  23. //
  24. // Purpose: Enable or disable a group of controls all at once.
  25. //
  26. // Arguments:
  27. // hDlg [in] Window handle of parent dialog.
  28. // ccid [in] Count of elements in array pointed to by acid.
  29. // acid [in] Array of control ids.
  30. // fEnable [in] TRUE to enable controls, FALSE to disable.
  31. //
  32. // Returns: nothing
  33. //
  34. // Author: shaunco 24 Mar 1997
  35. //
  36. // Notes: Don't forget to declare your array as 'static const'.
  37. //
  38. NOTHROW
  39. VOID
  40. EnableOrDisableDialogControls (
  41. IN HWND hDlg,
  42. IN INT ccid,
  43. IN const INT* acid,
  44. IN BOOL fEnable)
  45. {
  46. Assert (IsWindow (hDlg));
  47. Assert (FImplies (ccid, acid));
  48. while (ccid--)
  49. {
  50. EnableWindow (GetDlgItem (hDlg, *acid++), fEnable);
  51. }
  52. }
  53. //+---------------------------------------------------------------------------
  54. //
  55. // Function: FMapRadioButtonToValue
  56. //
  57. // Purpose: Maps the current state of a set of radio buttons to a DWORD
  58. // value based on a mapping table.
  59. //
  60. // Arguments:
  61. // hDlg [in] Window handle of parent dialog.
  62. // crbm [in] Count of elements in array pointed to by arbm.
  63. // arbm [in] Array of elements that map a radio button control id to
  64. // its associated value.
  65. // pdwValue [out] The returned value.
  66. //
  67. // Returns: TRUE if a radio button was set and the value returned.
  68. // FALSE otherwise.
  69. //
  70. // Author: shaunco 24 Mar 1997
  71. //
  72. // Notes: Don't forget to declare your array as 'static const'.
  73. //
  74. NOTHROW
  75. BOOL
  76. FMapRadioButtonToValue (
  77. IN HWND hDlg,
  78. IN INT crbm,
  79. IN const RADIO_BUTTON_MAP* arbm,
  80. OUT DWORD* pdwValue)
  81. {
  82. Assert (IsWindow (hDlg));
  83. Assert (FImplies (crbm, arbm));
  84. Assert (pdwValue);
  85. while (crbm--)
  86. {
  87. // If it is set, return the corresponding value.
  88. if (BST_CHECKED & IsDlgButtonChecked (hDlg, arbm->cid))
  89. {
  90. *pdwValue = arbm->dwValue;
  91. return TRUE;
  92. }
  93. arbm++;
  94. }
  95. *pdwValue = 0;
  96. return FALSE;
  97. }
  98. //+---------------------------------------------------------------------------
  99. //
  100. // Function: FMapValueToRadioButton
  101. //
  102. // Purpose: Set the state of a set of radio buttons based on the value of
  103. // of a DWORD and a mapping table.
  104. //
  105. // Arguments:
  106. // hDlg [in] Window handle of parent dialog.
  107. // crbm [in] Count of elements in array pointed to by arbm.
  108. // arbm [in] Array of elements that map a radio button control id to
  109. // its associated value.
  110. // dwValue [in] value which gets mapped to set the appropriate radio
  111. // button.
  112. //
  113. // Returns: TRUE if dwValue was found in the map. FALSE otherwise.
  114. //
  115. // Author: shaunco 24 Mar 1997
  116. //
  117. // Notes: Don't forget to declare your array as 'static const'.
  118. //
  119. NOTHROW
  120. BOOL
  121. FMapValueToRadioButton (
  122. IN HWND hDlg,
  123. IN INT crbm,
  124. IN const RADIO_BUTTON_MAP* arbm,
  125. IN DWORD dwValue,
  126. IN INT* pncid)
  127. {
  128. Assert (IsWindow (hDlg));
  129. Assert (FImplies (crbm, arbm));
  130. while (crbm--)
  131. {
  132. if (dwValue == arbm->dwValue)
  133. {
  134. // Set the radio button.
  135. CheckDlgButton (hDlg, arbm->cid, BST_CHECKED);
  136. // Return the control id if requested.
  137. if (pncid)
  138. {
  139. *pncid = arbm->cid;
  140. }
  141. return TRUE;
  142. }
  143. arbm++;
  144. }
  145. return FALSE;
  146. }
  147. //+---------------------------------------------------------------------------
  148. //
  149. // Function: LresFromHr
  150. //
  151. // Purpose: Translates an HRESULT into a valid LRESULT to be returned by
  152. // a dialog handler function.
  153. //
  154. // Arguments:
  155. // hr [in] HRESULT to be translated.
  156. //
  157. // Returns: LRESULT
  158. //
  159. // Author: danielwe 24 Mar 1997
  160. //
  161. // Notes:
  162. //
  163. LRESULT
  164. LresFromHr (
  165. IN HRESULT hr)
  166. {
  167. AssertSz (((LRESULT)hr) != PSNRET_INVALID, "Don't pass PSNRET_INVALID to "
  168. "LresFromHr! Use equivalent NETCFG_E_* value instead!");
  169. AssertSz (((LRESULT)hr) != PSNRET_INVALID_NOCHANGEPAGE, "Don't pass "
  170. "PSNRET_INVALID_NOCHANGEPAGE to "
  171. "LresFromHr! Use equivalent NETCFG_E_* value instead!");
  172. return (SUCCEEDED(hr)) ? PSNRET_NOERROR : (LRESULT)hr;
  173. }
  174. //+---------------------------------------------------------------------------
  175. //
  176. // Function: NcMsgBox
  177. //
  178. // Purpose: Displays a message box using resource strings and replaceable
  179. // parameters.
  180. //
  181. // Arguments:
  182. // hinst [in] hinstance for resource strings
  183. // hwnd [in] parent window handle
  184. // unIdCaption [in] resource id of caption string
  185. // unIdFormat [in] resource id of text string (with %1, %2, etc.)
  186. // unStyle [in] standard message box styles
  187. // ... [in] replaceable parameters (optional)
  188. // (these must be PCTSTRs as that is all
  189. // FormatMessage handles.)
  190. //
  191. // Returns: the return value of MessageBox()
  192. //
  193. // Author: shaunco 24 Mar 1997
  194. //
  195. // Notes: FormatMessage is used to do the parameter substitution.
  196. //
  197. INT
  198. WINAPIV
  199. NcMsgBox (
  200. IN HINSTANCE hinst,
  201. IN HWND hwnd,
  202. IN UINT unIdCaption,
  203. IN UINT unIdFormat,
  204. IN UINT unStyle,
  205. IN ...)
  206. {
  207. INT nRet = -1;
  208. PCWSTR pszwCaption = WszLoadString(hinst, unIdCaption);
  209. PCWSTR pszwFormat = WszLoadString(hinst, unIdFormat);
  210. if (pszwCaption && pszwFormat)
  211. {
  212. PCTSTR pszCaption = TszFromWsz(pszwCaption);
  213. PCTSTR pszFormat = TszFromWsz(pszwFormat);
  214. if (pszCaption && pszFormat)
  215. {
  216. PTSTR pszText = NULL;
  217. va_list val;
  218. va_start (val, unStyle);
  219. FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_STRING,
  220. pszFormat, 0, 0, (PTSTR)&pszText, 0, &val);
  221. va_end (val);
  222. nRet = MessageBox (hwnd, pszText, pszCaption, unStyle);
  223. LocalFree (pszText);
  224. }
  225. if (pszCaption)
  226. {
  227. free((VOID *)pszCaption);
  228. pszCaption = NULL;
  229. }
  230. if (pszFormat)
  231. {
  232. free((VOID *)pszFormat);
  233. pszFormat = NULL;
  234. }
  235. }
  236. return nRet;
  237. }
  238. //+---------------------------------------------------------------------------
  239. //
  240. // Function: SendDlgItemsMessage
  241. //
  242. // Purpose: Send a group of dialog items the same message.
  243. //
  244. // Arguments:
  245. // hDlg [in] Window handle of parent dialog.
  246. // ccid [in] Count of elements in array pointed to by acid.
  247. // acid [in] Array of control ids.
  248. // unMsg [in] Message to send
  249. // wParam [in] First message parameter
  250. // lParam [in] Second message parameter
  251. //
  252. // Returns: nothing
  253. //
  254. // Author: shaunco 11 Jun 1997
  255. //
  256. // Notes:
  257. //
  258. VOID
  259. SendDlgItemsMessage (
  260. IN HWND hDlg,
  261. IN INT ccid,
  262. IN const INT* acid,
  263. IN UINT unMsg,
  264. IN WPARAM wParam,
  265. IN LPARAM lParam)
  266. {
  267. Assert (IsWindow (hDlg));
  268. Assert (FImplies (ccid, acid));
  269. while (ccid--)
  270. {
  271. Assert (IsWindow (GetDlgItem (hDlg, *acid)));
  272. SendDlgItemMessage (hDlg, *acid++, unMsg, wParam, lParam);
  273. }
  274. }
  275. //
  276. // Function: SetDefaultButton
  277. //
  278. // Purpose: Set the new default pushbutton on a dialog
  279. //
  280. // Params: hdlg [in] - Dialog HWND
  281. // iddef [in] - Id of new default pushbutton
  282. //
  283. // Returns: nothing
  284. //
  285. VOID
  286. SetDefaultButton(
  287. IN HWND hdlg,
  288. IN INT iddef)
  289. {
  290. HWND hwnd;
  291. DWORD_PTR dwData;
  292. Assert(hdlg);
  293. dwData = SendMessage (hdlg, DM_GETDEFID, 0, 0L);
  294. if ((HIWORD(dwData) == DC_HASDEFID) && LOWORD(dwData))
  295. {
  296. hwnd = GetDlgItem (hdlg, (INT)LOWORD(dwData));
  297. if ((LOWORD(dwData) != iddef) && (hwnd))
  298. {
  299. SendMessage (hwnd, BM_SETSTYLE, (WPARAM)BS_PUSHBUTTON, TRUE);
  300. }
  301. }
  302. SendMessage (hdlg, DM_SETDEFID,(WPARAM)iddef, 0L);
  303. if (iddef)
  304. {
  305. hwnd = GetDlgItem (hdlg, iddef);
  306. Assert(hwnd);
  307. SendMessage (hwnd, BM_SETSTYLE, (WPARAM)BS_DEFPUSHBUTTON, TRUE);
  308. }
  309. }
  310. static const CONTEXTIDMAP c_adwContextIdMap[] =
  311. {
  312. { IDOK, IDH_OK, IDH_OK },
  313. { IDCANCEL, IDH_CANCEL, IDH_CANCEL },
  314. { 0, 0, 0 }, // end marker
  315. };
  316. //+---------------------------------------------------------------------------
  317. //
  318. // Function: DwContextIdFromIdc
  319. //
  320. // Purpose: Converts the given control ID to a context help ID
  321. //
  322. // Arguments:
  323. // idControl [in] Control ID to convert
  324. //
  325. // Returns: Context help ID for that control (mapping comes from help
  326. // authors)
  327. //
  328. // Author: danielwe 27 May 1998
  329. //
  330. // Notes:
  331. //
  332. DWORD DwContextIdFromIdc(
  333. PCCONTEXTIDMAP lpContextIdMap,
  334. BOOL bJpn,
  335. INT idControl)
  336. {
  337. DWORD idw;
  338. Assert(lpContextIdMap);
  339. for (idw = 0; lpContextIdMap[idw].idControl; idw++)
  340. {
  341. if (idControl == lpContextIdMap[idw].idControl)
  342. {
  343. if (!bJpn)
  344. {
  345. return lpContextIdMap[idw].dwContextId;
  346. }
  347. else
  348. {
  349. return lpContextIdMap[idw].dwContextIdJapan;
  350. }
  351. }
  352. }
  353. // Not found, just return 0
  354. return 0;
  355. }
  356. //+---------------------------------------------------------------------------
  357. //
  358. // Function: OnHelpGeneric
  359. //
  360. // Purpose: Handles help generically
  361. //
  362. // Arguments:
  363. // hwnd [in] HWND of parent window
  364. // lParam [in] lParam of the WM_HELP message
  365. //
  366. // Returns: Nothing
  367. //
  368. // Author: danielwe 27 May 1998
  369. // anbrad 18 May 1999 moved to common. common control id's added.
  370. //
  371. // Notes:
  372. //
  373. VOID OnHelpGeneric(
  374. HWND hwnd,
  375. LPHELPINFO lphi,
  376. PCCONTEXTIDMAP pContextMap,
  377. BOOL bJpn,
  378. PCTSTR pszHelpFile)
  379. {
  380. static const TCHAR c_szWindowsHelpFile[] = TEXT("windows.hlp");
  381. Assert(lphi);
  382. if (lphi->iContextType == HELPINFO_WINDOW)
  383. {
  384. switch(lphi->iCtrlId)
  385. {
  386. case -1: // IDC_STATIC
  387. break;
  388. case IDOK:
  389. case IDCANCEL:
  390. case IDABORT:
  391. case IDRETRY:
  392. case IDIGNORE:
  393. case IDYES:
  394. case IDNO:
  395. case IDCLOSE:
  396. case IDHELP:
  397. WinHelp(hwnd, c_szWindowsHelpFile, HELP_CONTEXTPOPUP,
  398. DwContextIdFromIdc(c_adwContextIdMap, bJpn, lphi->iCtrlId));
  399. break;
  400. default:
  401. WinHelp(hwnd, pszHelpFile, HELP_CONTEXTPOPUP,
  402. DwContextIdFromIdc(pContextMap, bJpn, lphi->iCtrlId));
  403. }
  404. }
  405. }