/*
 * 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: PGscn.c                                      */
/* Author: Paul Gardiner.                             */
/* Function: Provides a library of functions for      */
/*           producing virtual screens.               */
/*                                                    */ 
/******************************************************/

#include "kernel.h"
#include "swis.h"
#include "stdlib.h"
#include "PGspr.h"
#include "PGwin.h"
#include "PGwinH.h"
#include "PGscn.h"
#include "PGscnH.h"

static _kernel_swi_regs regs;


static void spr_plot(void)
{
    SprPlot((spr_sprite) WinData(), 0, 0);
}



void ScnInit(char *task_name)
{
    WinInit(task_name);
}

void ScnFin(void)
{
    WinFin();
}

scn_screen ScnCreate(void)
{
    scn_screen scn;

    scn = (scn_screen) malloc(sizeof(struct scn_screen_s));
    if(scn == NULL)
        return NULL;
    scn->win = WinCreate(WinStdTplt());
    scn->spr = SprCreate(SprScreen12);
    if(scn->win == NULL || scn->spr == NULL)
    {
        ScnKill(scn);
        return NULL;
    }
    WinSetData(scn->win, (void *) scn->spr);
    WinContent(scn->win, spr_plot);
    return scn;
}

void ScnTitle(scn_screen scn, char *title)
{
    WinTitle(scn->win, title);
}

void ScnOpen(scn_screen scn)
{
    WinOpen(scn->win);
}

void ScnKill(scn_screen scn)
{
    if(scn != NULL)
    {
        WinKill(scn->win);
        SprFree(scn->spr);
        free((void *) scn);
    }
}

void ScnVDU(scn_screen scn)
{
    static scn_screen vdu_scn = NULL;

    if(vdu_scn != NULL)
    {
        int *buf;
        int xmin, ymin, xmax, ymax;

        regs.r[0] = -1;
        _kernel_swi(OS_ChangedBox, &regs, &regs);
        buf = (int *) regs.r[1];
        xmin = buf[1] << 1;
        ymin = buf[2] << 2;
        xmax = buf[3] + 1 << 1;
        ymax = buf[4] + 1 << 2;
        SprVDU(NULL);
        WinPatch(vdu_scn->win, xmin, ymin, xmax, ymax);
    }
    WinCoOp();
    if(scn != NULL)
    {
        SprVDU(scn->spr);
        regs.r[0] = 1;
        _kernel_swi(OS_ChangedBox, &regs, &regs); /* Could be part of SprVDU ??? */
        regs.r[0] = 2;
        _kernel_swi(OS_ChangedBox, &regs, &regs);
    }
    vdu_scn = scn;
}

void ScnClickS(scn_screen scn, scn_action click)
{
    WinClickS(scn->win, click);
}

void ScnClickM(scn_screen scn, scn_action click)
{
    WinClickM(scn->win, click);
}

void ScnClickA(scn_screen scn, scn_action click)
{
    WinClickA(scn->win, click);
}

void ScnPress(scn_screen scn, scn_use_char key)
{
    WinPress(scn->win, key);
}

void ScnFocus(scn_screen scn, scn_caret caret)
{
    WinFocus(scn->win, caret);
}

void ScnDrag(scn_screen scn, scn_select select)
{
    WinDrag(scn->win, select);
}

void ScnLoad(scn_screen scn, scn_load load)
{
    WinLoad(scn->win, load);
}
