/* Generated by Interface Builder */

#import "DetController.h"
#import "SmsObject.h"
#import <appkit/appkit.h>

@implementation DetController

- init:(id)controller
{
  [super init];
  [NXApp loadNibSection:"Deterministic.nib" owner:self];
  [freqWindow makeKeyAndOrderFront:self];
  topController = controller;
  iFirstFrame = iFirstFrameMag = 0;
  iLastFrame = iLastFrameMag = 100;
  fLowestFreq = 0;
  fHighestFreq = 11025;
  iLowestPartialMag = 1;
  iHighestPartialMag = 16;
  fBottomDb = 0;
  fTopDb = 70;
  iDeterministicScale = 1;
  [topFreqForm setFloatValue:fHighestFreq];
  [bottomFreqForm setFloatValue:fLowestFreq];
  [leftTimeForm setFloatValue:0];
  [rightTimeForm setFloatValue:((float)iLastFrame/
  [[topController getTheSmsObject] getFrameRate])];
  [leftTimeFormMag setFloatValue:0];
  [rightTimeFormMag setFloatValue:((float)iLastFrame/
  [[topController getTheSmsObject] getFrameRate])];
  [bottomPartialFormMag setIntValue:iLowestPartialMag];
  [topPartialFormMag setIntValue: MIN([[topController getTheSmsObject] getNTrajectories], iHighestPartialMag)];
  [bottomDbForm setFloatValue:fBottomDb];
  [topDbForm setFloatValue:fTopDb];
  [deterministicScaleSwitch setState:iDeterministicScale];  
  return self;
}

- displayFrequency:sender
{
  [freqView display];
  return self;
}

- displayMagnitude:sender
{
  [magWindow makeKeyAndOrderFront:self];
  [magView display];
  return self;
}

- setLastFrame:sender
{
  id theSmsObject = [topController getTheSmsObject];
  
  if(theSmsObject != NULL)
  {
    int iFrame;
      
    iFrame = [sender floatValue] * [theSmsObject getFrameRate];
      
    if(iFrame < 0)
    {
      iLastFrame = 0;
      [sender setFloatValue:0];
    }
    else if(iFrame >= [theSmsObject getNRecords])
    {
      iLastFrame = [theSmsObject getNRecords];
      [sender setFloatValue:(float) iLastFrame / [theSmsObject getFrameRate]];
    }
    else
      iLastFrame = iFrame;
  }
//  [self displayFrequency:self];
  return self;
}

- (int)getLastFrame
{
  return iLastFrame;
}

- setFirstFrame:sender
{
  id theSmsObject = [topController getTheSmsObject];

  if(theSmsObject != NULL)
  {
    int iFrame;
      
    iFrame = [sender floatValue] * [theSmsObject getFrameRate];
      
    if(iFrame < 0)
    {
      iFirstFrame = 0;
      [sender setFloatValue:0];
    }
    else if(iFrame >= [theSmsObject getNRecords])
    {
      iFirstFrame = [theSmsObject getNRecords];
      [sender setFloatValue:(float) iFirstFrame / [theSmsObject getFrameRate]];
    }
    else
      iFirstFrame = iFrame;
  }
//   [self displayFrequency:self];
  return self;
}

- (int)getFirstFrame
{
  return iFirstFrame;
}

- setHighestFreq:sender
{
    fHighestFreq = [sender floatValue];
//   [self displayFrequency:self];
    return self;
}

- (float)getHighestFreq
{
  return fHighestFreq;
}

- setLowestFreq:sender
{
    fLowestFreq = [sender floatValue];
//   [self displayFrequency:self];
    return self;
}

- (float)getLowestFreq
{
  return fLowestFreq;
}

- setLastFrameMag:sender
{
  id theSmsObject = [topController getTheSmsObject];
  
  if(theSmsObject != NULL)
  {
    int iFrame;
      
    iFrame = [sender floatValue] * [theSmsObject getFrameRate];
      
    if(iFrame < 0)
    {
      iLastFrameMag = 0;
      [sender setFloatValue:0];
    }
    else if(iFrame >= [theSmsObject getNRecords])
    {
      iLastFrameMag = [theSmsObject getNRecords];
      [sender setFloatValue:(float) iLastFrameMag/[theSmsObject getFrameRate]];
    }
    else
      iLastFrameMag = iFrame;
  }
// [self displayMagnitude:self];
  return self;
}

- (int)getLastFrameMag
{
  return iLastFrameMag;
}

- setFirstFrameMag:sender
{
  id theSmsObject = [topController getTheSmsObject];

  if(theSmsObject != NULL)
  {
    int iFrame;
      
    iFrame = [sender floatValue] * [theSmsObject getFrameRate];
      
    if(iFrame < 0)
    {
      iFirstFrameMag = 0;
      [sender setFloatValue:0];
    }
    else if(iFrame >= [theSmsObject getNRecords])
    {
      iFirstFrameMag = [theSmsObject getNRecords];
      [sender setFloatValue:(float)iFirstFrameMag/[theSmsObject getFrameRate]];
    }
    else 
      iFirstFrameMag = iFrame;
  }
// [self displayMagnitude:self];
  return self;
}

- (int)getFirstFrameMag
{
  return iFirstFrameMag;
}

- setHighestPartialMag:sender
{
	id theSmsObject = [topController getTheSmsObject];

	if(theSmsObject != NULL)
	{
		int iPartial;

		iPartial  = [sender intValue];
      
		if (iPartial < 1)
		{
			iHighestPartialMag = 1;
			[sender setIntValue:1];
		}
		else if (iPartial > [theSmsObject getNTrajectories])
		{
			iHighestPartialMag = [theSmsObject getNTrajectories];
			[sender setIntValue:iHighestPartialMag];
		}
		else
			iHighestPartialMag = iPartial;
	}
//      [self displayMagnitude:self];
	return self;
}

- (int)getHighestPartialMag
{
  return iHighestPartialMag-1;
}

- setLowestPartialMag:sender
{
	id theSmsObject = [topController getTheSmsObject];

	if(theSmsObject != NULL)
	{
		int iPartial;

		iPartial  = [sender intValue];
      
		if (iPartial < 1)
		{
			iLowestPartialMag = 1;
			[sender setIntValue:1];
		}
		else if (iPartial > [theSmsObject getNTrajectories])
		{
			iLowestPartialMag = [theSmsObject getNTrajectories];
			[sender setIntValue:iHighestPartialMag];
		}
		else
			iLowestPartialMag = iPartial;
	}
//      [self displayMagnitude:self];
	return self;
}

- (int)getLowestPartialMag
{
  return iLowestPartialMag-1;
}


- getTheSmsObject
{
  return [topController getTheSmsObject];
} 

- setBottomDb:sender
{
    fBottomDb = [sender floatValue];
    if (fBottomDb < 0)
    {
      fBottomDb = 0;
      [sender setFloatValue:0];
    }
//   [self displayMagnitude:self];
    return self;
}

- (float)getBottomDb
{
  return fBottomDb;
}

- setTopDb:sender
{
    fTopDb = [sender floatValue];
//   [self displayMagnitude:self];
    return self;
}

- (float)getTopDb
{
  return fTopDb;
}

- setDeterministicScale:sender
{
  iDeterministicScale = [sender intValue];
  return self;
}

- (int)getDeterministicScale
{
  return iDeterministicScale;
}

@end
