/* Misc.c - miscellaneous Amiga specific routines                      */
/* Copyright (c) 1990 by J.K. Lindsey                                  */
/* Additions to XLISP-STAT 2.1 Copyright (c) 1990, by Luke Tierney     */
/* Additions to Xlisp 2.1, Copyright (c) 1989 by David Michael Betz    */
/* You may give out copies of this software; for conditions see the    */
/* file COPYING included with this distribution.                       */

#include <proto/intuition.h>
#include <proto/icon.h>
#include <workbench/workbench.h>
#include <proto/dos.h>
#include <proto/graphics.h>

#define FDwAT
#include <iff/ilbm.h>
#include "plot2.h"
#include "autil2.h"

typedef struct {ULONG ViewModes;} CamgChunk;
#define PutCAMG(context,camg) \
        PutCk(context,ID_CAMG,sizeof(CamgChunk),(BYTE *)camg)
#define CkErr(expression) {if(ifferr==IFF_OKAY)ifferr=(expression);}

SaveWindow(window,fname,leftedge,topedge,width,height)
int leftedge,topedge,width,height;
struct Window *window;
char fname[];{
   LONG file;
   BYTE buffer[512];
   BitMapHeader bmHdr;
   IFFP ifferr;
   struct BitMap *bitmap;
   struct ViewPort *viewport;
   GroupContext fileContext,formContext;
   if(!(file=Open(fname,MODE_NEWFILE)))return(1);
   Write(file,"x",1);
   viewport=&(window->WScreen->ViewPort);
   bitmap=(window->RPort)->BitMap;
   if(width%2)width++;
   if((RowBytes(width)<<3)-width<(window->LeftEdge+leftedge)%8)width+=16;
   ifferr=InitBMHdr(&bmHdr,bitmap,mskNone,cmpByteRun1,0,(short)width,
   (short)height,(window->WScreen)->Width,(window->WScreen)->Height);
   CkErr(OpenWIFF(file,&fileContext,szNotYetKnown));
   CkErr(StartWGroup(&fileContext,FORM,szNotYetKnown,ID_ILBM,&formContext));
   CkErr(PutBMHD(&formContext,&bmHdr));
   bmHdr.x=leftedge; bmHdr.y=topedge; bmHdr.w=width;
   CkErr(PutCMAP(&formContext,(WORD *)viewport->ColorMap->ColorTable,bitmap->Depth));
   CkErr(PutBODY(window,&formContext,bitmap,0,&bmHdr,buffer,512));
   CkErr(EndWGroup(&formContext));
   CkErr(CloseWGroup(&fileContext));
   Close(file);
   return(ifferr!=IFF_OKAY);}

MakeIcon(imagedata,w,h,deftool,fname,stack)
UWORD imagedata[];
int w,h,stack;
char *deftool,*fname;{ 
   static struct Image iconimage={0,0,0,0,2,0,3,0};
   static struct DiskObject iconobject={WB_DISKMAGIC,WB_DISKVERSION,0,0,0,0,0,
                 GADGHBOX|GADGIMAGE,RELVERIFY|GADGIMMEDIATE,BOOLGADGET,0,0,0,0,
                 0,0,0,4,0,0,NO_ICON_POSITION,NO_ICON_POSITION,0,0,0};
   iconimage.Width=w;
   iconimage.Height=h;
   iconimage.ImageData=&imagedata[0];
   iconobject.do_Gadget.Width=w;
   iconobject.do_Gadget.Height=h;
   iconobject.do_Gadget.GadgetRender=(APTR)&iconimage;
   iconobject.do_DefaultTool=deftool;
   iconobject.do_StackSize=stack;
   return(!PutDiskObject(fname,&iconobject));}
