/**PrintPix.c************************************************************
 *									*
 *   SeePix -- by Hank Schafer						*
 *									*
 *   SeePix is an IFF Picture Viewer.  It works with Lo-Res,		*
 *   Med-Res, Hi-Res, HAM, and EHB formats.  I'm working on support     *
 *   for X-Specs, and Anim5 formats.					*
 *									*
 *   SeePix is based on a program by Olaf Barthel, called		*
 *   'LoadImage'.  As released, LoadImage had a couple of bugs. 	*
 *   The Amiga-Key alternatives to menu use didn't all work.  That's	*
 *   been fixed.  There were two separate print functions in LoadImage	*
 *   which behaved exactly the same.  The redundancy was eliminated.	*
 *   LoadImage used the Topaz ROMFONT, and made no allowances for a	*
 *   different system default font (under 2.04).  I added a built-in	*
 *   font.  I've reworked the code considerably from the original       *
 *   release, to allow for optimizations based on time and space.	*
 *									*
 *   SeePix features a palette tool which allows "tweaking" of colors	*
 *   prior to printing, allowing you to modify the color printout to	*
 *   more closely resemble the original graphics (Blue is Blue, not	*
 *   Purple).  SeePix can be Iconified.  SeePix features an ARP 	*
 *   interface.  SeePix now uses the PathMaster File Selector.		*
 *									*
 ************************************************************************
 *                                                                      *
 *   SeePix Copyright © 1992 by Hank Schafer; all rights reserved.      *
 *                                                                      *
 *   This program is free software; you can redistribute it and/or      *
 *   modify it under the terms of the GNU General Public License as     *
 *   published by the Free Software Foundation, either version 1, or    *
 *   (at your option) any later version.                                *
 *                                                                      *
 *   This program is distributed in the hope that it will be useful,    *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of     *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU  *
 *   General Public License for more details.                           *
 *                                                                      *
 *   You should have received a copy of the GNU General Public License  *
 *   along with this program; if not, write to:                         *
 *                 Free Software Foundation, Inc.                       *
 *                 675 Massachusetts Ave.                               *
 *                 Cambridge  MA  02139, USA                            *
 *                                                                      *
 **************** Special Function Copyright Notices ********************
 *									*
 ****LoadImage Copyright Notice:					*
 *									*
 *   LoadImage is © Copyright 1988, 1989, 1990 by MXM, all rights	*
 *   reserved, written by Olaf Barthel.  No guarantees of any kind are	*
 *   made that this program is 100% reliable.  Use this program on	*
 *   your own risk!							*
 *									*
 ****Iconify Copyright Notice:						*
 *									*
 *   Copyright 1987 by Leo L. Schwab.					*
 *   Permission is hereby granted for use in any and all programs,	*
 *   both Public Domain and commercial in nature, provided this 	*
 *   Copyright notice is left intact.					*
 *									*
 ****ColorWindow (Palette) Copyright Notice:				*
 *									*
 * ColorWindow Routine	--  Color Window Routines			*
 *     from Book 1 of the Amiga Programmers' Suite by RJ Mical          *
 *									*
 * Copyright (C) 1986, 1987, Robert J. Mical				*
 * All Rights Reserved. 						*
 *									*
 ****PathMaster Copyright Notice:					*
 *									*
 * -------------------------------------------------------------------- *
 *   Copyright © 1989 Justin V. McCormick.  All Rights Reserved.	*
 * -------------------------------------------------------------------- *
 *									*
 *    The PathMaster name is a trademark of Justin V. McCormick.	*
 *									*
 *   PathMaster File Selector source and documentation written by:	*
 *									*
 *			 Justin V. McCormick.				*
 *	       Copyright © 1989 by Justin V. McCormick. 		*
 *			 All Rights Reserved.				*
 *									*
 * -------------------------------------------------------------------- *
 ************************************************************************
 *									*
 *   Hope this is something you can use and enjoy.			*
 *									*
 ************************************************************************/

/*
 * PrintPix()
 *
 * Sends the current SeePix screen to the printer.
 */

extern struct Screen *Screen;
extern struct Window *Window;

VOID
PrintPix()
{
    struct IODRPReq *PrinterDump;
    struct MsgPort *PrinterPort;

    struct IntuiMessage *Message;

    ULONG	    Class;
    UWORD	    Code;

    UBYTE	    HasTitle = (Screen->Flags & SHOWTITLE) ? TRUE : FALSE;
    UBYTE	    Cycling = IsCycle();

    LONG	    StartX, StartY, Width, Height;

    volatile LONG   OldX = -1, OldY = -1, TempX, TempY, X, Y;

    SetSnooze(Window);

    /* IO Replyport. */

    if (PrinterPort = (struct MsgPort *) CreatePort(NULL, 0)) {

	/* Custom RastPort dump structure. */

	if (PrinterDump = (struct IODRPReq *) CreateExtIO(PrinterPort, sizeof(struct IODRPReq))) {

	    /* Try to open the device. */

	    if (!OpenDevice("printer.device", 0, PrinterDump, 0)) {
		SetSize(Window);
		SetDrMd(Window->RPort, COMPLEMENT);

		/* Don't let anyone disturb the SeePix screen bitmap. */

		Window->Flags |= RMBTRAP;

		if (HasTitle)
		    ShowTitle(Screen, FALSE);

		if (Cycling)
		    ToggleCycle();

		/* Let the user set the initial point. */

		FOREVER
		{
		    WaitPort(Window->UserPort);

		    if (Message = (struct IntuiMessage *) GetMsg(Window->UserPort)) {
			Class = Message->Class;
			Code = Message->Code;

			ReplyMsg(Message);
		    }
		    if (Class == MOUSEBUTTONS && Code == SELECTDOWN) {

			X = Window->MouseX;
			Y = Window->MouseY;

			break;
		    }
		}

		/* Now lets resize the rubber box to the right dimensions. */

		ReportMouse(TRUE, Window);

		FOREVER
		{
		    UWORD	      Qualifier;

		    TempX = Window->MouseX;
		    TempY = Window->MouseY;

		    if (TempX < 0)
			TempX = 0;

		    if (TempY < 0)
			TempY = 0;

		    if (TempX > Window->Width - 1)
			TempX = Window->Width - 1;

		    if (OldX >= 0 && OldY >= 0) {
			Move(Window->RPort, X, Y);
			Draw(Window->RPort, OldX, Y);
			Draw(Window->RPort, OldX, OldY);
			Draw(Window->RPort, X, OldY);
			Draw(Window->RPort, X, (OldY >= Y) ? Y + 1 : Y - 1);
		    } else
			OldX = OldY = 0;

		    Move(Window->RPort, X, Y);
		    Draw(Window->RPort, TempX, Y);
		    Draw(Window->RPort, TempX, TempY);
		    Draw(Window->RPort, X, TempY);
		    Draw(Window->RPort, X, (TempY >= Y) ? Y + 1 : Y - 1);

		    OldX = TempX;
		    OldY = TempY;

		  WaitLoop:
		    WaitPort(Window->UserPort);

		    if (Message = (struct IntuiMessage *) GetMsg(Window->UserPort)) {
			Class = Message->Class;
			Code = Message->Code;
			Qualifier = Message->Qualifier;

			ReplyMsg(Message);
		    }
		    if ((Class == MOUSEBUTTONS && Code == MENUDOWN) || Class == RAWKEY) {
			DisplayBeep(Screen);

			Move(Window->RPort, X, Y);
			Draw(Window->RPort, TempX, Y);
			Draw(Window->RPort, TempX, TempY);
			Draw(Window->RPort, X, TempY);
			Draw(Window->RPort, X, (TempY >= Y) ? Y + 1 : Y - 1);

			goto HitAndQuit;
		    }
		    if (Class == MOUSEBUTTONS && Code == SELECTUP) {
			Move(Window->RPort, X, Y);
			Draw(Window->RPort, TempX, Y);
			Draw(Window->RPort, TempX, TempY);
			Draw(Window->RPort, X, TempY);
			Draw(Window->RPort, X, (TempY >= Y) ? Y + 1 : Y - 1);

			StartX = (X < TempX) ? X : TempX;
			StartY = (Y < TempY) ? Y : TempY;

			Width = ABS(TempX - X) + 1;
			Height = ABS(TempY - Y) + 1;

			if (Width < 2 || Height < 2)
			    goto HitAndQuit;

			break;
		    }
		}

		/* Initialize the IO Request for a RastPort dump. */

		PrinterDump->io_Command = PRD_DUMPRPORT;
		PrinterDump->io_RastPort = &Screen->RastPort;
		PrinterDump->io_ColorMap = Screen->ViewPort.ColorMap;
		PrinterDump->io_Modes = Screen->ViewPort.Modes;
		PrinterDump->io_SrcWidth = Width;
		PrinterDump->io_SrcHeight = Height;
		PrinterDump->io_SrcX = StartX;
		PrinterDump->io_SrcY = StartY;

		{
		    PrinterDump->io_DestCols = 0;
		    PrinterDump->io_Special = SPECIAL_FULLCOLS | SPECIAL_ASPECT;
		}

		SetSnooze(Window);

		/* Try the dump. */

		if (DoIO(PrinterDump))
		    DisplayBeep(Screen);

		/* Re-enable the window. */

	      HitAndQuit:
		Window->Flags &= ~RMBTRAP;

		SetPoint(Window);

		/* If set, re-enable the screen title. */

		if (HasTitle)
		    ShowTitle(Screen, TRUE);

		if (Cycling)
		    ToggleCycle();

		ReportMouse(FALSE, Window);

		CloseDevice(PrinterDump);
	    } else
		DisplayBeep(Screen);

	    DeleteExtIO(PrinterDump);
	} else
	    DisplayBeep(Screen);

	DeletePort(PrinterPort);
    } else
	DisplayBeep(Screen);

    SetPoint(Window);
}
