Index: trunk/SophyaPI/PI/piscdrawwdg.cc
===================================================================
--- trunk/SophyaPI/PI/piscdrawwdg.cc	(revision 8)
+++ trunk/SophyaPI/PI/piscdrawwdg.cc	(revision 9)
@@ -3,5 +3,4 @@
 #include <algo.h>
 
-
 PIScDrawWdg::PIScDrawWdg(PIContainerGen *par, char *nom,
                          int sx, int sy, int px, int py)
@@ -11,4 +10,6 @@
   limitsFixed = 0;
   xEndDrag = yEndDrag = xBegDrag = yBegDrag = 0;
+  SetAxesFlags(kAxesDflt);
+  clip = true;
 }
 
@@ -51,8 +52,10 @@
 PIScDrawWdg::CalcScale()
 {
-  xScale = XSize() / (xMax-xMin);
-  yScale = -YSize() / (yMax-yMin);
-  xOrg   = -xMin * xScale + XPos();
-  yOrg   = -yMax * yScale + YPos();
+  double margeH = (xMax-xMin)*0.2;
+  double margeV = (yMax-yMin)*0.2;
+  xScale = XSize() / (xMax-xMin + 2*margeH);
+  yScale = -YSize() / (yMax-yMin + 2*margeV);
+  xOrg   = (-xMin + margeH) * xScale + XPos();
+  yOrg   = (-yMax - margeV) * yScale + YPos();
 }
 
@@ -81,8 +84,14 @@
     if (yFirstMinTick < yMin) yFirstMinTick += yMinTickStep;
     
-    xMajTickLen = -2/yScale;
-    xMinTickLen = -1/yScale;
-    yMajTickLen = 2/xScale;
-    yMinTickLen = 1/xScale;
+//    xMajTickLen = -2/yScale;
+//    xMinTickLen = -1/yScale;
+//    yMajTickLen = 2/xScale;
+//    yMinTickLen = 1/xScale;
+
+    yMajTickLen = (xMax-xMin)/100;
+    yMinTickLen = (xMax-xMin)/250;
+    xMajTickLen = (yMax-yMin)/100;
+    xMinTickLen = (yMax-yMin)/250;
+
 }
 
@@ -108,4 +117,20 @@
   dypix = int(dy*yScale + .5);
 }
+
+double
+PIScDrawWdg::ScStringWidth(char const* s)
+{
+  return CalcStringWidth(s) / xScale;
+}
+
+void
+PIScDrawWdg::ScSelFontSz(double size, PIFontAtt att)
+{
+  int npt = int(fabs(size * yScale)+.5);
+  if (npt < 8) npt = 8;
+  if (npt > 127) npt = 127;
+  SelFontSzPt(npt, att);
+}
+
 
 void
@@ -120,4 +145,35 @@
 PIScDrawWdg::ScDrawLine(double x1, double y1, double x2, double y2)
 {
+  
+  if (clip) {
+    if (x2 < xMin && x1 < xMin) return;
+    if (x2 > xMax && x1 > xMax) return;
+    if (y1 < yMin && y2 < yMin) return;
+    if (y2 > yMax && y2 > yMax) return;
+    if (x2 < x1) {
+      swap(x1,x2);
+      swap(y1,y2);
+    }
+    if (x2>xMax) {
+      y2 = y1 + (y2-y1)/(x2-x1)*(xMax-x1);
+      x2 = xMax;
+    }
+    if (x1<xMin) {
+      y1 = y2 + (y1-y2)/(x1-x2)*(xMin-x2);
+      x1 = xMin;
+    }
+    if (y2 < y1) {
+      swap(x1,x2);
+      swap(y1,y2);
+    }
+    if (y2>yMax) {
+      x2 = x1 + (x2-x1)/(y2-y1)*(yMax-y1);
+      y2 = yMax;
+    }
+    if (y1<yMin) {
+      x1 = x2 + (x1-x2)/(y1-y2)*(yMin-y2);
+      y1 = yMin;
+    }
+  }
   int x1Pix, y1Pix, x2Pix, y2Pix;
   Sc2Local(x1, y1, x1Pix, y1Pix);
@@ -129,4 +185,27 @@
 PIScDrawWdg::ScDrawBox(double x, double y, double dx, double dy)
 {
+  if (clip) {
+    if (dx < 0) {
+      x = x+dx;
+      dx = -dx;
+    }
+    if (dy < 0) {
+      y = y+dy;
+      dy = -dy;
+    }
+    if (x > xMax || x+dx < xMin || y > yMax || y+dy < yMin) return;
+    if (x < xMin) {
+      dx -= (xMin-x);
+      x = xMin;
+    }
+    if (y < yMin) {
+      dy -= (yMin-y);
+      y = yMin;
+    }
+    if (x+dx > xMax)
+      dx = (xMax-x);
+    if (y+dy > yMax)
+      dy = (yMax-y);
+  }
   int xPix, yPix, x2Pix, y2Pix;
   Sc2Local(x, y, xPix, yPix);
@@ -138,4 +217,27 @@
 PIScDrawWdg::ScDrawFBox(double x, double y, double dx, double dy)
 {
+  if (clip) {
+    if (dx < 0) {
+      x = x+dx;
+      dx = -dx;
+    }
+    if (dy < 0) {
+      y = y+dy;
+      dy = -dy;
+    }
+    if (x > xMax || x+dx < xMin || y > yMax || y+dy < yMin) return;
+    if (x < xMin) {
+      dx -= (xMin-x);
+      x = xMin;
+    }
+    if (y < yMin) {
+      dy -= (yMin-y);
+      y = yMin;
+    }
+    if (x+dx > xMax)
+      dx = (xMax-x);
+    if (y+dy > yMax)
+      dy = (yMax-y);
+  }
   int xPix, yPix, x2Pix, y2Pix;
   Sc2Local(x, y, xPix, yPix);
@@ -163,106 +265,145 @@
 
 void
-PIScDrawWdg::DrawAxes(int tickLevel, int markLevel)
-{
-  // Les axes
-  
-  ScDrawLine(xMin, 0, xMax, 0);
-  ScDrawLine(0, yMin, 0, yMax);
-  
-  // Les ticks majeurs
-  
-  if (tickLevel > 0) {
-    for (double x=xFirstMajTick; x<=xMax; x += xMajTickStep)
-      ScDrawLine(x, -xMajTickLen, x, xMajTickLen);
-    for (double y=yFirstMajTick; y<=yMax; y += yMajTickStep)
-      ScDrawLine(-yMajTickLen, y, yMajTickLen, y);
-  }
-
-  // Les ticks mineurs
-  
-  if (tickLevel > 1) {
-    for (double x=xFirstMinTick; x<=xMax; x += xMinTickStep)
-      ScDrawLine(x, -xMinTickLen, x, xMinTickLen);
-    for (double y=yFirstMinTick; y<=yMax; y += yMinTickStep)
-      ScDrawLine(-yMinTickLen, y, yMinTickLen, y);
-  }
-
-
-}
-
-void
-PIScDrawWdg::SetBBoxMargin(double h, double v)
-{
-  bbmargeh = h;
-  bbmargev = v; 
-}
-
-void
-PIScDrawWdg::DrawBBox(int tickLevel, int tickFlags, int markLevel)
-{
-
-  double xLow = xMin + bbmargeh;
-  double xHig = xMax - bbmargeh;
-  double yLow = yMin + bbmargev;
-  double yHig = yMax - bbmargev;
-  
-  double extXMajTickLen = tickFlags&2 ? xMajTickLen : 0;
-  double intXMajTickLen = tickFlags&1 ? xMajTickLen : 0;
-  double extXMinTickLen = tickFlags&2 ? xMinTickLen : 0;
-  double intXMinTickLen = tickFlags&1 ? xMinTickLen : 0;
-  double extYMajTickLen = tickFlags&2 ? yMajTickLen : 0;
-  double intYMajTickLen = tickFlags&1 ? yMajTickLen : 0;
-  double extYMinTickLen = tickFlags&2 ? yMinTickLen : 0;
-  double intYMinTickLen = tickFlags&1 ? yMinTickLen : 0;
-  
-  
-  // Les bordures
-  
-  ScDrawLine(xLow, yLow, xHig, yLow);
-  ScDrawLine(xHig, yLow, xHig, yHig);
-  ScDrawLine(xHig, yHig, xLow, yHig);
-  ScDrawLine(xLow, yHig, xLow, yLow);
-  
-  // Les ticks majeurs
-  
-  if (tickLevel > 0) {
-    for (double x=xFirstMajTick; x<=xHig; x += xMajTickStep) {
-      ScDrawLine(x, yLow - extXMajTickLen, x, yLow + intXMajTickLen);
-      ScDrawLine(x, yHig + extXMajTickLen, x, yHig + intXMajTickLen);
-    }
-    for (double y=yFirstMajTick; y<=yHig; y += yMajTickStep) {
-      ScDrawLine(xLow - extYMajTickLen, y, xLow + intYMajTickLen, y);
-      ScDrawLine(xHig + extYMajTickLen, y, xHig - intYMajTickLen, y);
-    }
-  }
-  
-
-  // Les ticks mineurs
-  
-  if (tickLevel > 1) {
-    for (double x=xFirstMinTick; x<=xHig; x += xMinTickStep) {
-      ScDrawLine(x, yLow - extXMinTickLen, x, yLow + intXMinTickLen);
-      ScDrawLine(x, yHig + extXMinTickLen, x, yHig + intXMinTickLen);
-    }
-    for (double y=yFirstMinTick; y<=yHig; y += yMinTickStep) {
-      ScDrawLine(xLow - extYMinTickLen, y, xLow + intYMinTickLen, y);
-      ScDrawLine(xHig + extYMinTickLen, y, xHig - intYMinTickLen, y);
-    }
-  }
-  
-  // Les labels
-  if (markLevel > 0) {
-    for (double x=xFirstMajTick; x<=xHig; x += xMajTickStep) {
+PIScDrawWdg::DrawHTicks(double y, double tickUp, double tickDown, double xBeg, double xStep)
+{
+  for (double x=xBeg; x<=xMax; x += xStep)
+    ScDrawLine(x, y-tickDown, x, y+tickUp);
+}
+
+void
+PIScDrawWdg::DrawVTicks(double x, double tickLeft, double tickRight, double yBeg, double yStep)
+{
+  for (double y=yBeg; y<=yMax; y += yStep)
+    ScDrawLine(x-tickLeft, y, x+tickRight, y);
+}
+
+void
+PIScDrawWdg::DrawHLabels(double y, double xBeg, double xStep, int just)
+{
+  double xOffset = 0;
+  
+  ScSelFontSz(xMajTickLen*4);
+  for (double x=xBeg; x<=xMax; x += xStep) {
       char label[20]; sprintf(label, "%-6g", x);
-      ScDrawString(x, yLow - 8 * xMajTickLen, label);
-      ScDrawString(x, yHig + 2 * xMajTickLen, label);
-    }
-    for (double y=yFirstMajTick; y<=yHig; y += yMajTickStep) {
+      double largeur = ScStringWidth(label);
+      if (just == 1)
+        xOffset = -largeur;
+      else if (just == 0)
+        xOffset = -largeur/2;
+      else
+        xOffset = 0;
+      ScDrawString(x+xOffset, y, label);
+  }
+}
+
+void
+PIScDrawWdg::DrawVLabels(double x, double yBeg, double yStep, int just)
+{
+  double xOffset = 0;
+  ScSelFontSz(xMajTickLen*4);
+  for (double y=yBeg; y<=yMax; y += yStep) {
       char label[20]; sprintf(label, "%-6g", y);
-      ScDrawString(xLow - 8 * yMajTickLen, y, label);
-      ScDrawString(xHig + 2 * yMajTickLen, y, label);
-    }
-  }
-}
+      double largeur = ScStringWidth(label);
+      if (just == 1)
+        xOffset = -largeur;
+      else if (just == 0)
+        xOffset = -largeur/2;
+      else
+        xOffset = 0;
+      ScDrawString(x+xOffset, y, label);
+  }
+}
+
+
+void
+PIScDrawWdg::DrawAxes(int flags)
+{
+  NoClip();
+  if (flags == -1)
+    flags = axesFlags;
+    
+  
+  if (flags & kStdAxes) {
+  
+    // Les axes
+    
+    ScDrawLine(xMin, 0, xMax, 0);
+    ScDrawLine(0, yMin, 0, yMax);
+  
+    // Les ticks majeurs
+  
+    if (flags & kMajTicks) {
+        DrawHTicks(0, xMajTickLen, xMajTickLen, xFirstMajTick, xMajTickStep);
+        DrawVTicks(0, yMajTickLen, yMajTickLen, yFirstMajTick, yMajTickStep);
+    }
+    
+    // Les ticks mineurs
+  
+    if (flags & kMinTicks) {
+        DrawHTicks(0, xMinTickLen, xMinTickLen, xFirstMinTick, xMinTickStep);
+        DrawVTicks(0, yMinTickLen, yMinTickLen, yFirstMinTick, yMinTickStep);
+    }
+    
+    // Les labels
+    
+    if (flags & kLabels) {
+        DrawHLabels(-xMajTickLen*8, xFirstMajTick, xMajTickStep,0);
+        DrawVLabels(-yMajTickLen*2, yFirstMajTick, yMajTickStep,1);
+    }
+    
+  }
+  
+  if (flags & kBoxAxes) {
+  
+    // La boite
+    
+    ScDrawLine(xMin, yMin, xMax, yMin);
+    ScDrawLine(xMax, yMin, xMax, yMax);
+    ScDrawLine(xMax, yMax, xMin, yMax);
+    ScDrawLine(xMin, yMax, xMin, yMin);
+
+    // Longueur des ticks
+    
+    double extXMajTickLen = flags&kExtTicks ? xMajTickLen : 0;
+    double intXMajTickLen = flags&kIntTicks ? xMajTickLen : 0;
+    double extXMinTickLen = flags&kExtTicks ? xMinTickLen : 0;
+    double intXMinTickLen = flags&kIntTicks ? xMinTickLen : 0;
+    double extYMajTickLen = flags&kExtTicks ? yMajTickLen : 0;
+    double intYMajTickLen = flags&kIntTicks ? yMajTickLen : 0;
+    double extYMinTickLen = flags&kExtTicks ? yMinTickLen : 0;
+    double intYMinTickLen = flags&kIntTicks ? yMinTickLen : 0;
+
+    // Les ticks majeurs
+  
+    if (flags & kMajTicks) {
+        DrawHTicks(yMin, intXMajTickLen, extXMajTickLen, xFirstMajTick, xMajTickStep);
+        DrawHTicks(yMax, extXMajTickLen, intXMajTickLen, xFirstMajTick, xMajTickStep);
+        DrawVTicks(xMin, extYMajTickLen, intYMajTickLen, yFirstMajTick, yMajTickStep);
+        DrawVTicks(xMax, intYMajTickLen, extYMajTickLen, yFirstMajTick, yMajTickStep);
+    }
+    
+    // Les ticks mineurs
+  
+    if (flags & kMinTicks) {
+        DrawHTicks(yMin, intXMinTickLen, extXMinTickLen, xFirstMinTick, xMinTickStep);
+        DrawHTicks(yMax, extXMinTickLen, intXMinTickLen, xFirstMinTick, xMinTickStep);
+        DrawVTicks(xMin, extYMinTickLen, intYMinTickLen, yFirstMinTick, yMinTickStep);
+        DrawVTicks(xMax, intYMinTickLen, extYMinTickLen, yFirstMinTick, yMinTickStep);
+    }
+
+
+    // Les labels
+    
+    if (flags & kLabels) {
+        DrawHLabels(yMin-xMajTickLen*8, xFirstMajTick, xMajTickStep,0);
+        DrawHLabels(yMax+xMajTickLen*2, xFirstMajTick, xMajTickStep,0);
+        DrawVLabels(xMin-yMajTickLen*2, yFirstMajTick, yMajTickStep,1);
+        DrawVLabels(xMax+yMajTickLen*2, yFirstMajTick, yMajTickStep,-1);
+    }
+  }
+  Clip();
+}
+
+  
 
 void
@@ -302,9 +443,13 @@
 
 void
-PIScDrawWdg::SetAxesKind(int axesKind, int tickLevel, int markLevel)
-{
-  axesChoice  = axesKind;
-  tickChoice  = tickLevel;
-  labelChoice = markLevel;
+PIScDrawWdg::SetAxesFlags(int flags)
+{
+  axesFlags = flags;
+  if (axesFlags & (kIntTicks | kExtTicks | kMajTicks | kMinTicks))
+    axesFlags |= kTicks;
+  if ((axesFlags & (kTicks | kIntTicks | kExtTicks)) == kTicks)
+    axesFlags |= kIntTicks | kExtTicks;
+  if ((axesFlags & (kTicks | kMajTicks | kMinTicks)) == kTicks)
+    axesFlags |= kMajTicks;
 }
 
@@ -313,9 +458,7 @@
 {
   EraseWindow();
-  if (axesChoice == 1)
-    DrawAxes(tickChoice, labelChoice);
-  else if (axesChoice == 2)
-    DrawBBox(tickChoice, labelChoice);
+  DrawAxes();
   CallDrawers();
+  DrawSelf();
 }
 
@@ -329,5 +472,4 @@
   xEndDrag = xBegDrag = x;
   yEndDrag = yBegDrag = y;
-  PIBeep();
 }
 
@@ -429,5 +571,7 @@
 PIScGFFDrawer::PIScGFFDrawer(GeneralFitFunction* f)
 : mFunc(f), mNParms(f->NPar()), mParms(new double[mNParms])
-{}
+{
+  ASSERT(f->NVar() == 1);
+}
 
 PIScGFFDrawer::~PIScGFFDrawer()
@@ -437,6 +581,9 @@
 
 void
-PIScGFFDrawer::SetParms(double const*)
-{}
+PIScGFFDrawer::SetParms(double const* p)
+{
+  for (int i=0; i<mNParms; i++)
+    mParms[i] = p[i];
+}
 
 
@@ -444,4 +591,22 @@
 PIScGFFDrawer::Draw()
 {
-}
-
+  if (!mDrawWdg) return;
+  double xMax = mDrawWdg->XMax();
+  double xMin = mDrawWdg->XMin();
+  double yMax = mDrawWdg->YMax();
+  double yMin = mDrawWdg->YMin();
+  double xStep = (xMax - xMin)/100;
+  double xOld = xMin;
+  double yOld = 0;
+//  double yOld = f->Value(&xMin, mParms);
+  for (double x = xMin+xStep; x<xMax; x+=xStep) {
+   double y = 0; // $CHECK$
+//    double y = f->Value(&x, mParms);
+    if (y>yMin && yOld>yMin && 
+        y<yMax && yOld<yMax) 
+           mDrawWdg->ScDrawLine(xOld, yOld, x, y);
+    xOld = x;
+    yOld = y;
+  }
+}
+
