/*->c.tekr */
/* Tek redraw code */


#include <stdio.h>
#include <stdlib.h>
#include <string.h>


#include "h.os"
#include "h.bbc"
#include "h.sprite"
#include "h.wimp"
#include "h.flex"
#include "h.Drawlevel0"



#include "h.def"
#include "h.main"
#include "h.wos"
#include "h.trans"

#include "h.mym"
#include "h.key"

#include "h.vtdef"
#include "h.vtfile"
#include "h.vtscr"
#include "h.vtmenu"

#include "h.pr"

#include "h.tekint"
#include "h.tek"



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



void picrend(wimp_box * box,Draw_diag diag,wimp_redrawstr * redrawstr)
{
 Draw_redrawstr r;
 double scale=1.0;
 double sx1=1.0;
 double sx2=1.0;
 Draw_error e;
 int gx0;
 int gy0;
 int gx1; 
 int gy1;
 int ox;
 int oy;
 char string[128];

/* dprintf(0,"diag.len=%d teklast.len=%d tekdiag.len=%d",diag.length,teklast.length,tekdiag.length); */

 if(diag.length<=40 || !diag.data) return;      /* no buffer do nothing */

 ox=redrawstr->box.x0-redrawstr->scx;
 oy=redrawstr->box.y1-redrawstr->scy;


 gx0=ox+box->x0;  
 gy0=oy-box->y0;  
 gx1=ox+box->x1;  
 gy1=oy-box->y1;


 if(redrawstr->g.x0>gx0) r.g.x0=redrawstr->g.x0;
 else                    r.g.x0=gx0;

 if(redrawstr->g.x1<gx1) r.g.x1=redrawstr->g.x1;
 else                    r.g.x1=gx1;

 if(redrawstr->g.y0>gy0) r.g.y0=redrawstr->g.y0;
 else                    r.g.y0=gy0;

 if(redrawstr->g.y1<gy1) r.g.y1=redrawstr->g.y1;
 else                    r.g.y1=gy1;


 Draw_queryBox(diag, &r.box, TRUE);
 sx1=((double)(gx1-gx0))/((double)(r.box.x1 -r.box.x0 ));
 sx2=((double)(gy1-gy0))/((double)(r.box.y1 -r.box.y0 ));
 if(sx1>sx2) scale=sx2; else scale=sx1;


 r.scx = (r.box.x0 - gx0) + (int)((double)r.box.x0*scale);
                                             /* (ox+osunits(box->x)); */
 r.scy = (r.box.y1 - gy0) + (int)((double)r.box.y0*scale);
                                     /* (oy-osunits(yps+box->y+box->h)); */

 if(Draw_verify_diag(diag, &e))
 {
  /* Draw a box and set the clipping region */

  bbc_gwindow(r.g.x0,r.g.y0,r.g.x1-deltax,r.g.y1-deltay);

  if(!Draw_render_diag(diag,&r,scale,scale,&e))
  {
   if(e.type == DrawOwnError)    /*render failed: code %d at %d\n */
         sprintf(string,transtoken("TK2"),
                e.err.draw.code, e.err.draw.location);
      else
        sprintf(string,"{TK0} %s", e.err.os.errmess);
   errorbox(string);
 /*  savetekpic("xx"); */
   tekclg();
  }
 }
 else
 {                                         /* verify failed: code %d at %d */
  sprintf(string,transtoken("TK1"),
           e.err.draw.code,e.err.draw.location);

  errorbox(string);
/*  savetekpic("xx"); */
  tekclg();
 }

 bbc_gwindow(redrawstr->g.x0,redrawstr->g.y0,
                                redrawstr->g.x1-deltax,redrawstr->g.y1-deltay);
}





void tekpaintcursor(wimp_redrawstr * redrawstr,tekcstr * tekc)
{
 int ox;
 int oy;

 if(tekc->curs!=TEKNOCURS) 
 {
  ox=redrawstr->box.x0-redrawstr->scx;
  oy=redrawstr->box.y1-redrawstr->scy;
  seteorcol(7,0);

  if(tekc->curs==TEKVCURS)     /* V */
  {
   bbc_move(ox+tekos(tekc->x),oy-tekos(TEKVISH-tekc->y));
   bbc_draw(ox+tekos(tekc->x+tekdx/2),oy-tekos(TEKVISH-tekc->y-tekdy/2));
   bbc_draw(ox+tekos(tekc->x+tekdx),oy-tekos(TEKVISH-tekc->y));
  }
  else
  if(tekc->curs==TEKGINCURS)   /* GIN */
  {
   bbc_move(ox+tekos(tekc->x),oy);
   bbc_draw(ox+tekos(tekc->x),oy-tekos(TEKVISH));
   bbc_move(ox,oy-tekos(TEKVISH-tekc->y));
   bbc_draw(ox+tekos(TEKW),oy-tekos(TEKVISH-tekc->y));
  }
 }
}






void tekredraw(void)
{ 
 int      more;
 wimp_redrawstr redrawstr;
 wimp_box box;
 int      ox;
 int      oy;

 ewindow=wimpevent.data.o.w;
 redrawstr.w=ewindow;

 wimp_redraw_wind(&redrawstr,&more);
 while(more)
 {
  box.x0=0;
  box.x1=tekos(TEKW);
  box.y1=-tekos(TEKMAXH-TEKVISH);
  box.y0=tekos(TEKVISH);

  setgcol(tekbakpal);
  ox=redrawstr.box.x0-redrawstr.scx;
  oy=redrawstr.box.y1-redrawstr.scy;
/*  bbc_rectanglefill(ox,oy,tekos(TEKW),-tekos(TEKVISH)); */

  bbc_rectanglefill(redrawstr.box.x0,redrawstr.box.y1,
                    redrawstr.box.x1-redrawstr.box.x0,
                    redrawstr.box.y0-redrawstr.box.y1);

  picrend(&box,tekdiag,&redrawstr);
  picrend(&box,teklast,&redrawstr);
  tekpaintcursor(&redrawstr,&tekc);
  tekredrawprint(&redrawstr);

  wimp_get_rectangle(&redrawstr,&more);
 }
}





void tekupdatecursor(tekcstr * old,tekcstr * new)
{
 wimp_redrawstr redrawstr;
 int            handle=whandle[TEK];
 int            more;

 getw(handle);

 redrawstr.w=handle;
 redrawstr.box.x0=x0-bx;
 redrawstr.box.x1=x1-bx;
 redrawstr.box.y0=y0-by; 
 redrawstr.box.y1=y1-by;   

 wimp_update_wind(&redrawstr,&more);
 while(more)
 {
  tekpaintcursor(&redrawstr,old);
  tekpaintcursor(&redrawstr,new);
  wimp_get_rectangle(&redrawstr,&more);
 }
}





void tekupdatewindow(tekcstr * old,tekcstr *  new)
{
 wimp_redrawstr redrawstr;
 wimp_box       box;
 int            handle=whandle[TEK];
 int            more;

 getw(handle);

 redrawstr.w=handle;
 redrawstr.box.x0=x0-bx;
 redrawstr.box.x1=x1-bx;
 redrawstr.box.y0=y0-by; 
 redrawstr.box.y1=y1-by;   

 wimp_update_wind(&redrawstr,&more);
 while(more)
 {
  box.x0=0;
  box.x1=tekos(TEKW);
  box.y1=-tekos(TEKMAXH-TEKVISH);
  box.y0=tekos(TEKVISH);

  tekpaintcursor(&redrawstr,old);
  picrend(&box,teklast,&redrawstr);
  tekpaintcursor(&redrawstr,new);

  wimp_get_rectangle(&redrawstr,&more);
 }
}



void tekdrawinit(void)
{
 Draw_registerMemoryFunctions(flex_alloc,flex_extend,flex_free);

 tekdiag.length=teklast.length=0;
 tekdiag.data=teklast.data=NULL;
}



void tekdrawfinish(void)
{
 cleardiag(&tekdiag);
 cleardiag(&teklast);
}



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

static int tekselindexs;     /* which index is selected in select window */

static int  pala[3];
static int  pdrag;
static wimp_palettestr palt;
static int  temptekindex[TEKNINDEX];
static int  tempteklockcols;
static int  temptekmode;
static int  tempwhichindex;
static int  tempteklineindex;
static int  temptekfontindex;
static int  temptekfillindex;
static int  temptekbackindex;




void userdrag(int handle,int x0,int y0,int x1,int y1)
{
 wimp_dragstr dblock;

 dblock.window=handle;
 dblock.type=wimp_USER_HIDDEN;
 dblock.box.x0=mousex;
 dblock.box.y0=mousey;
 dblock.box.x1=mousex;
 dblock.box.y1=mousey;
 dblock.parent.x0=x0;
 dblock.parent.y0=y0;
 dblock.parent.x1=x1;
 dblock.parent.y1=y1;
 wimp_drag_box(&dblock);
}





void wrpal(int n)
{
 writeiconf(whandle[TDISPLAYTEK],33+n*5,"%d",pala[n]);
}




/* set sliders from colour integer */

void setrgb(int palc)
{
 int i; 
 pala[0]=(palc >>8)& 255; 
 pala[1]=(palc >>16)& 255; 
 pala[2]=(palc >>24)& 255; 
 for(i=0;i<3;i++)
 {
  seti(whandle[TDISPLAYTEK],34+i*5,0,0); /* forcing a redraw */
  wrpal(i);
 }
 seti(whandle[TDISPLAYTEK],46,0,0);
}


void setgcolfrompala(void)
{
 setgcol((pala[0]<<8)+(pala[1]<<16)+(pala[2]<<24));
}


void readcols(int * where)
{
 *where=(pala[0]<<8)+(pala[1]<<16)+(pala[2]<<24);
}


void setcolourkey(int icon)
{
 int n,val,p;

 p=(icon-33)/5;

 n=sscanf(iconaddr(whandle[TDISPLAYTEK],icon),"%d",&val);
 if(n!=1 || val<0 || val>255)
 {
  wrpal(p);
  return;
 }

 pala[p]=val;
 seti(whandle[TDISPLAYTEK],34+p*5,0,0);
 seti(whandle[TDISPLAYTEK],46,0,0);
 wrpal(p);
}


void tekselcoldragend(void)
{
 pdrag=0;
}

void tekselcoldragzero(void)
{
 int tx;
 int handle;
 int w;

 handle=whandle[TDISPLAYTEK];

 getpointer();
 getw(handle);
 geti(handle,pdrag);
 tx=(mousex-bx-ix0);
 w=ix1-ix0;
 if(tx<0) tx=0;
 if(tx>w) tx=w;
 tx=(tx*255)/w;

 if(pala[(pdrag-34) & 3]!=tx)
 {
  pala[(pdrag-34) & 3]=tx;
  seti(handle,pdrag,0,0);
  wrpal((pdrag-34) & 3);
  seti(handle,46,0,0);
 }
}





void tekredind(int index,int icon,wimp_redrawstr * redrawstr,int select)
{
 int ox=redrawstr->box.x0-redrawstr->scx;
 int oy=redrawstr->box.y1-redrawstr->scy;
 int window=redrawstr->w;

 geti(window,icon);

 if(iconinredraw(redrawstr))
 {
  setgcol(temptekindex[index]);
                                                                                  bbc_rectanglefill(ox+ix0,oy+iy0,ix1-ix0-deltax,iy1-iy0-deltay);
  if(select)
  {
   setoppgcol(temptekindex[index]);
   if(index<10)
   {                                                         /* Top LHS */
    bbc_move(ox+ix0+((ix1-ix0-16)/2),oy+iy1-((iy1-iy0-32)/2)-deltay);
    bbc_vdu('0'+index);
   }
   else
   {
    bbc_move(ox+ix0+((ix1-ix0-32)/2),oy+iy1-((iy1-iy0-32)/2)-deltay);
    bbc_vdu('1');
    bbc_vdu('0'+index-10);
   }
  }
 }
}





void tekdisplayred(void)
{
 int            more;
 wimp_redrawstr rblock;
 int            i;
 int            palv;
 int            w;


 getw(ewindow);
 rblock.w=ewindow;
 wimp_redraw_wind(&rblock,&more);
 while(more)
 { 
  for(i=0;i<3;i++)  
  {
   geti(ewindow,34+i*5);

   if(iconinredraw(&rblock))
   {
    w=ix1-ix0-deltax;

    wimp_setcolour(temptekmode?1:7);
    palv=(pala[i]*w)/255;
    if(palv>0) bbc_rectanglefill(bx+ix0,by+iy0,palv,iy1-iy0-deltay);
    wimp_setcolour(0);                  
    if(palv<w) bbc_rectanglefill(bx+ix0+palv,by+iy0,w-palv,iy1-iy0-deltay);
   }
  }

  geti(ewindow,46);
  if(iconinredraw(&rblock))
  {
   setgcolfrompala();
   bbc_rectanglefill(bx+ix0,by+iy0,ix1-ix0-2*deltax,iy1-iy0-deltay);
  }

  for(i=0;i<TEKNINDEX;i++)
  {
   tekredind(i,12+i,&rblock,i==tekselindexs); 
  }

  tekredind(tempteklineindex,4,&rblock,0);
  tekredind(temptekfontindex,6,&rblock,0); 
  tekredind(temptekfillindex,8,&rblock,0);
  tekredind(temptekbackindex,10,&rblock,0);

  wimp_get_rectangle(&rblock,&more);
 }
}





void tekdisplaykey(int * key)
{
 int ch=*key;

 switch(ch)
 {




 }
}



static void setindexs(int n)
{
 int handle=whandle[TDISPLAYTEK];

 seti(handle,12+tekselindexs,0,0);
 tekselindexs=n;
 setrgb(temptekindex[tekselindexs]);
 seti(handle,12+tekselindexs,0,0);
}




static void setdisplaymode(void)
{
 int handle;
 int i;

 handle=whandle[TDISPLAYTEK];

 selectst(handle,2,temptekmode);
 selectst(handle,67,!temptekmode);

 shadeiconst(handle,3,!temptekmode);
 shadeiconst(handle,5,!temptekmode);
 shadeiconst(handle,7,!temptekmode);
 shadeiconst(handle,9,!temptekmode);
 shadeiconst2(handle,3,!temptekmode);
 shadeiconst2(handle,5,!temptekmode);
 shadeiconst2(handle,7,!temptekmode);
 shadeiconst2(handle,9,!temptekmode);


 shadeiconst(handle,31,temptekmode);
 shadeiconst(handle,32,temptekmode);
 shadeiconst(handle,36,temptekmode);
 shadeiconst(handle,37,temptekmode);
 shadeiconst(handle,41,temptekmode);
 shadeiconst(handle,42,temptekmode);

 shadeiconst2(handle,34,temptekmode); /* sliders */
 shadeiconst2(handle,39,temptekmode);
 shadeiconst2(handle,44,temptekmode);

 shadeiconst2(handle,33,temptekmode); /* keys */
 shadeiconst2(handle,38,temptekmode);
 shadeiconst2(handle,43,temptekmode);
 shadeiconst2(handle,35,temptekmode); /* RGB text */
 shadeiconst2(handle,40,temptekmode);
 shadeiconst2(handle,45,temptekmode);

 for(i=47;i<=62;i++) shadeiconst(handle,i,temptekmode);

 if(temptekmode)
 {
  switch(tempwhichindex)
  {
   case 0:
          setindexs(tempteklineindex);
          break;

   case 1:
          setindexs(temptekfontindex);
          break;

   case 2:
          setindexs(temptekfillindex);
          break;

   case 3:
          setindexs(temptekbackindex);
          break;
  }
 }
}



void tekdisplayicon(void)
{
 int handle;
 int i;

 handle=whandle[TDISPLAYTEK];

 getw(handle);

 switch(icon)
 {
  case  2:
  case 67:
          i=icon==2;
          if(i!=temptekmode)
          {
           temptekmode=i;
           setdisplaymode();
          }
          break;

  case 3:
         if(tempwhichindex!=0)
         {
          selectst(handle,tempwhichindex*2+3,0);
          setindexs(tempteklineindex);
          tempwhichindex=0;
          selectst(handle,tempwhichindex*2+3,1);
         }
         break;

  case 5:
         if(tempwhichindex!=1)
         {
          selectst(handle,tempwhichindex*2+3,0);
          setindexs(temptekfontindex);
          tempwhichindex=1;
          selectst(handle,tempwhichindex*2+3,1);
         }
         break;

  case 7:
         if(tempwhichindex!=2)
         {
          selectst(handle,tempwhichindex*2+3,0);
          setindexs(temptekfillindex);
          tempwhichindex=2;
          selectst(handle,tempwhichindex*2+3,1);
         }
         break;

  case 9:
         if(tempwhichindex!=3)
         {
          selectst(handle,tempwhichindex*2+3,0);
          setindexs(temptekbackindex);
          tempwhichindex=3;
          selectst(handle,tempwhichindex*2+3,1);
         }
         break;


 case 11:
         selectst(handle,11,tempteklockcols^=1);
         break;


  case  34:     /* R */
  case  39:     /* G */
  case  44:     /* B */
           geti(handle,icon);
           if(!pdrag && !temptekmode) 
           { 
            userdrag(handle,bx+ix0,by+iy0,bx+ix1+deltax,by+iy1);
            startdrag(TEKPDRAG,handle);
            pdrag=icon; 
           }
           break;
                                      
   case 32:           /* inc R */
   case 31:           /* dec R */
           if(buttons==0x1) icon=(32+31)-icon;

           if(icon==32 && pala[0]<255) pala[0]++;
           else
           if(icon==31 && pala[0]>0)   pala[0]--;
           else                        return;

           seti(handle,34,0,0);
           seti(handle,46,0,0);
           wrpal(0);
           break;

   case 37:           /* inc G */
   case 36:           /* dec G */
           if(buttons==0x1) icon=(36+37)-icon;

           if(icon==37 && pala[1]<255) pala[1]++;
           else
           if(icon==36 && pala[1]>0)   pala[1]--;
           else                        return;

           seti(handle,39,0,0);
           seti(handle,46,0,0);
           wrpal(1);
           break;

   case 42:           /* inc B */
   case 41:           /* dec B */
           if(buttons==0x1) icon=(42+41)-icon;

           if(icon==42 && pala[2]<255) pala[2]++;
           else
           if(icon==41 && pala[2]>0)   pala[2]--;
           else                        return;

           seti(handle,44,0,0);
           seti(handle,46,0,0);
           wrpal(2);
           break;

   case 63:                                            /* OK */
           teklockcols=tempteklockcols;

           readcols(&temptekindex[tekselindexs]);

           for(i=0;i<TEKNINDEX;i++) tekindex[i]=temptekindex[i];

           seti(handle,12+tekselindexs,0,0);
           if(buttons==0x4) zapmenu();

           teklineindex=tempteklineindex;
           tekfontindex=temptekfontindex;
           tekfillindex=temptekfillindex;
           tekbackindex=temptekbackindex;
           tekrefreshbackground();

           break;

   case 64:
           tekclg();
           break;

   default:                                /* click on individual colour */
           if(icon>=47 && icon<=62 && !temptekmode)
           {
            wimp_readpalette(&palt);
            setrgb(palt.c[icon-47].word);
           }
           else
           if(icon>=12 && icon<=27)
           {
            if(!temptekmode)
            {
             readcols(&temptekindex[tekselindexs]);
             setindexs(icon-12);
            }
            else
            {
             switch(tempwhichindex)
             {
              case 0:
                     tempteklineindex=icon-12;
                     break;
              case 1:
                     temptekfontindex=icon-12;
                     break;
              case 2:
                     temptekfillindex=icon-12;
                     break;
              case 3:
                     temptekbackindex=icon-12;
             }
             seti(handle,4+2*tempwhichindex,0,0);
             setindexs(icon-12);
            }
           }
           break;
 }
}


int setuptekdisplay(void)
{
 int handle;
 int i;

 handle=createwindow(TDISPLAYTEK);

 for(i=0;i<TEKNINDEX;i++) temptekindex[i]=tekindex[i];

 tempteklockcols=teklockcols;

 tempteklineindex=teklineindex;
 temptekfontindex=tekfontindex;
 temptekfillindex=tekfillindex;
 temptekbackindex=tekbackindex;

 selectst(handle,11,tempteklockcols);

 selectst(handle,3,tempwhichindex==0);
 selectst(handle,5,tempwhichindex==1);
 selectst(handle,7,tempwhichindex==2);
 selectst(handle,9,tempwhichindex==3);

 setdisplaymode();

 return(handle);
}

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

static int temptek4010;
static int temptektextplane;


void misctekicon(void)
{
 int handle=whandle[TMISCTEK];

 switch(icon)
 {
  case 0:
         selectst(handle,0,temptek4010^=1);
         break;

  case 1:
         selectst(handle,1,temptektextplane^=1);
         break;

  case 2:
         tek4010=temptek4010;
         if(temptektextplane!=tektextplane)
         {
          tekdoenabletextplane(temptektextplane);
         }
         if(buttons==0x4) zapmenu();
         break;
 }
}


int setupmisctek(void)
{
 int handle=createwindow(TMISCTEK);

 temptek4010=tek4010;
 temptektextplane=tektextplane;

 selectst(handle,0,temptek4010);
 selectst(handle,1,temptektextplane);

 return(handle);
}


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

void tekdozoom(void)
{
 int handle;

 getw(handle=whandle[TEK]);

 extent(handle,0,-tekos(TEKVISH),tekos(TEKW),0);

 open(handle,x0,y0,x1,y1,scx,scy,bhandle);

 refreshwindow(handle);
}




void tekvarzoom(void)
{
 int handle;
 getw(handle=whandle[TEK]);
 extent(handle,0,-tekeos(TEKVISH),tekeos(TEKW),0);
 open(handle,x0,y0,x1,y1,scx,scy,bhandle);
 tekvarzoomlo();
}




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

#ifdef NEVER

void setpoptekdisplay(void)
{
 tickst(tekdisp_menu,5,teklockcols);
}


void decodetekdisplay(int m3,int m4)
{
 if(m3==5) teklockcols^=1;
 m3=m4;
}


void setpoptekgeneral(void)
{
 tickst(tekgen_menu,1,tek4010);
 tickst(tekgen_menu,2,tektextplane);
 setpoplinemode();                      /* VT */
}





void decodetekgeneral(int m3,int m4,int m5)
{
 switch(m3)
 {
  case 0:                               /* Line mode */
         decodelinemode(m4,m5);         /* VT */
         break;

  case 1:
         tek4010^=1;
         break;

  case 2:
         tekdoenabletextplane(!tektextplane);
         break;

  case 3:
         tekclg();
         break;

  case 4:
         tekdoreset();
         break;
 }
 m3=m4;
}



void setpoptekprint(void)
{
 tickst(tekprint_menu,1,tekmargins);
 tickst(tekprint_menu,2,printer);

 writemenuf(tekcopy_menu,0,"%d",tekpset.copies);
 writemenuf(tekxscale_menu,0,"%d%%",(tekpset.xmul*100)/tekpset.xdiv);
 writemenuf(tekyscale_menu,0,"%d%%",(tekpset.ymul*100)/tekpset.ydiv);

 gsprint(menuaddr(tekxcorner_menu,0),tekpset.cx,1);
 gsprint(menuaddr(tekycorner_menu,0),tekpset.cy,1);
}





void decodetekprint(int m3,int m4)
{

 switch(m3)
 {
  case 0:
         tekprintfile(!tekdumptype);
         break;

  case 1:
  case 2:
         tekdumptype=m3-1;
         break;

  case 3:                       /* toggle portrait mode */
         tekpset.flags^=PORT;
         tekprupdate();
         break;

  case 4:                       /* X scale */
         if(m4==0)
         {
          int temp;
          if(getmenuint(tekxscale_menu,0,&temp))
          {
           tekpset.xmul=temp;
           tekpset.xdiv=100;
           tekprupdate();
          }
         }
         break;

  case 5:                       /* Y scale */
         if(m4==0)
         {
          int temp;
          if(getmenuint(tekyscale_menu,0,&temp))
          {
           tekpset.ymul=temp;
           tekpset.ydiv=100;
           tekprupdate();
          }
         }
         break;

  case 6:                       /* X corner */
         if(m4==0)
         {
          int temp;
          int minus;

          temp=scang(menuaddr(tekxcorner_menu,0),&minus);
          if(temp>=0)
          {
           tekpset.cx=minus?-temp:temp;
           tekprupdate();
          }
         }
         break;

  case 7:                       /* Y corner */
         if(m4==0)
         {
          int temp;
          int minus;

          temp=scang(menuaddr(tekycorner_menu,0),&minus);
          if(temp>=0)
          {
           tekpset.cy=minus?-temp:temp;
           tekprupdate();
          }

         }
         break;

  case 8:                       /* show margins */
         tekprupdate();
         tekmargins^=1;
         tekprupdate();
         break;

  case 9:                       /* copies */
         if(m4==0)
         {
          int temp;
          if(getmenuint(tekcopy_menu,0,&temp))
          {
           tekpset.copies=temp;
          }
         }
         break;

 case 10:                       /* printer commands */
         printer^=1;
         break;
 }
 m3=m4;
}

#endif




void setpoptekprint(void)
{
 tickst(tekprint_menu,1,tekmargins);
 tickst(tekprint_menu,2,printer);
}




void decodetekprint(int m3,int m4)
{
 switch(m3)
 {
  case 0:
         tekprintfile(!tekdumptype);
         break;

  case 1:                       /* show margins */
         tekprupdate();
         tekmargins^=1;
         tekprupdate();
         break;

  case 2:                       /* printer commands */
         printer^=1;
         break;
 }
 m3=m4;
}



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

              /* N L R D U */

static char entryiclst[5][5]=
{
          1,          0,           0,           7,            0,
          7,          0,           9,           9,            1,
          9,          7,           0,          11,            7,
         11,          0,          13,          13,            9,
         13,         11,           0,           0,           11,
};




void tekprintkey(int * key)
{
 int cicon;
 int j;
 int ch;

 ch=*key;

 switch(ch)
 {
       case 27:
               zapmenu();
               break;

       case CR:
               ch=CDOWN;

    case 0x18E:
    case 0x18F:
    case 0x19C:
    case 0x19D:
    case 0x19E:
    case 0x19F:
               ch&=0x18F;
               for(j=0;j<5;j++) if(entryiclst[j][0]==icon) break;
               cicon=entryiclst[j][(ch-0x18B)];
               if(cicon) iecarrot(whandle[TEKPRINT],cicon);
               break;

    default:return;
 }
 *key=-1;
}




void tekprinticon(void)
{
 int handle=whandle[TEKPRINT];
 int temp;
 int minus;

 switch(icon)
 {

  case  2:
  case  3:
          if(icon==2 && !(tekpset.flags & PORT)) tekpset.flags|=PORT;
          else
          if(icon==3 &&  (tekpset.flags & PORT)) tekpset.flags&=~PORT;
          else break;
          selectst(handle,2,tekpset.flags & PORT);
          selectst(handle,3,!(tekpset.flags & PORT));
          tekprupdate();
          break;


  case  4:
  case  5:
          if(icon!=(5-tekdumptype))
          {
           deselect(handle,5-tekdumptype);
           select(handle,icon);
           tekdumptype=5-icon;
          }
          break;


  case 14:
          if(geticonint(handle,7,&temp))  /* X scale */
          {
           tekpset.xmul=temp;
           tekpset.xdiv=100;
          }

          if(geticonint(handle,9,&temp))  /* Y scale */
          {
           tekpset.ymul=temp;
           tekpset.ydiv=100;
          }

          temp=scang(iconaddr(handle,11),&minus);   /* Corner X */
          if(temp>=0)
          {
           tekpset.cx=minus?-temp:temp;
          }

          temp=scang(iconaddr(handle,13),&minus);   /* Corner Y */
          if(temp>=0)
          {
           tekpset.cy=minus?-temp:temp;
          }

          if(geticonint(handle,1,&temp))           /* Copies */
          {
           tekpset.copies=temp;
          }


          zapmenu();
          tekprintfile(!tekdumptype);
          break;
 }
}


int tekprintsetup(void)
{
 int handle=createwindow(TEKPRINT);
 
 selectst(handle,4,tekdumptype==1);
 selectst(handle,5,tekdumptype==0);

 selectst(handle,2,tekpset.flags & PORT);
 selectst(handle,3,!(tekpset.flags & PORT));

 writeiconf(handle,1,"%d",tekpset.copies);

 writeiconf(handle,7,"%d%%",(tekpset.xmul*100)/tekpset.xdiv);
 writeiconf(handle,9,"%d%%",(tekpset.ymul*100)/tekpset.ydiv);

 gsprint(iconaddr(handle,11),tekpset.cx,1);
 gsprint(iconaddr(handle,13),tekpset.cy,1);

 return(handle);
}


void poptekprint(void)
{
 int handle;
 handle=tekprintsetup();
 menuwindow(handle);
}


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


void setpoptekfile(void)
{
 unshadest(tekfile_menu,1,spfp==NULL);
 unshadest(tekfile_menu,2,spfp!=NULL);
 unshadest(tekfile_menu,3,spfp!=NULL);
 tickst(tekfile_menu,2,spoolflag);
 tickst(tekfile_menu,4,spconcode);
 tickst(tekfile_menu,5,tekinvert);
}




void decodetekfile(int m2,int m3,int m4)
{
 switch(m2)
 {
  case 0:         /* save screen */
         break;

  case 1:         /* open spool */
         break;

  case 2:
         spooltoggle();
         break;

  case 3:
         spoolclose();
         break;

  case 4:
         spoolcont();
         break;

  case 5:
         tekinvert^=1;
         break;
 }

 m2=m3=m4=0;
}



static void setpopsetup(void)
{
 tickst(vtsetup_menu,8,cedup);
}


static void decodesetup(int m2,int m3,int m4)
{
 int newsize;

 switch(m2)
 {
  case 6:
         macrosshow();
         break;

  case 7:
         tekdoreset();
         break;

  case 8:
         toggleced();
         break;
 }

 m3=m4;
}



void setpoptekterm(void)
{
 setpoptekprint();
 setpoptekfile();
 setpopsetup();
}



void decodetekterm(int m1,int m2,int m3,int m4)
{
 switch(m1)
 {
  case 0:                                 /* file   */
         decodetekfile(m2,m3,m4);
         break;

  case 1:                                 /* select */
         break;
                                    
  case 2:                                 /* printer */
         decodetekprint(m2,m3);
         break;


  case 3:
         decodesetup(m2,m3,m4);
         break;


 }
}


