home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Garbo
/
Garbo.cdr
/
mac
/
source
/
luschsrc.sit
/
print.c
< prev
next >
Wrap
Text File
|
1990-05-23
|
5KB
|
212 lines
/********************************************************************************
* print.c
*
* Printing Package
*
* Written by Paco Xander Nathan
* ⌐1990, Motorola Inc. Public domain source code.
********************************************************************************/
#include "applic.h"
#include "window.h"
#include "dialog.h"
#include "error.h"
#include "print.h"
#include <PrintMgr.h>
#include <IntlPkg.h>
#include "test.h"
#include "gnosis.h"
#include "analysis.h"
/* Local Data Structures
*/
static THPrint
printHdl = NULL;
static TPPrPort
printPort;
static TPrStatus
theStatus;
static Boolean
printFirst = TRUE;
#ifdef PROTOTYPES
/* Local Function Prototypes
*/
Boolean PrintStyle (WindowPtr theWindow);
void PrintText (TEHandle teHdl, Rect *pageRect);
#endif
/* Call the standard page setup dialog
*/
static Boolean
PrintStyle (theWindow)
register WindowPtr theWindow;
{
register Boolean result = TRUE;
/* Validate the print handle if the user has selected this already...
*/
if (printFirst || !theWindow) {
result = PrStlDialog(printHdl);
PrValidate(printHdl);
}
return result;
}
/* Print a TE record, page by page
*/
void
PrintText (teHdl, pageRect)
register TEHandle teHdl;
register Rect *pageRect;
{
register TEHandle pageTE;
register CharsHandle charHdl;
register WORD maxLines, linesInto, pageLines;
/* Create a temporary TE for imaging
*/
pageTE = TENew(pageRect, pageRect);
charHdl = TEGetText(teHdl);
TESetText(*charHdl, GetHandleSize(charHdl), pageTE);
TECalText(pageTE);
maxLines = (*pageTE)->nLines;
linesInto = 0;
pageLines = (pageRect->bottom - pageRect->top) / (*pageTE)->lineHeight;
while (TRUE) {
/* Draw this viewRect and track how many lines have been drawn
*/
TEUpdate(pageRect, pageTE);
linesInto += pageLines;
/* Go on to another page, si necessito
*/
if (linesInto < maxLines) {
PrClosePage(printPort);
PrOpenPage(printPort, NULL);
if (PrError() == noErr) {
SetPort(printPort);
/* Find the new pageRect and round it to an even number of
* lines per page
*/
*pageRect = (*printHdl)->prInfo.rPage;
pageLines = (pageRect->bottom - pageRect->top) / (*pageTE)->lineHeight;
pageRect->bottom = pageRect->top + (pageLines * (*pageTE)->lineHeight);
/* Scroll to the next page
*/
(*pageTE)->destRect = (*pageTE)->viewRect = *pageRect;
TECalText(pageTE);
TEScroll(0, linesInto * -(*pageTE)->lineHeight, pageTE);
}
}
else {
TEDispose(pageTE);
return;
}
}
}
/* Print the specified document window; pass in NULL for the window argument if
* only a Print Setup dialog is requested.
*/
void
PrintDocument (theWindow)
register WindowPtr theWindow;
{
register InfoPtr infoPtr = (InfoPtr) GetWRefCon(wPtrText);
register TEHandle teHdl = infoPtr->item.text.teHdl;
register WORD numPages, printErr;
Rect pageRect;
GrafPtr savePort;
GetPort(&savePort);
/* Unload tons of segments, since the print driver is larger than your
* worst/best nightmares of Divine after a Halloween binge
*/
/* New trip into this printing quagmire?
*/
if (printFirst)
printHdl = (THPrint) ErrNewHandle(sizeof(TPrint));
InitCursor();
PrOpen();
if (PrError() == noErr) {
PrintDefault(printHdl);
if (PrError() == noErr) {
if (PrintStyle(theWindow)) {
if (theWindow && PrJobDialog(printHdl)) {
/* get first and last pages of document to be printed
* from the iFstPage and iLastPage fields in the
* TPrJob record (IM II-151)
*/
numPages = (*printHdl)->prJob.iCopies;
printPort = PrOpenDoc(printHdl, NULL, NULL);
if (PrError() == noErr) {
for (; numPages-- > 0; ) {
PrOpenPage(printPort, NULL);
if (PrError() == noErr) {
/* rPage (IM II-150) is the printable area
* and the printer port is already open so
* just redraw your window here
*/
SetPort(printPort);
TextFont(FONTNUM);
TextSize(FONTSIZE);
pageRect = (*printHdl)->prInfo.rPage;
AnalPrint(&pageRect);
PrintText(teHdl, &pageRect);
}
PrClosePage(printPort);
}
}
PrCloseDoc(printPort);
}
}
}
}
/* Finish spool printing, if used
*/
if (theWindow && (((TPPrint) *printHdl)->prJob.bJDocLoop == bSpoolLoop) && (PrError() == noErr))
PrPicFile(printHdl, NULL, NULL, NULL, &theStatus);
/* Grab the printer errors before closing the PrintMgr and the error disappears
*/
printErr = PrError();
PrClose();
/* Don't report printing errors before falling through the print loop, to make
* sure that all of the PrintMgr's open calls get closed properly
*/
if (printErr != noErr)
SysBeep(5);
printFirst = FALSE;
SetPort(savePort);
}