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.

395 lines
9.4 KiB

  1. /*******************************************************************************
  2. *
  3. * (C) COPYRIGHT MICROSOFT CORP., 1993-1994
  4. *
  5. * TITLE: REGDWDED.C
  6. *
  7. * VERSION: 4.01
  8. *
  9. * AUTHOR: Tracy Sharpe
  10. *
  11. * DATE: 24 Sep 1994
  12. *
  13. * Dword edit dialog for use by the Registry Editor.
  14. *
  15. *******************************************************************************/
  16. #include "pch.h"
  17. #include "regresid.h"
  18. #include "reghelp.h"
  19. const DWORD s_EditDwordValueHelpIDs[] = {
  20. IDC_VALUEDATA, IDH_REGEDIT_VALUEDATA,
  21. IDC_VALUENAME, IDH_REGEDIT_VALUENAME,
  22. IDC_HEXADECIMAL, IDH_REGEDIT_DWORDBASE,
  23. IDC_DECIMAL, IDH_REGEDIT_DWORDBASE,
  24. 0, 0
  25. };
  26. const TCHAR s_DecimalFormatSpec[] = TEXT("%u");
  27. const TCHAR s_HexadecimalFormatSpec[] = TEXT("%x");
  28. // Subclassed IDC_VALUEDATA's previous window procedure. Only one instance of
  29. // this dialog is assumed to exist.
  30. WNDPROC s_PrevValueDataWndProc;
  31. // The radio button that is currently selected: IDC_HEXADECIMAL or IDC_DECIMAL.
  32. UINT s_SelectedBase;
  33. UINT
  34. PASCAL
  35. GetDlgItemHex(
  36. HWND hWnd,
  37. int nIDDlgItem,
  38. BOOL *lpTranslated
  39. );
  40. BOOL
  41. PASCAL
  42. EditDwordValue_OnInitDialog(
  43. HWND hWnd,
  44. HWND hFocusWnd,
  45. LPARAM lParam
  46. );
  47. VOID
  48. PASCAL
  49. EditDwordValue_SetValueDataText(
  50. HWND hWnd,
  51. LPEDITVALUEPARAM lpEditValueParam,
  52. UINT DlgItem
  53. );
  54. LRESULT
  55. CALLBACK
  56. EditDwordValue_ValueDataWndProc(
  57. HWND hWnd,
  58. UINT Message,
  59. WPARAM wParam,
  60. LPARAM lParam
  61. );
  62. BOOL EditDwordValue_StoreDlgValueData(HWND hWnd, LPEDITVALUEPARAM lpEditValueParam);
  63. /*******************************************************************************
  64. *
  65. * GetDlgItemHex
  66. *
  67. * DESCRIPTION:
  68. * Like GetDlgItemInt, only for hexadecimal numbers.
  69. *
  70. * PARAMETERS:
  71. * See GetDlgItemInt.
  72. *
  73. *******************************************************************************/
  74. UINT
  75. PASCAL
  76. GetDlgItemHex(
  77. HWND hWnd,
  78. int nIDDlgItem,
  79. BOOL *lpTranslated
  80. )
  81. {
  82. TCHAR Buffer[10]; // Enough to hold 8 digits, null, extra
  83. UINT Length;
  84. DWORD Dword;
  85. UINT Index;
  86. DWORD Nibble;
  87. Dword = 0;
  88. //
  89. // We'll assume that the edit control contains only valid characters and
  90. // doesn't begin with any spaces (for Regedit this will be true). So, we
  91. // only need to validate that the length of the string isn't too long.
  92. //
  93. Length = GetDlgItemText(hWnd, nIDDlgItem, Buffer, sizeof(Buffer)/sizeof(TCHAR));
  94. if (Length > 0 && Length <= 8) {
  95. for (Index = 0; Index < Length; Index++) {
  96. if (Buffer[Index] >= TEXT('0') && Buffer[Index] <= TEXT('9'))
  97. Nibble = Buffer[Index] - TEXT('0');
  98. else if (Buffer[Index] >= TEXT('a') && Buffer[Index] <= TEXT('f'))
  99. Nibble = Buffer[Index] - TEXT('a') + 10;
  100. else
  101. Nibble = Buffer[Index] - TEXT('A') + 10;
  102. Dword = (Dword << 4) + Nibble;
  103. }
  104. *lpTranslated = TRUE;
  105. }
  106. else
  107. *lpTranslated = FALSE;
  108. return Dword;
  109. }
  110. /*******************************************************************************
  111. *
  112. * EditDwordValueDlgProc
  113. *
  114. * DESCRIPTION:
  115. *
  116. * PARAMETERS:
  117. *
  118. *******************************************************************************/
  119. INT_PTR
  120. CALLBACK
  121. EditDwordValueDlgProc(
  122. HWND hWnd,
  123. UINT Message,
  124. WPARAM wParam,
  125. LPARAM lParam
  126. )
  127. {
  128. LPEDITVALUEPARAM lpEditValueParam;
  129. BOOL Translated;
  130. DWORD Dword;
  131. lpEditValueParam = (LPEDITVALUEPARAM) GetWindowLongPtr(hWnd, DWLP_USER);
  132. switch (Message) {
  133. HANDLE_MSG(hWnd, WM_INITDIALOG, EditDwordValue_OnInitDialog);
  134. case WM_COMMAND:
  135. switch (GET_WM_COMMAND_ID(wParam, lParam)) {
  136. case IDC_VALUEDATA:
  137. if (GET_WM_COMMAND_CMD(wParam, lParam) == EN_KILLFOCUS)
  138. {
  139. // EditDwordValue_StoreDlgValueData(hWnd, lpEditValueParam);
  140. }
  141. break;
  142. case IDC_DECIMAL:
  143. case IDC_HEXADECIMAL:
  144. if (EditDwordValue_StoreDlgValueData(hWnd, lpEditValueParam))
  145. {
  146. EditDwordValue_SetValueDataText(hWnd, lpEditValueParam,
  147. GET_WM_COMMAND_ID(wParam, lParam));
  148. }
  149. else
  150. {
  151. // Since the value cannot be stored (only happens with decimal to
  152. // hex) undo the user's choice.
  153. CheckRadioButton(hWnd, IDC_HEXADECIMAL, IDC_DECIMAL, IDC_DECIMAL);
  154. }
  155. break;
  156. case IDOK:
  157. if (!EditDwordValue_StoreDlgValueData(hWnd, lpEditValueParam))
  158. {
  159. break;
  160. }
  161. // FALL THROUGH
  162. case IDCANCEL:
  163. EndDialog(hWnd, GET_WM_COMMAND_ID(wParam, lParam));
  164. break;
  165. }
  166. break;
  167. case WM_HELP:
  168. WinHelp(((LPHELPINFO) lParam)-> hItemHandle, g_pHelpFileName,
  169. HELP_WM_HELP, (ULONG_PTR) s_EditDwordValueHelpIDs);
  170. break;
  171. case WM_CONTEXTMENU:
  172. WinHelp((HWND) wParam, g_pHelpFileName, HELP_CONTEXTMENU,
  173. (ULONG_PTR) s_EditDwordValueHelpIDs);
  174. break;
  175. default:
  176. return FALSE;
  177. }
  178. return TRUE;
  179. }
  180. /*******************************************************************************
  181. *
  182. * EditDwordValue_OnInitDialog
  183. *
  184. * DESCRIPTION:
  185. *
  186. * PARAMETERS:
  187. * hWnd, handle of EditDwordValue window.
  188. * hFocusWnd,
  189. * lParam,
  190. *
  191. *******************************************************************************/
  192. BOOL
  193. PASCAL
  194. EditDwordValue_OnInitDialog(
  195. HWND hWnd,
  196. HWND hFocusWnd,
  197. LPARAM lParam
  198. )
  199. {
  200. LPEDITVALUEPARAM lpEditValueParam;
  201. SetWindowLongPtr(hWnd, DWLP_USER, lParam);
  202. lpEditValueParam = (LPEDITVALUEPARAM) lParam;
  203. s_PrevValueDataWndProc = SubclassWindow(GetDlgItem(hWnd, IDC_VALUEDATA),
  204. EditDwordValue_ValueDataWndProc);
  205. SetDlgItemText(hWnd, IDC_VALUENAME, lpEditValueParam-> pValueName);
  206. CheckRadioButton(hWnd, IDC_HEXADECIMAL, IDC_DECIMAL, IDC_HEXADECIMAL);
  207. EditDwordValue_SetValueDataText(hWnd, lpEditValueParam, IDC_HEXADECIMAL);
  208. return TRUE;
  209. UNREFERENCED_PARAMETER(hFocusWnd);
  210. }
  211. /*******************************************************************************
  212. *
  213. * EditDwordValue_SetValueDataText
  214. *
  215. * DESCRIPTION:
  216. *
  217. * PARAMETERS:
  218. * hWnd, handle of EditDwordValue window.
  219. *
  220. *******************************************************************************/
  221. VOID
  222. PASCAL
  223. EditDwordValue_SetValueDataText(
  224. HWND hWnd,
  225. LPEDITVALUEPARAM lpEditValueParam,
  226. UINT DlgItem
  227. )
  228. {
  229. TCHAR Buffer[12]; // Enough to hold 2^32 in decimal
  230. LPCTSTR lpFormatSpec;
  231. UINT uEditLength;
  232. s_SelectedBase = DlgItem;
  233. if (s_SelectedBase == IDC_DECIMAL)
  234. {
  235. uEditLength = 10;
  236. }
  237. else
  238. {
  239. uEditLength = 8;
  240. }
  241. SendDlgItemMessage( hWnd, IDC_VALUEDATA, EM_LIMITTEXT, ( WPARAM )uEditLength, 0L );
  242. lpFormatSpec = (DlgItem == IDC_HEXADECIMAL) ? s_HexadecimalFormatSpec :
  243. s_DecimalFormatSpec;
  244. wsprintf(Buffer, lpFormatSpec, ((LPDWORD) lpEditValueParam->
  245. pValueData)[0]);
  246. SetDlgItemText(hWnd, IDC_VALUEDATA, Buffer);
  247. }
  248. /*******************************************************************************
  249. *
  250. * EditDwordValue_ValueDataEditProc
  251. *
  252. * DESCRIPTION:
  253. *
  254. * PARAMETERS:
  255. * hWnd, handle of EditDwordValue window.
  256. *
  257. *******************************************************************************/
  258. LRESULT
  259. CALLBACK
  260. EditDwordValue_ValueDataWndProc(
  261. HWND hWnd,
  262. UINT Message,
  263. WPARAM wParam,
  264. LPARAM lParam
  265. )
  266. {
  267. TCHAR Char;
  268. switch (Message) {
  269. case WM_CHAR:
  270. Char = (TCHAR) wParam;
  271. if (Char >= TEXT(' ')) {
  272. if ((Char >= TEXT('0') && Char <= TEXT('9')))
  273. break;
  274. if (s_SelectedBase == IDC_HEXADECIMAL &&
  275. ((Char >= TEXT('A') && Char <= TEXT('F')) || ((Char >= TEXT('a')) &&
  276. (Char <= TEXT('f')))))
  277. break;
  278. MessageBeep(0);
  279. return 0;
  280. }
  281. break;
  282. }
  283. return CallWindowProc(s_PrevValueDataWndProc, hWnd, Message, wParam,
  284. lParam);
  285. }
  286. BOOL EditDwordValue_StoreDlgValueData(HWND hWnd, LPEDITVALUEPARAM lpEditValueParam)
  287. {
  288. BOOL fTranslated = TRUE;
  289. DWORD dwValue = (s_SelectedBase == IDC_HEXADECIMAL) ?
  290. GetDlgItemHex(hWnd, IDC_VALUEDATA, &fTranslated) :
  291. GetDlgItemInt(hWnd, IDC_VALUEDATA, &fTranslated,
  292. FALSE);
  293. //
  294. // Special case: "" == 0
  295. //
  296. if(!fTranslated && GetWindowTextLength(GetDlgItem(hWnd, IDC_VALUEDATA)) == 0) {
  297. dwValue = 0;
  298. fTranslated = TRUE;
  299. }
  300. if (fTranslated)
  301. {
  302. ((LPDWORD) lpEditValueParam-> pValueData)[0] = dwValue;
  303. }
  304. else
  305. {
  306. MessageBeep(0);
  307. if (InternalMessageBox(g_hInstance, hWnd, MAKEINTRESOURCE(IDS_EDITDWTRUNCATEDEC),
  308. MAKEINTRESOURCE(IDS_EDITWARNOVERFLOW), MB_ICONQUESTION | MB_YESNO | MB_DEFBUTTON2,
  309. NULL) == IDYES)
  310. {
  311. // Truncate Value
  312. dwValue = 0xffffffff;
  313. ((LPDWORD) lpEditValueParam-> pValueData)[0] = dwValue;
  314. fTranslated = TRUE;
  315. }
  316. }
  317. return fTranslated;
  318. }