/*->c.selcol */


#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.wos"
#include "h.ram"
#include "h.file"
#include "h.main"
#include "h.DrawLib"
#include "h.pic"
#include "h.mym"


#include "h.pan"
#include "h.data"



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

static int whichindex;    /* which index are we setting */


static int  pala[3];
static int  ptag;                      /* tag for the selector window */
static int  pdrag;
static int  pbase;                     /* base icon number for the selector */
static wimp_palettestr palt;




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[ptag],pbase+19+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[ptag],pbase+20+i*5,0,0); /* forcing a redraw */
  wrpal(i);
 }
 seti(whandle[ptag],pbase+16,0,0);
}


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



void setcols(int tag,int icon_base,int col)    /* init colour selector */
{
 ptag=tag;
 pbase=icon_base;
 setrgb(col);
}




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




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

 p=(icon-pbase-19)/5;

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

 pala[p]=val;
 seti(whandle[ptag],pbase+20+p*5,0,0);
 seti(whandle[ptag],pbase+16,0,0);
 wrpal(p);
}





void selectcolicon(void)
{
 getw(whandle[ptag]);

 icon-=pbase;

 switch(icon)
 {
  case  20:     /* R */
  case  25:     /* G */
  case  30:     /* B */
           geti(whandle[ptag],pbase+icon);
           if(!pdrag) 
           { 
            userdrag(whandle[ptag],bx+ix0,by+iy0,bx+ix1+deltax,by+iy1);
            startdrag(PDRAG,whandle[SELECTCOL]);
            pdrag=icon; 
           }
           break;
                                      
   case 17:           /* inc R */
   case 18:           /* dec R */
           if(icon==17 && pala[0]==255) break;
           if(icon==18 && !pala[0]) break;
           pala[0]+=1-((icon==18)<<1);
           seti(whandle[ptag],pbase+20,0,0);
           seti(whandle[ptag],pbase+16,0,0);
           wrpal(0);
           break;

   case 22:           /* inc G */
   case 23:           /* dec G */
           if(icon==22 && pala[1]==255) break;
           if(icon==23 && !pala[1]) break;
           pala[1]+=1-((icon==23)<<1);
           seti(whandle[ptag],pbase+25,0,0);
           seti(whandle[ptag],pbase+16,0,0);
           wrpal(1);
           break;

   case 27:           /* inc B */
   case 28:           /* dec B */
           if(icon==27 && pala[2]==255) break;
           if(icon==28 && !pala[2]) break;
           pala[2]+=1-((icon==28)<<1);
           seti(whandle[ptag],pbase+30,0,0);
           seti(whandle[ptag],pbase+16,0,0);
           wrpal(2);
           break;


   case 33:                                            /* OK */
           readcols(&linecolour[whichindex]);
           if(buttons==0x4) zapmenu();
           break;


   default:
           if(icon>=0 && icon<16)    /* click on individual colour */
           {
            wimp_readpalette(&palt);
            setrgb(palt.c[icon].word);
           }
           break;
 }
}





void citycolicon(void)
{
 int temp;

 switch(icon)
 {

  case 34:
          showname^=1;
          optst(whandle[CITYCOL],34,showname);
          break;

  case 36:
          showcountry^=1;
          optst(whandle[CITYCOL],36,showcountry);
          break;

  case 38:
          showstate^=1;
          optst(whandle[CITYCOL],38,showstate);
          break;


  case 33: /* OK */
          if(sscanf(iconaddr(whandle[CITYCOL],40),"%d",&temp)==1)
                                                            textsize=temp;

  default:
          selectcolicon();
          break;
 }

}






void gridcolicon(void)
{
 int temp;

 switch(icon)
 {
  case 34:
          showbullet^=1;
          optst(whandle[GRIDCOL],34,showbullet);
          break;

  case 33: /* OK */
          if(sscanf(iconaddr(whandle[GRIDCOL],36),"%d",&temp)==1)
                                                           bulletsize=temp;

  default:
          selectcolicon();
          break;
 }
}








void selcoldragend(void)
{
 pdrag=0;
}




void selcoldragzero(void)
{
 int tx;

 getpointer();
 getw(whandle[ptag]);
 geti(whandle[ptag],pdrag);
 tx=(mousex-bx-ix0);
 if(tx<0) tx=0;
 if(tx>255) tx=255;
 if(pala[(pdrag-20) & 3]!=tx)
 {
  pala[(pdrag-20) & 3]=tx;
  seti(whandle[ptag],pbase+pdrag,0,0);
  wrpal((pdrag-20) & 3);
  seti(whandle[ptag],pbase+16,0,0);
 }
}












void selectcolred(void)
{
 int more;
 wimp_redrawstr rblock;
 int i;
 int palv;

 getw(ewindow);
 rblock.w=ewindow;
 wimp_redraw_wind(&rblock,&more);
 while(more)
 { 
  for(i=0;i<3;i++)  
  {
   geti(ewindow,pbase+20+i*5);
   wimp_setcolour(7);
   palv=pala[i];
   if(palv>252) palv=252;
   if(palv>0) bbc_rectanglefill(bx+ix0,by+iy0,palv,iy1-iy0-deltay);
   wimp_setcolour(0);                  
   if(palv<252) 
             bbc_rectanglefill(bx+ix0+palv,by+iy0,252-palv,iy1-iy0-deltay);
  }

  geti(ewindow,pbase+16);
  setgcolfrompala();
  bbc_rectanglefill(bx+ix0,by+iy0,ix1-ix0-2*deltax,iy1-iy0-deltay);

  wimp_get_rectangle(&rblock,&more);
 }
}



void citycolred(void)
{
 selectcolred();
}


void gridcolred(void)
{
 selectcolred();
}






void selectcolkey(int * key)
{




}





void citycolkey(int * key)
{




}



void gridcolkey(int * key)
{




}







int selcoldynam(int m3)
{
 int handle;

 whichindex=m3;

 if(whichindex==BULLET)
 {
  handle=createwindow(GRIDCOL);
  setcols(GRIDCOL,0,linecolour[whichindex]);    /* init colour selector */
  optst(handle,34,showbullet);
  writeiconf(handle,36,"%d",bulletsize);
 }
 else
 if(whichindex==CITY)
 {

  handle=createwindow(CITYCOL);
  setcols(CITYCOL,0,linecolour[whichindex]);    /* init colour selector */
  optst(handle,34,showname);
  optst(handle,36,showcountry);
  optst(handle,38,showstate);
  writeiconf(handle,40,"%d",textsize);
 }
 else
 {
  handle=createwindow(SELECTCOL);
  setcols(SELECTCOL,0,linecolour[whichindex]);    /* init colour selector */
 }

 return(handle);
}



