/* Generated by Interface Builder */

#import "DetFreqView.h"
#import "DetController.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 DetFreqView

- initFrame:(const NXRect *)frameRect  
{
  self = [super initFrame: frameRect];
  userPath = newUserPath();
  [self setAutosizing:NX_WIDTHSIZABLE|NX_HEIGHTSIZABLE];
  [superview setAutoresizeSubviews:YES];
  [self setAutoresizeSubviews:YES];
  return self;
}
  
- getTheSmsObject
{
  if (!winDelegate) winDelegate = [window delegate];
  if ([winDelegate respondsTo:@selector(getTheSmsObject)])
       return [winDelegate getTheSmsObject];
  else return NULL;
}

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

- drawSelf:(NXRect *)rects :(int)rectCount
{
  id    theSmsObject = [self getTheSmsObject];
  int   iTraj      = 0,
        FirstFrame = [winDelegate getFirstFrame], LastFrame  = [winDelegate getLastFrame],
        NFrames    = LastFrame - FirstFrame, NFrames10 = NFrames/10, iFrame = FirstFrame;
  float OffHigh    = 16, OffLow = 26, fFreq = 0, fLastFreq = 0,
        LowFreq    = [winDelegate getLowestFreq], HighFreq   = [winDelegate getHighestFreq],
        rangeFreq  = HighFreq - LowFreq, rangeFreq10 = rangeFreq / 10, 
        rangeHigh  = HEIGHT - OffHigh, rangeHigh10 = rangeHigh/10,
	rangeLow   = WIDTH - OffLow, rangeLow10 = rangeLow/10,	
	ratio1     = rangeHigh/rangeFreq, ratio2 = rangeLow/NFrames;

  NXEraseRect(&bounds);
  PSsetlinewidth(0.0);
  PWinit();
  if (theSmsObject != NULL)
  {
    [self lockFocus];
    PWdrawYruler((int)LowFreq,(int)HighFreq,(int)rangeFreq10,(float)rangeHigh10);
    PWdrawXruler((int)FirstFrame,(int)LastFrame,(int)NFrames10,(float)rangeLow10);
    [self unlockFocus];
    beginUserPath(userPath, NO);
    UPmoveto(userPath, OffLow-1, OffHigh-1);
    UPlineto(userPath, WIDTH, OffHigh-1);
    UPmoveto(userPath, OffLow-1,  OffHigh-1);
    UPlineto(userPath, OffLow-1, HEIGHT);
    endUserPath(userPath, dps_ustroke);
    sendUserPath(userPath);
    while(iTraj < [theSmsObject getNTrajectories])
    {
      beginUserPath(userPath, NO);
      fFreq = [theSmsObject getDeterministicFreq:iFrame iTraj:iTraj];
      UPmoveto(userPath,(OffHigh+ratio2*(iFrame-FirstFrame)), (OffLow+ratio1*(fFreq-LowFreq)));
      fLastFreq = fFreq;
      for (iFrame = FirstFrame+1; iFrame <= LastFrame; iFrame++)
       {
        fFreq = [theSmsObject getDeterministicFreq:iFrame iTraj:iTraj];
        if (fFreq <= LowFreq || fLastFreq <= LowFreq)
              UPmoveto(userPath,(OffHigh+ratio2*(iFrame-FirstFrame)), (OffLow+ratio1*(fFreq-LowFreq)));
        else  UPlineto(userPath,(OffHigh+ratio2*(iFrame-FirstFrame)), (OffLow+ratio1*(fFreq-LowFreq)));
        fLastFreq = fFreq;
       }
      endUserPath(userPath, dps_ustroke);
      sendUserPath(userPath);
      iTraj++;
    }
  }
  return self;
}
@end