/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "Licence").
 * You may not use this file except in compliance with the Licence.
 *
 * You can obtain a copy of the licence at RISC OS path @.^.LICENCE
 * or  http://www.riscosdev.com/lanman98/LICENCE.CDDL
 * See the Licence for the specific language governing permissions
 * and limitations under the Licence.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the Licence file. If applicable, add the
 * following below this CDDL HEADER, with the fields enclosed by
 * brackets "[]" replaced with your own identifying information:
 * Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
 
/*
 *   Portions Copyright 1996 Warm Silence Software Ltd.
 */

/******************************************************/
/*                                                    */
/* Name: PGprt.c                                      */
/* Author: Paul Gardiner.                             */
/* Function: Printing                                 */
/*                                                    */
/******************************************************/

#include "stdio.h"
#include "kernel.h"
#include "swis.h"

#include "PGerr.h"
#include "PGwin.h"
#include "PGwinH.h"
#include "PGprt.h"

#define MPPI               (72000)
#define UNIT                 (400)
#define FP               (0x10000)

static _kernel_swi_regs regs;


void PrtArea(int *xsize, int *ysize)
{
    int res;

    _kernel_swi(PDriver_Info, &regs, &regs);
    res = regs.r[1] < regs.r[2] ? regs.r[1] : regs.r[2];
    if(res ==  0) return;
    _kernel_swi(PDriver_PageSize, &regs, &regs);
    *xsize = (regs.r[6] - regs.r[4]) / (MPPI / res);
    *ysize = (regs.r[5] - regs.r[3]) / (MPPI / res);
}

void PrtPrint(prt_draw draw)
{
    _kernel_oserror *err;
    int file;
    int plx, ply, phx, phy;
    int res;
    int fact;
    int page[4];
    int tran[4];
    int pos[2];

    _kernel_swi(PDriver_Info, &regs, &regs);
    res = regs.r[1] < regs.r[2] ? regs.r[1] : regs.r[2];
    if(res ==  0) return;
    _kernel_swi(PDriver_PageSize, &regs, &regs);
    plx = regs.r[3]; ply = regs.r[4]; phx = regs.r[5]; phy = regs.r[6];
    pos[0] = phx; pos[1] = ply;
    fact = MPPI / UNIT * FP / res;
    tran[0] = 0; tran[1] = fact; tran[2] = -fact; tran[3] = 0;
    page[0] = page[1] = 0;
    page[2] = (phy - ply) / UNIT * FP / fact;
    page[3] = (phx - plx) / UNIT * FP / fact;
    regs.r[0] = 0x83;
    regs.r[1] = (int) "printer:";
    regs.r[2] = 0;
    err = _kernel_swi(OS_Find, &regs, &regs);
    file = regs.r[0];
    if(err || file == 0)
    {
        Error("Printers not loaded");
        return;
    }
    regs.r[1] = 0;
    err = _kernel_swi(PDriver_SelectJob, &regs, &regs);
    regs.r[0] = 0;
    regs.r[1] = 0;
    regs.r[2] = 0;
    if(!err) err = _kernel_swi(PDriver_DeclareFont, &regs, &regs);
    regs.r[0] = 0;
    regs.r[1] = (int) page;
    regs.r[2] = (int) tran;
    regs.r[3] = (int) pos;
    regs.r[4] = (int)0xFFFFFF00;
    if(!err) err = _kernel_swi(PDriver_GiveRectangle, &regs, &regs);
    regs.r[0] = 1;
    regs.r[1] = (int) &xyzREDRAW_AREAxyz;
    regs.r[2] = 0;
    regs.r[3] = 0;
    if(!err) err = _kernel_swi(PDriver_DrawPage, &regs, &regs);
    while(regs.r[0] != 0 && !err)
    {
        draw();
        err = _kernel_swi(PDriver_GetRectangle, &regs, &regs);
    }
    regs.r[0] = file;
    if(!err) err = _kernel_swi(PDriver_EndJob, &regs, &regs);
    if(err)
    {
        regs.r[0] = file;
        _kernel_swi(PDriver_AbortJob, &regs, &regs);
        Error("Print job aborted");
    }
    regs.r[0] = 0;
    regs.r[1] = file;
    _kernel_swi(OS_Find, &regs, &regs);
}
