// Module PI : Peida Interactive     PICMapView
// Visualisation de table de couleur     R. Ansari  07/98
// LAL (Orsay) / IN2P3-CNRS  DAPNIA/SPP (Saclay) / CEA

#include "picmapview.h"



/* --Methode-- */
PICMapView::PICMapView(PIContainerGen *par, char *nom,
                       int sx, int sy, int px, int py) 
: PIBaseWdg(par, nom, sx, sy, px, py)

{
  cmap = NULL;
  vmin = 1.;   vmax = -1.; 
}


/* --Methode-- */
PICMapView::~PICMapView()
{
  if (cmap) delete cmap;
}

 
/* --Methode-- */
void PICMapView::SetColMap(PIColorMap* cmp, float min, float max, bool refr)
{
  if (cmp == NULL)   { 
    if (cmap) delete cmap;
    cmap = NULL;
    vmin = 1.;  vmax = -1.;
    if (refr)  Refresh();  
    return;
    }
  if ( cmap && (cmp->Type() == cmap->Type()) && (cmp->Type() != CMAP_OTHER) 
       && ((vmin-min) < 1.e-19) &&  ((vmin-min) > -1.e-19) &&
       ((vmax-max) < 1.e-19) &&  ((vmax-max) > -1.e-19) )  return;
  if (cmap) delete cmap;
  cmap = new PIColorMap(*cmp);
  vmin = min;  vmax = max;
  if (refr)  Refresh();  
  return;
}

/* --Methode-- */
void PICMapView::SetColMapId(CMapId cmapid, float min, float max, bool refr)
{
  if ( cmap && (cmapid == cmap->Type()) && (cmapid != CMAP_OTHER) 
       && ((vmin-min) < 1.e-19) &&  ((vmin-min) > -1.e-19) &&
       ((vmax-max) < 1.e-19) &&  ((vmax-max) > -1.e-19) )  return;
  if (cmap) delete cmap;
  cmap = new PIColorMap(cmapid);
  vmin = min;  vmax = max;
  if (refr)  Refresh();  
  return;
}


/* --Methode-- */
void PICMapView::Draw(PIGraphicGen* g, int /*x0*/, int /*y0*/, int /*dx*/, int /*dy*/)
{
  if (!cmap) { 
    EraseWindow(0, 0, XSize(), YSize());
    return;
    }
  // On presente la table de couleur verticalement si SizeY > SizeX 
  // ou horizontale  si SizeX > SizeY

  int sx = XSize(); 
  int sy = YSize(); 

  int k;
  if (sx > sy) {    // horizontal
    int xc, dx;
    xc = 0;
    for(k=0; k<cmap->NCol(); k++) {
      dx = (sx-xc)/(cmap->NCol()-k);
      g->SelForeground((*cmap), k);
      g->DrawFBox(xc, 0, dx, sy);
      xc += dx;
    }
  }
  else  {    // vertical
    int yc, dy;
    yc = sy;
    for(k=0; k<cmap->NCol(); k++) {
      dy = yc/(cmap->NCol()-k);
      yc -= dy;
      g->SelForeground((*cmap), k);
      g->DrawFBox(0, yc, sx, dy);
    }
  }
  
  if (vmin > vmax)   return;
  if ( (cmap->Type() == CMAP_GREY32) || (cmap->Type() == CMAP_GREYINV32) ||
       (cmap->Type() == CMAP_GREY128) || (cmap->Type() == CMAP_GREYINV128) )  g->SelForeground(PI_Red);
  else if ( (cmap->Type() == CMAP_COLRJ32) || (cmap->Type() == CMAP_COLRJ128) ) g->SelForeground(PI_Magenta);
  else g->SelForeground(PI_White);

  if (sx > sy) {    // horizontal
    int fsz = (sy < 20) ? sy-8 : 12;
    g->SelFontSzPt(fsz);
    char buff[32];
    sprintf(buff, "%.2f", vmin);
    g->DrawString(4, sy-4, buff);
    sprintf(buff, "%.2f", vmax);
    g->DrawString(sx-4-(int)g->CalcStringWidth(buff), sy-4, buff);
    } 
  else {   // vertical 
    int fsz = (sx < 40) ? sx/4 : 12;
    g->SelFontSzPt(fsz);
    char buff[32];
    sprintf(buff, "%.2f", vmin);
    g->DrawString(4, sy-4, buff);
    sprintf(buff, "%.2f", vmax);
    int fa, fd;
    g->DrawString(4, g->GetFontHeight(fa,fd)+4, buff);
    } 
    
  return;
}






