/$ BtnLib
// Version 1.2

int btnX1[32],btnY1[32],
 btnX2[32],btnY2[32],btnStyle[32],
 btnCount,btnXDef,btnYDef,
 btnPress,btnPress2;
string btnText[32],btnIndex,
 btnCheckBitmap[2];

btnSelect2(int i){
 int b,j;
 b=btnStyle[i]/256;
 for(j=i-1;btnStyle[j]/256==b;--j)
  btnStyle[j]=btnStyle[j]+(i-b)*256;
 for(j=i;btnStyle[j]/256==b;++j)
  btnStyle[j]=btnStyle[j]+(i-b)*256;}

btnCreate2(string s,int x,int y,
 int width,int height,int style){
 int x1,y1,xl,xr,yr,xs,sel;
 if(!btnCount){
  btnCheckBitmap[0]=
"0d000000003fc02040204020402040204020403fc00000";
  btnCheckBitmap[1]=
"0d000000303f7020e02dc02f802740224020403fc00000";}
 ++btnCount;
 if(x<0)
  x=btnXDef;
 if(y<0)
  y=btnYDef;
 else
  btnYDef=y;
 btnX1[btnCount]=x;
 btnY1[btnCount]=y;
 btnX2[btnCount]=x+width;
 btnY2[btnCount]=y+height;
 btnText[btnCount]=s;
 if(style>127){
  if(style>511){
   sel=btnCount;
   style=style-512;}
  else
   sel=btnStyle[btnCount-1]/256;
  btnStyle[btnCount]=style%256+
   sel*256;
  if(style>255)
   btnSelect2(btnCount);}
 else
  btnStyle[btnCount]=style;
 btnXDef=x+width+1+
  (style%8>3)*5;
 xl=x/20;
 xr=(x+width)/20;
 yr=(y+height)/20*8-128;
 for(y1=y/20*8-128;y1<=yr;++y1)
  for(x1=xl;x1<=xr;++x1)
   btnIndex=btnIndex+
    (char)(x1+y1)+(char)btnCount;
 return btnCount;}

btnCreate(string s,int x,int y,
 int width,int style){
 if(width<0)
  width=3+strlen(s)*6;
 return btnCreate2(s,x,y,width,12,
  style);}

btnCreateBitmap(string s,int x,int y,
 int style){
 int width,height;
 width="0x"+strleft(s,2);
 height=(strlen(s)-2)/
  ((width+3)/4);
 return btnCreate2(s,x,y,width,
  height,style+8);}

btnCreateCheck(int x,int y,
 int style){
 if(style%16>7)
  return btnCreate2("1",x,y,13,11,
   style+8);
 else
  return btnCreate2("0",x,y,13,11,
   style+16);}

btnRect(int i,int c,int r){
 rect(c,btnX1[i],btnY1[i],btnX2[i],
  btnY2[i],r);}

btnPaint(int i){
 int r,c;
 if(btnStyle[i]%64>31)
  return;
 r=btnStyle[i]%8>3;
 c=btnStyle[i]%4;
 if((btnStyle[i]%32)>15)
  bitmap(btnX1[i],btnY1[i],
   btnCheckBitmap[btnText[i]]);
 else if((btnStyle[i]%16)>7)
  bitmap(btnX1[i],btnY1[i],
   btnText[i]);
 else{
  btnRect(i,0,0);
  text(btnX1[i]+1+r*2,btnY1[i],
   btnText[i]);}
 if(c)
  frame(c,btnX1[i],btnY1[i],
   btnX2[i],btnY2[i],r*4);
 if(btnStyle[i]/256==i)
  btnRect(i,3,r*4);}

btnPaintAll(){
 int i;
 for(i=1;i<=btnCount;++i)
  btnPaint(i);}

btnHit(int x,int y){
 int i,p;
 string b;
 b=(char)(x/20+y/20*8-128);
 while((p=strstr(btnIndex,b,p))
  !=-1){
  i=(char)substr(btnIndex,++p,1);
  if(btnStyle[i]%64<32&&
   x>=btnX1[i]&&x<=btnX2[i]&&
   y>=btnY1[i]&&y<=btnY2[i])
   return i;}
 return 0;}

btnSelect(int i){
 int b;
 if(b=btnStyle[i]/256){
  btnSelect2(i);
  btnPaint(i);
  btnPaint(b);}}

btnEvent(int e){
 int b,r;
 btnPress2=0;
 if(e==2&&!btnPress){
  if(btnPress=
   btnHit(penx(),peny())){
   if(btnStyle[btnPress]%32>15){
    bitmap(btnX1[btnPress],
     btnY1[btnPress],
     btnCheckBitmap[1-
     (int)btnText[btnPress]]);
     return 1;}
    b=btnStyle[btnPress]/256;
    r=(btnStyle[btnPress]%8>3)*4;
    btnRect(btnPress,3,r);
    if(b)
     btnRect(b,3,r);
    return 1;}}
 else if(e==3&&btnPress){
  r=(btnStyle[btnPress]%8>3)*4;
  b=btnStyle[btnPress]/256;
  if(btnHit(penx(),peny())==
   btnPress){
   btnPress2=btnPress;
   if(btnStyle[btnPress]%32>15)
    btnText[btnPress]=1-
    (int)btnText[btnPress];
   if(b){
    btnSelect2(btnPress);
    btnPress=0;
    return 1;}}
  btnPaint(btnPress);
  if(b)
   btnRect(b,3,r);
  btnPress=0;
  return 1;}
 return 0;}

btnSelected(int i)
{return btnStyle[i]/256;}

btnPressed() {return btnPress2;}

btnSetLabel(int i,string s)
{btnText[i]=s; btnPaint(i);}

btnChecked(int i)
{return (int)btnText[i];}

btnCheck(int i,int state)
{btnSetLabel(i,state);}

btnHide(int i,int state){
 if((btnStyle[i]/32)%2!=state)
  if(state){
   btnStyle[i]=btnStyle[i]+32;
   btnRect(i,0,0);
   if(btnStyle[i]%4)
    frame(0,btnX1[i],btnY1[i],
     btnX2[i],btnY2[i],0);}
  else{
   btnStyle[i]=btnStyle[i]-32;
   btnPaint(i);}}

btnReset(){
 btnIndex="";
 btnCount=0;}
