
/*
   MiscGaugeViewInspector.m
*/  
 
#import <AppKit/AppKit.h>
#import <MiscAppKit/MiscGaugeView.h>
#import "MiscGaugeViewInspector.h"
#import "MiscArrowButtonAndGaugeView.h"

@implementation MiscGaugeViewInspector

- init
{
    NSString* nibPath;
    NSBundle* bundle;
    
    [super init];
    
	// Load the inspector's nib file.
    bundle = [NSBundle bundleForClass:[MiscArrowButtonAndGaugeView class]];
    nibPath = [bundle pathForResource:@"MiscGaugeViewInspector" ofType:@"nib"];
    [NSBundle loadNibFile:nibPath
        externalNameTable:[NSDictionary dictionaryWithObjectsAndKeys:self, @"NSOwner", nil]
                 withZone:[self zone]];
	return self;
}

	
- (void) attribChanged:sender
{
	// One of the attributes changed, so find out which one and update us.
	// Syncronize the slider with it's matching form cell. The sender is
	// the attribForm. After we set the attrib. we check back with the
	// object and see what the value was really set to (since it may have
	// been illegal) and update the form cell.
	int selIndex = [sender selectedRow];
	
    switch (selIndex) {
      case 0:	// Start angle changed.
        [[self object] setStartAngle:[[sender cellAtIndex:0] floatValue]];
        [[sender cellAtIndex:0] setFloatValue:[[self object] startAngle]];
        break;

      case 1:	// Angle range changed.
        [[self object] setAngleRange:[[sender cellAtIndex:1] floatValue]];
        [[sender cellAtIndex:1] setFloatValue:[[self object] angleRange]];
        break;

      case 2: // Hand ratio changed.
        [[self object] setHandRatio:[[sender cellAtIndex:2] floatValue]];
        [[sender cellAtIndex:2] setFloatValue:[[self object] handRatio]];
        break;

      case 3:	// Tick ratio changed.
        [[self object] setTickRatio:[[sender cellAtIndex:3] floatValue]];
        [[sender cellAtIndex:3] setFloatValue:[[self object] tickRatio]];
        break;

      case 4: // Tick interval changed.
        [[self object] setTickInterval:[[sender cellAtIndex:4] intValue]];
        [[sender cellAtIndex:4] setFloatValue:[[self object] tickInterval]];
        break;
      }		
	
	// Tick interval has no slider so don't send a message.
    if (selIndex != 4) {
		[[sliderMatrix cellAtRow:selIndex column:0] 
			setFloatValue:[[sender cellAtIndex:selIndex] floatValue]];
    }
    
    [super ok:sender]; 
}


- (void) sliderChanged:sender
{
	// A slider changed, so update the inspected object and 
	// the attribForm. The sender is the sliderMatrix.
	NSCell* selectedCell = [sender selectedCell];
	
    switch ([sender selectedTag]) {
      case 0:	// Start angle changed.
        [[self object] setStartAngle:[selectedCell floatValue]];
        break;

      case 1:	// Angle range changed.
        [[self object] setAngleRange:[selectedCell floatValue]];
        break;

      case 2: // Hand ratio changed.
        [[self object] setHandRatio:[selectedCell floatValue]];
        break;

      case 3:	// Tick ratio changed.
        [[self object] setTickRatio:[selectedCell floatValue]];
        break;
      }

	// Set the matching attribute in the form, so it and it's
	// slider have the same value.
	[[attribsForm cellAtIndex:[sender selectedTag]] setFloatValue:[selectedCell floatValue]];
	[super ok: sender]; 
}


- (void) backgroundColorChanged:(id)sender
{
	// The sender is a NXColorWell.
	[[self object] setBackgroundColor:[sender color]];
    [super ok:sender]; 
}


- (void) gaugeColorChanged:(id)sender
{
	// The sender is a NXColorWell.
	[[self object] setGaugeColor:[sender color]];
	[super ok:sender]; 
}


- (void) textColorChanged:(id)sender
{
	// The sender is the NXColorWell representing text color.
	[[self object] setTextColor:[sender color]];
    [super ok:sender]; 
}


- (void) valueChanged:(id)sender
{
	// A value in the value form changed, so find out which one and
	// update the object. The sender is the valueForm.
	int selIndex = [sender indexOfSelectedItem];
	
    switch (selIndex) {
      case 0:	// Min value changed.
        [[self object] setMinValue:[[sender cellAtIndex:0] floatValue]];
        // The max. value may have changed if it became less than the min.
        [[sender cellAtIndex:1] setFloatValue:[[self object] maxValue]];
        break;

      case 1: // Max value changed.
        [[self object] setMaxValue:[[sender cellAtIndex:1] floatValue] ];
        // The min value may have changed if it became more than the max.
        [[sender cellAtIndex:0] setFloatValue:[[self object] minValue]];
        break;

      case 2:	// Current value changed.
        [[self object] setFloatValue:[[sender cellAtIndex:2] floatValue]];
        break;
    }
	 
	// Since any of the above could have changed the value of the gauge 
	// (or the new value wasn't acceptable..above or below the max and
	// min), get the current value from the object itself and update the
	// formcell.
	[[sender cellAtIndex:2] setFloatValue:[[self object] floatValue]];

    [super ok:sender]; 
}


- (void )titleFieldChanged:(id)sender
{
	[[self object] setTitle:[sender stringValue]];
	[super ok:sender]; 
}


- (void) titlePositionChanged:(id)sender
{
    switch ([sender selectedTag]) {
      case 0:
        [[self object] setTitlePosition:NSAtTop];
        break;

      case 1:
        [[self object] setTitlePosition:NSAtBottom];
        break;
      }
	[super ok:sender]; 
}


- (void) revert:(id)sender
{
	// The place to ask the inspected object for all it's values, so we
	// can fill up the inspector's fields and color wells.

	[gaugeColorWell setColor:[[self object] gaugeColor]];
	[textColorWell setColor: [[self object] textColor]];
	[titleField setStringValue:[[self object] title]];
	
	[[valueForm cellAtIndex:0] setFloatValue:[[self object] minValue]];
	[[valueForm cellAtIndex:1] setFloatValue:[[self object] maxValue]];
	[[valueForm cellAtIndex:2] setFloatValue:[[self object] floatValue]];
	
	[[attribsForm cellAtIndex:0] setFloatValue:[[self object] startAngle]];
	[[attribsForm cellAtIndex:1] setFloatValue:[[self object] angleRange]];
	[[attribsForm cellAtIndex:2] setFloatValue:[[self object] handRatio]];
	[[attribsForm cellAtIndex:3] setFloatValue:[[self object] tickRatio]];
	[[attribsForm cellAtIndex:4] setIntValue:[[self object] tickInterval]];

	[[sliderMatrix cellAtRow:0 column:0] setFloatValue:[[self object] startAngle]];
	[[sliderMatrix cellAtRow:1 column:0] setFloatValue:[[self object] angleRange]];
	[[sliderMatrix cellAtRow:2 column:0] setFloatValue:[[self object] handRatio]];
	[[sliderMatrix cellAtRow:3 column:0] setFloatValue:[[self object] tickRatio]];
	
    if ([[self object] titlePosition] == NSAtTop) {
		[titlePositionMatrix selectCellAtRow:0 column:0];
    }
    else {
		[titlePositionMatrix selectCellAtRow:1 column:0];
    }
    
	[super revert:sender]; 
}


- (BOOL) wantsButtons
{
	return NO;
}

@end


@implementation MiscGaugeViewInspector (NibInitialization)

- (void) awakeFromNib
{
	// Set all the decimal places for the attributes.
	[[attribsForm cellAtRow:0 column:0] setFloatingPointFormat:YES left:3 right:1];
	[[attribsForm cellAtRow:1 column:0] setFloatingPointFormat:YES left:3 right:1];
	[[attribsForm cellAtRow:2 column:0] setFloatingPointFormat:YES left:1 right:2];
	[[attribsForm cellAtRow:3 column:0] setFloatingPointFormat:YES left:1 right:2];
}		

@end

