/*->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.key"
#include "h.pr"
#include "h.strdef"
#include "h.xext"
#include "h.script"
#include "h.dir"
#include "h.batch"
#include "h.pic"
#include "h.vx"
#include "h.config"
#include "h.trans"
#include "h.csv"
#include "h.replay"
#include "h.view"
#include "h.dbug"
#include "h.def"
#include "h.txi"

#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             */




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


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



static 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 SAVEDBBUFF:
                            writetypes(transtoken("FILE07"),DATA);
                            break;

           case SAVEDBSPOOL:
                            writetypes(transtoken("FILE07"),DATA); 
                            break;

             case SAVEVOICE:
                            writetypes(transtoken("FILE06"),ASND);
                            break;

               case SAVECSV:
                            writetypes(transtoken("FILE05"),CSV);
                            break;

         case SAVEVXBSPRITE:
                            writetypes(transtoken("FILE00"),SPRITE);
                            break;

             case SAVESPSUB:
                            writetypes(transtoken("FILE01"),SPRITE);
                            break;

               case SAVELOG:
                            writetypes(transtoken("FILE02"),TEXT);
                            break;

          case SAVEDIRENTRY:
                            writetypes(transtoken("FILE03"),TEXT);
                            break;

          case      SAVETXI:
                            writetypes(transtoken("FILE03"),TEXT);
                            break;

 }

 return(handle);
}



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


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


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


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


static 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;
}


static void savedrag(void)
{
 dragicon(mousex,mousey,ewindow,icon);
 dragasprite(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();
 dragaspritestop();

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

 if(getview(mhandle))
 {
  if(savetype==SAVEVOICE)
  {
   savevoicebatch(iconaddr(whandle[SAVEFILE0],1));
  }
 }
 else
 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("{SAVE00}");
  return(0);
 }

 switch(savetype)
 {

   case    SAVEDBBUFF:
                      code=savedbbuffer(filename);
                      break;

   case   SAVEDBSPOOL:
                      code=savedbspool(filename);
                      break;

   case     SAVEVOICE:
                      code=savevoice(filename);
                      break;

   case       SAVECSV:
                      code=savecsv(filename);
                      break;

   case SAVEVXBSPRITE:
                      code=savevxbsprite(filename);
                      break;

       case SAVESPSUB:
                      code=savevxbsubsprite(filename);
                      break;

         case SAVELOG:
                      code=savelog(filename);
                      break;

    case SAVEDIRENTRY:
                      code=savetextentry(filename);
                      break;

     case     SAVETXI:
                      code=savetxi(filename);
                      break;

              default:
                      return(0);

 }

 if(!ram && !code) werr(0,"{FILE04}%s",kerror());
 return(code);
}




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


/* 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(wimpevent.data.msg.hdr.your_ref!=saveref) return;

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

 doprint=0;
 filetype=TEXT;

 switch(savetype)
 {
                         /* group 1, these can never be printed just get out */

         case SAVEVXBSPRITE:
         case SAVESPSUB:
                         break;

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

              
/*        case SAVEPRINTER:
                         filetype=savefiletype();
                         if(prdumpbuffer(name)) action=3;
                         break;                */


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

                 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)
  {





  }
 }
}





/*****************************************************************************/
/* 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 || *key==HT)  
 {
  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);
}



void fixzoom(dozoom zoomf,zoomer * zoom,int mul,int div)
{
 zoom->var=0;
 zoom->mul=mul;
 zoom->div=div;
 zoomf();
}

