/* $Id: ckmpri.c,v 1.5 91/12/15 23:17:46 rick Exp $
 * $Source: /uw/mackermit/RCS/ckmpri.c,v $
 *------------------------------------------------------------------
 * $Log:	ckmpri.c,v $
 * Revision 1.5  91/12/15  23:17:46  rick
 * ut9
 * 
 * Revision 1.4  91/10/13  13:44:33  rick
 * UT(7)
 * 
 * Revision 1.3  91/10/03  12:43:06  rick
 * UT(5)
 * 
 *------------------------------------------------------------------
 * $Endlog$
 */

/*
 * file ckmpri.c
 *
 * Module of mackermit containing code for handling printing.  This code was
 * originally put into ckmusr by John A. Oberschelp of Emory U.
 *
 */

/*
  Copyright (C) 1985, 1992, Trustees of Columbia University in the City of New
  York.  Permission is granted to any individual or institution to use this
  software as long as it is not sold for profit.  This copyright notice must be
  retained.  This software may not be included in commercial products without
  written permission of Columbia University.
*/

#include "ckcdeb.h"
#include "ckcker.h"

#include "ckmdef.h"		/* General Mac defs */
#include "ckmres.h"		/* Mac resource equates */
#include "ckmasm.h"		/* new A8 and A9 traps */
#include "ckmcon.h"		/* for fonts */
#include "ckmptp.h"		/* ckm* Prototypes */
#include "ckmwin.h"		/* text edit window structures */

int		to_printer = FALSE;
int		to_screen = TRUE;
int		printer_is_on_line_num;
#ifdef COMMENT
Handle	hPrintBuffer = 0L;
long	lPrintBufferSize;
long	lPrintBufferChars = 0L;
long	lPrintBufferAt;
#endif /* COMMENT */

Boolean printer_is_setup = false;
THPrint	PrintStuff = NULL;

extern	MenuHandle menus[];	/* handle on our menus */

extern struct cmdw *prntw;		/* local "printer" window */

DialogPtr	printingDialog;
DialogPtr	bufferingDialog;
DialogPtr	overflowingDialog;
DialogPtr	overflowedDialog;
DialogPtr	pauseDialog;

#define MIN(a,b) ((a)<(b))?(a):(b)
#define MAX(a,b) ((a)>(b))?(a):(b)

/****************************************************************************/
/*  add_to_print  --  Add string of length len to "printer" text edit record  */
/*		      Made from conxo();				    */
/****************************************************************************/
void
add_to_print (int len, char *s)
{
#ifdef COMMENT    
    register char *t;

    /* change NLs to CRs for the Mac */
    for (t = s; *t && (t - s < len); t++)
    	if (*t == LF)
	    *t = CR;
#endif

    /* debug (F101, "add_to_print here: ", s, len); */
    if ((prntw->teh == NULL) || (len <= 0))
	return;

    TEDeactivate(prntw->teh);
    if (trimcon(prntw, len)) {
    	/* put up a dialog saying that things are overflowing */
    }
    TEInsert (s, (long) len, prntw->teh);	/* insert the string */
    TESetSelect(TE_MAX, TE_MAX, prntw->teh);
    TEActivate(prntw->teh);
    rcdwscroll (prntw);		/* possibly scroll it */
    updatepstat();		/* possibly enable print menus */
}				/* add_to_print */

/*
 * Update the capturing printer output dialog
 */
void
upd_ptr_dlg()
{
    if (to_printer && !bufferingDialog) {
	bufferingDialog = GetNewDialog(BUFFERINGBOXID, NILPTR, (WindowPtr)-1);
	DrawDialog(bufferingDialog);
    } else if (!to_printer && bufferingDialog) {
	DisposDialog(bufferingDialog);
	bufferingDialog = NULL;
	if (overflowingDialog) {
	    DisposDialog(overflowingDialog);
	    overflowingDialog = NULL;
	}
    }
}

/*
 * Print the internal text capture buffer
 */
void
now_print()
{
    long length;
    CharsHandle h;
    /* CursHandle watchcurs; */

#ifdef COMMENT
    short itemhit;
	if (lPrintBufferChars >= lPrintBufferSize) {
		overflowedDialog = GetNewDialog(OVERFLOWEDBOXID, NILPTR,
						(WindowPtr) - 1);
		circleOK(overflowedDialog);

		ModalDialog ((ModalFilterProcPtr) NILPROC, &itemhit);
		DisposDialog(overflowedDialog);
		if (itemhit == 2)	/* if Cancel */
		    return;
		if (itemhit == 3) {
			DisposHandle(hPrintBuffer);
			hPrintBuffer = 0L;
			lPrintBufferChars = 0L;
			updatepstat();
			return;
		}
	}
#endif /* COMMENT */

#ifdef COMMENT
	if (lPrintBufferChars > lPrintBufferSize) {	/* if buffer wrapped */
		length = lPrintBufferSize;
		index = lPrintBufferAt;
	} else {
		length = lPrintBufferChars;
		index = 0L;
	}
	printer(hPrintBuffer, length, index, lPrintBufferSize);

	DisposHandle(hPrintBuffer);
	hPrintBuffer = 0L;
	lPrintBufferChars = 0L;
#endif /* COMMENT */

    if (!prntw) {
    	printerr("prntw is NULL!!!!", 0);
    	return;
    }
    
    length = (*prntw->teh)->teLength;
    h = TEGetText(prntw->teh);

    printer((Handle) h, length, 0L, 0x7FFFFFFFL);

    updatepstat();
}

/*
 * printer_init
 * initialize the printer stuff
 */
int
printer_init()
{
    Boolean brc;
    
    PrintStuff = (THPrint)NewHandle(sizeof(TPrint));
    PrOpen();
    if (PrError() != noErr) {
	DisposHandle((Handle) PrintStuff);
	PrintStuff = NULL;
	printerr("Could not open printer", 0);
	return 0;
    }
    brc = PrValidate(PrintStuff);	/* load reasonable defaults into PrintStuff */
    return 1;
}

/*
 * Release any resources used by the printing system.
 */
void
printer_close()
{
    if (!PrintStuff)
	return;

    PrClose();		/* close the printer file and release resources */
    DisposHandle((Handle) PrintStuff);
    PrintStuff = NULL;
}

/*
 * dopagesetup
 * Call the page setup dialog
 */
void
dopagesetup()
{
    if (!PrintStuff)
	if (!printer_init())
	    return;
    
    printer_is_setup = PrStlDialog(PrintStuff);
}

/*
 * printer
 * h - handle to text to print
 * length - number of chars in buffer
 * index - starting index in buffer
 * limit - buffer limit (to wrap index)
 */
void
printer (Handle h, long length, long index, long limit)
{
    char	PrintBufferChar;
    CursHandle	watchcurs;
    int		typeOfDriver;
    int		chrExtra;
    int		leftMargin;
    GrafPtr	oldPort;
    int		temp;
    TPPrPort	myPrPort;
    TPrStatus	myStRec;

    if (!PrintStuff)		/* make sure we have enabled printer stuff */
	if (!printer_init())
	    return;

    if (!printer_is_setup)	/* give the user a chance to set page settings */
	dopagesetup();

    GetPort(&oldPort);

    temp = PrJobDialog(PrintStuff);	/* do print job dialog */
    if (!temp) {
	PrClose();
	SetPort(oldPort);
	return;
    }
    printingDialog = GetNewDialog(PRINTINGBOXID, NILPTR, (WindowPtr) - 1);
    DrawDialog (printingDialog);
    watchcurs = GetCursor(watchCursor);
    SetCursor(*watchcurs);

    myPrPort = PrOpenDoc(PrintStuff, nil, nil);
    typeOfDriver = ((*PrintStuff)->prStl.wDev) >> 8;
    if (typeOfDriver == 3) {		 /* PostScript (laser) */
	TextFont(courier);
	TextSize(10);
	chrExtra = 0;
	leftMargin = 36;
    } else {
	TextFont(VT100FONT);
	TextSize(9);
	chrExtra = 1;
	leftMargin = 36;
    }

    printer_is_on_line_num = 1;
    PrOpenPage(myPrPort, nil);
    MoveTo(leftMargin, 1 * 12);

    do {
	if (PrError() != noErr) 
	    break;
	PrintBufferChar = (*h)[index];
	switch (PrintBufferChar) {
	  case 015:		/* CR */
	    if (++printer_is_on_line_num > 60) {
		PrClosePage(myPrPort);
		PrOpenPage(myPrPort, nil);
		printer_is_on_line_num = 1;
	    }
	    MoveTo(leftMargin, printer_is_on_line_num * 12);
	    break;

	  case 014:		/* FF */
	    if (printer_is_on_line_num != 1) {
        	PrClosePage(myPrPort);
        	PrOpenPage(myPrPort, nil);
        	printer_is_on_line_num = 1;
	    }
	    MoveTo(leftMargin, printer_is_on_line_num * 12);
	    break;

	  default:
	    DrawChar(PrintBufferChar);
	    Move(chrExtra, 0);
	    break;
	}

	if (++index == limit)    /* if time to wrap buffer */
	    index = 0L;

    } while (--length);
    PrClosePage(myPrPort);

    PrCloseDoc(myPrPort);
    if ((PrError() == noErr) &&
	((**PrintStuff).prJob.bJDocLoop == bSpoolLoop))
	PrPicFile(PrintStuff, nil, nil, nil, &myStRec);
    if ((PrError() != noErr) && (PrError() != 128)) {
	printerr ("Printer error: ", PrError());
    }

    DisposDialog(printingDialog);

    SetPort(oldPort);
    InitCursor();
}


void
pr_stat()
{
    DialogPtr	printDialog;
    Str255	arg1, arg2, arg3;
    long	length;
    short	itemhit;


    printDialog = GetNewDialog(PRINTBOXID, NILPTR, (WindowPtr) - 1);
    circleOK(printDialog);

#ifdef COMMENT
    NumToString(MAX(lPrintBufferChars - lPrintBufferSize, 0L), arg1);
    NumToString(MIN(lPrintBufferSize, lPrintBufferChars), arg2);
    NumToString(lPrintBufferSize, arg3);
    ParamText (arg1, arg2, arg3, "");
#endif
    length = (*prntw->teh)->teLength;

    /* NumToString(MAX(lPrintBufferChars - lPrintBufferSize, 0L), arg1); */
    arg1[0] = 1;
    arg1[1] = '?';				/* chars lost */
    NumToString(length, arg2);			/* chars buffered */
    NumToString(TE_TOOBIG, arg3);		/* buffer size */
    ParamText (arg1, arg2, arg3, "");

    do {
	ModalDialog ((ModalFilterProcPtr) NILPROC, &itemhit);

	switch (itemhit) {
	  case 1:
	  case 2:
	  case 3:
	  break;

	}
    } while (itemhit > 3);

    DisposDialog(printDialog);
}


/*
 * Junk so Emacs will set local variables to be compatible with Mac/MPW.
 * Should be at end of file.
 * 
 * Local Variables:
 * tab-width: 8
 * End:
 */
