/* Copyright (C) 1996,1997,1998,1999 by Salvador E. Tropea (SET),
   see copyrigh file for details */
#ifdef __DJGPP__
#include <stdio.h>

#define Uses_TDialog
#define Uses_TScrollBar
#define Uses_TRadioButtons
#define Uses_TRect
#define Uses_TSItem
#define Uses_TLabel
#define Uses_TEvent
#define Uses_TButton
#define Uses_TKeys
#define Uses_TKeys_Extended
#define Uses_TStaticText
#include <tv.h>

#include <tpaltext.h>
#define Uses_SETAppVarious
#define Uses_SETAppDialogs
#define Uses_SETAppConst
#include <setapp.h>

static char *patterNumShow="123456789012";

class TNumShow : public TStaticText
{
public:
 TNumShow(const TRect &bounds);
 void setValue(int val);
};

TNumShow::TNumShow(const TRect &bounds) :
   TStaticText(bounds,patterNumShow)
{
 setValue(0);
}

void TNumShow::setValue(int val)
{
 sprintf((char *)(text+1),"%d",val);
 *((char *)text)=3;
 draw();
}


class TRadioCol : public TRadioButtons
{
public:
 TRadioCol(const TRect &bounds, TSItem *items, TScrollBar *r, TScrollBar *g,
           TScrollBar *b, TNumShow *rr, TNumShow *rg, TNumShow *rb) :
   TRadioButtons(bounds,items),
   red(r), green(g), blue(b), nred(rr), ngreen(rg), nblue(rb) { press(0); }
 virtual void press(int item);
 virtual void movedTo(int item);
 unsigned getValue(void) { return value; }

protected:
 TScrollBar *red,*green,*blue;
 TNumShow *nred,*ngreen,*nblue;
 void SetBars(void);
};

void TRadioCol::SetBars(void)
{
 int r,g,b;
 EditorPalette->GetOne(value,r,g,b);
 red->setParams(r,0,63,8,1);
 green->setParams(g,0,63,8,1);
 blue->setParams(b,0,63,8,1);
 nred->setValue(r);
 ngreen->setValue(g);
 nblue->setValue(b);
}

void TRadioCol::press(int item)
{
 value=item;
 SetBars();
}

void TRadioCol::movedTo(int item)
{
 press(item);
}

class TDiaPal : public TDialog
{
public:
 TDiaPal(void);
 virtual void handleEvent(TEvent& event);
 virtual void draw(void);
 TRadioCol *colorsel;
 TScrollBar *red;
 TScrollBar *green;
 TScrollBar *blue;
 TNumShow *rn,*gn,*bn;
};

TDiaPal::TDiaPal(void) :
         TDialog(TRect(0,0,52,19),_("Palette editor")),
         TWindowInit(&TDiaPal::initFrame)
{
 options|=ofCentered;
 helpCtx=cmeEditPalette;
 TLabel *rl,*gl,*bl;

 TRect r(14,1,50,2);
 insert(new TStaticText(r,_("Color RGB composition")));
 r.move(0,3);
 red=new TScrollBar(r);
 r.move(0,1);
 rn=new TNumShow(r);
 r.move(0,-2);
 rl=new TLabel(r,_("Red"),red);
 r.move(0,5);
 green=new TScrollBar(r);
 r.move(0,1);
 gn=new TNumShow(r);
 r.move(0,-2);
 gl=new TLabel(r,_("Green"),green);
 r.move(0,5);
 blue=new TScrollBar(r);
 r.move(0,1);
 bn=new TNumShow(r);
 r.move(0,-2);
 bl=new TLabel(r,_("Blue"),blue);

 colorsel=new TRadioCol(TRect(2,2,7,18),
              new TSItem("",
              new TSItem("",
              new TSItem("",
              new TSItem("",
              new TSItem("",
              new TSItem("",
              new TSItem("",
              new TSItem("",
              new TSItem("",
              new TSItem("",
              new TSItem("",
              new TSItem("",
              new TSItem("",
              new TSItem("",
              new TSItem("",
              new TSItem("", 0 )))))))))))))))),red,green,blue,rn,gn,bn);
 insert(colorsel);
 insert(new TLabel(TRect(1,1,10,2),_("Color"),colorsel));
 insert(rl);
 insert(red);
 insert(rn);
 insert(gl);
 insert(green);
 insert(gn);
 insert(bl);
 insert(blue);
 insert(bn);
 insert(new TButton(TRect(15,16,27,18),_("~O~K"),cmOK,bfDefault));
 insert(new TButton(TRect(34,16,46,18),_("~D~efault"),cmCancel,bfNormal));
 selectNext(False);
}

void TDiaPal::draw(void)
{
 TDialog::draw();
 ushort buf[5];
 ushort col;
 int i,j;
 for (i=0; i<16; i++)
    {
     col=(((i<<4) | i)<<8) | ' ';
     for (j=0; j<5; j++)
         buf[j]=col;
     writeBuf(7,i+2,5,1,buf);
    }
}

void TDiaPal::handleEvent(TEvent& event)
{
 if (event.what==evBroadcast && event.message.command==cmScrollBarChanged)
   {
    int r,g,b;
    TScrollBar *p=(TScrollBar *)event.message.infoPtr;
    EditorPalette->GetOne(colorsel->getValue(),r,g,b);
    if (p==red)
      {
       r=p->value;
       rn->setValue(r);
      }
    else
    if (p==green)
      {
       g=p->value;
       gn->setValue(g);
      }
    else
    if (p==blue)
      {
       b=p->value;
       bn->setValue(b);
      }

    EditorPalette->SetOne(colorsel->getValue(),r,g,b);
   }
 else
 if ((event.what==evCommand && event.message.command==cmCancel) ||
     (event.what==evKeyDown && event.keyDown.keyCode==kbEsc))
   {
    EditorPalette->BackToDefault();
    endModal(cmCancel);
    clearEvent(event);
    return;
   }
 else
 if (event.what==evKeyDown)
   {
    int aux;
    switch (event.keyDown.keyCode)
      {
       case kbR:
            aux=red->value;
            if (aux>0)
              {
               aux--;
               red->setValue(aux);
               rn->setValue(aux);
              }
            break;
       case kbShR:
            aux=red->value;
            if (aux<63)
              {
               aux++;
               red->setValue(aux);
               rn->setValue(aux);
              }
            break;
       case kbG:
            aux=green->value;
            if (aux>0)
              {
               aux--;
               green->setValue(aux);
               gn->setValue(aux);
              }
            break;
       case kbShG:
            aux=green->value;
            if (aux<63)
              {
               aux++;
               green->setValue(aux);
               gn->setValue(aux);
              }
            break;
       case kbB:
            aux=blue->value;
            if (aux>0)
              {
               aux--;
               blue->setValue(aux);
               bn->setValue(aux);
              }
            break;
       case kbShB:
            aux=blue->value;
            if (aux<63)
              {
               aux++;
               blue->setValue(aux);
               bn->setValue(aux);
              }
            break;
      }
   }

 TDialog::handleEvent(event);
}

void EditPalette(void)
{
 TDialog *d=new TDiaPal();
 execDialog(d,0);
}
#else
void EditPalette(void)
{
}
#endif
