// DomainAndRange.m
// By Charles G. Fleming, Halchin and Fleming.
// You may freely copy, distribute and reuse this code. 
// Halchin and Fleming, and the author disclaim any warranty of any kind, 
// expressed or implied, as to its fitness for any particular use.

#import "DomainAndRange.h"
#import <appkit/TextField.h>
#import <appkit/Application.h>
#import <appkit/Button.h>
#import "MathGraph.h"
#import <appkit/Window.h>

@implementation DomainAndRange

// Initialize the instance variables used in this window.
- initContent:(const NXRect *)contentRect style:(int)aStyle 
      backing:(int)bufferingType buttonMask:(int)mask defer:(BOOL)flag
{
   [super initContent:contentRect style:aStyle 
      backing:bufferingType buttonMask:mask defer:flag];
    domainLow = 0;
    domainUp = 10;
    rangeLow = -5;
    rangeUp = 5;
    return self;
}    

// Disable the field give it a gray background.
- setRangeLower:anObject
{
   rangeLower = anObject;
   [rangeLower setEnabled:NO];
   [rangeLower setBackgroundGray:0.6];
   return self;
}
   
// Disable the field give it a gray background.
- setRangeUpper:anObject
{
   rangeUpper = anObject;
   [rangeUpper setEnabled:NO];
   [rangeUpper setBackgroundGray:0.6];
   return self;
}

// Get the values from the fields and store them in the instance variables.   
- setValues:sender
{
    domainLow = [domainLower doubleValue];
    domainUp = [domainUpper doubleValue];
    rangeLow = [rangeLower doubleValue];
    rangeUp = [rangeUpper doubleValue];
    [mathGraph clearMathGraph];    
    [NXApp stopModal];
    [parametersWindow makeKeyAndOrderFront:self];
    [self close];
    return self;
}

// Bring this window up and start a modal loop.
- activate:sender
{
    [self makeKeyAndOrderFront:sender];
    if([defaults state])
    {
       [rangeLower setEnabled:NO];
       [rangeUpper setEnabled:NO];
     }  
    [domainLower selectText:self];
    [NXApp runModalFor:self];
    return self;
}

// If the default button is clicked, disable the range fields and set
// their background to a light gray, otherwise enable the range fields and
// set their background to white.
-default:sender
{ 
   if([sender state])
   {
      [rangeLower setEnabled:NO];
      [rangeUpper setEnabled:NO];
      [rangeLower setBackgroundGray:0.6];
      [rangeUpper setBackgroundGray:0.6];	
    } 
    else
   {
      [rangeLower setEnabled:YES];
      [rangeUpper setEnabled:YES];
      [rangeLower setBackgroundGray:1.0];
      [rangeUpper setBackgroundGray:1.0];	
    } 
    return self;
} 

// If the defaults button has been clicked, send a performClick to
// the okButton, otherwise move to the next text field.
- performClick:sender
{
   if([defaults state])
      [okButton performClick:self];
   else
      [rangeLower selectText:self];   
    return sender;
}
    
- defaultButton:sender
{
   return defaults;
}   

-(double)domainLower
{
   return domainLow;
}

-(double)domainUpper
{
   return domainUp;
}  

-(double)rangeLower
{
   return rangeLow;
}

-(double)rangeUpper
{
   return rangeUp;
}

@end
