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.

659 lines
12 KiB

  1. /*++
  2. Copyright (C) Microsoft Corporation, 1995 - 1998
  3. All rights reserved.
  4. Module Name:
  5. portdlg.cxx
  6. Abstract:
  7. Printer Port Add / Delete and Monitor Add / Delete dialogs
  8. Author:
  9. Steve Kiraly (SteveKi) 11/06/95
  10. Revision History:
  11. --*/
  12. #include "precomp.hxx"
  13. #pragma hdrstop
  14. #include "psetup.hxx"
  15. #include "drvsetup.hxx"
  16. #include "portdlg.hxx"
  17. #include "portslv.hxx"
  18. /*++
  19. Routine Name:
  20. TAddPort
  21. Routine Description:
  22. Constructs the add port dialog.
  23. Arguments:
  24. hWnd - parent window handle
  25. strServerName - Print server name, NULL if local machine
  26. bAdministrator - Administrator flag TRUE if we
  27. --*/
  28. TAddPort::
  29. TAddPort(
  30. IN const HWND hWnd,
  31. IN LPCTSTR pszServerName,
  32. IN const BOOL bAdministrator
  33. ) : _hWnd( hWnd ),
  34. _bAdministrator( bAdministrator ),
  35. _bValid( FALSE ),
  36. _hPSetupMonitorInfo( NULL ),
  37. _pPSetup( NULL ),
  38. _pszServerName( pszServerName ),
  39. _bPortAdded( FALSE )
  40. {
  41. DBGMSG( DBG_TRACE, ( "TAddPort::ctor\n") );
  42. //
  43. // Load the setup library.
  44. //
  45. _pPSetup = new TPSetup;
  46. if( !VALID_PTR( _pPSetup ) ) {
  47. DBGMSG( DBG_WARN, ( "_pPSetup failed instantiation with %d.\n", GetLastError() ) );
  48. return;
  49. }
  50. //
  51. // Create the setup monitor handle.
  52. //
  53. _hPSetupMonitorInfo = _pPSetup->PSetupCreateMonitorInfo( pszServerName, _hDlg);
  54. if( !_hPSetupMonitorInfo ){
  55. DBGMSG( DBG_WARN, ( "_pPSetup.PSetupCreateMonitorInfo failed with %d.\n", GetLastError() ) );
  56. return;
  57. }
  58. _bValid = TRUE;
  59. }
  60. /*++
  61. Routine Name:
  62. TAddPort
  63. Routine Description:
  64. Destructor releases add port resources.
  65. Arguments:
  66. None.
  67. Return Value:
  68. Nothing.
  69. --*/
  70. TAddPort::
  71. ~TAddPort(
  72. )
  73. {
  74. DBGMSG( DBG_TRACE, ( "TAddPort::dtor\n") );
  75. //
  76. // Release the monitor info handle.
  77. //
  78. if( _hPSetupMonitorInfo )
  79. _pPSetup->PSetupDestroyMonitorInfo( _hPSetupMonitorInfo );
  80. //
  81. // Unload the setup library.
  82. //
  83. if( _pPSetup )
  84. delete _pPSetup;
  85. }
  86. /*++
  87. Routine Name:
  88. bValid
  89. Routine Description:
  90. Returns object validity
  91. Arguments:
  92. None.
  93. Return Value:
  94. Current object state.
  95. --*/
  96. BOOL
  97. TAddPort::
  98. bValid(
  99. VOID
  100. )
  101. {
  102. return _bValid;
  103. }
  104. /*++
  105. Routine Name:
  106. bDoModal
  107. Routine Description:
  108. Eexcutes the modal dialog.
  109. Arguments:
  110. None.
  111. Return Value:
  112. TRUE port was added successfully
  113. FALSE port was not added.
  114. --*/
  115. BOOL
  116. TAddPort::
  117. bDoModal(
  118. VOID
  119. )
  120. {
  121. //
  122. // Create a modal dialog.
  123. //
  124. return (BOOL)DialogBoxParam( ghInst,
  125. MAKEINTRESOURCE( kResourceId ),
  126. _hWnd,
  127. MGenericDialog::SetupDlgProc,
  128. (LPARAM)this );
  129. }
  130. /*++
  131. Routine Name:
  132. bSetUI
  133. Routine Description:
  134. Sets the data on the UI.
  135. Arguments:
  136. None.
  137. Return Value:
  138. TRUE data placed on UI.
  139. FALSE error occurred.
  140. --*/
  141. BOOL
  142. TAddPort::
  143. bSetUI(
  144. VOID
  145. )
  146. {
  147. DBGMSG( DBG_TRACE, ( "TAddPort::bSetUI\n") );
  148. //
  149. // Create local copies of the control ID's this saves
  150. // some execution time and alot of typing.
  151. //
  152. _hctlMonitorList = GetDlgItem( _hDlg, IDC_ADD_PORT_MONITOR_LIST );
  153. _hctlBrowse = GetDlgItem( _hDlg, IDC_ADD_PORT_BROWSE );
  154. _bPortAdded = FALSE;
  155. SPLASSERT( _hctlMonitorList );
  156. SPLASSERT( _hctlBrowse );
  157. //
  158. // Reset the monitor list box contents.
  159. //
  160. ListBox_ResetContent( _hctlMonitorList );
  161. //
  162. // Fill the list box with the list of installed monitors.
  163. //
  164. BOOL bStatus;
  165. TCHAR pszName[kStrMax];
  166. DWORD dwSize = COUNTOF( pszName );
  167. for( UINT i = 0 ; ; i++ ){
  168. bStatus = _pPSetup->PSetupEnumMonitor( _hPSetupMonitorInfo,
  169. i,
  170. pszName,
  171. &dwSize );
  172. if( bStatus ) {
  173. DBGMSG( DBG_TRACE, ( "Adding Monitor " TSTR " \n", pszName ) );
  174. //
  175. // Hide the fax monitor, users are not allowed to
  176. // add fax ports.
  177. //
  178. if( _tcsicmp( pszName, FAX_MONITOR_NAME ) )
  179. {
  180. ListBox_AddString( _hctlMonitorList, pszName );
  181. }
  182. dwSize = COUNTOF( pszName );
  183. } else {
  184. break;
  185. }
  186. }
  187. //
  188. // If we are an administrator selection the first monitor in the list.
  189. //
  190. if( _bAdministrator ){
  191. ListBox_SetCurSel( _hctlMonitorList, 0 );
  192. }
  193. //
  194. // Disable the controls if not an administrator.
  195. //
  196. vEnableCtl( _hDlg, IDC_ADD_PORT_MONITOR_LIST, _bAdministrator );
  197. vEnableCtl( _hDlg, IDC_ADD_PORT_BROWSE, _bAdministrator );
  198. vEnableCtl( _hDlg, IDC_ADD_PORT_TEXT, _bAdministrator );
  199. vEnableCtl( _hDlg, IDC_ADD_PORT, _bAdministrator );
  200. //
  201. // We cannot add new port monitors remotly, this is a monitor setup
  202. // and spooler limitation.
  203. //
  204. if( _pszServerName ){
  205. vEnableCtl( _hDlg, IDC_ADD_PORT_BROWSE, FALSE );
  206. }
  207. return TRUE;
  208. }
  209. /*++
  210. Routine Name:
  211. bReadUI
  212. Routine Description:
  213. Reads the data from the UI.
  214. Arguments:
  215. None.
  216. Return Value:
  217. TRUE UI data read successfully.
  218. FALSE failure reading UI data.
  219. --*/
  220. BOOL
  221. TAddPort::
  222. bReadUI(
  223. VOID
  224. )
  225. {
  226. TCHAR pszName[kStrMax];
  227. BOOL bStatus = TRUE;
  228. DBGMSG( DBG_TRACE, ( "TAddPort::bReadUI\n") );
  229. //
  230. // Ge the currently selected item.
  231. //
  232. UINT uSel = ListBox_GetCurSel( _hctlMonitorList );
  233. //
  234. // Get the name of the currently selected item.
  235. //
  236. if( ListBox_GetText( _hctlMonitorList, uSel, pszName ) == LB_ERR ){
  237. DBGMSG( DBG_WARN, ( "Monitor find string failed.\n" ) );
  238. vShowUnexpectedError( _hDlg, IDS_ERR_MONITOR_TITLE );
  239. bStatus = FALSE;
  240. } else {
  241. //
  242. // Save the selected monitor name.
  243. //
  244. bStatus = _strMonitorName.bUpdate( pszName );
  245. }
  246. return bStatus;
  247. }
  248. /*++
  249. Routine Name:
  250. bSaveUI
  251. Routine Description:
  252. Saves the UI data, this routien will do an AddPort call
  253. with the montior selected in the list box.
  254. Arguments:
  255. None.
  256. Return Value:
  257. Return value of the AddPort api call.
  258. --*/
  259. BOOL
  260. TAddPort::
  261. bSaveUI(
  262. VOID
  263. )
  264. {
  265. DBGMSG( DBG_TRACE, ( "TAddPort::bSavetUI\n") );
  266. DBGMSG( DBG_TRACE, ( "Adding port to Monitor " TSTR "\n", (LPCTSTR)_strMonitorName ) );
  267. TStatusB bStatus;
  268. //
  269. // Add the port using the selected montior.
  270. //
  271. bStatus DBGCHK = AddPort(
  272. (LPTSTR)_pszServerName,
  273. _hDlg,
  274. (LPTSTR)(LPCTSTR)_strMonitorName );
  275. if( !bStatus ){
  276. //
  277. // If not a cancel request then display error message.
  278. //
  279. if( GetLastError() != ERROR_CANCELLED ){
  280. DBGMSG( DBG_WARN, ( "Error adding port with %d.\n", GetLastError() ) );
  281. extern MSG_ERRMAP gaMsgErrMapPorts[];
  282. iMessage( _hDlg,
  283. IDS_ERR_MONITOR_TITLE,
  284. IDS_ERR_ADD_PORT,
  285. MB_OK|MB_ICONSTOP,
  286. kMsgGetLastError,
  287. gaMsgErrMapPorts );
  288. return FALSE;
  289. }
  290. } else {
  291. //
  292. // Indicate at least one port has been added. This
  293. // flag will be used to indicate to the caller if the ports
  294. // list needs to be refreshed.
  295. //
  296. _bPortAdded = TRUE;
  297. //
  298. // A port has changed modify the cancel button to close.
  299. //
  300. TString strClose;
  301. if( strClose.bLoadString( ghInst, IDS_TEXT_CLOSE ) ){
  302. SetWindowText( GetDlgItem( _hDlg, IDCANCEL ), strClose );
  303. }
  304. //
  305. // The followng code modfies the default button
  306. // style because on return from adding a port
  307. // the Ok has the default button style.
  308. // i.e. has a bold border.
  309. //
  310. // Make the OK button a normal button state.
  311. //
  312. SendMessage( GetDlgItem( _hDlg, IDC_ADD_PORT ),
  313. BM_SETSTYLE,
  314. MAKEWPARAM( BS_PUSHBUTTON, 0 ),
  315. MAKELPARAM( TRUE, 0 ) );
  316. //
  317. // Set the focus to the cancel button.
  318. //
  319. SetFocus( GetDlgItem( _hDlg, IDCANCEL ) );
  320. //
  321. // Change the cancel button to the default button.
  322. //
  323. SendMessage( GetDlgItem( _hDlg, IDCANCEL ),
  324. BM_SETSTYLE,
  325. MAKEWPARAM( BS_DEFPUSHBUTTON, 0 ),
  326. MAKELPARAM( TRUE, 0 ) );
  327. }
  328. return TRUE;
  329. }
  330. /*++
  331. Routine Name:
  332. bAddNewMonitor
  333. Routine Description:
  334. Handles destroying the current monitor list and
  335. calling setup to get a new monitor potentialy form
  336. another dirve or network resources.
  337. Arguments:
  338. None.
  339. Return Value:
  340. TRUE if new monitor updated and UI reflects this.
  341. FALSE error occured UI remains unchanged.
  342. --*/
  343. BOOL
  344. TAddPort::
  345. bAddNewMonitor(
  346. VOID
  347. )
  348. {
  349. DBGMSG( DBG_TRACE, ( "TAddPort::bAddNewMonitor\n") );
  350. //
  351. // Get a new monitor form a location or disk.
  352. //
  353. if( !_pPSetup->PSetupInstallMonitor(_hDlg ) ) {
  354. //
  355. // If the user canceled the request.
  356. //
  357. if( GetLastError() == ERROR_CANCELLED ){
  358. DBGMSG( DBG_TRACE, ( "Monitor install cancel request.\n" ) );
  359. return TRUE;
  360. }
  361. //
  362. // Some other error occured.
  363. //
  364. return FALSE;
  365. }
  366. //
  367. // Release the old monitor info
  368. //
  369. if( _hPSetupMonitorInfo )
  370. _pPSetup->PSetupDestroyMonitorInfo( _hPSetupMonitorInfo );
  371. //
  372. // Update current monitor info.
  373. //
  374. _hPSetupMonitorInfo = _pPSetup->PSetupCreateMonitorInfo( _pszServerName, _hDlg );
  375. if (!_hPSetupMonitorInfo)
  376. {
  377. DBGMSG( DBG_WARN, ( "PSetupCreateMonitorInfo failed with %d.\n", GetLastError() ) );
  378. }
  379. //
  380. // Refresh the UI with the new montiors.
  381. //
  382. bSetUI();
  383. //
  384. // The followng code modfies the default button
  385. // style because on return from the user
  386. // hitting the browse button it has become the default
  387. // button. i.e. has a bold border.
  388. // Make the new button a normal button state.
  389. //
  390. SendMessage( GetDlgItem( _hDlg, IDC_ADD_PORT_BROWSE ),
  391. BM_SETSTYLE,
  392. MAKEWPARAM( BS_PUSHBUTTON, 0 ),
  393. MAKELPARAM( TRUE, 0 ) );
  394. //
  395. // Change the focus to the ok button.
  396. //
  397. SetFocus( GetDlgItem( _hDlg, IDC_ADD_PORT ) );
  398. //
  399. // Change the ok button to the default button.
  400. //
  401. SendMessage( GetDlgItem( _hDlg, IDC_ADD_PORT ),
  402. BM_SETSTYLE,
  403. MAKEWPARAM( BS_DEFPUSHBUTTON, 0 ),
  404. MAKELPARAM( TRUE, 0 ) );
  405. return TRUE;
  406. }
  407. /*++
  408. Routine Name:
  409. bHandleMesage
  410. Routine Description:
  411. Dialog message handler.
  412. Arguments:
  413. IN UINT uMsg,
  414. IN WPARAM wParam,
  415. IN LPARAM lParam
  416. Return Value:
  417. TRUE message was handled.
  418. FALSE message was not handled.
  419. --*/
  420. BOOL
  421. TAddPort::
  422. bHandleMessage(
  423. UINT uMsg,
  424. WPARAM wParam,
  425. LPARAM lParam
  426. )
  427. {
  428. BOOL bStatus = FALSE;
  429. switch( uMsg ){
  430. case WM_INITDIALOG:
  431. bStatus = bSetUI ();
  432. bStatus = TRUE;
  433. break;
  434. case WM_HELP:
  435. case WM_CONTEXTMENU:
  436. bStatus = PrintUIHelp( uMsg, _hDlg, wParam, lParam );
  437. break;
  438. case WM_COMMAND:
  439. switch( GET_WM_COMMAND_ID( wParam, lParam )){
  440. //
  441. // Cancel add port
  442. //
  443. case IDCANCEL:
  444. EndDialog( _hDlg, _bPortAdded );
  445. bStatus = TRUE;
  446. break;
  447. //
  448. // OK will add a port.
  449. //
  450. case IDC_ADD_PORT:
  451. bReadUI();
  452. bSaveUI();
  453. bStatus = TRUE;
  454. break;
  455. //
  456. // Browse for new monitor.
  457. //
  458. case IDC_ADD_PORT_BROWSE:
  459. bStatus = bAddNewMonitor();
  460. bStatus = TRUE;
  461. break;
  462. //
  463. // Handle a list box event.
  464. //
  465. case IDC_ADD_PORT_MONITOR_LIST:
  466. switch ( GET_WM_COMMAND_CMD( wParam, lParam ) ){
  467. //
  468. // This forces an OK event.
  469. //
  470. case LBN_DBLCLK:
  471. PostMessage( _hDlg, WM_COMMAND, MAKEWPARAM( IDC_ADD_PORT, 0 ), lParam );
  472. bStatus = TRUE;
  473. break;
  474. }
  475. break;
  476. default:
  477. bStatus = FALSE;
  478. break;
  479. }
  480. default:
  481. bStatus = FALSE;
  482. break;
  483. }
  484. return bStatus;
  485. }