/* a GADGETUP has been received, this routine 
   takes the appropriate action
*/

#include "ifs.h"
#include <exec/types.h>
#include <intuition/intuition.h>

int checkvalidfun (funstr, currentfun, numoffun)
  char *funstr;
  int  currentfun, numoffun;
{
  int  numscaned,
  fnum = 0;

  numscaned = sscanf(funstr, "%5d", &fnum);
  if ((numscaned>2) || (numscaned==0) || (fnum>FUNLIMIT) || (fnum<1))
    return(currentfun);
  else
  return(fnum-1);		/* current function is 1 less than displayed */
}         

float checkvalidcooef (funstr, cooef)
   char *funstr;
   float cooef;
   
{
   int  numscaned;
   float   scooef = 0.;

   numscaned = sscanf(funstr,"%5f", &scooef);
   if ((numscaned == 0) || (scooef > MAXCOFF) || (scooef < MINCOFF))
      return(cooef);
   else
      return(scooef);
}         

float checkvalidper (funstr, per)
   char *funstr;
   float per;
   
{
   int  numscaned;
   float   sper = 0.;

   numscaned = sscanf(funstr, "%5f", &sper);
   if ((numscaned == 0) || (sper > 1.) || (sper < 0.))
      return(per);
   else
      return(sper);
}         
      
    
void setpots(adjwin, gads, PInfos, funs, percent, 
             xyscale, xoff, yoff, currentfun, numoffun, stxt, adjopen) 

  struct Window *adjwin;
  struct Gadget *gads;
  struct PropInfo *PInfos;
  float funs[][6], percent[], xyscale;
  int    xoff, yoff, currentfun, numoffun;
  UBYTE stxt[NUMSTRS][GSTRLEN];
  short adjopen;
{
  int i;
  if (adjopen) {
    if(numoffun>0) 
      PInfos[0].HorizPot = currentfun*FFFF / numoffun;  
    for(i=0; i<6; i++) 
      PInfos[i+1].HorizPot = (funs[currentfun][i]+1)*FFFF/3;
    PInfos[7].HorizPot = (currentfun>0) 
                     ? (percent[currentfun]-percent[currentfun-1])*FFFF
                     : percent[currentfun]*FFFF;
    PInfos[8].VertPot  = xyscale*FFFF/3;
    PInfos[9].HorizPot= ((xoff/(float)WIDTH)+1)*FFFF/3;
    PInfos[9].VertPot = FFFF *(.66 - (yoff/(3. * HEIGHT)));
    sprintf(stxt[0],"%5d",currentfun+1);
    for(i=0; i<6; i++)            /* change needed */
      sprintf(stxt[i+1],"%5.2f", funs[currentfun][i]);
    sprintf(stxt[7],"%5.2f", (currentfun>0) 
                             ? percent[currentfun]-percent[currentfun-1] 
                             : percent[currentfun]);
    PInfos[0].HorizBody = FFFF / (numoffun+1);
    if((adjwin->DetailPen = (currentfun+2)%MAXCOLORS) < 2)
      adjwin->DetailPen = adjwin->DetailPen + 2;
    RefreshGadgets(&gads[NUMGADS-1],adjwin,NULL);
  }
}

void adjpercent(numoffun, percent)
int numoffun;
float percent[];
{
  int i;

  percent[0] = 1.0/(numoffun+1);
  for (i=1;i<=numoffun;i++) {
    percent[i] = percent[i-1] + 1.0/(numoffun+1);
       
  }
}

void gadgetmessage(aptr,adjwin,funs,percent,xyscale,xoff,yoff,gads,PInfos,
                   numoffun,currentfun,stxt,displaynumsw,adjopen)
/* returns whether or not the screen needs to be cleared */
APTR aptr;
float funs[][6],percent[],*xyscale;
int   *xoff, *yoff,  *numoffun, *currentfun;
struct Gadget *gads;
struct Window *adjwin;
struct PropInfo *PInfos;
UBYTE stxt[NUMSTRS][GSTRLEN];
short displaynumsw;
{

  short needrefresh = TRUE;
  int i;

  if (aptr == (APTR)&gads[0]) { 
      /* this *numoffun+1 insures that Pot is placed right */
      *currentfun = PInfos[0].HorizPot * (*numoffun+1)/ FFFF;
      /* insure that currentfun is always <= numoffun */
      if (*currentfun > *numoffun) (*currentfun)--; 
      needrefresh = FALSE;
  }
  else if (aptr == (APTR)&gads[7]) {
      percent[*currentfun] = (PInfos[7].HorizPot*1/ (float) FFFF); /*comp bug*/
      if (*currentfun>0) percent[*currentfun] += percent[*currentfun-1];
      }
  else if (aptr == (APTR)&gads[8])              
      *xyscale = (PInfos[8].VertPot*3/(float) FFFF);
  else if (aptr == (APTR)&gads[9]) {
      *xoff = ((PInfos[9].HorizPot*3/(float) FFFF) - 1)*WIDTH;
      *yoff = HEIGHT * (2.0 - (3. * (PInfos[9].VertPot*1) / (float) FFFF)); 
      /* another compiler bug*/
  }
  else if (aptr == (APTR)&gads[10]) {	/* Function Number String */
      *currentfun = checkvalidfun(stxt[0],*currentfun,*numoffun);
      if(*numoffun < *currentfun) {
        *numoffun = *currentfun;
        adjpercent(*numoffun, percent);
        PInfos[0].HorizBody = FFFF / (*numoffun+1);
        needrefresh = displaynumsw;
      }
      else needrefresh = FALSE;
  }
  else if (aptr == (APTR)&gads[17]) {
      percent[*currentfun] = checkvalidper(stxt[7], percent[*currentfun]);
      if (*currentfun>0) percent[*currentfun] += percent[*currentfun-1];
      }

  for (i=0; i<6; i++) {         /* change needed */
    if (aptr == (APTR)&gads[i+1]) 
      funs[*currentfun][i] = ((PInfos[i+1].HorizPot)*3/(float) FFFF) - 1.0;
    if (aptr == (APTR)&gads[i+11])   /* one of the other strings */
      funs[*currentfun][i] = checkvalidcooef(stxt[i+1], funs[*currentfun][i]);
  }

  setpots(adjwin, gads, PInfos, funs, percent, *xyscale, 
          *xoff, *yoff, *currentfun, *numoffun, stxt, adjwin);
  if (needrefresh) clearscreen();
}

