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.

364 lines
12 KiB

  1. //*************************************************************
  2. // File name: NAMES.C
  3. //
  4. // Description: Profile control panel applet
  5. // This file contains the source code for
  6. // "Computer Names" dialog box.
  7. //
  8. // Microsoft Confidential
  9. // Copyright (c) Microsoft Corporation 1992-1994
  10. // All rights reserved
  11. //
  12. //*************************************************************
  13. #include <windows.h>
  14. #include "profile.h"
  15. //*************************************************************
  16. //
  17. // NamesDlgProc()
  18. //
  19. // Purpose: Dialog box procedure
  20. //
  21. // Parameters: HWND hDlg - Window handle of dialog box
  22. // UINT message - Window message
  23. // WPARAM wParam - WORD parameter
  24. // LPARAM lParam - LONG parameter
  25. //
  26. //
  27. // Return: (BOOL) TRUE if message was processed
  28. // FALSE if not
  29. //
  30. //*************************************************************
  31. INT_PTR CALLBACK NamesDlgProc(HWND hDlg, UINT message,
  32. WPARAM wParam, LPARAM lParam)
  33. {
  34. TCHAR szNewName [MAX_COMPUTER_NAME];
  35. LPTSTR lpNewName;
  36. BOOL bEnableAdd;
  37. switch (message)
  38. {
  39. case WM_INITDIALOG:
  40. //
  41. // Add the names to the list box.
  42. //
  43. ParseAndAddComputerNames(hDlg, IDD_NAMESLIST, glpList);
  44. //
  45. // Find out if anything was added to the list.
  46. // If so, then highlight the selected item. Otherwise,
  47. // disable the delete buttons.
  48. //
  49. if (SendDlgItemMessage (hDlg, IDD_NAMESLIST, LB_GETCOUNT, 0,0)) {
  50. SendDlgItemMessage (hDlg, IDD_NAMESLIST, LB_SETSEL, 1, lParam);
  51. } else {
  52. EnableWindow(GetDlgItem(hDlg, IDD_DELETE), FALSE);
  53. EnableWindow(GetDlgItem(hDlg, IDD_CLEARALL), FALSE);
  54. }
  55. //
  56. // Disable the "Add" button, and free the buffer. It will
  57. // be created again if the users presses the OK button.
  58. //
  59. EnableWindow (GetDlgItem(hDlg, IDD_ADD), FALSE);
  60. GlobalFree (glpList);
  61. //
  62. // Post ourselves a message so we can set the focus
  63. // appropriately.
  64. //
  65. PostMessage (hDlg, WM_USER+1, 0, 0);
  66. return TRUE;
  67. case WM_USER+1:
  68. //
  69. // Set the focus to the new computer name edit control
  70. // since most people will be starting from here.
  71. //
  72. SetFocus (GetDlgItem(hDlg, IDD_NEWNAME));
  73. break;
  74. case WM_COMMAND:
  75. switch (LOWORD(wParam)) {
  76. case IDD_NEWNAME:
  77. //
  78. // Enable the Add button if appropriate.
  79. //
  80. if (HIWORD(wParam) == EN_UPDATE) {
  81. bEnableAdd = GetDlgItemText(hDlg, IDD_NEWNAME,
  82. szNewName, MAX_COMPUTER_NAME);
  83. EnableWindow (GetDlgItem (hDlg, IDD_ADD), bEnableAdd);
  84. if (bEnableAdd) {
  85. SetDefButton (hDlg, IDD_ADD);
  86. } else {
  87. SetDefButton (hDlg, IDOK);
  88. }
  89. }
  90. break;
  91. case IDD_ADD:
  92. //
  93. // Retrieve the new name from the edit control
  94. //
  95. GetDlgItemText(hDlg, IDD_NEWNAME,
  96. szNewName, MAX_COMPUTER_NAME);
  97. //
  98. // Check to see if the user entered the new computer
  99. // name with a \\ infront of it. If so, remove it.
  100. //
  101. lpNewName = szNewName;
  102. if ( (szNewName[0] == TEXT('\\')) &&
  103. (szNewName[1] == TEXT('\\')) ) {
  104. lpNewName += 2;
  105. }
  106. //
  107. // Add the new name if it doesn't already exist.
  108. //
  109. if (SendDlgItemMessage (hDlg, IDD_NAMESLIST, LB_FINDSTRINGEXACT,
  110. 0, (LPARAM) lpNewName) == LB_ERR) {
  111. SendDlgItemMessage (hDlg, IDD_NAMESLIST, LB_ADDSTRING,
  112. 0, (LPARAM) lpNewName);
  113. }
  114. //
  115. // Erase the contents of the edit control, and set the
  116. // focus back to the edit control for quick entry of
  117. // names.
  118. //
  119. SetWindowText (GetDlgItem(hDlg, IDD_NEWNAME), NULL);
  120. SetFocus (GetDlgItem(hDlg, IDD_NEWNAME));
  121. SetDefButton (hDlg, IDOK);
  122. //
  123. // Enable the delete buttons.
  124. //
  125. EnableWindow (GetDlgItem(hDlg, IDD_DELETE), TRUE);
  126. EnableWindow (GetDlgItem(hDlg, IDD_CLEARALL), TRUE);
  127. break;
  128. case IDD_DELETE:
  129. {
  130. INT iSel [MAX_NUM_COMPUTERS];
  131. INT iCount, i;
  132. //
  133. // Retrieve an array of selected items.
  134. //
  135. iCount = (int)SendDlgItemMessage (hDlg, IDD_NAMESLIST, LB_GETSELITEMS,
  136. MAX_NUM_COMPUTERS, (LPARAM) iSel);
  137. if (iCount == LB_ERR) {
  138. break;
  139. }
  140. //
  141. // Now loop through the array and delete the items.
  142. // Note that we have to do this from the bottom up,
  143. // or the index's would be wrong as items are deleted
  144. // from the top of the list.
  145. //
  146. for (i = iCount-1; i >= 0; i--) {
  147. SendDlgItemMessage (hDlg, IDD_NAMESLIST, LB_DELETESTRING,
  148. iSel[i], 0);
  149. }
  150. //
  151. // Find out if anything is left in the list.
  152. // If not, then disable the delete buttons.
  153. //
  154. if (!SendDlgItemMessage (hDlg, IDD_NAMESLIST,
  155. LB_GETCOUNT, 0,0)) {
  156. EnableWindow(GetDlgItem(hDlg, IDD_DELETE), FALSE);
  157. EnableWindow(GetDlgItem(hDlg, IDD_CLEARALL), FALSE);
  158. }
  159. }
  160. break;
  161. case IDD_CLEARALL:
  162. //
  163. // User requested to empty the entire list.
  164. //
  165. SendDlgItemMessage (hDlg, IDD_NAMESLIST, LB_RESETCONTENT,
  166. 0, 0);
  167. EnableWindow(GetDlgItem(hDlg, IDD_DELETE), FALSE);
  168. EnableWindow(GetDlgItem(hDlg, IDD_CLEARALL), FALSE);
  169. break;
  170. case IDOK:
  171. {
  172. BOOL bFound;
  173. TCHAR szProfile[MAX_ERROR_MSG];
  174. TCHAR szErrorMsg [MAX_ERROR_MSG];
  175. //
  176. // Make sure the user doesn't still have a name
  177. // in the "New Name" field that he forgot to "add"
  178. //
  179. if (GetDlgItemText(hDlg, IDD_NEWNAME,
  180. szNewName, MAX_COMPUTER_NAME)) {
  181. LoadString (hInstance, IDS_NAME, szProfile, MAX_ERROR_MSG);
  182. LoadString (hInstance, IDS_ADDNAME, szErrorMsg, MAX_ERROR_MSG);
  183. if (MessageBox (hDlg, szErrorMsg, szProfile,
  184. MB_YESNO | MB_ICONQUESTION) == IDYES) {
  185. //
  186. // User would like to add the name before leaving.
  187. // Check to see if the user entered the new computer
  188. // name with a \\ infront of it. If so, remove it.
  189. //
  190. lpNewName = szNewName;
  191. if ( (szNewName[0] == TEXT('\\')) &&
  192. (szNewName[1] == TEXT('\\')) ) {
  193. lpNewName += 2;
  194. }
  195. //
  196. // Check for duplicates first, then add the name
  197. // if it doesn't exist.
  198. //
  199. if (SendDlgItemMessage (hDlg, IDD_NAMESLIST,
  200. LB_FINDSTRINGEXACT,
  201. 0,
  202. (LPARAM) lpNewName) == LB_ERR) {
  203. SendDlgItemMessage (hDlg, IDD_NAMESLIST,
  204. LB_ADDSTRING,
  205. 0,
  206. (LPARAM) lpNewName);
  207. }
  208. }
  209. }
  210. //
  211. // Create the list of names to be passed back.
  212. //
  213. glpList = CreateList (hDlg, IDD_NAMESLIST, NULL, &bFound);
  214. EndDialog(hDlg, TRUE);
  215. return TRUE;
  216. }
  217. case IDCANCEL:
  218. //
  219. // Close the dialog box.
  220. //
  221. EndDialog(hDlg, FALSE);
  222. return TRUE;
  223. case IDD_NAMESHELP:
  224. //
  225. // User requested help
  226. //
  227. WinHelp (hDlg, szHelpFileName, HELP_CONTEXT,
  228. NAMES_HELP_CONTEXT);
  229. break;
  230. default:
  231. break;
  232. }
  233. break;
  234. default:
  235. //
  236. // User requested help via the F1 key.
  237. //
  238. if (message == uiShellHelp) {
  239. WinHelp (hDlg, szHelpFileName, HELP_CONTENTS, 0);
  240. }
  241. break;
  242. }
  243. return FALSE;
  244. }
  245. //*************************************************************
  246. //
  247. // SetDefButton()
  248. //
  249. // Purpose: Sets the default button
  250. //
  251. // Parameters: HWND hDlg - Window handle of dialog box
  252. // INT idButton - ID of button
  253. //
  254. // Return: void
  255. //
  256. //*************************************************************
  257. void SetDefButton(HWND hwndDlg, INT idButton)
  258. {
  259. LRESULT lr;
  260. if (HIWORD(lr = SendMessage(hwndDlg, DM_GETDEFID, 0, 0)) == DC_HASDEFID)
  261. {
  262. HWND hwndOldDefButton = GetDlgItem(hwndDlg, LOWORD(lr));
  263. //
  264. // If we are setting the default button to the button which is
  265. // already default, then exit now.
  266. //
  267. if (LOWORD(lr) == idButton) {
  268. return;
  269. }
  270. SendMessage (hwndOldDefButton,
  271. BM_SETSTYLE,
  272. MAKEWPARAM(BS_PUSHBUTTON, 0),
  273. MAKELPARAM(TRUE, 0));
  274. }
  275. SendMessage( hwndDlg, DM_SETDEFID, idButton, 0L );
  276. SendMessage( GetDlgItem(hwndDlg, idButton),
  277. BM_SETSTYLE,
  278. MAKEWPARAM( BS_DEFPUSHBUTTON, 0 ),
  279. MAKELPARAM( TRUE, 0 ));
  280. }