

/*
 * do_print : dump the window to the printer
 */

#include <stdio.h>
#include "defines.h"

long show = FALSE; 
long density = DENSITYDEF; 

extern long screenpos;
extern struct Window	*Wind ;
extern struct RastPort *rp;
extern struct IntuiText *prfailtxt, oktxt;

union printerIO {
	struct IOStdReq ios;
	struct IODRPReq iodrp;
	struct IOPrtCmdReq iopc;
	};

extern union printerIO *CreateExtIO();
extern struct MsgPort *CreatePort();

long special_dens[7] = { SPECIAL_DENSITY1, SPECIAL_DENSITY2, 
	SPECIAL_DENSITY3, SPECIAL_DENSITY4, SPECIAL_DENSITY5, 
	SPECIAL_DENSITY6, SPECIAL_DENSITY7 };

do_print()
{
struct IntuiMessage *GetMsg();
struct IntuiMessage *message;	/* the message from the IDCMP */
union printerIO *request;
struct MsgPort *printerPort;
struct ViewPort *vp;
struct Screen *sc;
UWORD code;        /* important info in message */
ULONG class;
APTR object;
int hite;

	if (show)
		{

		while(1)
			{
			WaitPort(Wind->UserPort);
				while( (message = (struct IntuiMessage *)
					GetMsg(Wind->UserPort) ) != NULL)
					{
					code = message->Code;  /* MENUNUM */
					object = message->IAddress;  /* Gadget */
					class = message->Class;  /* IDCMP Flags */
					ReplyMsg(message);  /* Free the sender */
					switch (class)
						{
						case CLOSEWINDOW :
							done(0);   /* close gadget clicked */
							break;
						case MOUSEBUTTONS :
							if (code == SELECTUP) return;
						}
					}
			}
		}

	/* set up for dump rastport request */
	printerPort = CreatePort("myprport",0L);
	request = CreateExtIO(printerPort, sizeof(union printerIO));
	if (OpenDevice("printer.device",0L,request,0L) !=0)
		{
		AutoRequest(Wind,&prfailtxt,0L,&oktxt,0L,0L,300L,75L);
		goto cleanup;
		}

	if (screenpos > Wind->Height) hite = Wind->Height-SCREENTOP;
	else hite = screenpos-SCREENTOP;

	request->iodrp.io_Command = PRD_DUMPRPORT;
	request->iodrp.io_RastPort = rp;
	sc = Wind->WScreen;
	vp = &sc->ViewPort;
	request->iodrp.io_ColorMap = vp->ColorMap;
	request->iodrp.io_Modes = vp->Modes;
	request->iodrp.io_RastPort = rp;
	request->iodrp.io_SrcX=2L;
	request->iodrp.io_SrcY=SCREENTOP;
	request->iodrp.io_SrcWidth=(long)Wind->Width-3;
	request->iodrp.io_SrcHeight=(long) hite;
	request->iodrp.io_DestCols=0L;
	request->iodrp.io_DestRows=0L;
	request->iodrp.io_Special=SPECIAL_ASPECT | SPECIAL_FULLCOLS;
	request->iodrp.io_Special |= special_dens[density-1];
	DoIO(request);
	CloseDevice(request);
  cleanup:
	DeleteExtIO(request, sizeof(union printerIO));
	DeletePort(printerPort);

	/* check for close gadget */
	while( (message = (struct IntuiMessage *)
		GetMsg(Wind->UserPort) ) != NULL)
		{
		class = message->Class;  /* IDCMP Flags */
		ReplyMsg(message);  /* Free the sender */
		switch (class)
			{
			case CLOSEWINDOW :
				done(0);   /* close gadget clicked */
				break;
			}
		}
}
