/* Generated by Interface Builder */

#import "StocMagView.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 StocMagView

- initFrame:(const NXRect *)frameRect  
{
  self = [super initFrame: frameRect];
  userPath = newUserPath();
  
  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)x 
{
  float a = x-[winDelegate getFirstFrameMag];
  float b = [winDelegate getLastFrameMag]-[winDelegate getFirstFrameMag];
  return (WIDTH * a/b);
}

- (float) yToPix:(float)y 
{
  float a = y-[winDelegate getBottomDb];
  float b = [winDelegate getTopDb]-[winDelegate getBottomDb];
  return (16 + (HEIGHT - 16) * a/b);
}

//-------------------------------------------------------------------
// - drawSelf:: - Draw the magnitude trajectories in the view

- drawSelf:(NXRect *)rects :(int)rectCount
{
  id theSmsObject = [self getTheSmsObject];
  int iFrame = [winDelegate getFirstFrameMag];
  float fMag = 0, fLastMag = 0;
  float OffHigh = 16, OffLow = 1,
  FirstFrame = [winDelegate getFirstFrameMag],
  LastFrame = [winDelegate getLastFrameMag],
  range10    = (LastFrame - FirstFrame) / 10,
  rangeLow10 = WIDTH/10;
  
  NXEraseRect(&bounds);
  PSsetlinewidth(0.0);
  PWinit();
  
  if (theSmsObject != NULL)
  {
    [self lockFocus];
    PWdrawStocruler((int)FirstFrame,(int)LastFrame,(int)range10,(float)rangeLow10);
    [self unlockFocus];
  
    beginUserPath(userPath, NO);
    UPmoveto(userPath, OffLow-1, OffHigh-1);
    UPlineto(userPath, WIDTH, OffHigh-1);
    
    fMag = [theSmsObject getStochasticGain:iFrame];
    UPmoveto(userPath, [self xToPix:(float)iFrame], [self yToPix:fMag]);
    fLastMag = fMag;
    
    for (iFrame = [winDelegate getFirstFrameMag]+1; 
         iFrame <= [winDelegate getLastFrameMag]; iFrame++)
    {
      fMag = [theSmsObject getStochasticGain:iFrame];
      UPlineto(userPath, [self xToPix:(float)iFrame], [self yToPix:fMag]);
      fLastMag = fMag;
    }
    endUserPath(userPath, dps_ustroke);
    sendUserPath(userPath);
  }
  return self;
}

@end
