/* ->c.pic */
/* picture box handling Draw code */


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "os.h"
#include "bbc.h"
#include "h.sprite"
#include "h.wimp"
#include "h.werr"
#include "h.flex"


#include "h.DrawLevel0"


#include "h.wos"
#include "h.ram"
#include "h.file"
#include "h.DrawLib"

#include "h.pan"

#include "h.pic"



/****************************************************************************/
/*                              Render a picture box                        */



void picrend(wimp_box * box,Draw_diag diag,wimp_redrawstr * redrawstr)
{
 Draw_redrawstr r;
 double scale=1.0;
 double sx1=1.0;
 double sx2=1.0;
 Draw_error e;
 int gx0;
 int gy0;
 int gx1; 
 int gy1;
 int ox;
 int oy;


 if(!diag.length || !diag.data) return;      /* no buffer do nothing */


 ox=redrawstr->box.x0-redrawstr->scx;
 oy=redrawstr->box.y1-redrawstr->scy;


 gx0=ox+box->x0;  
 gy0=oy-box->y0;  
 gx1=ox+box->x1;  
 gy1=oy-box->y1;


 if(redrawstr->g.x0>gx0) r.g.x0=redrawstr->g.x0;
 else                    r.g.x0=gx0;

 if(redrawstr->g.x1<gx1) r.g.x1=redrawstr->g.x1;
 else                    r.g.x1=gx1;

 if(redrawstr->g.y0>gy0) r.g.y0=redrawstr->g.y0;
 else                    r.g.y0=gy0;

 if(redrawstr->g.y1<gy1) r.g.y1=redrawstr->g.y1;
 else                    r.g.y1=gy1;


 Draw_queryBox(diag, &r.box, TRUE);
 sx1=((double)(gx1-gx0))/((double)(r.box.x1 -r.box.x0 ));
 sx2=((double)(gy1-gy0))/((double)(r.box.y1 -r.box.y0 ));
 if(sx1>sx2) scale=sx2; else scale=sx1;


 r.scx = (r.box.x0 - gx0) + (int)((double)r.box.x0*scale);
                                             /* (ox+osunits(box->x)); */
 r.scy = (r.box.y1 - gy0) + (int)((double)r.box.y0*scale);
                                     /* (oy-osunits(yps+box->y+box->h)); */

 if (Draw_verify_diag(diag, &e))
 {
  /* Draw a box and set the clipping region */

  bbc_gwindow(r.g.x0,r.g.y0,r.g.x1-deltax,r.g.y1-deltay);

  if(!Draw_render_diag(diag,&r,scale,scale,&e))
  {
   if (e.type == DrawOwnError)
         werr(0,"render failed: code %d at %d\n",
                e.err.draw.code, e.err.draw.location);
      else
        werr(0,"render failed: os error %s\n", e.err.os.errmess);

   /*   vecopen();
      vecclose(); */
    }
  }
  else
  {
    werr(0,"verify failed: code %d at %d\n",
                     e.err.draw.code, e.err.draw.location);
   /* vecopen();
    vecclose(); */
  }

 bbc_gwindow(redrawstr->g.x0,redrawstr->g.y0,
                                redrawstr->g.x1-deltax,redrawstr->g.y1-deltay);
}




/***************************************************************************/
/* boot draw file code                                                     */

void picinit(void)
{
 Draw_registerMemoryFunctions(flex_alloc,flex_extend,flex_free);
}


/*****************************************************************************/


int saveasdraw(char * filename)
{ 
 FILE * fp;
 int    realfile;
 int    code;

 if(!drawdiag.length || !drawdiag.data) return(0);

 fp=ropen(filename,"wb");
 if(!fp) return(0);

 rwrite(drawdiag.data,1,drawdiag.length,fp);
 realfile=!ramfp(fp);

 code=rerror(fp);

 if(code) rclose(fp);
 else     code=rclose(fp);

 if(realfile)
 {
  setftype(filename,DRAW);
  if(code) remove(filename);
 }

 return(!code);
}


