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.

1251 lines
35 KiB

  1. /*
  2. * npprint.c -- Code for printing from sxspad.
  3. * Copyright (C) 1984-2000 Microsoft Inc.
  4. */
  5. #define NOMINMAX
  6. #include "precomp.h"
  7. //#define DBGPRINT
  8. /* indices into chBuff */
  9. #define LEFT 0
  10. #define CENTER 1
  11. #define RIGHT 2
  12. INT tabSize; /* Size of a tab for print device in device units*/
  13. HWND hAbortDlgWnd;
  14. INT fAbort; /* true if abort in progress */
  15. INT yPrintChar; /* height of a character */
  16. RECT rtMargin;
  17. /* left,center and right string for header or trailer */
  18. #define MAXTITLE MAX_PATH
  19. TCHAR chBuff[RIGHT+1][MAXTITLE];
  20. /* date and time stuff for headers */
  21. #define MAXDATE MAX_PATH
  22. #define MAXTIME MAX_PATH
  23. TCHAR szFormattedDate[MAXDATE]=TEXT("Y"); // formatted date (may be internationalized)
  24. TCHAR szFormattedTime[MAXTIME]=TEXT("Y"); // formatted time (may be internaltionalized)
  25. SYSTEMTIME PrintTime; // time we started printing
  26. INT xPrintRes; // printer resolution in x direction
  27. INT yPrintRes; // printer resolution in y direction
  28. INT yPixInch; // pixels/inch
  29. INT xPhysRes; // physical resolution x of paper
  30. INT yPhysRes; // physical resolution y of paper
  31. INT xPhysOff; // physical offset x
  32. INT yPhysOff; // physical offset y
  33. INT dyTop; // width of top border (pixels)
  34. INT dyBottom; // width of bottom border
  35. INT dxLeft; // width of left border
  36. INT dxRight; // width of right border
  37. INT iPageNum; // global page number currently being printed
  38. /* define a type for NUM and the base */
  39. typedef long NUM;
  40. #define BASE 100L
  41. /* converting in/out of fixed point */
  42. #define NumToShort(x,s) (LOWORD(((x) + (s)) / BASE))
  43. #define NumRemToShort(x) (LOWORD((x) % BASE))
  44. /* rounding options for NumToShort */
  45. #define NUMFLOOR 0
  46. #define NUMROUND (BASE/2)
  47. #define NUMCEILING (BASE-1)
  48. #define ROUND(x) NumToShort(x,NUMROUND)
  49. #define FLOOR(x) NumToShort(x,NUMFLOOR)
  50. /* Unit conversion */
  51. #define InchesToCM(x) (((x) * 254L + 50) / 100)
  52. #define CMToInches(x) (((x) * 100L + 127) / 254)
  53. void DestroyAbortWnd(void) ;
  54. VOID TranslateString(TCHAR *);
  55. BOOL CALLBACK AbortProc(HDC hPrintDC, INT reserved)
  56. {
  57. MSG msg;
  58. while( !fAbort && PeekMessage((LPMSG)&msg, NULL, 0, 0, TRUE) )
  59. {
  60. if( !hAbortDlgWnd || !IsDialogMessage( hAbortDlgWnd, (LPMSG)&msg ) )
  61. {
  62. TranslateMessage( (LPMSG)&msg );
  63. DispatchMessage( (LPMSG)&msg );
  64. }
  65. }
  66. return( !fAbort );
  67. UNREFERENCED_PARAMETER(hPrintDC);
  68. UNREFERENCED_PARAMETER(reserved);
  69. }
  70. INT_PTR CALLBACK AbortDlgProc(
  71. HWND hwnd,
  72. UINT msg,
  73. WPARAM wParam,
  74. LPARAM lParam)
  75. {
  76. static HMENU hSysMenu;
  77. switch( msg )
  78. {
  79. case WM_COMMAND:
  80. fAbort= TRUE;
  81. DestroyAbortWnd();
  82. return( TRUE );
  83. case WM_INITDIALOG:
  84. hSysMenu= GetSystemMenu( hwnd, FALSE );
  85. SetDlgItemText( hwnd, ID_FILENAME,
  86. fUntitled ? szUntitled : PFileInPath(szFileName) );
  87. SetFocus( hwnd );
  88. return( TRUE );
  89. case WM_INITMENU:
  90. EnableMenuItem( hSysMenu, (WORD)SC_CLOSE, (DWORD)MF_GRAYED );
  91. return( TRUE );
  92. }
  93. return( FALSE );
  94. UNREFERENCED_PARAMETER(wParam);
  95. UNREFERENCED_PARAMETER(lParam);
  96. }
  97. /*
  98. * print out the translated header/footer string in proper position.
  99. * uses globals xPrintWidth, ...
  100. *
  101. * returns 1 if line was printed, otherwise 0.
  102. */
  103. INT PrintHeaderFooter (HDC hDC, INT nHF)
  104. {
  105. SIZE Size; // to compute the width of each string
  106. INT yPos; // y position to print
  107. INT xPos; // x position to print
  108. if( *chPageText[nHF] == 0 ) // see if anything to do
  109. return 0; // we didn't print
  110. TranslateString( chPageText[nHF] );
  111. // figure out the y position we are printing
  112. if( nHF == HEADER )
  113. yPos= dyTop;
  114. else
  115. yPos= yPrintRes - dyBottom - yPrintChar;
  116. // print out the various strings
  117. // N.B. could overprint which seems ok for now
  118. if( *chBuff[LEFT] ) // left string
  119. {
  120. TextOut( hDC, dxLeft, yPos, chBuff[LEFT], lstrlen(chBuff[LEFT]) );
  121. }
  122. if( *chBuff[CENTER] ) // center string
  123. {
  124. GetTextExtentPoint32( hDC, chBuff[CENTER], lstrlen(chBuff[CENTER]), &Size );
  125. xPos= (xPrintRes-dxRight+dxLeft)/2 - Size.cx/2;
  126. TextOut( hDC, xPos, yPos, chBuff[CENTER], lstrlen(chBuff[CENTER]) );
  127. }
  128. if( *chBuff[RIGHT] ) // right string
  129. {
  130. GetTextExtentPoint32( hDC, chBuff[RIGHT], lstrlen(chBuff[RIGHT]), &Size );
  131. xPos= xPrintRes - dxRight - Size.cx;
  132. TextOut( hDC, xPos, yPos, chBuff[RIGHT], lstrlen(chBuff[RIGHT]) );
  133. }
  134. return 1; // we did print something
  135. }
  136. /*
  137. * GetResolutions
  138. *
  139. * Gets printer resolutions.
  140. * sets globals: xPrintRes, yPrintRes, yPixInch
  141. *
  142. */
  143. VOID GetResolutions(HDC hPrintDC)
  144. {
  145. xPrintRes = GetDeviceCaps( hPrintDC, HORZRES );
  146. yPrintRes = GetDeviceCaps( hPrintDC, VERTRES );
  147. yPixInch = GetDeviceCaps( hPrintDC, LOGPIXELSY );
  148. xPhysRes = GetDeviceCaps( hPrintDC, PHYSICALWIDTH );
  149. yPhysRes = GetDeviceCaps( hPrintDC, PHYSICALHEIGHT );
  150. xPhysOff = GetDeviceCaps( hPrintDC, PHYSICALOFFSETX );
  151. yPhysOff = GetDeviceCaps( hPrintDC, PHYSICALOFFSETY );
  152. }
  153. /* GetMoreText
  154. *
  155. * Gets the next line of text from the MLE, returning a pointer
  156. * to the beginning and just past the end.
  157. *
  158. * linenum - index into MLE (IN)
  159. * pStartText - start of MLE (IN)
  160. * ppsStr - pointer to where to put pointer to start of text (OUT)
  161. * ppEOL - pointer to where to put pointer to just past EOL (OUT)
  162. *
  163. */
  164. VOID GetMoreText( INT linenum, PTCHAR pStartText, PTCHAR* ppsStr, PTCHAR* ppEOL )
  165. {
  166. INT Offset; // offset in 'chars' into edit buffer
  167. INT nChars; // number of chars in line
  168. Offset= (INT)SendMessage( hwndEdit, EM_LINEINDEX, linenum, 0 );
  169. nChars= (INT)SendMessage( hwndEdit, EM_LINELENGTH, Offset, 0 );
  170. *ppsStr= pStartText + Offset;
  171. *ppEOL= (pStartText+Offset) + nChars;
  172. }
  173. #ifdef DBGPRINT
  174. TCHAR dbuf[100];
  175. VOID ShowMargins( HDC hPrintDC )
  176. {
  177. INT xPrintRes, yPrintRes;
  178. RECT rct;
  179. HBRUSH hBrush;
  180. xPrintRes= GetDeviceCaps( hPrintDC, HORZRES );
  181. yPrintRes= GetDeviceCaps( hPrintDC, VERTRES );
  182. hBrush= GetStockObject( BLACK_BRUSH );
  183. SetRect( &rct, 0,0,xPrintRes-1, yPrintRes-1 );
  184. FrameRect( hPrintDC, &rct, hBrush );
  185. SetRect( &rct, dxLeft, dyTop, xPrintRes-dxRight, yPrintRes-dyBottom );
  186. FrameRect( hPrintDC, &rct, hBrush );
  187. }
  188. VOID PrintLogFont( LOGFONT lf )
  189. {
  190. wsprintf(dbuf,TEXT("lfHeight %d\n"), lf.lfHeight ); ODS(dbuf);
  191. wsprintf(dbuf,TEXT("lfWidth %d\n"), lf.lfWidth ); ODS(dbuf);
  192. wsprintf(dbuf,TEXT("lfEscapement %d\n"), lf. lfEscapement ); ODS(dbuf);
  193. wsprintf(dbuf,TEXT("lfOrientation %d\n"), lf.lfOrientation ); ODS(dbuf);
  194. wsprintf(dbuf,TEXT("lfWeight %d\n"), lf.lfWeight ); ODS(dbuf);
  195. wsprintf(dbuf,TEXT("lfItalic %d\n"), lf.lfItalic ); ODS(dbuf);
  196. wsprintf(dbuf,TEXT("lfUnderline %d\n"), lf.lfUnderline ); ODS(dbuf);
  197. wsprintf(dbuf,TEXT("lfStrikeOut %d\n"), lf.lfStrikeOut ); ODS(dbuf);
  198. wsprintf(dbuf,TEXT("lfCharSet %d\n"), lf.lfCharSet ); ODS(dbuf);
  199. wsprintf(dbuf,TEXT("lfOutPrecision %d\n"), lf.lfOutPrecision ); ODS(dbuf);
  200. wsprintf(dbuf,TEXT("lfClipPrecison %d\n"), lf.lfClipPrecision ); ODS(dbuf);
  201. wsprintf(dbuf,TEXT("lfQuality %d\n"), lf.lfQuality ); ODS(dbuf);
  202. wsprintf(dbuf,TEXT("lfPitchAndFamily %d\n"), lf.lfPitchAndFamily); ODS(dbuf);
  203. wsprintf(dbuf,TEXT("lfFaceName %s\n"), lf.lfFaceName ); ODS(dbuf);
  204. }
  205. #endif
  206. // GetPrinterDCviaDialog
  207. //
  208. // Use the common dialog PrintDlgEx() function to get a printer DC to print to.
  209. //
  210. // Returns: valid HDC or INVALID_HANDLE_VALUE if error.
  211. //
  212. HDC GetPrinterDCviaDialog( VOID )
  213. {
  214. PRINTDLGEX pdTemp;
  215. HDC hDC;
  216. HRESULT hRes;
  217. //
  218. // Get the page setup information
  219. //
  220. if( !g_PageSetupDlg.hDevNames ) /* Retrieve default printer if none selected. */
  221. {
  222. g_PageSetupDlg.Flags |= (PSD_RETURNDEFAULT|PSD_NOWARNING );
  223. PageSetupDlg(&g_PageSetupDlg);
  224. g_PageSetupDlg.Flags &= ~(PSD_RETURNDEFAULT|PSD_NOWARNING);
  225. }
  226. //
  227. // Initialize the dialog structure
  228. //
  229. ZeroMemory( &pdTemp, sizeof(pdTemp) );
  230. pdTemp.lStructSize= sizeof(pdTemp);
  231. pdTemp.hwndOwner= hwndSP;
  232. pdTemp.nStartPage= START_PAGE_GENERAL;
  233. pdTemp.Flags= PD_NOPAGENUMS | PD_RETURNDC | PD_NOCURRENTPAGE |
  234. PD_NOSELECTION | 0;
  235. // if use set printer in PageSetup, use it here too.
  236. if( g_PageSetupDlg.hDevMode )
  237. {
  238. pdTemp.hDevMode= g_PageSetupDlg.hDevMode;
  239. }
  240. if( g_PageSetupDlg.hDevNames )
  241. {
  242. pdTemp.hDevNames= g_PageSetupDlg.hDevNames;
  243. }
  244. //
  245. // let user select printer
  246. //
  247. hRes= PrintDlgEx( &pdTemp );
  248. //
  249. // get DC if valid return
  250. //
  251. hDC= INVALID_HANDLE_VALUE;
  252. if( hRes == S_OK )
  253. {
  254. if( (pdTemp.dwResultAction == PD_RESULT_PRINT) || (pdTemp.dwResultAction == PD_RESULT_APPLY) )
  255. {
  256. if( pdTemp.dwResultAction == PD_RESULT_PRINT )
  257. {
  258. hDC= pdTemp.hDC;
  259. }
  260. //
  261. // Get the page setup information for the printer selected in case it was
  262. // the first printer added by the user through sxspad.
  263. //
  264. if( !g_PageSetupDlg.hDevMode )
  265. {
  266. g_PageSetupDlg.Flags |= (PSD_RETURNDEFAULT|PSD_NOWARNING );
  267. PageSetupDlg(&g_PageSetupDlg);
  268. g_PageSetupDlg.Flags &= ~(PSD_RETURNDEFAULT|PSD_NOWARNING);
  269. }
  270. // change devmode if user pressed print or apply
  271. g_PageSetupDlg.hDevMode= pdTemp.hDevMode;
  272. g_PageSetupDlg.hDevNames= pdTemp.hDevNames;
  273. }
  274. }
  275. // FEATURE: free hDevNames
  276. return( hDC );
  277. }
  278. INT NpPrint( PRINT_DIALOG_TYPE type)
  279. {
  280. HDC hPrintDC;
  281. SetCursor( hWaitCursor );
  282. switch( type )
  283. {
  284. case UseDialog:
  285. hPrintDC= GetPrinterDCviaDialog();
  286. break;
  287. case NoDialogNonDefault:
  288. hPrintDC= GetNonDefPrinterDC();
  289. break;
  290. case DoNotUseDialog:
  291. default:
  292. hPrintDC= GetPrinterDC();
  293. break;
  294. }
  295. if( hPrintDC == INVALID_HANDLE_VALUE )
  296. {
  297. SetCursor( hStdCursor );
  298. return( 0 ); // message already given
  299. }
  300. return( NpPrintGivenDC( hPrintDC ) );
  301. }
  302. INT NpPrintGivenDC( HDC hPrintDC )
  303. {
  304. HANDLE hText= NULL; // handle to MLE text
  305. HFONT hPrintFont= NULL; // font to print with
  306. HANDLE hPrevFont= NULL; // previous font in hPrintDC
  307. BOOL fPageStarted= FALSE; // true if StartPage called for this page
  308. BOOL fDocStarted= FALSE; // true if StartDoc called
  309. PTCHAR pStartText= NULL; // start of edit text (locked hText)
  310. TEXTMETRIC Metrics;
  311. TCHAR msgbuf[MAX_PATH]; // Document name for tracking print job
  312. INT nLinesPerPage; // not inc. header and footer
  313. // iErr will contain the first error discovered ie it is sticky
  314. // This will be the value returned by this function.
  315. // It does not need to translate SP_* errors except for SP_ERROR which should be
  316. // GetLastError() right after it is first detected.
  317. INT iErr=0; // error return
  318. DOCINFO DocInfo;
  319. LOGFONT lfPrintFont; // local version of FontStruct
  320. LCID lcid; // locale id
  321. fAbort = FALSE;
  322. hAbortDlgWnd= NULL;
  323. SetCursor( hWaitCursor );
  324. GetResolutions( hPrintDC );
  325. // Get the time and date for use in the header or trailer.
  326. // We use the GetDateFormat and GetTimeFormat to get the
  327. // internationalized versions.
  328. GetLocalTime( &PrintTime ); // use local, not gmt
  329. lcid= GetUserDefaultLCID();
  330. GetDateFormat( lcid, DATE_LONGDATE, &PrintTime, NULL, szFormattedDate, MAXDATE );
  331. GetTimeFormat( lcid, 0, &PrintTime, NULL, szFormattedTime, MAXTIME );
  332. /*
  333. * This part is to select the current font to the printer device.
  334. * We have to change the height because FontStruct was created
  335. * assuming the display. Using the remembered pointsize, calculate
  336. * the new height.
  337. */
  338. lfPrintFont= FontStruct; // make local copy
  339. lfPrintFont.lfHeight= -(iPointSize*yPixInch)/(72*10);
  340. lfPrintFont.lfWidth= 0;
  341. //
  342. // convert margins to pixels
  343. // ptPaperSize is the physical paper size, not the printable area.
  344. // do the mapping in physical units
  345. //
  346. SetMapMode( hPrintDC, MM_ANISOTROPIC );
  347. SetViewportExtEx( hPrintDC,
  348. xPhysRes,
  349. yPhysRes,
  350. NULL );
  351. SetWindowExtEx( hPrintDC,
  352. g_PageSetupDlg.ptPaperSize.x,
  353. g_PageSetupDlg.ptPaperSize.y,
  354. NULL );
  355. rtMargin = g_PageSetupDlg.rtMargin;
  356. LPtoDP( hPrintDC, (LPPOINT) &rtMargin, 2 );
  357. SetMapMode( hPrintDC,MM_TEXT ); // restore to mm_text mode
  358. hPrintFont= CreateFontIndirect(&lfPrintFont);
  359. if( !hPrintFont )
  360. {
  361. goto ErrorExit;
  362. }
  363. hPrevFont= SelectObject( hPrintDC, hPrintFont );
  364. if( !hPrevFont )
  365. {
  366. goto ErrorExit;
  367. }
  368. SetBkMode( hPrintDC, TRANSPARENT );
  369. if( !GetTextMetrics( hPrintDC, (LPTEXTMETRIC) &Metrics ) )
  370. {
  371. goto ErrorExit;
  372. }
  373. // The font may not a scalable (say on a bubblejet printer)
  374. // In this case, just pick some font
  375. // For example, FixedSys 9 pt would be non-scalable
  376. if( !(Metrics.tmPitchAndFamily & (TMPF_VECTOR | TMPF_TRUETYPE )) )
  377. {
  378. // remove just created font
  379. hPrintFont= SelectObject( hPrintDC, hPrevFont ); // get old font
  380. DeleteObject( hPrintFont );
  381. memset( lfPrintFont.lfFaceName, 0, LF_FACESIZE*sizeof(TCHAR) );
  382. hPrintFont= CreateFontIndirect( &lfPrintFont );
  383. if( !hPrintFont )
  384. {
  385. goto ErrorExit;
  386. }
  387. hPrevFont= SelectObject( hPrintDC, hPrintFont );
  388. if( !hPrevFont )
  389. {
  390. goto ErrorExit;
  391. }
  392. if( !GetTextMetrics( hPrintDC, (LPTEXTMETRIC) &Metrics ) )
  393. {
  394. goto ErrorExit;
  395. }
  396. }
  397. yPrintChar= Metrics.tmHeight+Metrics.tmExternalLeading; /* the height */
  398. tabSize = Metrics.tmAveCharWidth * 8; /* 8 ave char width pixels for tabs */
  399. // compute margins in pixels
  400. dxLeft= max(rtMargin.left - xPhysOff,0);
  401. dxRight= max(rtMargin.right - (xPhysRes - xPrintRes - xPhysOff), 0 );
  402. dyTop= max(rtMargin.top - yPhysOff,0);
  403. dyBottom= max(rtMargin.bottom - (yPhysRes - yPrintRes - yPhysOff), 0 );
  404. #ifdef DBGPRINT
  405. {
  406. TCHAR dbuf[100];
  407. RECT rt= g_PageSetupDlg.rtMargin;
  408. POINT pt;
  409. wsprintf(dbuf,TEXT("Print pOffx %d pOffy %d\n"),
  410. GetDeviceCaps(hPrintDC, PHYSICALOFFSETX),
  411. GetDeviceCaps(hPrintDC, PHYSICALOFFSETY));
  412. ODS(dbuf);
  413. wsprintf(dbuf,TEXT("PHYSICALWIDTH: %d\n"), xPhysRes);
  414. ODS(dbuf);
  415. wsprintf(dbuf,TEXT("HORZRES: %d\n"),xPrintRes);
  416. ODS(dbuf);
  417. wsprintf(dbuf,TEXT("PHYSICALOFFSETX: %d\n"),xPhysOff);
  418. ODS(dbuf);
  419. wsprintf(dbuf,TEXT("LOGPIXELSX: %d\n"),
  420. GetDeviceCaps(hPrintDC,LOGPIXELSX));
  421. ODS(dbuf);
  422. GetViewportOrgEx( hPrintDC, (LPPOINT) &pt );
  423. wsprintf(dbuf,TEXT("Viewport org: %d %d\n"), pt.x, pt.y );
  424. ODS(dbuf);
  425. GetWindowOrgEx( hPrintDC, (LPPOINT) &pt );
  426. wsprintf(dbuf,TEXT("Window org: %d %d\n"), pt.x, pt.y );
  427. ODS(dbuf);
  428. wsprintf(dbuf,TEXT("PrintRes x: %d y: %d\n"),xPrintRes, yPrintRes);
  429. ODS(dbuf);
  430. wsprintf(dbuf,TEXT("PaperSize x: %d y: %d\n"),
  431. g_PageSetupDlg.ptPaperSize.x,
  432. g_PageSetupDlg.ptPaperSize.y );
  433. ODS(dbuf);
  434. wsprintf(dbuf,TEXT("unit margins: l: %d r: %d t: %d b: %d\n"),
  435. rt.left, rt.right, rt.top, rt.bottom);
  436. ODS(dbuf);
  437. wsprintf(dbuf,TEXT("pixel margins: l: %d r: %d t: %d b: %d\n"),
  438. rtMargin.left, rtMargin.right, rtMargin.top, rtMargin.bottom);
  439. ODS(dbuf);
  440. wsprintf(dbuf,TEXT("dxLeft %d dxRight %d\n"),dxLeft,dxRight);
  441. ODS(dbuf);
  442. wsprintf(dbuf,TEXT("dyTop %d dyBot %d\n"),dyTop,dyBottom);
  443. ODS(dbuf);
  444. }
  445. #endif
  446. /* Number of lines on a page with margins */
  447. /* two lines are used by header and footer */
  448. nLinesPerPage = ((yPrintRes - dyTop - dyBottom) / yPrintChar);
  449. if( *chPageText[HEADER] )
  450. nLinesPerPage--;
  451. if( *chPageText[FOOTER] )
  452. nLinesPerPage--;
  453. /*
  454. ** There was a bug in NT once where a printer driver would
  455. ** return a font that was larger than the page size which
  456. ** would then cause sxspad to constantly print blank pages
  457. ** To keep from doing this we check to see if we can fit ANYTHING
  458. ** on a page, if not then there is a problem so quit. MarkRi 8/92
  459. */
  460. if( nLinesPerPage <= 0 )
  461. {
  462. FontTooBig:
  463. MessageBox( hwndSP, szFontTooBig, szNN, MB_APPLMODAL | MB_OK | MB_ICONEXCLAMATION );
  464. SetLastError(0); // no error
  465. ErrorExit:
  466. iErr= GetLastError(); // remember the first error
  467. ExitWithThisError: // preserve iErr (return SP_* errors)
  468. if( hPrevFont )
  469. {
  470. SelectObject( hPrintDC, hPrevFont );
  471. DeleteObject( hPrintFont );
  472. }
  473. if( pStartText ) // were able to lock hText
  474. LocalUnlock( hText );
  475. if( fPageStarted )
  476. {
  477. if( EndPage( hPrintDC ) <= 0 )
  478. {
  479. // if iErr not already set then set it to the new error code.
  480. if( iErr == 0 )
  481. {
  482. iErr= GetLastError();
  483. }
  484. }
  485. }
  486. if( fDocStarted )
  487. {
  488. if( fAbort ) {
  489. AbortDoc( hPrintDC );
  490. }
  491. else {
  492. if( EndDoc( hPrintDC ) <= 0 )
  493. {
  494. // if iErr not already set then set it to the new error code.
  495. if (iErr == 0)
  496. {
  497. iErr= GetLastError();
  498. }
  499. }
  500. }
  501. }
  502. DeleteDC( hPrintDC );
  503. DestroyAbortWnd();
  504. SetCursor( hStdCursor );
  505. if (!fAbort)
  506. {
  507. return( iErr );
  508. }
  509. else
  510. {
  511. return( SP_USERABORT );
  512. }
  513. }
  514. if( (iErr= SetAbortProc (hPrintDC, AbortProc)) < 0 )
  515. {
  516. goto ExitWithThisError;
  517. }
  518. // get printer to MLE text
  519. hText= (HANDLE) SendMessage( hwndEdit, EM_GETHANDLE, 0, 0 );
  520. if( !hText )
  521. {
  522. goto ErrorExit;
  523. }
  524. pStartText= LocalLock( hText );
  525. if( !pStartText )
  526. {
  527. goto ErrorExit;
  528. }
  529. GetWindowText( hwndSP, msgbuf, CharSizeOf(msgbuf) );
  530. EnableWindow( hwndSP, FALSE ); // Disable window to prevent reentrancy
  531. hAbortDlgWnd= CreateDialog( hInstanceSP,
  532. (LPTSTR) MAKEINTRESOURCE(IDD_ABORTPRINT),
  533. hwndSP,
  534. AbortDlgProc);
  535. if( !hAbortDlgWnd )
  536. {
  537. goto ErrorExit;
  538. }
  539. DocInfo.cbSize= sizeof(DOCINFO);
  540. DocInfo.lpszDocName= msgbuf;
  541. DocInfo.lpszOutput= NULL;
  542. DocInfo.lpszDatatype= NULL; // Type of data used to record print job
  543. DocInfo.fwType= 0; // not DI_APPBANDING
  544. SetLastError(0); // clear error so it reflects errors in the future
  545. if( StartDoc( hPrintDC, &DocInfo ) <= 0 )
  546. {
  547. iErr = GetLastError();
  548. goto ExitWithThisError;
  549. }
  550. fDocStarted= TRUE;
  551. // Basicly, this is just a loop surrounding the DrawTextEx API.
  552. // We have to calculate the printable area which will not include
  553. // the header and footer area.
  554. {
  555. INT iTextLeft; // amount of text left to print
  556. INT iSta; // status
  557. UINT dwDTFormat; // drawtext flags
  558. DRAWTEXTPARAMS dtParm; // drawtext control
  559. RECT rect; // rectangle to draw in
  560. UINT dwDTRigh = 0; // drawtext flags (RTL)
  561. iPageNum= 1;
  562. fPageStarted= FALSE;
  563. // calculate the size of the printable area for the text
  564. // not including the header and footer
  565. ZeroMemory( &rect, sizeof(rect) );
  566. rect.left= dxLeft; rect.right= xPrintRes-dxRight;
  567. rect.top= dyTop; rect.bottom= yPrintRes-dyBottom;
  568. if( *chPageText[HEADER] != 0 )
  569. {
  570. rect.top += yPrintChar;
  571. }
  572. if( *chPageText[FOOTER] != 0 )
  573. {
  574. rect.bottom -= yPrintChar;
  575. }
  576. iTextLeft= lstrlen(pStartText);
  577. //Get the edit control direction.
  578. if (GetWindowLong(hwndEdit, GWL_EXSTYLE) & WS_EX_RTLREADING)
  579. dwDTRigh = DT_RIGHT | DT_RTLREADING;
  580. while( !fAbort && (iTextLeft>0) )
  581. {
  582. #define MAXSTATUS 100
  583. TCHAR szPagePrinting[MAXSTATUS+1];
  584. // update abort dialog box to inform user where we are in the printing
  585. _sntprintf( szPagePrinting, MAXSTATUS, szCurrentPage, iPageNum );
  586. SetDlgItemText( hAbortDlgWnd, ID_PAGENUMBER, szPagePrinting );
  587. PrintHeaderFooter( hPrintDC, HEADER );
  588. ZeroMemory( &dtParm, sizeof(dtParm) );
  589. dtParm.cbSize= sizeof(dtParm);
  590. dtParm.iTabLength= tabSize;
  591. dwDTFormat= DT_EDITCONTROL | DT_LEFT | DT_EXPANDTABS | DT_NOPREFIX |
  592. DT_WORDBREAK | dwDTRigh | 0;
  593. if( StartPage( hPrintDC ) <= 0 )
  594. {
  595. iErr= GetLastError();
  596. goto ExitWithThisError;
  597. }
  598. fPageStarted= TRUE;
  599. #ifdef DBGPRINT
  600. ShowMargins(hPrintDC);
  601. #endif
  602. /* Ignore errors in printing. EndPage or StartPage will find them */
  603. iSta= DrawTextEx( hPrintDC,
  604. pStartText,
  605. iTextLeft,
  606. &rect,
  607. dwDTFormat,
  608. &dtParm);
  609. PrintHeaderFooter( hPrintDC, FOOTER );
  610. if( EndPage( hPrintDC ) <= 0 )
  611. {
  612. iErr= GetLastError();
  613. goto ExitWithThisError;
  614. }
  615. fPageStarted= FALSE;
  616. iPageNum++;
  617. // if we can't print a single character (too big perhaps)
  618. // just bail now.
  619. if( dtParm.uiLengthDrawn == 0 )
  620. {
  621. goto FontTooBig;
  622. }
  623. pStartText += dtParm.uiLengthDrawn;
  624. iTextLeft -= dtParm.uiLengthDrawn;
  625. }
  626. }
  627. iErr=0; // no errors
  628. goto ExitWithThisError;
  629. }
  630. VOID DestroyAbortWnd (void)
  631. {
  632. EnableWindow(hwndSP, TRUE);
  633. DestroyWindow(hAbortDlgWnd);
  634. hAbortDlgWnd = NULL;
  635. }
  636. const DWORD s_PageSetupHelpIDs[] = {
  637. ID_HEADER_LABEL, IDH_PAGE_HEADER,
  638. ID_HEADER, IDH_PAGE_HEADER,
  639. ID_FOOTER_LABEL, IDH_PAGE_FOOTER,
  640. ID_FOOTER, IDH_PAGE_FOOTER,
  641. 0, 0
  642. };
  643. /*******************************************************************************
  644. *
  645. * PageSetupHookProc
  646. *
  647. * DESCRIPTION:
  648. * Callback procedure for the PageSetup common dialog box.
  649. *
  650. * PARAMETERS:
  651. * hWnd, handle of PageSetup window.
  652. * Message,
  653. * wParam,
  654. * lParam,
  655. * (returns),
  656. *
  657. *******************************************************************************/
  658. UINT_PTR CALLBACK PageSetupHookProc(
  659. HWND hWnd,
  660. UINT Message,
  661. WPARAM wParam,
  662. LPARAM lParam
  663. )
  664. {
  665. INT id; /* ID of dialog edit controls */
  666. POINT pt;
  667. switch (Message)
  668. {
  669. case WM_INITDIALOG:
  670. for (id = ID_HEADER; id <= ID_FOOTER; id++)
  671. {
  672. SendDlgItemMessage(hWnd, id, EM_LIMITTEXT, PT_LEN-1, 0L);
  673. SetDlgItemText(hWnd, id, chPageText[id - ID_HEADER]);
  674. }
  675. SendDlgItemMessage(hWnd, ID_HEADER, EM_SETSEL, 0,
  676. MAKELONG(0, PT_LEN-1));
  677. return TRUE;
  678. case WM_DESTROY:
  679. // We don't know if the user hit OK or Cancel, so we don't
  680. // want to replace our real copies until we know! We _should_ get
  681. // a notification from the common dialog code!
  682. for( id = ID_HEADER; id <= ID_FOOTER; id++ )
  683. {
  684. GetDlgItemText(hWnd, id, chPageTextTemp[id - ID_HEADER],PT_LEN);
  685. }
  686. break;
  687. case WM_HELP:
  688. //
  689. // We only want to intercept help messages for controls that we are
  690. // responsible for.
  691. //
  692. id = GetDlgCtrlID(((LPHELPINFO) lParam)-> hItemHandle);
  693. if (id < ID_HEADER || id > ID_FOOTER_LABEL)
  694. break;
  695. WinHelp(((LPHELPINFO) lParam)-> hItemHandle, szHelpFile,
  696. HELP_WM_HELP, (UINT_PTR) (LPVOID) s_PageSetupHelpIDs);
  697. return TRUE;
  698. case WM_CONTEXTMENU:
  699. //
  700. // If the user clicks on any of our labels, then the wParam will
  701. // be the hwnd of the dialog, not the static control. WinHelp()
  702. // handles this, but because we hook the dialog, we must catch it
  703. // first.
  704. //
  705. if( hWnd == (HWND) wParam )
  706. {
  707. GetCursorPos(&pt);
  708. ScreenToClient(hWnd, &pt);
  709. wParam = (WPARAM) ChildWindowFromPoint(hWnd, pt);
  710. }
  711. //
  712. // We only want to intercept help messages for controls that we are
  713. // responsible for.
  714. //
  715. id = GetDlgCtrlID((HWND) wParam);
  716. if (id < ID_HEADER || id > ID_FOOTER_LABEL)
  717. break;
  718. WinHelp((HWND) wParam, szHelpFile, HELP_CONTEXTMENU,
  719. (UINT_PTR) (LPVOID) s_PageSetupHelpIDs);
  720. return TRUE;
  721. }
  722. return FALSE;
  723. }
  724. /***************************************************************************
  725. * VOID TranslateString(TCHAR *src)
  726. *
  727. * purpose:
  728. * translate a header/footer strings
  729. *
  730. * supports the following:
  731. *
  732. * && insert a & char
  733. * &f current file name or (untitled)
  734. * &d date in Day Month Year
  735. * &t time
  736. * &p page number
  737. * &p+num set first page number to num
  738. *
  739. * Alignment:
  740. * &l, &c, &r for left, center, right
  741. *
  742. * params:
  743. * IN/OUT src this is the string to translate
  744. *
  745. *
  746. * used by:
  747. * Header Footer stuff
  748. *
  749. * uses:
  750. * lots of c lib stuff
  751. *
  752. ***************************************************************************/
  753. VOID TranslateString (TCHAR * src)
  754. {
  755. // File, Page, Time, Date, Center, Right, Left
  756. // these *never* change so don't put into resources for localizers
  757. TCHAR letters[15]=TEXT("fFpPtTdDcCrRlL");
  758. TCHAR buf[MAX_PATH];
  759. TCHAR *ptr;
  760. INT page;
  761. INT nAlign=CENTER; // current string to add chars to
  762. INT foo;
  763. INT nIndex[RIGHT+1]; // current lengths of (left,center,right)
  764. struct tm *newtime;
  765. time_t long_time;
  766. INT iLen; // length of strings
  767. nIndex[LEFT] = 0;
  768. nIndex[CENTER] = 0;
  769. nIndex[RIGHT] = 0;
  770. /* Get the time we need in case we use &t. */
  771. time (&long_time);
  772. newtime = localtime (&long_time);
  773. while (*src) /* look at all of source */
  774. {
  775. while (*src && *src != TEXT('&'))
  776. {
  777. chBuff[nAlign][nIndex[nAlign]] = *src++;
  778. nIndex[nAlign] += 1;
  779. }
  780. if (*src == TEXT('&')) /* is it the escape char? */
  781. {
  782. src++;
  783. if (*src == letters[0] || *src == letters[1])
  784. { /* &f file name (no path) */
  785. if (!fUntitled)
  786. {
  787. GetFileTitle(szFileName, buf, CharSizeOf(buf));
  788. }
  789. else
  790. {
  791. lstrcpy(buf, szUntitled);
  792. }
  793. /* Copy to the currently aligned string. */
  794. if( nIndex[nAlign] + lstrlen(buf) < MAXTITLE )
  795. {
  796. lstrcpy( chBuff[nAlign] + nIndex[nAlign], buf );
  797. /* Update insertion position. */
  798. nIndex[nAlign] += lstrlen (buf);
  799. }
  800. }
  801. else if (*src == letters[2] || *src == letters[3]) /* &P or &P+num page */
  802. {
  803. src++;
  804. page = 0;
  805. if (*src == TEXT('+')) /* &p+num case */
  806. {
  807. src++;
  808. while (_istdigit(*src))
  809. {
  810. /* Convert to int on-the-fly*/
  811. page = (10*page) + (*src) - TEXT('0');
  812. src++;
  813. }
  814. }
  815. wsprintf( buf, TEXT("%d"), iPageNum+page ); // convert to chars
  816. if( nIndex[nAlign] + lstrlen(buf) < MAXTITLE )
  817. {
  818. lstrcpy( chBuff[nAlign] + nIndex[nAlign], buf );
  819. nIndex[nAlign] += lstrlen (buf);
  820. }
  821. src--;
  822. }
  823. else if (*src == letters[4] || *src == letters[5]) /* &t time */
  824. {
  825. iLen= lstrlen( szFormattedTime );
  826. /* extract time */
  827. if( nIndex[nAlign] + iLen < MAXTITLE )
  828. {
  829. _tcsncpy (chBuff[nAlign] + nIndex[nAlign], szFormattedTime, iLen);
  830. nIndex[nAlign] += iLen;
  831. }
  832. }
  833. else if (*src == letters[6] || *src == letters[7]) /* &d date */
  834. {
  835. iLen= lstrlen( szFormattedDate );
  836. /* extract day month day */
  837. if( nIndex[nAlign] + iLen < MAXTITLE )
  838. {
  839. _tcsncpy (chBuff[nAlign] + nIndex[nAlign], szFormattedDate, iLen);
  840. nIndex[nAlign] += iLen;
  841. }
  842. }
  843. else if (*src == TEXT('&')) /* quote a single & */
  844. {
  845. if( nIndex[nAlign] + 1 < MAXTITLE )
  846. {
  847. chBuff[nAlign][nIndex[nAlign]] = TEXT('&');
  848. nIndex[nAlign] += 1;
  849. }
  850. }
  851. /* Set the alignment for whichever has last occured. */
  852. else if (*src == letters[8] || *src == letters[9]) /* &c center */
  853. nAlign=CENTER;
  854. else if (*src == letters[10] || *src == letters[11]) /* &r right */
  855. nAlign=RIGHT;
  856. else if (*src == letters[12] || *src == letters[13]) /* &d date */
  857. nAlign=LEFT;
  858. src++;
  859. }
  860. }
  861. /* Make sure all strings are null-terminated. */
  862. for (nAlign= LEFT; nAlign <= RIGHT ; nAlign++)
  863. chBuff[nAlign][nIndex[nAlign]] = (TCHAR) 0;
  864. }
  865. /* GetPrinterDC() - returns printer DC or INVALID_HANDLE_VALUE if none. */
  866. HANDLE GetPrinterDC (VOID)
  867. {
  868. LPDEVMODE lpDevMode;
  869. LPDEVNAMES lpDevNames;
  870. HDC hDC;
  871. if( !g_PageSetupDlg.hDevNames ) /* Retrieve default printer if none selected. */
  872. {
  873. g_PageSetupDlg.Flags |= PSD_RETURNDEFAULT;
  874. PageSetupDlg(&g_PageSetupDlg);
  875. g_PageSetupDlg.Flags &= ~PSD_RETURNDEFAULT;
  876. }
  877. if( !g_PageSetupDlg.hDevNames )
  878. {
  879. MessageBox( hwndSP, szLoadDrvFail, szNN, MB_APPLMODAL | MB_OK | MB_ICONEXCLAMATION);
  880. return INVALID_HANDLE_VALUE;
  881. }
  882. lpDevNames= (LPDEVNAMES) GlobalLock (g_PageSetupDlg.hDevNames);
  883. lpDevMode= NULL;
  884. if( g_PageSetupDlg.hDevMode )
  885. lpDevMode= (LPDEVMODE) GlobalLock( g_PageSetupDlg.hDevMode );
  886. /* For pre 3.0 Drivers,hDevMode will be null from Commdlg so lpDevMode
  887. * will be NULL after GlobalLock()
  888. */
  889. /* The lpszOutput name is null so CreateDC will use the current setting
  890. * from PrintMan.
  891. */
  892. hDC= CreateDC (((LPTSTR)lpDevNames)+lpDevNames->wDriverOffset,
  893. ((LPTSTR)lpDevNames)+lpDevNames->wDeviceOffset,
  894. NULL,
  895. lpDevMode);
  896. GlobalUnlock( g_PageSetupDlg.hDevNames );
  897. if( g_PageSetupDlg.hDevMode )
  898. GlobalUnlock( g_PageSetupDlg.hDevMode );
  899. if( hDC == NULL )
  900. {
  901. MessageBox( hwndSP, szLoadDrvFail, szNN, MB_APPLMODAL | MB_OK | MB_ICONEXCLAMATION);
  902. return INVALID_HANDLE_VALUE;
  903. }
  904. return hDC;
  905. }
  906. /* GetNonDefPrinterDC() - returns printer DC or INVALID_HANDLE_VALUE if none. */
  907. /* using the name of the Printer server */
  908. HANDLE GetNonDefPrinterDC (VOID)
  909. {
  910. HDC hDC;
  911. HANDLE hPrinter;
  912. DWORD dwBuf;
  913. DRIVER_INFO_1 *di1;
  914. // open the printer and retrieve the driver name.
  915. if (!OpenPrinter(szPrinterName, &hPrinter, NULL))
  916. {
  917. return INVALID_HANDLE_VALUE;
  918. }
  919. // get the buffer size.
  920. GetPrinterDriver(hPrinter, NULL, 1, NULL, 0, &dwBuf);
  921. di1 = (DRIVER_INFO_1 *) LocalAlloc(LPTR, dwBuf);
  922. if (!di1)
  923. {
  924. ClosePrinter(hPrinter);
  925. return INVALID_HANDLE_VALUE;
  926. }
  927. if (!GetPrinterDriver(hPrinter, NULL, 1, (LPBYTE) di1, dwBuf, &dwBuf))
  928. {
  929. LocalFree(di1);
  930. ClosePrinter(hPrinter);
  931. return INVALID_HANDLE_VALUE;
  932. }
  933. // Initialize the PageSetup dlg to default values.
  934. // using default printer's value for another printer !!
  935. g_PageSetupDlg.Flags |= PSD_RETURNDEFAULT;
  936. PageSetupDlg(&g_PageSetupDlg);
  937. g_PageSetupDlg.Flags &= ~PSD_RETURNDEFAULT;
  938. // create printer dc with default initialization.
  939. hDC= CreateDC (di1->pName, szPrinterName, NULL, NULL);
  940. // cleanup.
  941. LocalFree(di1);
  942. ClosePrinter(hPrinter);
  943. if( hDC == NULL )
  944. {
  945. MessageBox( hwndSP, szLoadDrvFail, szNN, MB_APPLMODAL | MB_OK | MB_ICONEXCLAMATION);
  946. return INVALID_HANDLE_VALUE;
  947. }
  948. return hDC;
  949. }
  950. /* PrintIt() - print the file, giving popup if some error */
  951. void PrintIt(PRINT_DIALOG_TYPE type)
  952. {
  953. INT iError;
  954. TCHAR* szMsg= NULL;
  955. TCHAR msg[400]; // message info on error
  956. /* print the file */
  957. iError= NpPrint( type );
  958. if(( iError != 0) &&
  959. ( iError != SP_APPABORT ) &&
  960. ( iError != SP_USERABORT ) )
  961. {
  962. // translate any known spooler errors
  963. if( iError == SP_OUTOFDISK ) iError= ERROR_DISK_FULL;
  964. if( iError == SP_OUTOFMEMORY ) iError= ERROR_OUTOFMEMORY;
  965. if( iError == SP_ERROR ) iError= GetLastError();
  966. /* SP_NOTREPORTED not handled. Does it happen? */
  967. //
  968. // iError may be 0 because the user aborted the printing.
  969. // Just ignore.
  970. //
  971. if( iError == 0 ) return;
  972. // Get system to give reasonable error message
  973. // These will also be internationalized.
  974. if(!FormatMessage( FORMAT_MESSAGE_IGNORE_INSERTS |
  975. FORMAT_MESSAGE_FROM_SYSTEM,
  976. NULL,
  977. iError,
  978. GetUserDefaultLangID(),
  979. msg, // where message will end up
  980. CharSizeOf(msg), NULL ) )
  981. {
  982. szMsg= szCP; // couldn't get system to say; give generic msg
  983. }
  984. else
  985. {
  986. szMsg= msg;
  987. }
  988. AlertBox( hwndSP, szNN, szMsg, fUntitled ? szUntitled : szFileName,
  989. MB_APPLMODAL | MB_OK | MB_ICONEXCLAMATION);
  990. }
  991. }