//->colpal


int colpal_handle;
int colpal_thandle;
int colpal_palette = 1;
int colpal_views;
int colpal_currnum;
int colpal_centre = 1;
int colpal_file;
int colpal_open = 0;
int colpal_type;
int colpal_windx;
int colpal_windy;
int colpal_icon = -1;
int colpal_x;
int colpal_y;
int colpal_tick;
int colpal_tintstatus;

int colpal_horiz;
int colpal_w = 44;
int colpal_h = 44;
int colpal_min = 5;
int colpal_max = 30;

string colpal_choicespath;


// read horiz/vert choices

void colpal_readchoices(void)
{
 int h;

 h = fileopen(colpal_choicespath, "rb");
 if(h)
 {
  colpal_horiz = filegetc(h);
  fileclose(h);
 }
 else
  colpal_horiz = 2;
}


// write horiz/vert choices

void colpal_quit(int user)
{
 int h;

 h = fileopen(colpal_choicespath, "wb");
 if(h)
 {
  if(colpal_horiz)
   fileputc(2, h);
  else
   fileputc(0, h);
  fileclose(h);
 }
}


// open colour palette, centring it first time

void colpal_openpalette(void)
{
 display_window(colpal_handle, 0, colpal_centre);
 colpal_centre = 0;
} 


void colpal_updatepal(int file)
{
 string s;

 colpal_file = file;
 colpal_currnum = 0;
 while(getcolour(colpal_currnum, s, colpal_file))
  colpal_currnum++;

 refresh_icon(0, colpal_handle, -1);
 colpal_openpalette();
}


// close palette

void colpal_closepalette()
{
 close_window(colpal_handle);
 colpal_open = 0;
 colpal_file=0;
}


// open/close colour palette

int colpal_entry(int entry, int subcode)
{
 colpal_palette = !colpal_palette;
 if(colpal_palette)
  colpal_updatepal(currentfile());
 else
  colpal_closepalette();
 return(0);
}


// tick or un-tick 'Style palette' menu entry

int colpal_flags(int entry, string &text)
{
 return(colpal_palette);
}


// given x, return column number

int colpal_column(int x)
{
 return((x - colpal_windx - 1) / colpal_w);
}


// given y, return row number

int colpal_row(int y)
{
 return((colpal_windy - y - 1) / colpal_h);
}


// given r and c, return icon number

int colpal_geticon(int r, int c)
{
 int i;

 if(!colpal_horiz)
  i = (r - 1) * colpal_min + c;
 else
  i = (r - 1) * colpal_max + c;

 if(i >= colpal_currnum)
  return(-1);
 return(i);
}


// given icon number, return x

int colpal_getx(int i)
{
 int c;

 if(!colpal_horiz)
  c = i % colpal_min;
 else
  c = i % colpal_max;

 return(colpal_w * c);
}


// given icon number, return y

int colpal_gety(int i)
{
 int r;

 if(!colpal_horiz)
  r = i / colpal_min + 1;
 else
  r = i / colpal_max + 1;

 return(-colpal_h * r);
}


// update colour name icon

void colpal_colname(int file)
{
 string s, t;

 getcolour(colpal_icon, s, file);

 if(s != readicon(colpal_handle, 4))
 {
  writeicon(colpal_handle, 4, "");
  refresh_icon(0, colpal_handle, 4);
  writeicon(colpal_handle, 4, s);
  refresh_icon(0, colpal_handle, 4);
 }

 t = itos((getselectioncolour(colpal_type, 1) * 100 + 256) / 0x10000);
 if(t != readicon(colpal_handle, 3))
 {
  writeicon(colpal_handle, 3, t);
  refresh_icon(0, colpal_handle, 3);
 }
}



// refresh colour rectangle

void colpal_refresh(int i)
{
 int x, y;

 x = colpal_getx(i);
 y = colpal_gety(i);
 refresh_area(0, colpal_handle, x, y - colpal_h, x + colpal_w, y);
}


//

void colpal_readcol(int file)
{
 int i, c, t;
 string s;

 i = colpal_icon;
 c = getselectioncolour(colpal_type, 0);
 colpal_icon = 0;
 while((t = getcolour(colpal_icon, s, file)))
 {
  if(t == c)
   break;
  colpal_icon++;
 }
 if(i != colpal_icon)
 {
  colpal_refresh(i);
  colpal_refresh(colpal_icon);
 }
 colpal_colname(file);
}


// deal with clicks in Colour Palette window

void colpal_click(int handle, int icon, int bbits, int mx, int my)
{
 int i;
 string s;

 if(colpal_file!=0)
 {
  setcurrentfile(colpal_file);

  if(bbits==1 && icon==-1)
  {
   i=colpal_geticon(colpal_row(my),colpal_column(mx));
   if(i<0) i=0;
   openeditcolourbox(getcolour(i,s,colpal_file));
  }
  else 
  if(bbits == 4 || bbits == 1)
  {
   if(icon >= 0 && icon <= 2)
   { 
    colpal_type = (icon == 2) ? 4 : icon + 1;
    colpal_readcol(colpal_file);
   }
   else
   if(colpal_type)
   {
    if(colpal_geticon(colpal_row(my), colpal_column(mx)) != -1)
    {
     i = colpal_icon;
     colpal_icon = colpal_geticon(colpal_row(my), colpal_column(mx));

     if(colpal_icon != i)
     {
      colpal_refresh(i);
      colpal_refresh(colpal_icon);
      colpal_colname(colpal_file);
     }
     i = stoi(readicon(colpal_handle, 3)) * 0x10000 / 100;
     setselectioncolour(colpal_type, getcolour(colpal_icon, s, colpal_file), i, -1);
    }
   }
  }

  if(bbits == 2 && icon == 3)
   open_menu(colpal_thandle);
 }
}


//

int colpal_key(int handle, int icon, int key)
{
 int t;
 string s;

 t = stoi(readicon(colpal_handle, 3));
 if(t > 100)
 {
  t = 100;
  writeicon(colpal_handle, 3, itos(t));
 }

 setselectioncolour(colpal_type, getcolour(colpal_icon, s, colpal_file), t * 0x10000 / 100, -1);

 return(key);
}


// handler for Colour palette close icon

void colpal_closepal(int handle)
{
 colpal_closepalette();
 colpal_palette = 0;
}


void colpal_openwindowfn(int h, int x0, int y0, int x1, int y1, int scx, int scy, int bh)
{
 int t, width, height;

 t = colpal_horiz;
 if(x1 - x0 > 220 || colpal_horiz == 2)
 {
  width = colpal_currnum * colpal_w;
  if(colpal_currnum >= colpal_max)
   width = colpal_max * colpal_w;
  if(colpal_currnum < 16)
   width = 16 * colpal_w;
  height = (colpal_currnum - 1) / colpal_max * colpal_w + 2 * colpal_w;

  if(colpal_centre)
  {
   y1 = y1 + ((y0 - y1) + height) / 2;
   x0 = x0 + ((x1 - x0) - width) / 2;
  }

  x1 = x0 + width;
  y0 = y1 - height;

  extent_window(colpal_handle, 0, y0 - y1, x1 - x0, 0);
  colpal_horiz = 1;
 }
 else
 {
  width = colpal_min * colpal_w;
  height = ((colpal_currnum - 1) / colpal_min * colpal_w) + 2 * colpal_w;

  if(colpal_centre)
   y1 = y1 + ((y0 - y1) + (height)) / 2;

  x1 = x0 + width;
  y0 = y1 - height;

  extent_window(colpal_handle, 0, -880, 1452, 0);
  colpal_horiz = 0;
 }
 open_window(h, x0, y0, x1, y1, scx, scy, bh);
 if(colpal_horiz != t)
  refresh_icon(0, colpal_handle, -1);
 colpal_open = 1;
 colpal_windx = x0;
 colpal_windy = y1;
}


void colpal_plot(int p, int x, int y)
{
 swi13(69, p, x, y);
}


void colpal_redraw(int h, int x0, int x1, int y0, int y1, int scx, int scy, int gx0, int gx1, int gy0, int gy1)
{
 int r, c, i, x, y, p;

 colpal_windx = x0;
 colpal_windy = y1;

 for(r = colpal_row(gy1); r <= colpal_row(gy0); ++r)
 {
  for(c = colpal_column(gx0); c <= colpal_column(gx1); ++c)
  {
   if((i = colpal_geticon(r, c)) < 0)
    continue;

   p = getcolourpal(i, 0x10000, 0, colpal_file);
   x = colpal_getx(i);
   y = colpal_gety(i);

   if(p == -1)
    plot_icon(x + 4, y - colpal_h + 4, x + colpal_w + 4, y + 4, (1 << 1), "colpal_trans", 0, "");
   else
   {
    swin8(0, 0x40743, p, 0, 0, 256, 0, 0, 0, 0);
    colpal_plot(4, x0 + 4 + x, y1 - colpal_h + 4 + y);
    colpal_plot(0x61, colpal_w - 10, colpal_h - 10);
   }

   if(i == colpal_icon)
    plot_icon(x, y - colpal_h, x + colpal_w, y, (1 << 0) | (1 << 2) | (0x17 << 24), "", 0, "");
  }
 }
}


// count number of views opened and display colpal toolbox

void colpal_openview(int user, int view)
{
 ++colpal_views;
}


// if state == 1 shade icon, otherwise unshade

void colpal_shadeicon(int icon, int state)
{
 set_icon_state(colpal_handle, icon, (1 << 22) * state, (1 << 22));
}


// set state of buttons on info palette

void colpal_setbuttons(int s0, int s1, int s2, int s3, int t)
{
 colpal_shadeicon(0, s0);
 colpal_shadeicon(1, s1);
 colpal_shadeicon(2, s2);
 if(s3 != colpal_tintstatus)
  colpal_shadeicon(3, s3);
 select_icon(colpal_handle, 0, t == 1);
 select_icon(colpal_handle, 1, t == 2);
 select_icon(colpal_handle, 2, t == 4);

 colpal_type = t;
 colpal_tintstatus = s3;
}


// close colour palette when all views have been closed

void colpal_closeview(int user, int view)
{
 if(--colpal_views == 0)
  colpal_closepalette();
 else
 if(colpal_open && colpal_file && colpal_file==filefromview(view))
 {
  colpal_file = 0;
  colpal_setbuttons(1, 1, 1, 1, 0);
  colpal_updatepal(0);
 }
}


// update palette whenever a view gains the caret

void colpal_gaincaret(int user, int view, int gain)
{
 if(colpal_palette && gain && filefromview(view) != colpal_file)
  colpal_updatepal(filefromview(view));
}


// update palette whenever the colours are edited

void colpal_colsedited(int user, int file)
{
 if(colpal_palette)
  colpal_updatepal(file);
}


//

void colpal_updatepalette(int file)
{
 int tempview, i;

 if(!file)
  return;

 tempview = currentview();
 setcurrentfile(file);

 if(activetype(TEXTFRAME))
  colpal_setbuttons(0, 0, 0, 0, 1);
 else
 if(activetype(PICTFRAME))
  colpal_setbuttons(0, 0, 1, 0, 1);
 else
 if(activetype(0x1004))
  colpal_setbuttons(0, 0, 1, 0, 2);
 else
 {
  colpal_setbuttons(1, 1, 1, 1, 0);
  i = colpal_icon;
  colpal_icon = -1;
  colpal_refresh(i);
  colpal_colname(file);
 }

 colpal_readcol(file);

 setcurrentview(tempview);
}


void colpal_objectselect(int user, int file, int object)
{
 colpal_updatepalette(file);
}


void colpal_objectmodify(int user, int file)
{
 colpal_updatepalette(file);
}


void colpal_textupdate(int user, int file)
{
 int tempview;

 if(!file)
  return;

 tempview = currentview();
 setcurrentfile(file);

 if(activetype(TEXTFRAME) && zoneorder())
  colpal_setbuttons(0, 0, 0, 0, 4);

 colpal_readcol(file);

 setcurrentview(tempview);
}


int colpal_tentry(int entry, int subcode)
{
 int t;
 string s;

 t = stoi(string_menu(colpal_thandle, entry));
 writeicon(colpal_handle, 3, itos(t));
 setselectioncolour(colpal_type, getcolour(colpal_icon, s, colpal_file), t * 0x10000 / 100, -1);
 return(0);
}

int colpal_tflags(int entry,string & text)
{
 int t;

 t = stoi(readicon(colpal_handle, 3));

 switch(entry)
 {
  case  0:
          colpal_tick = 0;
  default:
          if(t == stoi(text)) return(colpal_tick = 1);
          break;
 }
 return(0);         
}



// add 'Colour palette' entry to 'Applets' menu

void main(void)
{
 int i;

 script_menu_initialise();

 colpal_choicespath = "OvationPro$AppletsDir";
 getenvs(colpal_choicespath);
 colpal_choicespath += ".!ColPal.Choices";

 addentry_menu(script_handle,"colpal_entry","colpal_flags","","","{COLPAL_00}");

 colpal_handle = create_window("colpal_main");
 addwindowhandler(0, colpal_handle, "colpal_click");
 addwindowhandler(1, colpal_handle, "colpal_key");
 addwindowhandler(2, colpal_handle, "colpal_closepal");
 addwindowhandler(3, colpal_handle, "colpal_openwindowfn");
 addwindowhandler(4, colpal_handle, "colpal_redraw");

 addeventhandler(0x004, 0, "colpal_quit");

 addeventhandler(0x214, 0, "colpal_openview");
 addeventhandler(0x006, 0, "colpal_closeview");
 addeventhandler(0x212, 0, "colpal_gaincaret");

 addeventhandler(0x154, 0, "colpal_colsedited");   // defined colours have changed
 addeventhandler(0x00f, 0, "colpal_objectselect"); // object selected
 addeventhandler(0x011, 0, "colpal_objectselect"); // object deselected
 addeventhandler(0x01d, 0, "colpal_objectselect"); // object deleted
 addeventhandler(0x093, 0, "colpal_objectmodify"); // object modified
 addeventhandler(0x304, 0, "colpal_textupdate");   // text modified

 colpal_thandle=create_menu("{TNM00}");
 for(i = 10; i <= 100; i = i + 10)
  addentry_menu(colpal_thandle,"colpal_tentry","colpal_tflags","","",itos(i));

 colpal_readchoices();
}
