// Copyright 1998 Acorn Computers Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#include "display.h"
#include "kernel.h"
#include "walkth.h"

void
PalWin::set_update(int bool)
{
 char buffer[20];

    if (update == bool)
       return;

    update = bool;

    if (bool == TRUE)
        sprintf( buffer, "Palette *%s", pal_file);
    else
        sprintf( buffer, "Palette %s", pal_file);

    SetTitle( 0, buffer);
}

/* ------------------------------------------------------------------------ */
void
PalWin::DispatchEvent( ToolboxEvent *event, IdBlock *idb )
{
  if (event->hdr.event_code == WritableField_ValueChanged){
    set_update(TRUE);
    return;
  }
  if (event->hdr.event_code != ActionButton_Selected){
    Window::DispatchEvent(event, idb);
    return;
  }
  switch (idb->self_component)
    {
      case 0x19:	// walkthrough
        new drawfile();
      	break;
      case 0x26:	// save
         read_toolbox_window();
         if (!validate_calib(&pal_table.tab.calib))
         	Error(error_construct("Err_ValidCalib"));
         set_tables(&pal_table.tab.calib, &pal_table.tab);
         write_palette();
         write_toolbox_window();
         update_cached_palette();
         set_update(FALSE);
        break;
      case 0x27:	// refresh
         read_palette();
         write_toolbox_window();
         set_update(FALSE);
        break;
      case 0x2f:	// cancel
         SpalWin *temp;			// temp now floats addressable trough the toolbox
         temp = new SpalWin(pal_file);
         temp->pal_table = pal_table;
         temp->write_toolbox_window();
         Hide(0);
        break;
      case 0x30:	// clear
         pal_table.set_default_calib();
         write_toolbox_window();
         set_update(TRUE);
        break;
      default:
  	Window::DispatchEvent(event, idb);
    }
}

PalWin::PalWin(void)
{	// no op so I can run the child constructor
}

PalWin::PalWin(char *palette_file)
{
  // This function should read a palette and write those values to the
  // writables of the toolbox window
  ComponentId comp_id;
  ObjectId parent;

  pal_file = new char[strlen(palette_file)+1];
  strcpy( pal_file, palette_file);

  Window::Create(0, "Control");
  for (comp_id = 2; comp_id <= 23; comp_id++)
  	PalVal[comp_id-2].Attach(mOID, comp_id);

  walk.Attach(mOID, 0x19);
  save.Attach(mOID, 0x26);
  refresh.Attach(mOID, 0x27);

  paldata = NULL;

  if (!read_palette()){ // read parameters set to default if old version
       Error(error_construct("Err_File"));
       return ;
  }

  write_toolbox_window();

  comp_id = 0;
  GetParent(0, &parent, &comp_id);
  Show( 0, 0, NULL, parent, comp_id );

  set_update(FALSE);
}

PalWin::~PalWin(void)
{
  TRACE("deleting a palette instance");
  // raise a do you want to save dialogue box
  delete pal_file;
  delete paldata;
  Window::~Window;
}

int
PalWin::read_palette()
{
  FILE *fp;
  char file_path[FILE_PATH_LENGTH];
  sprintf(file_path, "Printers:palettes.%s",pal_file);

  fp = fopen(file_path, "r");
    if ( fp == NULL){
        Error(error_construct("Err_File"));
        return FALSE;
    }
    fread(&palhdr,sizeof(palhdr_t),1,fp);

    if ((palhdr.version != 1) && (palhdr.version != 2)){
  	Error(error_construct("Err_PalVer"));
	fclose(fp);
  	return FALSE;
    }
    datasize = palhdr.reproinfo - sizeof(palhdr_t);

    delete paldata;			// no effect on null
    paldata = new uint8[datasize];	// freed in the delete
    if (paldata == NULL){
    	BadError(error_construct("Err_Memory"));
    	fclose(fp);
  	return FALSE;
    }

    fread(paldata,datasize,1,fp);
    if ( palhdr.version == 1 ){
  	// notify that the palette is of version 1
       read_toolbox_window();	// set the default from the template
    }
    else
        fread(&pal_table, sizeof(tables_t),1,fp);
  fclose(fp);

  return TRUE;
}

int
PalWin::write_palette()
{
  FILE *fp;
  char file_path[FILE_PATH_LENGTH];
  sprintf(file_path, "PrinterChoices:palettes.%s",pal_file);

  set_tables(&pal_table.tab.calib, &pal_table.tab); // call the palgen code calib -> tables
  fp = fopen(file_path, "w");
  if ( fp == NULL){
        Error(error_construct("Err_File"));
        return FALSE;
  }

  palhdr.version = 2;
  fwrite(&palhdr,sizeof(palhdr_t),1,fp);
  fwrite(paldata,datasize,1,fp);
  fwrite(&pal_table.tab, sizeof(tables_t),1,fp);
  fclose(fp);
  return TRUE;
}

void
PalWin::update_cached_palette()
{
    /* only updates the new-tables part */

  _kernel_swi_regs r;
  int           handle,syshandle,fsno;
  uint32        *u, palette_version, hdr;
  calib_table   *t;
  char 		fname[FILE_PATH_LENGTH];

  sprintf(fname,"Resources:Resources.Printers.Palettes.%s",pal_file);

  r.r[0] = 0x43;
  r.r[1] = (int)fname;
  Error(_kernel_swi(OS_Find, &r, &r));
  handle = r.r[0];

  if (handle != 0)
  {
    r.r[0] = 21; 		// return filing system file handle
    r.r[1] = handle;
    Error(_kernel_swi(OS_FSControl,&r, &r));
    syshandle = r.r[1];		// pointer to the palette in memory
    fsno = r.r[2] & 255;	// filing system information word
    if (fsno == 46) 		// ResourceFS
    {
      u = (uint32 *)syshandle;
      palette_version = *u;	// the first element in the palette
      u = (uint32 *)(syshandle - ((strlen(fname)-strlen("Resources:")+4) & ~3) - 28);
      hdr = *u;

      if ((palette_version == 2) && (hdr == 0x48434143))
      {
        t = (calib_table *)(syshandle + (sizeof(palhdr_t) + datasize));
        (*t) = pal_table;
      }
      else  Error(error_construct("Err_File"));

      r.r[0] = 0;
      r.r[1] = handle;
      Error(_kernel_swi(OS_Find,&r, &r)); /* close */
    }// end if resorce FS
  } // else it is not cached
}

void
PalWin::write_toolbox_window(void)
{
  char text[10];
  TRACE("Palwin write");

  sprintf(text, "%3.1f",pal_table.tab.calib.ediff_density);
  PalVal[0].SetValue(0, text);
  sprintf(text, "%2.2f",pal_table.tab.calib.bright[0]);
  PalVal[1].SetValue(0, text);
  sprintf(text, "%2.2f",pal_table.tab.calib.bright[1]);
  PalVal[2].SetValue(0, text);
  sprintf(text, "%2.2f",pal_table.tab.calib.contrast);
  PalVal[3].SetValue(0, text);
  sprintf(text, "%2.2f",pal_table.tab.calib.red_adj_my);
  PalVal[4].SetValue(0, text);
  sprintf(text, "%2.2f",pal_table.tab.calib.green_adj_cy);
  PalVal[5].SetValue(0, text);
  sprintf(text, "%2.2f",pal_table.tab.calib.blue_adj_cm);
  PalVal[6].SetValue(0, text);
  sprintf(text, "%1.2f",pal_table.tab.calib.gamma[0]);
  PalVal[7].SetValue(0, text);
  sprintf(text, "%1.2f",pal_table.tab.calib.gamma[1]);
  PalVal[8].SetValue(0, text);
  sprintf(text, "%1.2f",pal_table.tab.calib.gamma[2]);
  PalVal[9].SetValue(0, text);
  sprintf(text, "%1.2f",pal_table.tab.calib.gamma[3]);
  PalVal[10].SetValue(0, text);
  sprintf(text, "%3.1f",pal_table.tab.calib.k_gcr[0]);
  PalVal[11].SetValue(0, text);
  sprintf(text, "%3.1f",pal_table.tab.calib.k_gcr[1]);
  PalVal[12].SetValue(0, text);
  sprintf(text, "%3.1f",pal_table.tab.calib.k_gen[0]);
  PalVal[13].SetValue(0, text);
  sprintf(text, "%3.1f",pal_table.tab.calib.k_gen[1]);
  PalVal[14].SetValue(0, text);
  sprintf(text, "%i", (int)(((double)pal_table.tab.calib.k_mnz/256)*100));
  PalVal[15].SetValue(0, text);
  sprintf(text, "%3.1f",pal_table.tab.calib.deplete1ink[0]);
  PalVal[16].SetValue(0, text);
  sprintf(text, "%3.1f",pal_table.tab.calib.deplete1ink[1]);
  PalVal[17].SetValue(0, text);
  sprintf(text, "%3.1f",pal_table.tab.calib.deplete1ink[2]);
  PalVal[18].SetValue(0, text);
  sprintf(text, "%3.1f",pal_table.tab.calib.deplete1ink[3]);
  PalVal[19].SetValue(0, text);
  sprintf(text, "%3.1f",pal_table.tab.calib.deplete2ink);
  PalVal[20].SetValue(0, text);
  sprintf(text, "%3.1f",pal_table.tab.calib.deplete3ink);
  PalVal[21].SetValue(0, text);
}

void
PalWin::read_toolbox_window(void)
{
  TRACE("PalWin read");
  char text[10], *chrptr;
  int  nbytes;

  PalVal[0].GetValue(0, text, 10, &nbytes); chrptr = text + nbytes;
  pal_table.tab.calib.ediff_density = (float)strtod(text, &chrptr);
  PalVal[1].GetValue(0, text, 10, &nbytes); chrptr = text + nbytes;
  pal_table.tab.calib.bright[0] = (float)strtod(text, &chrptr);
  PalVal[2].GetValue(0, text, 10, &nbytes); chrptr = text + nbytes;
  pal_table.tab.calib.bright[1] = (float)strtod(text, &chrptr);
  PalVal[3].GetValue(0, text, 10, &nbytes); chrptr = text + nbytes;
  pal_table.tab.calib.contrast = (float)strtod(text, &chrptr);
  PalVal[4].GetValue(0, text, 10, &nbytes); chrptr = text + nbytes;
  pal_table.tab.calib.red_adj_my = (float)strtod(text, &chrptr);
  PalVal[5].GetValue(0, text, 10, &nbytes); chrptr = text + nbytes;
  pal_table.tab.calib.green_adj_cy = (float)strtod(text, &chrptr);
  PalVal[6].GetValue(0, text, 10, &nbytes); chrptr = text + nbytes;
  pal_table.tab.calib.blue_adj_cm = (float)strtod(text, &chrptr);
  PalVal[7].GetValue(0, text, 10, &nbytes); chrptr = text + nbytes;
  pal_table.tab.calib.gamma[0] = (float)strtod(text, &chrptr);
  PalVal[8].GetValue(0, text, 10, &nbytes); chrptr = text + nbytes;
  pal_table.tab.calib.gamma[1] = (float)strtod(text, &chrptr);
  PalVal[9].GetValue(0, text, 10, &nbytes); chrptr = text + nbytes;
  pal_table.tab.calib.gamma[2] = (float)strtod(text, &chrptr);
  PalVal[10].GetValue(0, text, 10, &nbytes); chrptr = text + nbytes;
  pal_table.tab.calib.gamma[3] = (float)strtod(text, &chrptr);
  PalVal[11].GetValue(0, text, 10, &nbytes); chrptr = text + nbytes;
  pal_table.tab.calib.k_gcr[0] = (float)strtod(text, &chrptr);
  PalVal[12].GetValue(0, text, 10, &nbytes); chrptr = text + nbytes;
  pal_table.tab.calib.k_gcr[1] = (float)strtod(text, &chrptr);
  PalVal[13].GetValue(0, text, 10, &nbytes); chrptr = text + nbytes;
  pal_table.tab.calib.k_gen[0] = (float)strtod(text, &chrptr);
  PalVal[14].GetValue(0, text, 10, &nbytes); chrptr = text + nbytes;
  pal_table.tab.calib.k_gen[1] = (float)strtod(text, &chrptr);
  PalVal[15].GetValue(0, text, 10, &nbytes); chrptr = text + nbytes;
  pal_table.tab.calib.k_mnz = (int)((strtod(text, &chrptr)/100)*256);
  PalVal[16].GetValue(0, text, 10, &nbytes); chrptr = text + nbytes;
  pal_table.tab.calib.deplete1ink[0] = (float)strtod(text, &chrptr);
  PalVal[17].GetValue(0, text, 10, &nbytes); chrptr = text + nbytes;
  pal_table.tab.calib.deplete1ink[1] = (float)strtod(text, &chrptr);
  PalVal[18].GetValue(0, text, 10, &nbytes); chrptr = text + nbytes;
  pal_table.tab.calib.deplete1ink[2] = (float)strtod(text, &chrptr);
  PalVal[19].GetValue(0, text, 10, &nbytes); chrptr = text + nbytes;
  pal_table.tab.calib.deplete1ink[3] = (float)strtod(text, &chrptr);
  PalVal[20].GetValue(0, text, 10, &nbytes); chrptr = text + nbytes;
  pal_table.tab.calib.deplete2ink = (float)strtod(text, &chrptr);
  PalVal[21].GetValue(0, text, 10, &nbytes); chrptr = text + nbytes;
  pal_table.tab.calib.deplete3ink = (float)strtod(text, &chrptr);

  if (!validate_calib(&pal_table.tab.calib)){
         	Error(error_construct("Err_ValidCalib"));
         	write_toolbox_window();
  }
}

void
PalWin::OnHasBeenHiddenEvent( WindowHasBeenHiddenEvent *has_been_hidden, IdBlock *idb )
{
  Window::OnHasBeenHiddenEvent( has_been_hidden, idb );
  delete this;
}
/* ------------------------------------------------------------------------ */
void
SpalWin::set_no_black(int bool)
{
  unsigned int flags;
  if (bool == TRUE){
  	// sets the grey variables to 'faded' in the toolbox, de&f
  	Error(PalVal[0xD].GetFlags(0,&flags));
  	flags = flags | Gadget_Faded;
  	Error(PalVal[0xD].SetFlags(0,flags));
  	Error(PalVal[0xe].GetFlags(0,&flags));
  	flags = flags | Gadget_Faded;
   	Error(PalVal[0xe].SetFlags(0,flags));
  	Error(PalVal[0xf].GetFlags(0,&flags));
  	flags = flags | Gadget_Faded;
   	Error(PalVal[0xf].SetFlags(0,flags));
  	Error(PalVal[0xa].GetFlags(0,&flags));
  	flags = flags | Gadget_Faded;
   	Error(PalVal[0xa].SetFlags(0,flags));
 	pal_table.tab.calib.k_gcr[0] = 0.0f;
  }
  else{
  	Error(PalVal[0xD].GetFlags(0,&flags));
  	flags = flags & ~Gadget_Faded;
   	Error(PalVal[0xD].SetFlags(0,flags));
  	Error(PalVal[0xe].GetFlags(0,&flags));
  	flags = flags & ~Gadget_Faded;
   	Error(PalVal[0xe].SetFlags(0,flags));
  	Error(PalVal[0xf].GetFlags(0,&flags));
  	flags = flags & ~Gadget_Faded;
   	Error(PalVal[0xf].SetFlags(0,flags));
  	Error(PalVal[0xa].GetFlags(0,&flags));
  	flags = flags & ~Gadget_Faded;
   	Error(PalVal[0xa].SetFlags(0,flags));
 	pal_table.tab.calib.k_gcr[0] = 100.0f;
  }
  Radio_col.SetState(0, bool);
  set_update(FALSE);
}

void
SpalWin::DispatchEvent( ToolboxEvent *event, IdBlock *idb )
{
  if ((event->hdr.event_code == RadioButton_StateChanged )
      &&(idb->self_component == 0x2c)){
        RadioButtonStateChangedEvent *r_event = (RadioButtonStateChangedEvent*)event;
    	set_no_black(r_event->state);
    	write_toolbox_window();
  }
  else if (event->hdr.event_code == ActionButton_Selected){
     if (idb->self_component == 0x31){
     	  PalWin *temp;
     	  temp = new PalWin(pal_file);
     	  read_toolbox_window();
     	  temp->pal_table = pal_table;	// nifty isn't it.
     	  temp->write_toolbox_window();
     	  Hide(0);
     } else
     if(idb->self_component == 0x2f)
     	  Hide(0);
     else
     	  PalWin::DispatchEvent(event, idb);
  }
  else
  PalWin::DispatchEvent(event, idb);
}

SpalWin::SpalWin(char *palette_file)
{
  // This function should read a palette and write those values to the
  // writables of the toolbox window
  ComponentId comp_id;
  ObjectId parent;

  pal_file = new char[strlen(palette_file)+1];
  strcpy( pal_file, palette_file);

  Window::Create(0, "SControl");
  for (comp_id = 1; comp_id <= 18; comp_id++)
  	PalVal[comp_id].Attach(mOID, comp_id);

  walk.Attach(mOID, 0x19);
  save.Attach(mOID, 0x26);
  refresh.Attach(mOID, 0x27);

  Radio_b_w.Attach(mOID, 0x2D);
  Radio_col.Attach(mOID, 0x2c);

  paldata = NULL;

  if (!read_palette()){ // read parameters set to default if old version
       Error(error_construct("Err_File"));
       return ;
  }

  write_toolbox_window();
  set_no_black(!pal_table.tab.calib.k_gcr[0]); // if this value is zero we are using a CMY palette

  comp_id = 0;
  GetParent(0, &parent, &comp_id);
  Show( 0, 0, NULL, parent, comp_id );


  set_update(FALSE);
}

void
SpalWin::write_toolbox_window(void)	// return boolean success
{
   char text[10];
   float temp;

  temp = 100.0 - pal_table.tab.calib.ediff_density;

  sprintf(text, "%3.1f",pal_table.tab.calib.bright[0]);
  PalVal[1].SetValue(0, text);
  sprintf(text, "%3.1f",pal_table.tab.calib.contrast);
  PalVal[2].SetValue(0, text);
  sprintf(text, "%1.2f",pal_table.tab.calib.gamma[0]);
  PalVal[3].SetValue(0, text);
  sprintf(text, "%1.2f",pal_table.tab.calib.gamma[1]);
  PalVal[4].SetValue(0, text);
  sprintf(text, "%1.2f",pal_table.tab.calib.gamma[2]);
  PalVal[5].SetValue(0, text);
  sprintf(text, "%1.2f",pal_table.tab.calib.gamma[3]);
  PalVal[6].SetValue(0, text);
  sprintf(text, "%3.1f",pal_table.tab.calib.deplete1ink[0]-temp);
  PalVal[7].SetValue(0, text);
  sprintf(text, "%3.1f",pal_table.tab.calib.deplete1ink[1]-temp);
  PalVal[8].SetValue(0, text);
  sprintf(text, "%3.1f",pal_table.tab.calib.deplete1ink[2]-temp);
  PalVal[9].SetValue(0, text);
  sprintf(text, "%3.1f",pal_table.tab.calib.deplete1ink[3]-temp);
  PalVal[10].SetValue(0, text);
  sprintf(text, "%3.1f",pal_table.tab.calib.deplete2ink-temp);
  PalVal[11].SetValue(0, text);
  sprintf(text, "%3.1f",pal_table.tab.calib.deplete3ink-temp);
  PalVal[12].SetValue(0, text);

  sprintf(text, "%2.2f",pal_table.tab.calib.red_adj_my);
  PalVal[16].SetValue(0, text);
  sprintf(text, "%2.2f",pal_table.tab.calib.green_adj_cy);
  PalVal[17].SetValue(0, text);
  sprintf(text, "%2.2f",pal_table.tab.calib.blue_adj_cm);
  PalVal[18].SetValue(0, text);

  sprintf(text, "%3.1f",pal_table.tab.calib.k_gcr[0]/2.0);
  PalVal[13].SetValue(0, text);
  sprintf(text, "%3.1f",pal_table.tab.calib.k_gen[0]/2.0);
  PalVal[14].SetValue(0, text);
  sprintf(text, "%i",(int)(((double)pal_table.tab.calib.k_mnz/256)*100) );
  PalVal[15].SetValue(0, text);
}

void
SpalWin::read_toolbox_window(void)	// writes the structures to the palette
{
  char text[10], *chrptr;
  int  nbytes;
  float temp;

  PalVal[1].GetValue(0, text, 10, &nbytes); chrptr = text + nbytes;
  pal_table.tab.calib.bright[0] = (float)strtod(text, &chrptr);
  PalVal[2].GetValue(0, text, 10, &nbytes); chrptr = text + nbytes;
  pal_table.tab.calib.contrast = (float)strtod(text, &chrptr);

  PalVal[3].GetValue(0, text, 10, &nbytes); chrptr = text + nbytes;
  pal_table.tab.calib.gamma[0] = (float)strtod(text, &chrptr);
  PalVal[4].GetValue(0, text, 10, &nbytes); chrptr = text + nbytes;
  pal_table.tab.calib.gamma[1] = (float)strtod(text, &chrptr);
  PalVal[5].GetValue(0, text, 10, &nbytes); chrptr = text + nbytes;
  pal_table.tab.calib.gamma[2] = (float)strtod(text, &chrptr);
  PalVal[6].GetValue(0, text, 10, &nbytes); chrptr = text + nbytes;
  pal_table.tab.calib.gamma[3] = (float)strtod(text, &chrptr);

  PalVal[7].GetValue(0, text, 10, &nbytes); chrptr = text + nbytes;
  pal_table.tab.calib.deplete1ink[0] = (float)strtod(text, &chrptr);
  PalVal[8].GetValue(0, text, 10, &nbytes); chrptr = text + nbytes;
  pal_table.tab.calib.deplete1ink[1] = (float)strtod(text, &chrptr);
  PalVal[9].GetValue(0, text, 10, &nbytes); chrptr = text + nbytes;
  pal_table.tab.calib.deplete1ink[2] = (float)strtod(text, &chrptr);
  PalVal[10].GetValue(0, text, 10, &nbytes); chrptr = text + nbytes;
  pal_table.tab.calib.deplete1ink[3] = (float)strtod(text, &chrptr);

  PalVal[11].GetValue(0, text, 10, &nbytes); chrptr = text + nbytes;
  pal_table.tab.calib.deplete2ink = (float)strtod(text, &chrptr);
  PalVal[12].GetValue(0, text, 10, &nbytes); chrptr = text + nbytes;
  pal_table.tab.calib.deplete3ink = (float)strtod(text, &chrptr);

  PalVal[13].GetValue(0, text, 10, &nbytes); chrptr = text + nbytes;
  pal_table.tab.calib.k_gcr[0] = 2.0*(float)strtod(text, &chrptr);
  PalVal[14].GetValue(0, text, 10, &nbytes); chrptr = text + nbytes;
  pal_table.tab.calib.k_gen[0] = 2.0*(float)strtod(text, &chrptr);
  PalVal[15].GetValue(0, text, 10, &nbytes); chrptr = text + nbytes;
  pal_table.tab.calib.k_mnz = (int)((strtod(text, &chrptr)*256/100));

  PalVal[16].GetValue(0, text, 10, &nbytes); chrptr = text + nbytes;
  pal_table.tab.calib.red_adj_my = (float)strtod(text, &chrptr);
  PalVal[17].GetValue(0, text, 10, &nbytes); chrptr = text + nbytes;
  pal_table.tab.calib.green_adj_cy = (float)strtod(text, &chrptr);
  PalVal[18].GetValue(0, text, 10, &nbytes); chrptr = text + nbytes;
  pal_table.tab.calib.blue_adj_cm = (float)strtod(text, &chrptr);
 	// calculate the remainder of the values

  pal_table.tab.calib.ediff_density = max_depletion();			// set the density to the maximum value
  temp = 100.0 -  pal_table.tab.calib.ediff_density;			// scale the others by this value

  pal_table.tab.calib.deplete1ink[3] =  pal_table.tab.calib.deplete1ink[3] + temp;
  pal_table.tab.calib.deplete1ink[0] =  pal_table.tab.calib.deplete1ink[0] + temp;
  pal_table.tab.calib.deplete1ink[1] =  pal_table.tab.calib.deplete1ink[1] + temp;
  pal_table.tab.calib.deplete1ink[2] =  pal_table.tab.calib.deplete1ink[2] + temp;
  pal_table.tab.calib.deplete2ink =  pal_table.tab.calib.deplete2ink + temp;
  pal_table.tab.calib.deplete3ink =  pal_table.tab.calib.deplete3ink + temp;

  pal_table.tab.calib.bright[1] = 0.75; // half tone
  pal_table.tab.calib.k_gcr[1]  = 100; 	// 100% value
  pal_table.tab.calib.k_gen[1]  = 100;

  if (!validate_calib(&pal_table.tab.calib)){
         	Error(error_construct("Err_ValidCalib"));
         	write_toolbox_window();
  }
}

float
SpalWin::max_depletion(void)
{
  float max;
  int state;
  ComponentId id = 0x2c;

  Radio_col.GetState(0, &state, &id);

  if (!state)	// if col not selected we need to include black
  	max = pal_table.tab.calib.deplete1ink[3];
  else
  	max = pal_table.tab.calib.deplete1ink[2];

  if (pal_table.tab.calib.deplete1ink[0] > max) max = pal_table.tab.calib.deplete1ink[0];
  if (pal_table.tab.calib.deplete1ink[1] > max) max = pal_table.tab.calib.deplete1ink[1];
  if (pal_table.tab.calib.deplete1ink[2] > max) max = pal_table.tab.calib.deplete1ink[2];
  if (pal_table.tab.calib.deplete2ink > max)    max = pal_table.tab.calib.deplete2ink;
  if (pal_table.tab.calib.deplete3ink > max)    max = pal_table.tab.calib.deplete3ink;

  return max;
}
