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.

665 lines
15 KiB

  1. /*++
  2. Copyright (C) Microsoft Corporation, 1995 - 1998
  3. All rights reserved.
  4. Module Name:
  5. docdef.cxx
  6. Abstract:
  7. Document defaults
  8. Author:
  9. Albert Ting (AlbertT) 29-Sept-1995
  10. Revision History:
  11. --*/
  12. #include "precomp.hxx"
  13. #pragma hdrstop
  14. #include "time.hxx"
  15. #include "psetup.hxx"
  16. #include "drvsetup.hxx"
  17. #include "instarch.hxx"
  18. #include "portslv.hxx"
  19. #include "dsinterf.hxx"
  20. #include "prtprop.hxx"
  21. #include "propmgr.hxx"
  22. #include "docdef.hxx"
  23. /********************************************************************
  24. Public interface to this module.
  25. ********************************************************************/
  26. VOID
  27. vDocumentDefaultsWOW64(
  28. IN HWND hwnd,
  29. IN LPCTSTR pszPrinterName,
  30. IN INT nCmdShow,
  31. IN LPARAM lParam
  32. )
  33. /*++
  34. Routine Description:
  35. WOW64 version. see vDocumentDefaults below.
  36. Arguments:
  37. see vDocumentDefaults below.
  38. Return Value:
  39. --*/
  40. {
  41. //
  42. // This function potentially may load the driver UI so we call a private API
  43. // exported by winspool.drv, which will RPC the call to a special 64 bit surrogate
  44. // process where the 64 bit driver can be loaded.
  45. //
  46. CDllLoader dll(TEXT("winspool.drv"));
  47. ptr_PrintUIDocumentDefaults pfnPrintUIDocumentDefaults =
  48. (ptr_PrintUIDocumentDefaults )dll.GetProcAddress(ord_PrintUIDocumentDefaults);
  49. if( pfnPrintUIDocumentDefaults )
  50. {
  51. pfnPrintUIDocumentDefaults( hwnd, pszPrinterName, nCmdShow, lParam );
  52. }
  53. }
  54. VOID
  55. vDocumentDefaultsNative(
  56. IN HWND hwnd,
  57. IN LPCTSTR pszPrinterName,
  58. IN INT nCmdShow,
  59. IN LPARAM lParam
  60. )
  61. /*++
  62. Routine Description:
  63. Native version. see vDocumentDefaults below.
  64. Arguments:
  65. see vDocumentDefaults below.
  66. Return Value:
  67. --*/
  68. {
  69. (VOID)dwDocumentDefaultsInternal( hwnd,
  70. pszPrinterName,
  71. nCmdShow,
  72. LOWORD( lParam ),
  73. HIWORD( lParam ),
  74. FALSE );
  75. }
  76. VOID
  77. vDocumentDefaults(
  78. IN HWND hwnd,
  79. IN LPCTSTR pszPrinterName,
  80. IN INT nCmdShow,
  81. IN LPARAM lParam
  82. )
  83. /*++
  84. Routine Description:
  85. Public entrypoint to bring up document defaults.
  86. Arguments:
  87. hwnd - Parent hwnd.
  88. pszPrinterName - Printer name.
  89. nCmdShow - Show command.
  90. lParam - lParam, currently unused.
  91. Return Value:
  92. --*/
  93. {
  94. if( IsRunningWOW64() )
  95. {
  96. vDocumentDefaultsWOW64( hwnd, pszPrinterName, nCmdShow, lParam );
  97. }
  98. else
  99. {
  100. vDocumentDefaultsNative( hwnd, pszPrinterName, nCmdShow, lParam );
  101. }
  102. }
  103. DWORD
  104. dwDocumentDefaultsInternal(
  105. IN HWND hwnd,
  106. IN LPCTSTR pszPrinterName,
  107. IN INT nCmdShow,
  108. IN DWORD dwSheet,
  109. IN BOOL bModal,
  110. IN BOOL bGlobal
  111. )
  112. /*++
  113. Routine Description:
  114. Private internal entry point to bring up document defaults.
  115. Arguments:
  116. hwnd - Parent hwnd.
  117. pszPrinterName - Printer name.
  118. nCmdShow - Show command.
  119. dwSheet - Sheet index of initial sheet to display.
  120. bModal - Display modal version of dialog.
  121. Return Value:
  122. ERROR_SUCCESS if dialog displayed, GetLastError() on error.
  123. --*/
  124. {
  125. //
  126. // Construct the printer data.
  127. //
  128. TPrinterData* pPrinterData = new TPrinterData( pszPrinterName,
  129. nCmdShow,
  130. NULL,
  131. dwSheet,
  132. hwnd,
  133. bModal );
  134. if( !VALID_PTR( pPrinterData )){
  135. goto Fail;
  136. }
  137. //
  138. // Set the Global dev mode flag.
  139. //
  140. pPrinterData->bGlobalDevMode() = bGlobal;
  141. //
  142. // If dialog is modal.
  143. //
  144. if( bModal ){
  145. TDocumentDefaultPropertySheetManager::iDocumentDefaultsProc( pPrinterData );
  146. return ERROR_SUCCESS;
  147. }
  148. //
  149. // Create the thread which handles the UI. vPrinterPropPages adopts
  150. // pPrinterData.
  151. //
  152. DWORD dwIgnore;
  153. HANDLE hThread;
  154. hThread = TSafeThread::Create( NULL,
  155. 0,
  156. (LPTHREAD_START_ROUTINE)TDocumentDefaultPropertySheetManager::iDocumentDefaultsProc,
  157. pPrinterData,
  158. 0,
  159. &dwIgnore );
  160. if( !hThread ){
  161. goto Fail;
  162. }
  163. CloseHandle( hThread );
  164. return ERROR_SUCCESS;
  165. Fail:
  166. if( !pPrinterData ){
  167. vShowResourceError( hwnd );
  168. } else {
  169. iMessage( hwnd,
  170. IDS_ERR_DOC_PROP_TITLE,
  171. IDS_ERR_DOCUMENT_PROP,
  172. MB_OK|MB_ICONSTOP,
  173. kMsgGetLastError,
  174. NULL );
  175. }
  176. delete pPrinterData;
  177. return ERROR_ACCESS_DENIED;
  178. }
  179. /********************************************************************
  180. Private support routines.
  181. ********************************************************************/
  182. INT
  183. TDocumentDefaultPropertySheetManager::
  184. iDocumentDefaultsProc(
  185. IN TPrinterData* pPrinterData ADOPT
  186. )
  187. /*++
  188. Routine Description:
  189. Bring up the document defaults dialog.
  190. Arguments:
  191. pPrinterData - Data about the printer.
  192. Return Value:
  193. --*/
  194. {
  195. DBGMSG( DBG_TRACE, ( "iDocumentDefaultsProc\n") );
  196. //
  197. // Increment our reference count.
  198. //
  199. pPrinterData->vIncRef();
  200. //
  201. // Set the pidl type for the correct dialog type. The
  202. // all users document defaults and the my document defaults
  203. // are the same dialog just started differently.
  204. //
  205. DWORD dwPidlType = pPrinterData->bGlobalDevMode() ? PRINTER_PIDL_TYPE_ALL_USERS_DOCDEF : PRINTER_PIDL_TYPE_DOCUMENTDEFAULTS;
  206. //
  207. // Register this property sheet window.
  208. //
  209. BOOL bStatus = pPrinterData->bRegisterWindow( dwPidlType );
  210. if( bStatus ){
  211. //
  212. // Check if the window is already present. If it is, then
  213. // exit immediately.
  214. //
  215. if( pPrinterData->bIsWindowPresent() ){
  216. DBGMSG( DBG_TRACE, ( "iDocumentDefaultsProc: currently running.\n" ) );
  217. bStatus = FALSE;
  218. }
  219. }
  220. if( bStatus ){
  221. //
  222. // Load the printer data.
  223. //
  224. bStatus = pPrinterData->bLoad();
  225. if( !bStatus ){
  226. iMessage( NULL,
  227. IDS_ERR_DOC_PROP_TITLE,
  228. IDS_ERR_DOCUMENT_PROP,
  229. MB_OK|MB_ICONSTOP|MB_SETFOREGROUND,
  230. kMsgGetLastError,
  231. NULL );
  232. } else {
  233. //
  234. // Create the ducument property sheet windows.
  235. //
  236. TDocumentDefaultPropertySheetManager DocDefPropSheetManger( pPrinterData );
  237. //
  238. // Were the document windows create
  239. //
  240. if( !VALID_OBJ( DocDefPropSheetManger ) ){
  241. vShowResourceError( pPrinterData->hwnd() );
  242. bStatus = FALSE;
  243. }
  244. //
  245. // If we do not have access, don't bring up the
  246. // device sheets with incorrect dev mode data. Just
  247. // inform the use they don't have access.
  248. //
  249. if( pPrinterData->bNoAccess( ) ){
  250. SetLastError( ERROR_ACCESS_DENIED );
  251. iMessage( pPrinterData->hwnd(),
  252. IDS_ERR_DOC_PROP_TITLE,
  253. IDS_ERR_DOCUMENT_PROP,
  254. MB_OK|MB_ICONSTOP|MB_SETFOREGROUND,
  255. kMsgGetLastError,
  256. NULL );
  257. bStatus = FALSE;
  258. }
  259. //
  260. // Display the property pages.
  261. //
  262. if( bStatus ){
  263. if( !DocDefPropSheetManger.bDisplayPages( pPrinterData->hwnd() ) ){
  264. vShowResourceError( pPrinterData->hwnd() );
  265. bStatus = FALSE;
  266. }
  267. }
  268. }
  269. }
  270. //
  271. // Ensure we release the printer data.
  272. //
  273. pPrinterData->cDecRef();
  274. return bStatus;
  275. }
  276. /********************************************************************
  277. Document Default Property Sheet Manager.
  278. ********************************************************************/
  279. /*++
  280. Routine Description:
  281. Document default property sheet manager
  282. Arguments:
  283. pPrinterData - PrinterData to display.
  284. Return Value:
  285. TRUE - Success, FALSE - failure.
  286. --*/
  287. TDocumentDefaultPropertySheetManager::
  288. TDocumentDefaultPropertySheetManager(
  289. TPrinterData *pPrinterData
  290. ) : _pPrinterData( pPrinterData )
  291. {
  292. DBGMSG( DBG_TRACE, ( "TDocumentDefaultPropertySheetManager ctor\n") );
  293. }
  294. TDocumentDefaultPropertySheetManager::
  295. ~TDocumentDefaultPropertySheetManager(
  296. )
  297. {
  298. DBGMSG( DBG_TRACE, ( "TDocumentDefaultPropertySheetManager dtor\n") );
  299. }
  300. BOOL
  301. TDocumentDefaultPropertySheetManager::
  302. bValid(
  303. VOID
  304. )
  305. {
  306. return _pPrinterData != NULL;
  307. }
  308. /*++
  309. Routine Name:
  310. bBuildPages
  311. Routine Description:
  312. Builds the document property windows.
  313. Arguments:
  314. None - class specific.
  315. Return Value:
  316. TRUE pages built ok, FALSE failure building pages.
  317. --*/
  318. BOOL
  319. TDocumentDefaultPropertySheetManager::
  320. bBuildPages(
  321. IN PPROPSHEETUI_INFO pCPSUIInfo
  322. )
  323. {
  324. DBGMSG( DBG_TRACE, ( "TDocumentDefaultPropertySheetManager::bBuildPages\n") );
  325. BOOL bStatus = TRUE;
  326. //
  327. // If we have a null dev mode here, get printer must not have a dev mode
  328. // associated to this printer ( this should not happen ), then get the
  329. // default devmode.
  330. //
  331. if( !_pPrinterData->pDevMode() )
  332. {
  333. bStatus = VDataRefresh::bGetDefaultDevMode( _pPrinterData->hPrinter(),
  334. (LPTSTR)(LPCTSTR)_pPrinterData->strPrinterName(),
  335. &_pPrinterData->_pDevMode,
  336. TRUE );
  337. }
  338. if( bStatus )
  339. {
  340. //
  341. // Set the default activation context to be V6 prior calling into
  342. // compstui to create the pages. This will force V6 context unless
  343. // the callbacks which create the compstui pages specify otherwise
  344. // on a per page basis.
  345. //
  346. bStatus = (BOOL)pCPSUIInfo->pfnComPropSheet(
  347. pCPSUIInfo->hComPropSheet,
  348. CPSFUNC_SET_FUSION_CONTEXT,
  349. reinterpret_cast<LPARAM>(g_hActCtx),
  350. static_cast<LPARAM>(0));
  351. if( bStatus )
  352. {
  353. //
  354. // Set the default dev mode flags.
  355. //
  356. DWORD dwFlag = DM_IN_BUFFER | DM_OUT_BUFFER | DM_PROMPT | DM_USER_DEFAULT;
  357. ZeroMemory( &_dph, sizeof( _dph ) );
  358. _dph.cbSize = sizeof( _dph );
  359. _dph.hPrinter = _pPrinterData->hPrinter();
  360. _dph.pszPrinterName = (LPTSTR)(LPCTSTR)_pPrinterData->strPrinterName();
  361. _dph.pdmOut = _pPrinterData->pDevMode();
  362. _dph.pdmIn = _pPrinterData->pDevMode();
  363. _dph.fMode = dwFlag;
  364. //
  365. // Tell compstui to load the driver and start the ui.
  366. //
  367. if( pCPSUIInfo->pfnComPropSheet( pCPSUIInfo->hComPropSheet,
  368. CPSFUNC_ADD_PFNPROPSHEETUI,
  369. (LPARAM)DocumentPropertySheets,
  370. (LPARAM)&_dph ) <= 0 )
  371. {
  372. DBGMSG( DBG_TRACE, ( "CPSFUNC_ADD_PFNPROPSHEETUI failed.\n") );
  373. bStatus = FALSE;
  374. }
  375. }
  376. }
  377. else
  378. {
  379. DBGMSG( DBG_TRACE, ( "Failed to allocate devmode buffer with %d.\n", GetLastError () ) );
  380. bStatus = FALSE;
  381. }
  382. return bStatus;
  383. }
  384. /*++
  385. Routine Name:
  386. bCreatePropertySheetTitle.
  387. Routine Description:
  388. Creates the property sheet title.
  389. Arguments:
  390. Nothing.
  391. Return Value:
  392. TRUE success, FALSE error occurred.
  393. --*/
  394. BOOL
  395. TDocumentDefaultPropertySheetManager::
  396. bCreateTitle(
  397. VOID
  398. )
  399. {
  400. //
  401. // Create the formatted property sheet title.
  402. //
  403. TStatusB bStatus;
  404. TCHAR szBuffer[kStrMax+kPrinterBufMax];
  405. UINT nSize = COUNTOF( szBuffer );
  406. //
  407. // Create the printer friendly name.
  408. //
  409. bStatus DBGCHK = ConstructPrinterFriendlyName( _pPrinterData->strPrinterName(), szBuffer, &nSize );
  410. //
  411. // Change the title post fix based on whether we are displaying
  412. // the per user or per printer document defaults settings.
  413. //
  414. if( _pPrinterData->bGlobalDevMode() )
  415. {
  416. //
  417. // Construct the property sheet title.
  418. //
  419. bStatus DBGCHK = bConstructMessageString( ghInst, _strTitle, IDS_DOCUMENT_DEFAULT_PER_PRINTER_TITLE_FORMAT, szBuffer );
  420. }
  421. else
  422. {
  423. //
  424. // Construct the property sheet title.
  425. //
  426. bStatus DBGCHK = bConstructMessageString( ghInst, _strTitle, IDS_DOCUMENT_DEFAULT_PER_USER_TITLE_FORMAT, szBuffer );
  427. }
  428. return bStatus;
  429. }
  430. BOOL
  431. TDocumentDefaultPropertySheetManager::
  432. bSetHeader(
  433. IN PPROPSHEETUI_INFO pCPSUIInfo,
  434. IN PPROPSHEETUI_INFO_HEADER pPSUIInfoHdr
  435. )
  436. {
  437. DBGMSG( DBG_TRACE, ( "TDocumentDefaultPropertySheetManager::bSetHeader\n") );
  438. UNREFERENCED_PARAMETER( pCPSUIInfo );
  439. bCreateTitle();
  440. pPSUIInfoHdr->cbSize = sizeof( PROPSHEETUI_INFO_HEADER );
  441. pPSUIInfoHdr->Flags = PSUIHDRF_EXACT_PTITLE;
  442. pPSUIInfoHdr->pTitle = (LPTSTR)(LPCTSTR)_strTitle;
  443. pPSUIInfoHdr->hInst = ghInst;
  444. pPSUIInfoHdr->IconID = IDI_PRINTER;
  445. pPSUIInfoHdr->hWndParent = _pPrinterData->hwnd();
  446. return TRUE;
  447. }
  448. /*++
  449. Routine Name:
  450. bSaveResult
  451. Routine Description:
  452. Save the result from the previous handler to our parent.
  453. Arguments:
  454. pCPSUIInfo - Pointer to commonui property sheet info header,
  455. pSetResultInfo - Pointer to result info header
  456. Return Value:
  457. TRUE success, FALSE error occurred.
  458. --*/
  459. BOOL
  460. TDocumentDefaultPropertySheetManager::
  461. bSaveResult(
  462. IN PPROPSHEETUI_INFO pCPSUIInfo,
  463. IN PSETRESULT_INFO pSetResultInfo
  464. )
  465. {
  466. DBGMSG( DBG_TRACE, ( "TDocumentDefaultPropertySheetManager::bSaveResult\n") );
  467. TStatusB bStatus;
  468. bStatus DBGNOCHK = FALSE;
  469. if( pSetResultInfo->Result == CPSUI_OK )
  470. {
  471. //
  472. // Attempt to save the printer data, if an error occurrs
  473. // display a message.
  474. //
  475. bStatus DBGCHK = _pPrinterData->bSave( TRUE );
  476. if( !bStatus )
  477. {
  478. //
  479. // Display the error message.
  480. //
  481. iMessage( _pPrinterData->hwnd(),
  482. IDS_ERR_DOC_PROP_TITLE,
  483. IDS_ERR_SAVE_PRINTER,
  484. MB_OK|MB_ICONSTOP,
  485. kMsgGetLastError,
  486. NULL );
  487. }
  488. else
  489. {
  490. pCPSUIInfo->Result = pSetResultInfo->Result;
  491. }
  492. }
  493. return bStatus;
  494. }