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

#import <appkit/appkit.h>
#import <math.h>
#import "PieChartInspector.h"
#import "PieChart.h"

// -------------------------------------------------------------------------------------
// nib mode methods
@interface PieChart(Nib)
- _initDefaultPie:(int)count;
@end

// -------------------------------------------------------------------------------------
@implementation PieChartInspector

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

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

- init
{
	NXRect	r;
	char	buff[MAXPATHLEN + 1];
	id		pie, bundle;
  
	[super init];
	bundle = [NXBundle bundleForClass:[self class]];
	[bundle getPath:buff forResource:"PieChartInspector" ofType:"nib"];
	[NXApp loadNibFile:buff owner:self withNames:NO fromZone:[self zone]];
  
	[colorPie getFrame:&r];
	pie = [[PieChart alloc] initFrame:&r];
	[colorPie setContentView:pie];
	[pie setDrawLabels:NO];
	[pie setDefaultSliceCount:0];
	[pie setAcceptColor:YES];
	[pie setDelegate:self];
	colorPie = pie;
  
	return self;
}

- revert:sender
{
  int	i, cnt = [object defaultSliceCount];
  
  /* options */
  [[optionsMatrix findCellWithTag:0] setState:[object showPercent]];
  [[optionsMatrix findCellWithTag:1] setState:[object drawSliceOutline]];
  
  /* colors */
  [colorPie setBackgroundColor:[object backgroundColor]];
  [colorPie setDefaultSliceCount:cnt];
  [useDefaultGray setState:((cnt>0)?NO:YES)];
  [maxSliceSlider setIntValue:cnt];
  [maxSliceCount setIntValue:cnt];
  [colorPie _initDefaultPie:cnt];
  for (i = 0; i < cnt; i++) [colorPie setSliceColor:[object sliceColorAt:i] at:i];

  /* redisplay and return */
  [window display];
  return [super revert:sender];
}

- ok:sender
{
  int	i;

  /* options */
  [object setShowPercent:[[optionsMatrix findCellWithTag:0] state]];
  [object setDrawSliceOutline:[[optionsMatrix findCellWithTag:1] state]];
  
  /* set slice colors */
  [object setBackgroundColor:[colorPie backgroundColor]];
  if ([useDefaultGray state]) [object setDefaultSliceCount:0];
  else {
	[object setDefaultSliceCount:[colorPie defaultSliceCount]];
	for (i = 0; i < [colorPie defaultSliceCount]; i++)
	  [object setSliceColor:[colorPie sliceColorAt:i] at:i];
  }
  [object _initDefaultPie:-1];
  
  return [super ok:sender];
}

- (BOOL)wantsButtons
{
    return YES;
}

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

- changeUseDefaultGray:sender
{
  [colorPie setDefaultSliceCount:([sender state]? 0 : [maxSliceSlider intValue])];
  [colorPie _initDefaultPie:([sender state]? [maxSliceSlider intValue] : -1)];
  return [colorPie display];
}

- changeSliceCount:sender
{
  [maxSliceCount setIntValue:[sender intValue]];
  if (![useDefaultGray state]) [colorPie setDefaultSliceCount:[sender intValue]];
  [colorPie _initDefaultPie:[sender intValue]];
  return [colorPie display];
}

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

/* PieChart delegate: pie slice selected */
- selectedPieSlice:(int)n
{
  NXEvent	*e = [NXApp currentEvent];
  NXColor	color = (n >= 0)? [colorPie sliceColorAt:n] : [colorPie backgroundColor];
  if (e->data.mouse.click == 2) [sliceColorWell setColor:color];
  else [NXColorPanel dragColor:color withEvent:e fromView:colorPie];
  return self;
}

@end
