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.

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. TString strPostFix;
  405. TCHAR szBuffer[kStrMax+kPrinterBufMax];
  406. UINT nSize = COUNTOF( szBuffer );
  407. //
  408. // Create the printer friendly name.
  409. //
  410. bStatus DBGCHK = ConstructPrinterFriendlyName( _pPrinterData->strPrinterName(), szBuffer, &nSize );
  411. //
  412. // Change the title post fix based on whether we are displaying
  413. // the per user or per printer document defaults settings.
  414. //
  415. if( _pPrinterData->bGlobalDevMode() )
  416. {
  417. bStatus DBGCHK = strPostFix.bLoadString( ghInst, IDS_PRINTER_PREFERENCES_DEFAULT );
  418. }
  419. else
  420. {
  421. bStatus DBGCHK = strPostFix.bLoadString( ghInst, IDS_PRINTER_PREFERENCES );
  422. }
  423. //
  424. // Construct the property sheet title.
  425. //
  426. bStatus DBGCHK = bConstructMessageString( ghInst, _strTitle, IDS_DOCUMENT_DEFAULT_TITLE_FORMAT, szBuffer, (LPCTSTR)strPostFix );
  427. return bStatus;
  428. }
  429. BOOL
  430. TDocumentDefaultPropertySheetManager::
  431. bSetHeader(
  432. IN PPROPSHEETUI_INFO pCPSUIInfo,
  433. IN PPROPSHEETUI_INFO_HEADER pPSUIInfoHdr
  434. )
  435. {
  436. DBGMSG( DBG_TRACE, ( "TDocumentDefaultPropertySheetManager::bSetHeader\n") );
  437. UNREFERENCED_PARAMETER( pCPSUIInfo );
  438. bCreateTitle();
  439. pPSUIInfoHdr->cbSize = sizeof( PROPSHEETUI_INFO_HEADER );
  440. pPSUIInfoHdr->Flags = PSUIHDRF_EXACT_PTITLE;
  441. pPSUIInfoHdr->pTitle = (LPTSTR)(LPCTSTR)_strTitle;
  442. pPSUIInfoHdr->hInst = ghInst;
  443. pPSUIInfoHdr->IconID = IDI_PRINTER;
  444. pPSUIInfoHdr->hWndParent = _pPrinterData->hwnd();
  445. return TRUE;
  446. }
  447. /*++
  448. Routine Name:
  449. bSaveResult
  450. Routine Description:
  451. Save the result from the previous handler to our parent.
  452. Arguments:
  453. pCPSUIInfo - Pointer to commonui property sheet info header,
  454. pSetResultInfo - Pointer to result info header
  455. Return Value:
  456. TRUE success, FALSE error occurred.
  457. --*/
  458. BOOL
  459. TDocumentDefaultPropertySheetManager::
  460. bSaveResult(
  461. IN PPROPSHEETUI_INFO pCPSUIInfo,
  462. IN PSETRESULT_INFO pSetResultInfo
  463. )
  464. {
  465. DBGMSG( DBG_TRACE, ( "TDocumentDefaultPropertySheetManager::bSaveResult\n") );
  466. TStatusB bStatus;
  467. bStatus DBGNOCHK = FALSE;
  468. if( pSetResultInfo->Result == CPSUI_OK )
  469. {
  470. //
  471. // Attempt to save the printer data, if an error occurrs
  472. // display a message.
  473. //
  474. bStatus DBGCHK = _pPrinterData->bSave( TRUE );
  475. if( !bStatus )
  476. {
  477. //
  478. // Display the error message.
  479. //
  480. iMessage( _pPrinterData->hwnd(),
  481. IDS_ERR_DOC_PROP_TITLE,
  482. IDS_ERR_SAVE_PRINTER,
  483. MB_OK|MB_ICONSTOP,
  484. kMsgGetLastError,
  485. NULL );
  486. }
  487. else
  488. {
  489. pCPSUIInfo->Result = pSetResultInfo->Result;
  490. }
  491. }
  492. return bStatus;
  493. }