// tabs:3 indent:3

#import <appkit/appkit.h>
#import <stdlib.h>

#import "McFilter.h"

@implementation McFilter

+ new
/*
    Called first time this section's button is clicked.
*/
{
	const NXDefaultsVector myDefaults = {
		{"gamma", "1.5"},{"JPEGgamma", "1.5"},
		{"showMessages", "1"},{NULL, NULL}
	};

	self = [super new];
    
	if( ![NXApp loadNibForLayout: "McFilter" owner: self] )
		return 0;
	
		// 'window' is an interface builder outlet.
		view = [window contentView];	// 'view' MUST be initialized
		[view removeFromSuperview];
		[window setContentView: 0];	// Don't need the window for anything
		[window free];		// So free it.

		// Do any other set up here.
		NXRegisterDefaults("McFilter", myDefaults);
		gamma = atof(NXGetDefaultValue("McFilter", "gamma"));
		JPEGgamma = atof(NXGetDefaultValue("McFilter", "JPEGgamma"));
		showMessages = (BOOL)atoi(NXGetDefaultValue("McFilter", "showMessages"));

		return self;
}

- willSelect: sender
/*
	Before the view is added via addSubview
*/
{
	gamma = atof(NXGetDefaultValue("McFilter", "gamma"));
	JPEGgamma = atof(NXGetDefaultValue("McFilter", "JPEGgamma"));
	showMessages = (BOOL)atoi(NXGetDefaultValue("McFilter", "showMessages"));

	[showSwitch setIntValue:showMessages];
	[gammaSlider setIntValue:(int)(gamma*10.0)];
	[gammaTextField setFloatValue:gamma];
	[JPEGgammaSlider setIntValue:(int)(JPEGgamma*10.0)];
	[JPEGgammaTextField setFloatValue:JPEGgamma];

	return self;
}

- didSelect: sender
/*
	After the view is added via addSubview
*/
{
	/*
	Enable all of the edit and window menu items, just for the heck of it.
	*/
	[NXApp enableEdit: CUT_ITEM|COPY_ITEM|PASTE_ITEM|SELECTALL_ITEM];
	[NXApp enableWindow: MINIATURIZE_ITEM|CLOSE_ITEM];
	return self;
}


- willUnselect: sender
/*
	Before removeFromSuperview
*/
{
	/* Make sure that we aren't editing any text fields. */
	[[NXApp appWindow] endEditingFor: self];  
	return self;
}

- didUnselect: sender
/*
	Before removeFromSuperview
*/
{
	/*
	Disable all of the edit and window menu items, just for the heck of it.
	*/
	[NXApp enableEdit: NO];
	[NXApp enableWindow: NO];
	return self;
}

- didHide: sender
/*
	Application was just hidden.
*/
{
	return self;
}

- didUnhide: sender
/*
	Application was just unhidden.
*/
{
	gamma = atof(NXGetDefaultValue("McFilter", "gamma"));
	JPEGgamma = atof(NXGetDefaultValue("McFilter", "JPEGgamma"));
	showMessages = (BOOL)atoi(NXGetDefaultValue("McFilter", "showMessages"));

	[showSwitch setIntValue:showMessages];
	[gammaSlider setIntValue:(int)(gamma*10.0)];
	[gammaTextField setFloatValue:gamma];
	[JPEGgammaSlider setIntValue:(int)(JPEGgamma*10.0)];
	[JPEGgammaTextField setFloatValue:JPEGgamma];

	return self;
}

- setMessagesState:sender
{
	char buffer[20];
	
	showMessages = (BOOL)[sender intValue];
	sprintf(buffer, "%d", showMessages);
	NXWriteDefault("McFilter", "showMessages", buffer);
	return self;
}

- gammaSliderChanged: sender
{
	int temp;
	char buffer[20];

	temp = [gammaSlider intValue];
	gamma = (float)temp/10.0;	// scale to float value
	[gammaTextField setFloatValue:gamma];
	sprintf(buffer, "%3.1f", gamma);
	NXWriteDefault("McFilter", "gamma", buffer);
	return self;
}

- gammaTextFieldChanged: sender
{
	char buffer[20];

	gamma = [gammaTextField floatValue];
	[gammaSlider setIntValue:(int)(gamma*10.0)];
	sprintf(buffer, "%3.1f", gamma);
	NXWriteDefault("McFilter", "gamma", buffer);
	return self;
}

- JPEGgammaSliderChanged:sender
{
	int temp;
	char buffer[20];

	temp = [JPEGgammaSlider intValue];
	JPEGgamma = (float)temp/10.0;	// scale to float value
	[JPEGgammaTextField setFloatValue:JPEGgamma];
	sprintf(buffer, "%3.1f", JPEGgamma);
	NXWriteDefault("McFilter", "JPEGgamma", buffer);
	return self;
}

- JPEGgammaTextFieldChanged:sender;
{
	char buffer[20];

	JPEGgamma = [JPEGgammaTextField floatValue];
	[JPEGgammaSlider setIntValue:(int)(JPEGgamma*10.0)];
	sprintf(buffer, "%3.1f", JPEGgamma);
	NXWriteDefault("McFilter", "JPEGgamma", buffer);
	return self;
}

@end

