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.

826 lines
17 KiB

  1. /*++
  2. Copyright (C) Microsoft Corporation, 1995 - 1998
  3. All rights reserved.
  4. Module Name:
  5. F:\nt\private\windows\spooler\printui.pri\procdlg.cxx
  6. Abstract:
  7. Print Processor dialog.
  8. Author:
  9. Steve Kiraly (SteveKi) 11/10/95
  10. Revision History:
  11. --*/
  12. #include "precomp.hxx"
  13. #pragma hdrstop
  14. #include "procdlg.hxx"
  15. #include "psetup.hxx"
  16. #include "drvver.hxx"
  17. /********************************************************************
  18. Print Processor Dialog class
  19. ********************************************************************/
  20. /*++
  21. Routine Name:
  22. TPrintProcessor
  23. Routine Description:
  24. Contstucts the print processor dialog.
  25. Arguments:
  26. hWnd - Parent window handle
  27. strServerName - Currnet printer server name
  28. strPrintProcessor - Current print processor
  29. strDatatype - Current datatype
  30. Return Value:
  31. Nothing. bValid() inidicated valid object.
  32. --*/
  33. TPrintProcessor::
  34. TPrintProcessor(
  35. IN HWND hWnd,
  36. IN LPCTSTR pszServerName,
  37. IN TString &strPrintProcessor,
  38. IN TString &strDatatype,
  39. IN BOOL bAdministrator
  40. ) : _hWnd( hWnd ),
  41. _pPrintProcessors( NULL ),
  42. _cPrintProcessors( 0 ),
  43. _hctlPrintProcessorList( 0 ),
  44. _hctlDatatypeList( 0 ),
  45. _bAdministrator( bAdministrator ),
  46. _bValid( FALSE ),
  47. _pszServerName( pszServerName )
  48. {
  49. DBGMSG( DBG_TRACE, ( "TPrintProcessor::ctor\n") );
  50. DBGMSG( DBG_TRACE, ( "ServerName = " TSTR "\n", DBGSTR( (LPCTSTR)pszServerName ) ) );
  51. DBGMSG( DBG_TRACE, ( "PrintProcessorName = " TSTR "\n", (LPCTSTR)strPrintProcessor ) );
  52. DBGMSG( DBG_TRACE, ( "DataType = " TSTR "\n", (LPCTSTR)strDatatype ) );
  53. //
  54. // Update the string oobjects.
  55. //
  56. if( !_strPrintProcessor.bUpdate( strPrintProcessor ) ||
  57. !_strDatatype.bUpdate( strDatatype ) ){
  58. DBGMSG( DBG_WARN, ( "String update failed with %d\n", GetLastError() ) );
  59. return;
  60. }
  61. //
  62. // Do loading for this object.
  63. //
  64. if( !bLoad( ) ){
  65. return;
  66. }
  67. _bValid = TRUE;
  68. }
  69. /*++
  70. Routine Name:
  71. ~TPrintProcessor
  72. Routine Description:
  73. Destructs the print processor dialog.
  74. Arguments:
  75. None.
  76. Return Value:
  77. Nothing.
  78. --*/
  79. TPrintProcessor::
  80. ~TPrintProcessor(
  81. )
  82. {
  83. DBGMSG( DBG_TRACE, ( "TPrintProcessor::dtor\n") );
  84. FreeMem( _pPrintProcessors );
  85. }
  86. /*++
  87. Routine Name:
  88. bValid
  89. Routine Description:
  90. Returns valid object indicator.
  91. Arguments:
  92. None.
  93. Return Value:
  94. Nothing.
  95. --*/
  96. BOOL
  97. TPrintProcessor::
  98. bValid(
  99. VOID
  100. )
  101. {
  102. return _bValid;
  103. }
  104. /*++
  105. Routine Name:
  106. bDoModal
  107. Routine Description:
  108. Start modal execution of dialog.
  109. Arguments:
  110. None.
  111. Return Value:
  112. TRUE dialog ok button chosen.
  113. FALSE cancel button chosen.
  114. --*/
  115. BOOL
  116. TPrintProcessor::
  117. bDoModal(
  118. VOID
  119. )
  120. {
  121. //
  122. // Create a modal dialog.
  123. //
  124. return (BOOL)DialogBoxParam( ghInst,
  125. MAKEINTRESOURCE( TPrintProcessor::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 dialog.
  135. Arguments:
  136. None.
  137. Return Value:
  138. TRUE data set on UI successfully.
  139. FALSE error occurred setting UI data.
  140. --*/
  141. BOOL
  142. TPrintProcessor::
  143. bSetUI(
  144. VOID
  145. )
  146. {
  147. //
  148. // Create local copies of the control ID's this saves
  149. // some execution time and alot of typing.
  150. //
  151. _hctlPrintProcessorList = GetDlgItem( _hDlg, IDC_PRINT_PROCESSOR_LIST );
  152. _hctlDatatypeList = GetDlgItem( _hDlg, IDC_PRINT_DATATYPE_LIST );
  153. //
  154. // Set the UI controls.
  155. //
  156. if( !_hctlPrintProcessorList ||
  157. !_hctlDatatypeList ||
  158. !bSetPrintProcessorList() ||
  159. !bSetDatatypeList() ||
  160. !bDataTypeAssociation( TRUE ) ){
  161. DBGMSG( DBG_WARN, ( "bSetUI failed %d\n", GetLastError( )));
  162. return FALSE;
  163. }
  164. //
  165. // If not an administrator disable the controls.
  166. //
  167. vEnableCtl( _hDlg, IDC_PRINT_PROCESSOR_LIST, _bAdministrator );
  168. vEnableCtl( _hDlg, IDC_PRINT_DATATYPE_LIST, _bAdministrator );
  169. vEnableCtl( _hDlg, IDC_SPOOL_DATATYPE, _bAdministrator );
  170. vEnableCtl( _hDlg, IDOK, _bAdministrator );
  171. vEnableCtl( _hDlg, IDC_PRINT_PROCESSOR_DESC, _bAdministrator );
  172. vEnableCtl( _hDlg, IDC_PRINT_PROCESSOR_TEXT, _bAdministrator );
  173. vEnableCtl( _hDlg, IDC_PRINT_DATATYPE_TEXT, _bAdministrator );
  174. return TRUE;
  175. }
  176. /*++
  177. Routine Name:
  178. bReadUI
  179. Routine Description:
  180. Reads the UI data back into the public members.
  181. Arguments:
  182. None.
  183. Return Value:
  184. TRUE data read ok.
  185. FALSE error reading UI data.
  186. --*/
  187. BOOL
  188. TPrintProcessor::
  189. bReadUI(
  190. VOID
  191. )
  192. {
  193. UINT uSel;
  194. CAutoHandleHLOCAL spText;
  195. //
  196. // If we do not have administrator privilages.
  197. //
  198. if ( !_bAdministrator )
  199. return FALSE;
  200. //
  201. // Read the selected print proccessor form the list box.
  202. //
  203. spText = NULL;
  204. uSel = ListBox_GetCurSel( _hctlPrintProcessorList );
  205. if(( uSel == LB_ERR ) ||
  206. FAILED( LBGetTextHelper( _hctlPrintProcessorList, uSel, (LPWSTR *)&spText ) ) ||
  207. !_strPrintProcessor.bUpdate( spText.GetPtrAs<LPWSTR>() )){
  208. DBGMSG( DBG_WARN, ( "Read print processor listbox failed %d\n", GetLastError( )));
  209. return FALSE;
  210. }
  211. //
  212. // Read the selected datatype form the list box.
  213. //
  214. spText = NULL;
  215. uSel = ListBox_GetCurSel( _hctlDatatypeList );
  216. if(( uSel == LB_ERR ) ||
  217. FAILED( LBGetTextHelper( _hctlDatatypeList, uSel, (LPWSTR *)&spText ) ) ||
  218. !_strDatatype.bUpdate( spText.GetPtrAs<LPWSTR>() )){
  219. DBGMSG( DBG_WARN, ( "Read datatype listbox failed %d\n", GetLastError( )));
  220. return FALSE;
  221. }
  222. //
  223. // Trace message to display UI data.
  224. //
  225. DBGMSG( DBG_TRACE, ( "PrintProcessorName = " TSTR "\n", (LPCTSTR)strPrintProcessor() ) );
  226. DBGMSG( DBG_TRACE, ( "DataType = " TSTR "\n", (LPCTSTR)strDatatype() ) );
  227. return TRUE;
  228. }
  229. /*++
  230. Routine Name:
  231. bSetPrintProcessorList
  232. Routine Description:
  233. Fills the print processors list box.
  234. Arguments:
  235. None.
  236. Return Value:
  237. TRUE list box fill successfully.
  238. FALSE if error occurred.
  239. --*/
  240. BOOL
  241. TPrintProcessor::
  242. bSetPrintProcessorList(
  243. VOID
  244. )
  245. {
  246. //
  247. // Reset the list box in case we are called to refresh.
  248. //
  249. ListBox_ResetContent( _hctlPrintProcessorList );
  250. //
  251. // Build list of print processors.
  252. //
  253. UINT i;
  254. for( i = 0; i < _cPrintProcessors; i++ ){
  255. ListBox_InsertString( _hctlPrintProcessorList, -1, (LPARAM)_pPrintProcessors[i].pName );
  256. ListBox_SetItemData( _hctlPrintProcessorList, i, 0 );
  257. }
  258. //
  259. // Set the highlight on the current print processor.
  260. //
  261. UINT uSel;
  262. uSel = ListBox_FindString( _hctlPrintProcessorList, -1, _strPrintProcessor );
  263. uSel = ( uSel == LB_ERR ) ? 0 : uSel;
  264. ListBox_SetCurSel( _hctlPrintProcessorList, uSel );
  265. return TRUE;
  266. }
  267. /*++
  268. Routine Name:
  269. bSetDatatypeList
  270. Routine Description:
  271. Fills the datatype list box.
  272. Arguments:
  273. None.
  274. Return Value:
  275. TRUE list box fill successfully.
  276. FALSE if error occurred.
  277. --*/
  278. BOOL
  279. TPrintProcessor::
  280. bSetDatatypeList(
  281. VOID
  282. )
  283. {
  284. HRESULT hr = S_OK;
  285. BOOL bStatus = FALSE;
  286. CAutoHandleHLOCAL spText;
  287. UINT uSel;
  288. //
  289. // Get the currently selected print processor name.
  290. //
  291. uSel = ListBox_GetCurSel( _hctlPrintProcessorList );
  292. hr = LBGetTextHelper( _hctlPrintProcessorList, uSel, (LPWSTR *)&spText );
  293. bStatus = SUCCEEDED(hr);
  294. if( bStatus ){
  295. //
  296. // Enumerate the data types.
  297. //
  298. DATATYPES_INFO_1 *pDatatypes = NULL;
  299. DWORD cDatatypes = 0;
  300. bStatus = bEnumPrintProcessorDatatypes( (LPTSTR)_pszServerName,
  301. spText.GetPtrAs<LPWSTR>(),
  302. 1,
  303. (PVOID *)&pDatatypes,
  304. &cDatatypes );
  305. if( bStatus ){
  306. //
  307. // Reset the list box in case we are called to refresh.
  308. //
  309. ListBox_ResetContent( _hctlDatatypeList );
  310. //
  311. // Build list of datatypes.
  312. //
  313. UINT i;
  314. for( i = 0; i < cDatatypes; i++ ){
  315. ListBox_InsertString( _hctlDatatypeList, -1, (LPARAM)pDatatypes[i].pName );
  316. }
  317. }
  318. //
  319. // Clean up any allocated resources.
  320. //
  321. FreeMem( pDatatypes );
  322. //
  323. // Select the correct datatype for the slected print processor.
  324. //
  325. ListBox_SetCurSel( _hctlDatatypeList,
  326. ListBox_GetItemData( _hctlPrintProcessorList,
  327. ListBox_GetCurSel( _hctlPrintProcessorList ) ) );
  328. } else {
  329. //
  330. // LBGetTextHelper failed. Set the last error.
  331. //
  332. SetLastError(HRESULT_CODE(hr));
  333. }
  334. return bStatus;
  335. }
  336. /*++
  337. Routine Name:
  338. bDataTypeAssociation
  339. Routine Description:
  340. The currently selected datatype item is tracted for
  341. each print processor. The print processor list box item
  342. data contains the index of its corresponding datatype.
  343. Arguments:
  344. TRUE to select the highlight on the default datatype.
  345. FALSE to the association for the currently selected datatype.
  346. Return Value:
  347. Always returns success
  348. --*/
  349. BOOL
  350. TPrintProcessor::
  351. bDataTypeAssociation(
  352. IN BOOL bSetDatatype
  353. )
  354. {
  355. //
  356. // Set the highlight using the current datatype.
  357. //
  358. if( bSetDatatype ){
  359. //
  360. // Locate and select the index of the default datatype. The
  361. // default datatype is the datatype string passed into this object.
  362. //
  363. UINT uSel;
  364. uSel = ListBox_FindString( _hctlDatatypeList, -1, _strDatatype );
  365. uSel = ( uSel == LB_ERR ) ? 0 : uSel;
  366. ListBox_SetCurSel( _hctlDatatypeList, uSel );
  367. }
  368. //
  369. // Get the currently selected print processor and set the item data
  370. // to associated selected data type.
  371. //
  372. ListBox_SetItemData( _hctlPrintProcessorList,
  373. ListBox_GetCurSel( _hctlPrintProcessorList ),
  374. ListBox_GetCurSel( _hctlDatatypeList ) );
  375. return TRUE;
  376. }
  377. /*++
  378. Routine Name:
  379. bLoad
  380. Routine Description:
  381. Gets the list of print processors.
  382. Arguments:
  383. None.
  384. Return Value:
  385. TRUE list box fill successfully.
  386. FALSE if error occurred.
  387. --*/
  388. BOOL
  389. TPrintProcessor::
  390. bLoad(
  391. VOID
  392. )
  393. {
  394. //
  395. // Get the current driver / version.
  396. //
  397. DWORD dwDriverVersion = 0;
  398. if( !bGetCurrentDriver( _pszServerName, &dwDriverVersion ) ){
  399. DBGMSG( DBG_WARN, ( "bGetDriverVersion failed.\n" ) );
  400. return FALSE;
  401. }
  402. DBGMSG( DBG_TRACE, ( "Driver Version %d\n", dwDriverVersion ) );
  403. //
  404. // Convert the driver / version to spooler usable environment string.
  405. //
  406. TString strDriverEnv;
  407. if( !bGetDriverEnv( dwDriverVersion, strDriverEnv ) ){
  408. DBGMSG( DBG_WARN, ( "bGetDriverEnv failed.\n" ) );
  409. return FALSE;
  410. }
  411. DBGMSG( DBG_TRACE, ( "Driver Environment " TSTR "\n", (LPCTSTR)strDriverEnv ) );
  412. //
  413. // Enumerate the currently installed print processors.
  414. //
  415. BOOL bStatus;
  416. bStatus = bEnumPrintProcessors( (LPTSTR)_pszServerName,
  417. (LPTSTR)(LPCTSTR)strDriverEnv,
  418. 1,
  419. (PVOID *)&_pPrintProcessors,
  420. &_cPrintProcessors );
  421. if( !bStatus ){
  422. DBGMSG( DBG_ERROR, ( "bEnumPrintProccessors failed = %d\n", GetLastError () ) );
  423. return FALSE;
  424. }
  425. return TRUE;
  426. }
  427. /*++
  428. Routine Name:
  429. bHandleMesage
  430. Routine Description:
  431. Dialog message handler.
  432. Arguments:
  433. IN UINT uMsg,
  434. IN WPARAM wParam,
  435. IN LPARAM lParam
  436. Return Value:
  437. TRUE message was handled.
  438. FALSE message was not handled.
  439. --*/
  440. BOOL
  441. TPrintProcessor::
  442. bHandleMessage(
  443. UINT uMsg,
  444. WPARAM wParam,
  445. LPARAM lParam
  446. )
  447. {
  448. UNREFERENCED_PARAMETER( lParam );
  449. BOOL bStatus = FALSE;
  450. switch( uMsg ){
  451. case WM_INITDIALOG:
  452. bSetUI( );
  453. bStatus = TRUE;
  454. break;
  455. case WM_HELP:
  456. case WM_CONTEXTMENU:
  457. bStatus = PrintUIHelp( uMsg, _hDlg, wParam, lParam );
  458. break;
  459. case WM_COMMAND:
  460. switch( GET_WM_COMMAND_ID( wParam, lParam )){
  461. //
  462. // Read back the UI data and indicate success.
  463. //
  464. case IDOK:
  465. bStatus = bReadUI();
  466. EndDialog( _hDlg, bStatus );
  467. break;
  468. //
  469. // Indicate cancel request.
  470. //
  471. case IDCANCEL:
  472. bStatus = TRUE;
  473. EndDialog( _hDlg, FALSE );
  474. break;
  475. //
  476. // Handle the print processor list change.
  477. //
  478. case IDC_PRINT_PROCESSOR_LIST:
  479. switch ( GET_WM_COMMAND_CMD( wParam, lParam ) ){
  480. case LBN_SELCHANGE:
  481. bSetDatatypeList();
  482. bStatus = TRUE;
  483. break;
  484. }
  485. break;
  486. //
  487. // Handle the data type list change.
  488. //
  489. case IDC_PRINT_DATATYPE_LIST:
  490. switch ( GET_WM_COMMAND_CMD( wParam, lParam ) ){
  491. case LBN_SELCHANGE:
  492. bDataTypeAssociation( FALSE );
  493. bStatus = TRUE;
  494. break;
  495. }
  496. break;
  497. default:
  498. bStatus = FALSE;
  499. break;
  500. }
  501. default:
  502. bStatus = FALSE;
  503. break;
  504. }
  505. return bStatus;
  506. }
  507. /*++
  508. Routine Name:
  509. bEnumPrintProcessors
  510. Routine Description:
  511. Enumerates the installed print processors.
  512. Arguments:
  513. Return Value:
  514. TRUE list box fill successfully.
  515. FALSE if error occurred.
  516. --*/
  517. BOOL
  518. TPrintProcessor::
  519. bEnumPrintProcessors(
  520. IN LPTSTR pszServerName,
  521. IN LPTSTR pszEnvironment,
  522. IN DWORD dwLevel,
  523. OUT PVOID *ppvBuffer,
  524. OUT PDWORD pcReturned
  525. )
  526. {
  527. DWORD dwNeeded;
  528. DWORD dwReturned;
  529. PBYTE pBuf = NULL;
  530. BOOL bStatus = FALSE;
  531. //
  532. // First query spooler for installed print processors.
  533. //
  534. if ( !EnumPrintProcessors( pszServerName, pszEnvironment, dwLevel, NULL, 0, &dwNeeded, &dwReturned) ) {
  535. if ( GetLastError() != ERROR_INSUFFICIENT_BUFFER ||
  536. ((pBuf = (PBYTE)AllocMem( dwNeeded )) == NULL) ||
  537. !EnumPrintProcessors( pszServerName,
  538. pszEnvironment,
  539. dwLevel,
  540. pBuf,
  541. dwNeeded,
  542. &dwNeeded,
  543. &dwReturned) ) {
  544. bStatus = FALSE;
  545. } else {
  546. bStatus = TRUE;
  547. }
  548. }
  549. //
  550. // If success copy back the data.
  551. //
  552. if( bStatus ){
  553. *ppvBuffer = pBuf;
  554. *pcReturned = dwReturned;
  555. }
  556. return bStatus;
  557. }
  558. /*++
  559. Routine Name:
  560. bEnumPrintProcessorDatatypes
  561. Routine Description:
  562. Enumerates the print processors datatypes.
  563. Arguments:
  564. Return Value:
  565. TRUE list box fill successfully.
  566. FALSE if error occurred.
  567. --*/
  568. BOOL
  569. TPrintProcessor::
  570. bEnumPrintProcessorDatatypes(
  571. IN LPTSTR pszServerName,
  572. IN LPTSTR pszPrintProcessor,
  573. IN DWORD dwLevel,
  574. OUT PVOID *ppvBuffer,
  575. OUT PDWORD pcReturned
  576. )
  577. {
  578. DWORD dwNeeded;
  579. DWORD dwReturned;
  580. PBYTE pBuf = NULL;
  581. BOOL bStatus = FALSE;
  582. //
  583. // First query spooler for installed print processors.
  584. //
  585. if ( !EnumPrintProcessorDatatypes( pszServerName, pszPrintProcessor, dwLevel, NULL, 0, &dwNeeded, &dwReturned) ) {
  586. if ( GetLastError() != ERROR_INSUFFICIENT_BUFFER ||
  587. ((pBuf = (PBYTE)AllocMem( dwNeeded )) == NULL) ||
  588. !EnumPrintProcessorDatatypes( pszServerName,
  589. pszPrintProcessor,
  590. dwLevel,
  591. pBuf,
  592. dwNeeded,
  593. &dwNeeded,
  594. &dwReturned) ) {
  595. bStatus = FALSE;
  596. } else {
  597. bStatus = TRUE;
  598. }
  599. }
  600. //
  601. // If success copy back the data.
  602. //
  603. if( bStatus ){
  604. *ppvBuffer = pBuf;
  605. *pcReturned = dwReturned;
  606. }
  607. return bStatus;
  608. }