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.

1253 lines
36 KiB

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