// -------------------------------------------------------------------------------------
// BarChartInspector.m
// Martin D. Flynn, NeXT Computer, Inc.
// -------------------------------------------------------------------------------------

#import <appkit/appkit.h>
#import "BarChartInspector.h"
#import "BarChart.h"

// -------------------------------------------------------------------------------------
@implementation BarChartInspector

// -------------------------------------------------------------------------------------
// This method exists for linker purposes only.  It is not executed. 
+ forceBarChartLink { return [BarChart class]; }

// -------------------------------------------------------------------------------------
// << These two class methods don't get inherited >>
//+ finishLoading:(struct mach_header*) hdr { NIBDidLoadClass(self, hdr); return (id)nil; }
//+ startUnloading { NIBWillUnloadClass(self); return (id)nil; }

// -------------------------------------------------------------------------------------

- _centerInspectorBox:box
{
  id		newView = [[View alloc] initFrame:&inspectorFrame];
  NXRect	bFrame;
  [newView addSubview:box];
  [box setBorderType:NX_NOBORDER];
  [box getFrame:&bFrame];
  bFrame.origin.x = (inspectorFrame.size.width  - bFrame.size.width ) / 2.0;
  bFrame.origin.y = inspectorFrame.size.height - bFrame.size.height;
  [box setFrame:&bFrame];
  return newView;
}

- init
{
	char	buff[MAXPATHLEN + 1];
	id		bundle;

	[super init];
	bundle = [NXBundle bundleForClass:[self class]];
	[bundle getPath:buff forResource:"BarChartInspector" ofType:"nib"];
	[NXApp loadNibFile:buff owner:self withNames:NO fromZone:[self zone]];
  
	[inspectorBox setBorderType:NX_NOBORDER];
	[[inspectorBox contentView] getFrame:&inspectorFrame];
  
	generalBox = [self _centerInspectorBox:generalBox];
	xAxisBox   = [self _centerInspectorBox:xAxisBox  ];
	yAxisBox   = [self _centerInspectorBox:yAxisBox  ];

	[self inspectGeneral:self];
  
	return self;
  
}

- revert:sender
{
  int		chartType;
  NXRect	*dataRange, *tickMark;

  /* color/gray state */
  [drawInColorBtn setState:[object drawInColor]];
  [self setColorGrayMode:drawInColorBtn];

  /* background */
  [backgroundTransSwitch setState:[object backgroundTransparent]];
  [backgroundGrayMatrix selectCellWithTag:rint([object backgroundGray] * 3.0)];
  [backgroundColorWell	setColor:[object backgroundColor]];
  [self setBackgroundTransState:backgroundTransSwitch];

  /* chart/grid colors */
  [dftChartGrayMatrix   selectCellWithTag:rint([object dftChartGray  ] * 3.0)];
  [xGridGrayMatrix      selectCellWithTag:rint([object xGridGray     ] * 3.0)];
  [yGridGrayMatrix      selectCellWithTag:rint([object yGridGray     ] * 3.0)];
  [dftChartColorWell	setColor:[object dftChartColor  ]];
  [xGridColorWell		setColor:[object xGridColor     ]];
  [yGridColorWell		setColor:[object yGridColor     ]];

  /* grid state */
  [xGridDraw setState:[object xGridDraw]];
  [self setXGridDrawState:xGridDraw];
  [yGridDraw setState:[object yGridDraw]];
  [self setYGridDrawState:yGridDraw];

  /* chart type */
  chartType = [object dftChartType];
  [[dftChartTypeMatrix findCellWithTag:0] setState:((chartType & chartBAR  )?YES:NO)];
  [[dftChartTypeMatrix findCellWithTag:1] setState:((chartType & chartLINE )?YES:NO)];
  [[dftChartTypeMatrix findCellWithTag:2] setState:((chartType & chartPOINT)?YES:NO)];
  [[dftChartTypeMatrix findCellWithTag:3] setState:((chartType & chartAREA )?YES:NO)];

  /* axis data ranges & scaling */
  dataRange = [object dataRange];
  [xDataRangeLo setFloatValue:dataRange->origin.x];
  [xDataRangeHi setFloatValue:dataRange->origin.x + dataRange->size.width];
  [yDataRangeLo setFloatValue:dataRange->origin.y];
  [yDataRangeHi setFloatValue:dataRange->origin.y + dataRange->size.height];
  [xLogScaling  setState:[object xLogScaling]];
  [yLogScaling  setState:[object yLogScaling]];
  if ([xLogScaling state]&&([xDataRangeLo floatValue]<=0.0))[xDataRangeLo setFloatValue:1.0];
  if ([yLogScaling state]&&([yDataRangeLo floatValue]<=0.0))[yDataRangeLo setFloatValue:1.0];

  /* tick marks */
  tickMark = [object tickMark];
  [xTickIncrement setFloatValue:tickMark->size.width];
  [yTickIncrement setFloatValue:tickMark->size.height];

  /* axis labels */
  [xShowLabels  setState:[object xShowLabels]];
  [xLabelRotate setState:[object xLabelRotate]];
  [yShowLabels  setState:[object yShowLabels]];
  [yLabelRotate setState:[object yLabelRotate]];

  [window display];
  
  return [super revert:sender];
}

- ok:sender
{
  int		chartType;
  NXRect	dataRange, tickMark;
  
  /* background transparent */
  [object setBackgroundTransparent:[backgroundTransSwitch state]];

  /* color/gray state */
  if ([drawInColorBtn state]) {
	[object setBackgroundColor:[backgroundColorWell color]];
	[object setDftChartColor:  [dftChartColorWell color]];
	[object setXGridColor:     [xGridColorWell color]];
	[object setYGridColor:     [yGridColorWell color]];
  } else {
	[object setBackgroundGray: [[backgroundGrayMatrix selectedCell] tag] / 3.0];
	[object setDftChartGray:   [[dftChartGrayMatrix   selectedCell] tag] / 3.0];
	[object setXGridGray:      [[xGridGrayMatrix      selectedCell] tag] / 3.0];
	[object setYGridGray:      [[yGridGrayMatrix      selectedCell] tag] / 3.0];
  }

  /* grid state */
  [object setXGridDraw:[xGridDraw state]];
  [object setYGridDraw:[yGridDraw state]];

  /* chart type */
  chartType = 0;
  if ([[dftChartTypeMatrix findCellWithTag:0] state]) chartType |= chartBAR;
  if ([[dftChartTypeMatrix findCellWithTag:1] state]) chartType |= chartLINE;
  if ([[dftChartTypeMatrix findCellWithTag:2] state]) chartType |= chartPOINT;
  if ([[dftChartTypeMatrix findCellWithTag:3] state]) chartType |= chartAREA;
  [object setDftChartType:chartType];

  /* axis data ranges & scaling */
  [object setXLogScaling:[xLogScaling state]];
  [object setYLogScaling:[yLogScaling state]];
  if ([xLogScaling state]&&([xDataRangeLo floatValue]<=0.0))[xDataRangeLo setFloatValue:1.0];
  if ([yLogScaling state]&&([yDataRangeLo floatValue]<=0.0))[yDataRangeLo setFloatValue:1.0];
  dataRange.origin.x    = [xDataRangeLo floatValue];
  dataRange.size.width  = [xDataRangeHi floatValue] - dataRange.origin.x;
  dataRange.origin.y    = [yDataRangeLo floatValue];
  dataRange.size.height = [yDataRangeHi floatValue] - dataRange.origin.y;
  [object setDataRange:&dataRange];

  /* tick marks */
  tickMark.origin.x     = 0.0;
  tickMark.size.width   = [xTickIncrement floatValue];
  tickMark.origin.y     = 0.0;
  tickMark.size.height  = [yTickIncrement floatValue];
  [object setTickMark:&tickMark];

  /* axis labels */
  [object setXShowLabels:[xShowLabels state]];
  [object setXLabelRotate:[xLabelRotate state]];
  [object setYShowLabels:[yShowLabels state]];
  [object setYLabelRotate:[yLabelRotate state]];

  [window display];

  return [super ok:sender];
}

- (BOOL)wantsButtons
{
    return YES;
}

// -------------------------------------------------------------------------------------

/* set background transparent */
- setBackgroundTransState:sender
{
  BOOL	state = ![backgroundTransSwitch state];
  [backgroundColorWell setEnabled:state];
  [backgroundGrayMatrix setEnabled:state];
  [[backgroundGrayMatrix selectedCell] setState:state];
  [backgroundGrayMatrix display];
  return self;
}

/* set xGrid drawing state */
- setXGridDrawState:sender
{
  BOOL	state = [xGridDraw state];
  [xGridColorWell setEnabled:state];
  [xGridGrayMatrix setEnabled:state];
  [[xGridGrayMatrix selectedCell] setState:state];
  [xGridGrayMatrix display];
  return self;
}

/* set yGrid drawing state */
- setYGridDrawState:sender
{
  BOOL	state = [yGridDraw state];
  [yGridColorWell setEnabled:state];
  [yGridGrayMatrix setEnabled:state];
  [[yGridGrayMatrix selectedCell] setState:state];
  [yGridGrayMatrix display];
  return self;
}

/* set color/gray mode */
- setColorGrayMode:sender
{
  if ([drawInColorBtn state]) {
	[backgroundGrayMatrix removeFromSuperview];
	[[backgroundCGBox contentView] addSubview:backgroundColorWell];
	[dftChartGrayMatrix removeFromSuperview];
	[[chartCGBox contentView] addSubview:dftChartColorWell];
	[xGridGrayMatrix removeFromSuperview];
	[[xGridCGBox contentView] addSubview:xGridColorWell];
	[yGridGrayMatrix removeFromSuperview];
	[[yGridCGBox contentView] addSubview:yGridColorWell];
  } else {
	[backgroundColorWell removeFromSuperview];
	[[backgroundCGBox contentView] addSubview:backgroundGrayMatrix];
	[dftChartColorWell removeFromSuperview];
	[[chartCGBox contentView] addSubview:dftChartGrayMatrix];
	[xGridColorWell removeFromSuperview];
	[[xGridCGBox contentView] addSubview:xGridGrayMatrix];
	[yGridColorWell removeFromSuperview];
	[[yGridCGBox contentView] addSubview:yGridGrayMatrix];
  }
  [window display];
  return self;
}

/* inspect general */
- inspectGeneral:sender
{
  [inspectorBox setContentView:generalBox];
  [self setColorGrayMode:drawInColorBtn];
  return [inspectorBox display];
}

/* inspect x-axis */
- inspectXAxis:sender
{
  [inspectorBox setContentView:xAxisBox];
  return [inspectorBox display];
}

/* inspect y-axis */
- inspectYAxis:sender
{
  [inspectorBox setContentView:yAxisBox];
  return [inspectorBox display];
}

@end
