/*->c.con */

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

#include "h.os"
#include "h.wimp"
#include "h.bbc"
#include "h.flex"
#include "h.swis"

#include "h.Drawlevel0"

#include "h.def"

#include "h.wos"
#include "h.main"
#include "h.mym"
#include "h.fsx"
#include "h.timex"

#include "h.strdef"
#include "h.xext"


#include "h.buffer"
#include "h.state"
#include "h.dir"
#include "h.txfax"
#include "h.view"
#include "h.batch"
#include "h.trans"
#include "h.config"
#include "h.sched"
#include "h.log"
#include "h.tw"
#include "h.key"


#include "h.con"


static int  conactive;
static char saveact[64];
static char savefax[24];
static char savepage[12];
static char savecomplete[12];
static char saveres[12];
static char saverate[12];
static char savecompression[12];
static char savesend[24];
static char saveid[32];
static char connum[NUMLEN];
static char savecid[NUMLEN];
static int  sentstate;        /* 0==none, 1=OK, 2=fail */



int         serversn;


void conaction(char * format, ...)
{
 va_list args;

 va_start(args, format);
 vsprintf(saveact,format,args);
 va_end(args);

 trans(saveact,sizeof(saveact));

 if(conactive)
 {
  writeicon(whandle[CONTROL],1,saveact);
 }
}



static char * constates[]={"R2;","R2;Ssent","R2;Snotsent"};


static void conwritestate(void)
{
 if(conactive)
 {
  writevalid(whandle[CONTROL],38,constates[sentstate]);
 }
}


void consent(int sent)
{
 if(sent) sentstate=1;
 else     sentstate=2;
 conwritestate();
}




void confax(char * string) 
{
 strcpy(savefax,string);
 if(conactive)
 {
  writeicon(whandle[CONTROL],6,savefax);
  sentstate=0;
  conwritestate();
 }
}




void stripid(char * srcid,char * destid)
{
 char   id[IDLEN];
 char * p;
 char * q;

 strcpy(id,srcid);

 p=id;
 q=id+strlen(id);
 if(q>id) q--;

 while(*p   && (*p==' ' || *p=='"')) p++;
 while(q>p  && (*q==' ' || *q=='"')) q--;
 *(q+1)=0;

 strcpy(destid,p);
}


void conid(char * id)
{
 stripid(id,saveid);
 if(conactive)
 {
  writeicon(whandle[CONTROL],20,saveid);
 }
}


void conpage(int pn,int pt)
{
 if(pt>0) sprintf(savepage,"%d/%d",pn,pt);
 else     sprintf(savepage,"%d",pn);

 if(conactive)
 {
  writeicon(whandle[CONTROL],7,savepage);
 }
}


void conparam(int rate,int res,int compression)
{
 strcpy(saveres,res?"High":"Low");
 sprintf(saverate,"%d",ratecode[rate]);
 strcpy(savecompression,compression?"2D":"1D");

 if(conactive)
 {
  writeicon(whandle[CONTROL],15,saveres);
  writeiconf(whandle[CONTROL],14,saverate);
  writeicon(whandle[CONTROL],11,savecompression);
 }
}


void concomplete(int pc)
{
 sprintf(savecomplete,"%d%%",pc);

 if(conactive)
 {
  writeicon(whandle[CONTROL],9,savecomplete);
 }
}



void conclrfax(void)
{
 savepage[0]=0;
 savecomplete[0]=0;
 saverate[0]=0;
 saveres[0]=0;
 savecompression[0]=0;
 saveid[0]=0;

 if(conactive)
 {
  writeicon(whandle[CONTROL],7,"");
  writeicon(whandle[CONTROL],9,"");
  writeicon(whandle[CONTROL],15,"");
  writeiconf(whandle[CONTROL],14,"");
  writeicon(whandle[CONTROL],11,"");
  writeicon(whandle[CONTROL],20,"");
 }
}


static int contimer;
static int constart;

void conzero(void)
{
 if((zerotime-contimer)>100)
 {
  if(conactive)
  {
   writeiconf(whandle[CONTROL],37,"%d",(zerotime-constart)/100);
   contimer=zerotime;
  }
 }
}


void contime(int start)
{
 if(start)
 {
  contimer=constart=zerotime;
  addzeroevent(CONZERO);
 }
 else
 {
  remzeroevent(CONZERO);
  if(conactive) 
  {
   writeicon(whandle[CONTROL],37,"");
  }
 }
}



os_error * confn(char * name)
{
 writeicon(whandle[CONTROL],31,name);
 return(NULL);
}


static void convoice(void)
{
 strcpy(connum,iconaddr(whandle[CONTROL],31));
 voicedial(connum,1,prefix);
}


static void condial(int conrecv)
{
 strcpy(connum,iconaddr(whandle[CONTROL],31));
 directdial(connum,conrecv);
}


void conicon(void)
{

 if(icon==30) openentrymenu(confn);
 else
 if(buttons==2) popinfo();
 else
 switch(icon)
 {
  case 19:
          holdall(1);
          break;

  case 22:
          holdall(0);
          break;

  case 23:
          showlog();
          break;

  case 17: /* Send */
          if(!checkactive()) directsend();
          break;

  case  4: /* Cancel */
          faxcancel();
          break;

  case 16: /* Receive */
          if(!checkactive()) directreceive();
          break;

  case 28:
          forcenext();
          break;

  case 29: /* rx sched */
          rxsched();
          break;

  case 33:
  case 35:
          if(!checkactive()) condial(icon==35);
          break;

  case 34:
          if(!checkactive()) convoice();
          break;

 }
}


void loadserver(void)
{
 int n;
 int posn;

 n=selected(&posn);
 if(n)
 {
  serversn=vtable[view][posn].schedule;
  strcpy(savesend,vtable[view][posn].name);
  writeicon(whandle[CONTROL],18,savesend);
 }
}


void conclose(void)
{
 strcpy(connum,iconaddr(whandle[CONTROL],31));
 closedownt(CONTROL);
 conactive=0;
}

/*
void bottomwindow(wimp_wstate * wst,int handle)
{
  int width;
  int height;
  int left;
  int top;

  wimp_get_wind_state(handle,wst);

  width=wst->o.box.x1-wst->o.box.x0;
  height=wst->o.box.y1-wst->o.box.y0;

  left=((3*screenx)/2-width)>>1;
  top=((screeny/2)-height)>>1;

  wst->o.box.x0=left;
  wst->o.box.y0=top;
  wst->o.box.x1=wst->o.box.x0+width;
  wst->o.box.y1=wst->o.box.y0+height;

  wst->o.behind=-1;
}



void conpopup(int handle)
{
 wimp_wstate wst;
 bottomwindow(&wst,handle);
 wimp_open_wind(&wst.o);
}
*/


void conopen(void)
{
 int handle;

 handle=whandle[CONTROL];

 if(handle) 
 {
  forward(handle);
 }
 else
 {
  handle=createwindow(CONTROL);
  if(handle)
  {
   writeicon(handle,1,saveact);
   writeicon(handle,6,savefax);
   writeicon(handle,7,savepage);
   writeicon(handle,9,savecomplete);
   writeicon(handle,15,saveres);
   writeicon(handle,14,saverate);
   writeicon(handle,11,savecompression);
   writeicon(handle,18,savesend);
   writeicon(handle,20,saveid);
   writeicon(handle,31,connum);
   writeicon(handle,37,"");

   conactive=1;

   conwritestate();

   popup(handle,0);
  }
 }
}


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

void keyboot(void)
{
 int handle;

 handle=createwindow(GRABBER);
 if(handle)
 {
  open(handle,-0x7FFF,0,0,0,0,0,-1);
 }
}


void grabkey(int * key)
{
 int ch;

 ch=*key;

 switch(ch)
 {

  case       F2:
                if(whandle[TDIR]) dirclose();
                else              dirshow();
                break;

  case SHFT_F10:
                twainselect();
                break;

  case      F10:
                twainacquire();
                break;

  case  SHFT_F6:
                if(!viewhandle[TXBFILE]) opentxbatch();
                else                     closeview(TXBFILE);
                break;

  case       F6:
                if(!viewhandle[RXBFILE]) openrxbatch();
                else                     closeview(RXBFILE);
                break;

        default:
                return;
 }

 *key=-1;
}


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

void callerid(string & s);

Called by the modem driver when it picks up a callers id.

 */

void callerid(int fp)
{
 stripid(stringptr(stack[fp]),savecid);

 if(conactive)
 {
  writeicon(whandle[CONTROL],25,savecid);
 }
}

