
/* Generated by Interface Builder */

#import "StocCoeffView.h"
#import "StocController.h"
#import "SmsObject.h"
#import <dpsclient/wraps.h>				// for PSxxx stuff
#import "PWfft.h"

#define WIDTH bounds.size.width
#define HEIGHT bounds.size.height


@implementation StocCoeffView

- initFrame:(const NXRect *)frameRect  
{
  self = [super initFrame: frameRect];
  userPath = newUserPath();
  fOffset = 0;
  
  return self;
}
  
- getTheSmsObject
{
  if (!winDelegate)
    winDelegate = [window delegate];

  if ([winDelegate respondsTo:@selector(getTheSmsObject)])
    return [winDelegate getTheSmsObject];
  else
    return NULL;
}

//-------------------------------------------------------------------
// Translate from x/y values to display pixel coordinates
  
- (float) xToPix:(float)iCoeff:(float)x
{
  return (30 + 
         (iCoeff - [winDelegate getLowestCoeff]) * gSection + 
	  x * fSection);
}

- (float) yToPix:(float)y:(float)iCoeff:(float)iFrame
{
  int d = [winDelegate getHighestCoeff]-[winDelegate getLowestCoeff];
  float a, b, c;

  a = y-[winDelegate getBottomValue];
  b = [winDelegate getTopValue]-[winDelegate getBottomValue];
  c = [winDelegate getLastFrameCoeff]-[winDelegate getFirstFrameCoeff];
  return (80 + 
         (HEIGHT - 100) * iFrame / c + 
	  40 * a/b - 
	  60 * (iCoeff - [winDelegate getLowestCoeff]) / d);
}

//-------------------------------------------------------------------
// - drawCoeff  draw one coefficient of the SmsObject
  
- drawCoeff:(int)iFrame
{
  id theSmsObject = [self getTheSmsObject];
  int iCoeff;
  int iFirstFrame = [winDelegate getFirstFrameCoeff];
  int iLastFrame = [winDelegate getLastFrameCoeff];
  int incFrame = (iLastFrame - iFirstFrame) / 100;
  float i = iFrame - iFirstFrame;
  float fCoeff = 0;
  
  if (theSmsObject != NULL)
  {    
    beginUserPath(userPath, NO);
    if (incFrame < 1) incFrame = 1;
    for (iCoeff = [winDelegate getLowestCoeff]; iCoeff <= [winDelegate getHighestCoeff]; iCoeff = iCoeff + incFrame)
       {
        UPmoveto(userPath, [self xToPix:iCoeff:i],[self yToPix:0:iCoeff:i]);
        UPlineto(userPath, [self xToPix:iCoeff:i], [self yToPix:0:iCoeff:i]);
       }

    iCoeff = [winDelegate getLowestCoeff];
    fCoeff = [theSmsObject getStochasticCoefficient:iFrame iCoeff:iCoeff];
    UPmoveto(userPath, [self xToPix:iCoeff:i],[self yToPix:fCoeff:iCoeff:i]);
    for (iCoeff = [winDelegate getLowestCoeff]+1; iCoeff <= [winDelegate getHighestCoeff]; iCoeff++)
       { 
        fCoeff = [theSmsObject getStochasticCoefficient:iFrame iCoeff:iCoeff];
        UPlineto(userPath, [self xToPix:iCoeff:i],[self yToPix:fCoeff:iCoeff:i]);
       }

    endUserPath(userPath, dps_ustroke);
    sendUserPath(userPath);
  }
  return self;
}
//-------------------------------------------------------------------
// - drawSelf:: - Draw the coefficient trajectories in the view

- drawSelf:(NXRect *)rects :(int)rectCount
{
  id theSmsObject = [self getTheSmsObject];
  int iFrame   = [winDelegate getFirstFrameCoeff];
  float lowW = 30, lowH = 20, highH = 80;

  fSection =  (.35 * WIDTH) / ([winDelegate getLastFrameCoeff] - [winDelegate getFirstFrameCoeff]);
  gSection =  (.6 * WIDTH) / ([winDelegate getHighestCoeff]-[winDelegate getLowestCoeff]);
  NXEraseRect(&bounds);
  PSsetlinewidth(0.0);
  PWinit();

  if (theSmsObject != NULL)
  {
    beginUserPath(userPath, NO);
    UPmoveto(userPath, lowW, highH);
    UPlineto(userPath, lowW+(.6 * WIDTH), lowH);
    endUserPath(userPath, dps_ustroke);
    sendUserPath(userPath);

    [self lockFocus];
    PWdrawX1ruler((int)([winDelegate getLowestCoeff] + 1), (int)([winDelegate getHighestCoeff] + 1), 
		  (int)2, 2*gSection,-(120.0 / ([winDelegate getHighestCoeff] - [winDelegate getLowestCoeff])));
    [self unlockFocus];

    while(iFrame <= [winDelegate getLastFrameCoeff])
     {
      [self drawCoeff:iFrame];
      iFrame++;
     }
  }
  return self;
}

@end
