/*-- AutoRev header do NOT edit!
*
*   Program         :   Print.c
*   Copyright       :   Copyright © 1991-92 Jaba Development
*   Author          :   Jan van den Baard
*   Creation Date   :   05-Apr-92
*   Current version :   2.0
*   Translator      :   Dice v2.06.40
*
*   REVISION HISTORY
*
*   Date          Version         Comment
*   ---------     -------         ------------------------------------------
*   05-Apr-92     2.0             Printer routines. (rewrite)
*
*-- REV_END --*/

#include "View.h"

Prototype void PrintFile( void );
Prototype long CheckPrinter( void );
Prototype __geta4 void PrintProc( void );

extern struct Screen            *vwScreen;
extern struct Window            *vwWindow;
extern struct RastPort          *vwRPort;
extern struct AsciiText         *vwText;
extern struct Line              *vwFirst, *vwLast;
extern struct TextFont          *vwFont;
extern struct TextBlock          vwBlocks[];
extern UWORD                     vwPMark, vwPMode;
extern UBYTE                     vwPrinting;
extern ULONG                     vwClass;
#ifdef __DETACH
extern ULONG                    *vwArgs;
#else
extern ULONG                     vwArgs[];
#endif

extern __far struct Custom       custom;

void PrintFile( void )
{
    if ( ! vwText )     return;

    if ( vwPMode == PRT_BLOCK ) {
        if ( ! CheckBlock( vwPMark ))
            return;
    }

    if ( ! CreateNewProcTags( NP_Entry,     PrintProc,
                              NP_StackSize, 4096L,
                              NP_Name,      "ViewPrinting",
                              NP_Cli,       TRUE,
                              TAG_DONE ))
        Error( "Can't start the printing process." );
    else {
        while( ! FindTask( "ViewPrinting" ));
        vwPrinting = TRUE;
    }
}

long CheckPrinter( void )
{
    struct Data {
        UBYTE   LSB, MSB;
    } data;

    struct MsgPort  *port;
    struct IOStdReq *pio;
    UBYTE           *ptr;
    long             ret = TRUE;
    UBYTE            ch = FALSE;

    if ( port = CreateMsgPort()) {
        if ( pio = CreateStdIO( port )) {
            if ( ! OpenDevice( "printer.device", NULL, ( struct IORequest * )pio, NULL )) {

                retry:

                pio->io_Data    =   (APTR)&data;
                pio->io_Command =   PRD_QUERY;

                DoIO(( struct IORequest * )pio );

                if (( data.LSB & 1 ) || ( data.LSB & 2 )) {

                    if( vwArgs[ 11 ] )  {
                        vwArgs[ 11 ] = NULL;
                        ch = TRUE;
                        ON_SPRITE;
                    }

                    if ( rtEZRequestTags( "Your printer isn't online or\nyou must insert paper.",
                                      "Retry|Cancel",
                                      NULL,
                                      NULL,
                                      RT_Window,      vwWindow,
                                      RT_ReqPos,      REQPOS_CENTERSCR,
                                      RTEZ_Flags,     EZREQF_CENTERTEXT,
                                      RTEZ_ReqTitle,  CX_NAME ": Printer trouble...",
                                      TAG_DONE ))
                        goto retry;
                    else
                        ret = FALSE;
                }
                CloseDevice(( struct IORequest * )pio );
            }
            DeleteIORequest(( struct IORequest * )pio );
        }
        DeleteMsgPort( port );
    }

    if ( ptr = AllocMem( 102857600, MEMF_ANY ))
        FreeMem( ptr, 102857600 );

    if ( ch ) {
        vwArgs[ 11 ] = ~0;
        OFF_SPRITE;
    }

    return( ret );
}

__geta4 void PrintProc( void )
{
    struct BuffIO           *pfile = NULL;
    struct Window           *pwin  = NULL;
    struct Line             *l, *t;
    UBYTE                   *title = "Printing...";

    if ( ! CheckPrinter())
        goto CleanExit;

    if ( pfile = BOpen( "PRT:", MODE_NEWFILE )) {
        if ( pwin = OpenWindowTags( NULL, WA_Width,         TextLength( vwRPort, title, 11 ) + 20,
                                          WA_Height,        vwScreen->WBorTop + vwFont->tf_YSize + 1,
                                          WA_Title,         title,
                                          WA_IDCMP,         IDCMP_CLOSEWINDOW,
                                          WA_Flags,         WFLG_SMART_REFRESH | WFLG_CLOSEGADGET | WFLG_DRAGBAR  | WFLG_RMBTRAP,
                                          WA_CustomScreen,  vwScreen,
                                          TAG_DONE )) {
            switch( vwPMode ) {
                case    PRT_PAGE:
                    l = vwFirst;
                    t = vwLast;
                    break;

                case    PRT_FILE:
                    l = vwText->First;
                    t = vwText->Last->Next;
                    break;

                case    PRT_BLOCK:
                    l = vwBlocks[ vwPMark ].TopLine;
                    t = vwBlocks[ vwPMark ].BottomLine;
                    break;
            }

            for ( ; l != t; l = l->Next ) {
                if ( ReadMsgPort( pwin->UserPort )) {
                    if ( vwClass == IDCMP_CLOSEWINDOW )
                        goto CleanExit;
                }
                BPutS( pfile, l );
            }
        } else
            Error( "Could not open the printer window." );
    } else
        Error( "Printer trouble." );

CleanExit:
    if ( pwin )     CloseWindow( pwin );
    if ( pfile )    BClose( pfile );
    vwPrinting = FALSE;
}
