/*->c.file */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <ctype.h>
#include <time.h>

#include "h.os"
#include "h.wimp"
#include "h.werr"

#include "h.Drawlevel0"


#include "h.wos"
#include "h.main"
#include "h.ram"
#include "h.mym"
#include "h.mytrace"
#include "h.key"
#include "h.pic"

#include "h.file"


/****************************************************************************/
                       /* code for dragging save icons */


int savetype;             /* type of save operation that is pending */
int saveftype;            /* file type for the above                */
int saveref;              /* reference for save message             */
int internalsave=0;       /* set for internal saves                 */




int savefiletype(void)
{
 return(saveftype);
}


void setsavetype(int type)
{
 savetype=type;
}

void setupsavetypes(int stype,int ftype)
{
 savetype=stype;
 saveftype=ftype;
}


void writetypes(char * name,int type)
{
 char string[32];
 int  handle;

 saveftype=type;
 sprintf(string,"file_%03x",type);

 if((handle=whandle[SAVEFILE0])!=0)
 {
  writeicon(handle,1,name);
  writeicon(handle,0,string);
 }
}



/* this routine, tells savefile what type of save operation is taking place */
/* it is called by the dynamic menus                                        */


int setsave(int m2)
{
 int handle=createwindow(SAVEFILE0);

 setsavetype(m2);

 switch(m2)
 {
  case   SAVEASDRAW:
                    writetypes("DrawFile",DRAW);
                    break;

  case   SAVESPRITE:
                    writetypes("SpriteFile",SPRITE);
                    break;

 }

 return(handle);
}



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


void opensave(int savetype)
{
 int handle;
 handle=setsave(savetype);
 menuwindow(handle);
}


void closesave(int ok)
{
 ok=0;
 zapmenu();
 closedownt(SAVEFILE0);
}


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


void sendsave(int filetype,char * filename)
{
 char * p;
 wimp_msgstr msg;

 /* assumes mhandle etc. are valid here */

 p=leaf(filename);
 if(strlen(p)>10) {/* error */ }

 msg.hdr.size=((48+12) & 0xFFFFFFFC);
 msg.hdr.task=taskhandle;
 msg.hdr.your_ref=0;
 msg.hdr.action=1;

 msg.data.datasave.w=mhandle;
 msg.data.datasave.i=micon;
 msg.data.datasave.x=mousex;
 msg.data.datasave.y=mousey;
 msg.data.datasave.estsize=1;
 msg.data.datasave.type=filetype;
 strcpy(msg.data.datasave.leaf,p);

 wimp_sendwmessage(17,&msg,mhandle,micon);

 saveref=msg.hdr.my_ref;
}


void savekey(int * key) 
{
 if(*key==13) 
 {
  if(savefile(iconaddr(whandle[SAVEFILE0],1)))
  {
   closesave(1);
  }
  else return;
 }
 else
 if(*key!=27) return;
 closesave(0);
 *key=-1;
}


void savedrag(void)
{
 dragicon(mousex,mousey,ewindow,icon);
}


void saveicon(void)
{
 if(icon==2) 
 {
  if(!savefile(iconaddr(whandle[SAVEFILE0],1))) return;
  if(buttons & 0x4) zapmenu();
 }
 else
 if(icon==0 && (buttons==0x40 || buttons==0x10)) savedrag();
}



void savedragend(void)
{
 getpointer();


 /* find out if we are loading stuff back into our selves */

 if(mhandle==whandle[SAVEFILE0]) return;
 else
 sendsave(savefiletype(),iconaddr(whandle[SAVEFILE0],1));
}



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

/* try to save file */
/* return 1 on OK or 0 on failure */

int savefile(char * filename)
{
 int code;
 int ram=!filename;

 if(!filename)   /* we are doing a ram save */
 {
  filename=iconaddr(whandle[SAVEFILE0],1);
 }
 else
 if(
    strlen(leaf(filename))==strlen(filename) && 
    cstrcmp(filename,"<Wimp$Scrap>") &&
    cstrcmp(filename,"<Printer$Scrap>")
   )
 {
  errorbox("To save, drag the icon to a directory viewer.");
  return(0);
 }


 switch(savetype)
 {

  case SAVEASDRAW:
                  code=saveasdraw(filename);
                  break;

  case SAVESPRITE:
                  code=savesprite(filename);
                  break;

 }

 if(!ram && !code) werr(0,"Error writing file:%s",kerror());
 return(code);
}



void savetypeclose(FILE * fp,char * filename)
{
 int wasram=ramfp(fp);

 rclose(fp);

 if(!wasram)
 {
  setftype(filename,saveftype);
 }
}




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


/* a drag from our save box has ended on a printer driver */

void printfilepd(void)
{
 int filetype;
 int action=wimp_MWillPrint;
 int doprint;

 char * name=wimpevent.data.msg.data.print.name;

 if(!(buttons & 0x1)) zapmenu();

 doprint=0;

 switch(savetype)
 {
 

                        /* group 1, these can never be printed just get out */

/*         case SAVEASGRID:
         case  SAVEDTEXT:
                         break; */

                          /* we print these ourselves */
/*         case SAVEVTBUFF:
       case SAVEVTSELECT:
       case SAVEVXCFRAME:       
      case SAVEVXCSPRITE:
        case SAVEVXCTEXT: 
      case SAVETEKSCREEN:
                         doprint=1;
                         break;    */

              

                          /* group of just carry on and do it */



         case SAVEASDRAW:
                         filetype=DRAW;
                         if(saveasdraw(name)) action=3;
                         break;

         case SAVESPRITE:
                         filetype=SPRITE;
                         if(savesprite(name)) action=3;
                         break;

                 default:
                         return;

 }

 wimpfirst.data.msg.hdr.your_ref=wimpfirst.data.msg.hdr.my_ref;
 wimpfirst.data.msg.hdr.action=action;
 wimpfirst.data.msg.data.print.type=filetype;
 wimp_sendmessage(17,&wimpfirst.data.msg,wimpfirst.data.msg.hdr.task);


 if(doprint)
 {
  switch(savetype)
  {

#ifdef NEVER
         case SAVEVTBUFF:
                         printbuffer();
                         break;

       case SAVEVTSELECT:
                         printzone();
                         break;

       case SAVEVXCFRAME:       
      case SAVEVXCSPRITE:
                         printframe(&vscr);
                         break;

        case SAVEVXCTEXT: 
                         vxtextdump(&vscr);
                         break;

      case SAVETEKSCREEN:
                         tekdumpscreen(0); /* nb using a script fn */
                         break;

#endif

  }
 }
}





/*****************************************************************************/
/* Zoom boxes */


static zoomer * czoom;
static dozoom   exzoom;
static dozoom   exvzoom;

void setzoom(int mul,int div)
{
 if(mul!=czoom->mul) writeiconf(whandle[ZOOM],0,"%d",mul);
 if(div!=czoom->div) writeiconf(whandle[ZOOM],1,"%d",div);

 czoom->mul=mul;
 czoom->div=div;

 exzoom();
}



void zoomicon(void)
{
 int mul=czoom->mul;
 int div=czoom->div;

 if(icon==7)
 {
  czoom->var^=1;
  selectst(whandle[ZOOM],7,czoom->var);

  if(czoom->var)
  {
   exvzoom();
   if(mul!=czoom->mul) writeiconf(whandle[ZOOM],0,"%d",czoom->mul);
   if(div!=czoom->div) writeiconf(whandle[ZOOM],1,"%d",czoom->div);
  }
  else
   exvzoom();

  return;
 }
 else
 {
  if(czoom->var)
  {
   selectst(whandle[ZOOM],7,0);
   czoom->var=0;
  }
 }


 switch(icon ^ (buttons==1))
 {
  case 2:                /* inc mul */
         mul++;
         break;

  case 3:                /* dec mul */
         if(mul>1) mul--;
         break;

  case 4:                /* inc div */
         div++;
         break;

  case 5:                /* dec div */
         if(div>1) div--;
         break;
 }

 if(mul!=czoom->mul || div!=czoom->div) setzoom(mul,div);
}




void zoomkey(int * key) 
{
 int mul;
 int div;

 if(*key==13 || *key==CDOWN || *key==CUP)  
 {
  if(czoom->var)
  {
   selectst(whandle[ZOOM],7,0);
   czoom->var=0;
  }

  if(*key==13 && icon==1)
  {
   if(sscanf(iconaddr(whandle[ZOOM],0),"%d",&mul)==1 &&
      sscanf(iconaddr(whandle[ZOOM],1),"%d",&div)==1 &&
      (mul!=czoom->mul || div!=czoom->div)) setzoom(mul,div);
  }

  if(icon==1) iecarrot(whandle[ZOOM],0);
  else
  if(icon==0) iecarrot(whandle[ZOOM],1);
 }
 else return;
 *key=-1;
}





int dynamzoom(dozoom zoomf,zoomer * zoom,dozoom vzoomf)
{
 int handle=createwindow(ZOOM);
 writeiconf(handle,0,"%d",zoom->mul);
 writeiconf(handle,1,"%d",zoom->div);

 czoom=zoom;
 exzoom=zoomf;
 exvzoom=vzoomf;

 selectst(handle,7,czoom->var);

 return(handle);
}






