Leaked source code of windows server 2003
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.

469 lines
13 KiB

  1. /******************************************************************************
  2. Source File: Profile Association Page.CPP
  3. Copyright (c) 1997 by Microsoft Corporation
  4. Change History:
  5. 05-09-1997 hideyukn - Created
  6. ******************************************************************************/
  7. #include "ICMUI.H"
  8. #include "Resource.H"
  9. // It looks like the way to make the icon draw is to subclass the Icon control
  10. // in the window. So, here's a Window Procedure for the subclass
  11. CONST DWORD ProfileAssociationUIHelpIds[] = {
  12. AddButton, IDH_ASSOCDEVICE_ADD,
  13. RemoveButton, IDH_ASSOCDEVICE_REMOVE,
  14. #if !defined(_WIN95_) // context-sentitive help
  15. ProfileFilename, IDH_ASSOCDEVICE_NAME,
  16. DeviceListControl, IDH_ASSOCDEVICE_LIST,
  17. DeviceListControlText, IDH_ASSOCDEVICE_LIST,
  18. StatusIcon, IDH_DISABLED,
  19. #endif
  20. 0, 0
  21. };
  22. // CProfileAssociationPage member functions
  23. // Class Constructor
  24. CProfileAssociationPage::CProfileAssociationPage(HINSTANCE hiWhere,
  25. LPCTSTR lpstrTarget) {
  26. m_pcpTarget = NULL;
  27. m_csProfile = lpstrTarget;
  28. m_psp.dwSize = sizeof m_psp;
  29. m_psp.dwFlags |= PSP_USETITLE;
  30. m_psp.hInstance = hiWhere;
  31. m_psp.pszTemplate = MAKEINTRESOURCE(AssociateDevicePage);
  32. m_psp.pszTitle = MAKEINTRESOURCE(AssociatePropertyString);
  33. }
  34. // Class destructor
  35. CProfileAssociationPage::~CProfileAssociationPage() {
  36. if (m_pcpTarget) {
  37. delete m_pcpTarget;
  38. }
  39. }
  40. // Dialog box (property sheet) initialization
  41. BOOL CProfileAssociationPage::OnInit() {
  42. m_pcpTarget = new CProfile(m_csProfile);
  43. if (m_pcpTarget) {
  44. // Set profile filename
  45. SetDlgItemText(m_hwnd, ProfileFilename, m_pcpTarget->GetName());
  46. // Update ICON to show installed/non-installed status.
  47. HICON hIcon = LoadIcon(CGlobals::Instance(),
  48. MAKEINTRESOURCE(m_pcpTarget->IsInstalled() ? DefaultIcon : UninstalledIcon));
  49. if (hIcon) {
  50. SendDlgItemMessage(m_hwnd, StatusIcon, STM_SETICON, (WPARAM) hIcon, 0);
  51. }
  52. // Clean up add/delete list.
  53. m_cuaAdd.Empty();
  54. m_cuaDelete.Empty();
  55. // Build tentitive association list.
  56. ConstructAssociations();
  57. // And then, fill up device listbox
  58. UpdateDeviceListBox();
  59. // And set focus on AddButton.
  60. SetFocus(GetDlgItem(m_hwnd,AddButton));
  61. DisableApplyButton();
  62. SettingChanged(FALSE);
  63. return TRUE;
  64. } else {
  65. return FALSE;
  66. }
  67. }
  68. // Private ConstructAssociations function- this constructs the list of
  69. // tentative associations- this starts out as the true list from the profile
  70. // object.
  71. VOID CProfileAssociationPage::ConstructAssociations() {
  72. m_cuaAssociate.Empty(); // Clean it up!
  73. for (unsigned u = 0; u < m_pcpTarget->AssociationCount(); u++) {
  74. for (unsigned uDelete = 0;
  75. uDelete < m_cuaDelete.Count();
  76. uDelete++) {
  77. if (m_cuaDelete[uDelete] == m_pcpTarget->Association(u))
  78. break;
  79. }
  80. if (uDelete == m_cuaDelete.Count()) // Not yet deleted
  81. m_cuaAssociate.Add(m_pcpTarget->Association(u));
  82. }
  83. // Now, add any added associations
  84. for (u = 0; u < m_cuaAdd.Count(); u++)
  85. m_cuaAssociate.Add(m_cuaAdd[u]);
  86. }
  87. // Public method for noting tentative associations to be added
  88. void CProfileAssociationPage::Associate(unsigned uAdd) {
  89. // First, see if it's on the delete list. If it is, remove it from there
  90. // Otherwise, add us to the add list, if it's a new association.
  91. for (unsigned u = 0; u < m_cuaDelete.Count(); u++)
  92. if (uAdd == m_cuaDelete[u])
  93. break;
  94. if (u < m_cuaDelete.Count())
  95. m_cuaDelete.Remove(u);
  96. else {
  97. for (u = 0; u < m_cuaAssociate.Count(); u++)
  98. if (m_cuaAssociate[u] == uAdd)
  99. break;
  100. if (u == m_cuaAssociate.Count())
  101. m_cuaAdd.Add(uAdd);
  102. }
  103. DeviceListChanged();
  104. }
  105. // Public Method for removing tentative associations
  106. void CProfileAssociationPage::Dissociate(unsigned uRemove) {
  107. // First, see if it's on the add list. If it is, remove it from there
  108. // Otherwise, add us to the delete list.
  109. for (unsigned u = 0; u < m_cuaAdd.Count(); u++)
  110. if (uRemove == m_cuaAdd[u])
  111. break;
  112. if (u < m_cuaAdd.Count())
  113. m_cuaAdd.Remove(u);
  114. else
  115. m_cuaDelete.Add(uRemove);
  116. DeviceListChanged();
  117. }
  118. VOID CProfileAssociationPage::UpdateDeviceListBox() {
  119. // Add the associations to the list
  120. SendDlgItemMessage(m_hwnd, DeviceListControl, LB_RESETCONTENT, 0, 0);
  121. for (unsigned u = 0; u < AssociationCount(); u++ ) {
  122. LRESULT iItem = SendDlgItemMessage(m_hwnd, DeviceListControl, LB_ADDSTRING,
  123. 0, (LPARAM) DisplayName(u));
  124. SendDlgItemMessage(m_hwnd, DeviceListControl, LB_SETITEMDATA, iItem,
  125. (LPARAM) Association(u));
  126. }
  127. // If there are no associations, disable the Remove Devices button
  128. HWND hwndRemove = GetDlgItem(m_hwnd,RemoveButton);
  129. // If focus is on Remove, move it to Add button.
  130. if (GetFocus() == hwndRemove) {
  131. HWND hwndAdd = GetDlgItem(m_hwnd, AddButton);
  132. SetFocus(hwndAdd);
  133. SendMessage(hwndRemove, BM_SETSTYLE, BS_PUSHBUTTON, MAKELPARAM(TRUE, 0));
  134. SendMessage(hwndAdd, BM_SETSTYLE, BS_DEFPUSHBUTTON, MAKELPARAM(TRUE, 0));
  135. }
  136. EnableWindow(hwndRemove, !!(AssociationCount()));
  137. // If there is any device, set focus to 1st entry.
  138. if (SendDlgItemMessage(m_hwnd, DeviceListControl, LB_GETCOUNT, 0, 0))
  139. SendDlgItemMessage(m_hwnd, DeviceListControl, LB_SETCURSEL, 0, 0);
  140. }
  141. BOOL CProfileAssociationPage::OnCommand(WORD wNotifyCode, WORD wid, HWND hwndControl) {
  142. switch (wid) {
  143. case AddButton :
  144. if (wNotifyCode == BN_CLICKED) {
  145. CAddDeviceDialog cadd(this, m_hwnd);
  146. if (!cadd.bCanceled()) {
  147. UpdateDeviceListBox();
  148. EnableApplyButton();
  149. SettingChanged(TRUE);
  150. }
  151. return TRUE;
  152. }
  153. break;
  154. case RemoveButton :
  155. if (wNotifyCode == BN_CLICKED) {
  156. LRESULT i = SendDlgItemMessage(m_hwnd, DeviceListControl,
  157. LB_GETCURSEL, 0, 0);
  158. if (i == -1)
  159. return TRUE;
  160. unsigned uItem = (unsigned)SendDlgItemMessage(m_hwnd, DeviceListControl,
  161. LB_GETITEMDATA, i, 0);
  162. Dissociate(uItem);
  163. UpdateDeviceListBox();
  164. EnableApplyButton();
  165. SettingChanged(TRUE);
  166. return TRUE;
  167. }
  168. break;
  169. case DeviceListControl :
  170. if (wNotifyCode == LBN_SELCHANGE) {
  171. EnableWindow(GetDlgItem(m_hwnd, RemoveButton),
  172. -1 != SendDlgItemMessage(m_hwnd, DeviceListControl, LB_GETCURSEL, 0, 0));
  173. return TRUE;
  174. }
  175. break;
  176. }
  177. return TRUE;
  178. }
  179. BOOL CProfileAssociationPage::OnDestroy() {
  180. if (m_pcpTarget) {
  181. delete m_pcpTarget;
  182. m_pcpTarget = (CProfile *) NULL;
  183. }
  184. return FALSE; // still need to handle this message by def. proc.
  185. }
  186. // Common control notification override
  187. BOOL CProfileAssociationPage::OnNotify(int idCtrl, LPNMHDR pnmh) {
  188. switch (pnmh -> code) {
  189. case PSN_APPLY: {
  190. if (SettingChanged()) {
  191. // Disable apply button, because current request are
  192. // going to be "Applied".
  193. DisableApplyButton();
  194. // We are going to update changed.
  195. SettingChanged(FALSE);
  196. // Remove any associations we're removing
  197. while (m_cuaDelete.Count()) {
  198. m_pcpTarget->Dissociate(m_pcpTarget->DeviceName(m_cuaDelete[0]));
  199. m_cuaDelete.Remove(0);
  200. }
  201. // Add any associations we're adding
  202. while (m_cuaAdd.Count()) {
  203. m_pcpTarget->Associate(m_pcpTarget->DeviceName(m_cuaAdd[0]));
  204. m_cuaAdd.Remove(0);
  205. }
  206. // Re-create CProfile object.
  207. //
  208. delete m_pcpTarget;
  209. m_pcpTarget = new CProfile(m_csProfile);
  210. if (!m_pcpTarget)
  211. {
  212. // WINBUG #83126 2-7-2000 bhouse Improve error handling in OnNotify
  213. // Old Comment:
  214. // - proper error should happen.
  215. return FALSE;
  216. }
  217. // Re-Build tentitive association list.
  218. ConstructAssociations();
  219. UpdateDeviceListBox();
  220. // check the install status to refect icon.
  221. HICON hIcon = LoadIcon(CGlobals::Instance(),
  222. MAKEINTRESOURCE(m_pcpTarget->IsInstalled() ? DefaultIcon : UninstalledIcon));
  223. if (hIcon) {
  224. SendDlgItemMessage(m_hwnd, StatusIcon, STM_SETICON, (WPARAM) hIcon, 0);
  225. }
  226. }
  227. break;
  228. }
  229. }
  230. return TRUE;
  231. }
  232. // Context-sensitive help handler
  233. BOOL CProfileAssociationPage::OnHelp(LPHELPINFO pHelp) {
  234. if (pHelp->iContextType == HELPINFO_WINDOW) {
  235. WinHelp((HWND) pHelp->hItemHandle, WINDOWS_HELP_FILE,
  236. HELP_WM_HELP, (ULONG_PTR) (LPSTR) ProfileAssociationUIHelpIds);
  237. }
  238. return (TRUE);
  239. }
  240. BOOL CProfileAssociationPage::OnContextMenu(HWND hwnd) {
  241. DWORD iCtrlID = GetDlgCtrlID(hwnd);
  242. WinHelp(hwnd, WINDOWS_HELP_FILE,
  243. HELP_CONTEXTMENU, (ULONG_PTR) (LPSTR) ProfileAssociationUIHelpIds);
  244. return (TRUE);
  245. }
  246. // Context Help for AddDevice Dialog.
  247. CONST DWORD AddDeviceUIHelpIds[] = {
  248. AddDeviceButton, IDH_ADDDEVICEUI_ADD,
  249. DeviceListControl, IDH_ADDDEVICEUI_LIST,
  250. DeviceListControlText, IDH_ADDDEVICEUI_LIST,
  251. 0, 0
  252. };
  253. // CAddDeviceDialog class constructor
  254. CAddDeviceDialog::CAddDeviceDialog(CProfileAssociationPage *pcpas,
  255. HWND hwndParent) :
  256. CDialog(pcpas->Instance(), AddDeviceDialog, hwndParent) {
  257. m_pcpasBoss = pcpas;
  258. m_bCanceled = TRUE;
  259. DoModal();
  260. }
  261. // Dialog Initialization routine
  262. BOOL CAddDeviceDialog::OnInit() {
  263. CProfile * pcpThis = m_pcpasBoss->Profile();
  264. m_hwndList = GetDlgItem(m_hwnd, DeviceListControl);
  265. m_hwndButton = GetDlgItem(m_hwnd, AddDeviceButton);
  266. // This must not list associated (tentatively) devices, per the spec
  267. for (unsigned uDevice = 0; uDevice < pcpThis->DeviceCount(); uDevice++) {
  268. for (unsigned u = 0; u < m_pcpasBoss->AssociationCount(); u++)
  269. if (uDevice == m_pcpasBoss->Association(u))
  270. break;
  271. if (u < m_pcpasBoss->AssociationCount())
  272. continue; // Don't insert this one...
  273. LRESULT idItem = SendMessage(m_hwndList, LB_ADDSTRING, (WPARAM)0,
  274. (LPARAM) pcpThis->DisplayName(uDevice));
  275. SendMessage(m_hwndList, LB_SETITEMDATA, idItem, (LPARAM) uDevice);
  276. }
  277. if (SendMessage(m_hwndList, LB_GETCOUNT, 0, 0))
  278. SendMessage(m_hwndList, LB_SETCURSEL, 0, 0);
  279. EnableWindow(m_hwndButton, -1 !=
  280. SendMessage(m_hwndList, LB_GETCURSEL, 0, 0));
  281. return TRUE;
  282. }
  283. // Dialog notification handler
  284. BOOL CAddDeviceDialog::OnCommand(WORD wNotification, WORD wid,
  285. HWND hwndControl){
  286. switch (wNotification) {
  287. case LBN_SELCHANGE:
  288. EnableWindow(m_hwndButton, -1 !=
  289. SendMessage(m_hwndList, LB_GETCURSEL, 0, 0));
  290. return TRUE;
  291. case BN_CLICKED:
  292. if (wid == AddDeviceButton) {
  293. LRESULT i = SendMessage(m_hwndList, LB_GETCURSEL, 0, 0);
  294. if (i == -1)
  295. return TRUE;
  296. unsigned uItem = (unsigned) SendMessage(m_hwndList,
  297. LB_GETITEMDATA, i, 0);
  298. m_pcpasBoss->Associate(uItem);
  299. // Selection has been made.
  300. m_bCanceled = FALSE;
  301. }
  302. break;
  303. case LBN_DBLCLK:
  304. return OnCommand(BN_CLICKED, AddDeviceButton, m_hwndButton);
  305. }
  306. return CDialog::OnCommand(wNotification, wid, hwndControl);
  307. }
  308. // Context-sensitive help handler
  309. BOOL CAddDeviceDialog::OnHelp(LPHELPINFO pHelp) {
  310. if (pHelp->iContextType == HELPINFO_WINDOW) {
  311. WinHelp((HWND) pHelp->hItemHandle, WINDOWS_HELP_FILE,
  312. HELP_WM_HELP, (ULONG_PTR) (LPSTR) AddDeviceUIHelpIds);
  313. }
  314. return (TRUE);
  315. }
  316. BOOL CAddDeviceDialog::OnContextMenu(HWND hwnd) {
  317. DWORD iCtrlID = GetDlgCtrlID(hwnd);
  318. WinHelp(hwnd, WINDOWS_HELP_FILE,
  319. HELP_CONTEXTMENU, (ULONG_PTR) (LPSTR) AddDeviceUIHelpIds);
  320. return (TRUE);
  321. }