//->stylepal


int stylepal_handle;
int stylepal_palette = 1;
int stylepal_views = 0;
int stylepal_currnum;
int stylepal_prevnum = 0;
int stylepal_centre = 1;
int stylepal_start = -1;
int stylepal_user;
int stylepal_file;
int stylepal_open = 0;
int stylepal_stylesshaded;
int stylepal_remstylesshaded;
int stylepal_remeffectsshaded;


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

void stylepal_shadeicon(int icon, int state)
{
 set_icon_state(stylepal_handle, icon, (1 << 22) * state, (1 << 22));
}


// if state has changed, shade/unshade the 'Remove styles' option

void stylepal_shaderemstyles(int state)
{
 if(state != stylepal_remstylesshaded)
 {
  stylepal_shadeicon(0, state);
  stylepal_shadeicon(1, state);
  stylepal_remstylesshaded = !stylepal_remstylesshaded;
 }
}


// if state has changed, shade/unshade the 'Remove effects' option

void stylepal_shaderemeffects(int state)
{
 if(state != stylepal_remeffectsshaded)
 {
  stylepal_shadeicon(2, state);
  stylepal_shadeicon(3, state);
  stylepal_remeffectsshaded = !stylepal_remeffectsshaded;
 }
}


// if state has changed, shade/unshade all of the Styles

void stylepal_shadestyles(int state)
{
 int i;

 if(state != stylepal_stylesshaded)
 {
  for(i = stylepal_start; i < stylepal_currnum * 4 + stylepal_start; ++i)
   stylepal_shadeicon(i, state);
  stylepal_stylesshaded = !stylepal_stylesshaded;
 }
}


// add rows from stylepal_prevnum to n

void stylepal_addrows(int n)
{
 int i, j;
 int f0 = (1 << 0) | (3 << 12) | (0x17 << 24);
 int f1 = (1 << 0) | (3 << 12) | (1 << 8) | (0x17 << 24);
 int f2 = (1 << 0) | (1 << 9) | (0x17 << 24);
 int f3 = (1 << 0) | (1 << 3) | (0x71 << 24);

 for(i = stylepal_prevnum; i < n; ++i)
 {
  j = create_icon(stylepal_handle,  0, i * -44 -44 -112,  28, i * -44 -112, f0, "", 0, "");
  if(stylepal_start < 0)
   stylepal_start = j;
  create_icon(stylepal_handle,  28, i * -44 -44 -112, 260, i * -44 -112, f1, "", 17, "");
  create_icon(stylepal_handle, 260, i * -44 -44 -112, 350, i * -44 -112, f2, "", 0, "");
  create_icon(stylepal_handle, 350, i * -44 -30 -118, 378, i * -44 -118, f3, "", 0, "");
 }
}


// delete rows from n to stylepal_prevnum

void stylepal_delrows(int n)
{
 int i;

 for(i = n * 4 + stylepal_start; i < stylepal_prevnum * 4 + stylepal_start; ++i)
  delete_icon(stylepal_handle, i);
}


// open style palette, centring it first time

void stylepal_openpalette(void)
{
 stylepal_user = 1;
 display_window(stylepal_handle, 0, stylepal_centre);
 stylepal_user = 0;
 stylepal_centre = 0;
}


// update icon and return 1 if it has changed

int stylepal_updateicon(int icon, string s)
{
 if(readicon(stylepal_handle, icon) != s)
 {
  writeicon(stylepal_handle, icon, s);
  refresh_icon(0, stylepal_handle, icon);
  return(1);
 }
 return(0);
}


// set state of 's' and 'w' scope flags

void stylepal_setscopestate(int icon, int state)
{
 set_icon_state(stylepal_handle, icon * 4 + stylepal_start + 3, (1 << 5) * state, (1 << 5));
}


// update the style palette if it has changed

void stylepal_update(int file)
{
 int tempview, i, c, active;
 string name, list, t;

 if(!(stylepal_file=file))
 {
  stylepal_shaderemstyles(1);
  stylepal_shaderemeffects(1);
  stylepal_shadestyles(1);
 }
 else
 {
  tempview = currentview();
  setcurrentfile(stylepal_file);

  stylepal_currnum=0;
  while(getuserstyle(stylepal_currnum++,name,1));

  if(--stylepal_currnum!=stylepal_prevnum)
  {
   if(stylepal_currnum > stylepal_prevnum)
    stylepal_addrows(stylepal_currnum);
   else
    stylepal_delrows(stylepal_currnum); 
   refresh_icon(0, stylepal_handle, -1);
   extent_window(stylepal_handle, 0, -stylepal_currnum * 44 - 112, 386, 0);
   stylepal_openpalette();
   stylepal_prevnum = stylepal_currnum;
  }

  if((active=activetype(TEXTFRAME))<2)
  {
   stylepal_shaderemstyles(1);
   stylepal_shaderemeffects(1);
   stylepal_shadestyles(1);
  }
  else
  {
   stylepal_shadestyles(0);

   i = 0;
   while(getcurrentstyle(i++, name, 2))
    list += "\n" + name + "\n";
   stylepal_shaderemstyles(i == 1); 
   stylepal_shaderemeffects(!islocaleffect());
  }
  
  i = 0;
  while(getuserstyle(i, name, 0))
  {
   t = list / ("\n" + name + "\n") >= 0 ? "" : "";
   t = (active && iscurrentstyle(i)) ? "" : t;
   stylepal_updateicon(i * 4 + stylepal_start, t);

   stylepal_updateicon(i * 4 + stylepal_start + 1, name);

   getuserstyle(i, t, 1);
   stylepal_updateicon(i * 4 + stylepal_start + 2, name == t ? "" : t << slen(name));

   if(stylepal_updateicon(i * 4 + stylepal_start + 3, mids("s w", getstylescope(i), 1)))
     stylepal_setscopestate(i, getstylescope(i) != 1);
   ++i;
  }

  if(!stylepal_open) stylepal_openpalette();
  setcurrentview(tempview);
 }
}


// close palette

void stylepal_closepalette()
{
 close_window(stylepal_handle);
 stylepal_open = 0;
}


// open/close style palette

int stylepal_entry(int entry, int subcode)
{
 stylepal_palette=!stylepal_palette;
 if(stylepal_palette)  stylepal_update(currentfile());
 else                  stylepal_closepalette();
 return(0);
}


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

int stylepal_flags(int entry, string &text)
{
 return(stylepal_palette);
}


// return index of style present in selection

int stylepal_stylepresent(string style)
{
 int i = 0;
 string t;

 while(getcurrentstyle(i++, t, 2))
  if(t == style)
   return(i - 1);
}


// deal with clicks in Style palette window

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

 setcurrentfile(stylepal_file);

 if(bbits == 1 && icon > 3)
  openstylebox(getuserstyle((icon - stylepal_start) / 4, s, 2));
 if(bbits == 4 && icon >= 0)
 {
  if(icon == 0)
   remove_styles(0, 0);
  else
  if(icon == 2)
   cleareffects(0);
  else
   if((icon -  stylepal_start) % 4 == 0 && readicon(stylepal_handle, icon) == "")
    remstyle(stylepal_stylepresent(readicon(stylepal_handle, icon + 1)));
  else
   addstyle((icon - stylepal_start) / 4);
 }
}


// handler for close icon

void stylepal_closepal(int handle)
{
 stylepal_closepalette();
 stylepal_palette = 0;
}


// set window size according to the total number of styles

void stylepal_openwindowfn(int h, int x0, int y0, int x1, int y1, int scx, int scy, int bh)
{
 if(stylepal_user)
  y0 = y1 - stylepal_currnum * 44 - 112;
 open_window(h, x0, y0, x1, y1, scx, scy, bh);
 stylepal_open = 1;
}


// increment view counter when document is opened

void stylepal_openview(int user, int view)
{
 if(++stylepal_views==1 && stylepal_open) stylepal_update(filefromview(view));
}


// close palette when all views have been closed

void stylepal_closeview(int user, int view)
{
 if(--stylepal_views==0) stylepal_closepalette();
 else
 if(stylepal_open && stylepal_file && stylepal_file==filefromview(view))
 {
  stylepal_file=0;
  stylepal_shaderemstyles(1);
  stylepal_shaderemeffects(1);
  stylepal_shadestyles(1);
 }
}


// update palette whenever a key is pressed or Edit Style box closed

void stylepal_textupdate(int user, int file)
{
 if(stylepal_palette && (!user || file==stylepal_file)) stylepal_update(file);
}


// add 'Style palette' option to 'Applets' menu

void main(void)
{
 script_menu_initialise();
 addentry_menu(script_handle,"stylepal_entry","stylepal_flags","","","{STYLEPAL_00}");

 stylepal_handle = create_window("stylepal_m");
 addwindowhandler(0, stylepal_handle, "stylepal_click");
 addwindowhandler(2, stylepal_handle, "stylepal_closepal");
 addwindowhandler(3, stylepal_handle, "stylepal_openwindowfn");

 addeventhandler(0x214, 0, "stylepal_openview");
 addeventhandler(0x006, 0, "stylepal_closeview");
 addeventhandler(0x304, 0, "stylepal_textupdate");
 addeventhandler(0x305, 1, "stylepal_textupdate"); // Edit Style box closed
}
