#import "ColorPanelXModule.h"
#import "ColorPanelXModuleConstants.h"

#import <appkit/Application.h>
#import <strings.h>

/*
 * Define the list of commands and functions this XModule implements
 */

#import <appkit/NXColorPanel.h>

const char *handlerList[] = {
	// Existing handlerList[] definitions here...
	"CP_ALPHA",
	"CP_COLOR",
	"CP_DOESSHOWALPHA",
	"CP_ISCONTINUOUS",
	"CP_MODE",
	"cp_setcolor",
	"cp_setcontinuous",
	"cp_setmode",
	"cp_setshowalpha",
	"CP_FUNCTIONS",
	"cp_infopanel",
	"CP_VERSION",
	NULL
};

static struct mach_header	*loadHeader = NULL;


@implementation ColorPanelXModule : XModule

- init
{
	[super init];
	
	return ( self );
}


- free
{
	return ( [super free] );
}



// ---------------------------------------------------------------
//   Standard XModule methods
// ---------------------------------------------------------------

+ (const char *)moduleName
{
	return ( "ColorPanelXModule" );
}


+ (const char **)xCmdsAndFcns
{
	return ( handlerList );
}


- prepareToExecuteHandlers
{
	// [self loadInterfaceData];
	return ( self );
}


// Functions and handlers this puppy contains...
- executeHandler:(NXAtom)handlerName
{
	// Existing -executeHandler: code here...

	if ( handlerName == NXUniqueString("CP_ALPHA") ) {
		return ( [ self cp_alpha ] );
	}

	if ( handlerName == NXUniqueString("CP_COLOR") ) {
		return ( [ self cp_color ] );
	}

	if ( handlerName == NXUniqueString("CP_DOESSHOWALPHA") ) {
		return ( [ self cp_doesShowAlpha ] );
	}

	if ( handlerName == NXUniqueString("CP_ISCONTINUOUS") ) {
		return ( [ self cp_isContinuous ] );
	}

	if ( handlerName == NXUniqueString("CP_MODE") ) {
		return ( [ self cp_mode ] );
	}

	if ( handlerName == NXUniqueString("cp_setcolor") ) {
		return ( [ self cp_setColor ] );
	}

	if ( handlerName == NXUniqueString("cp_setcontinuous") ) {
		return ( [ self cp_setContinuous ] );
	}

	if ( handlerName == NXUniqueString("cp_setshowalpha") ) {
		return ( [ self cp_setShowAlpha ] );
	}

	if ( handlerName == NXUniqueString("cp_setmode") ) {
		return ( [ self cp_setMode ] );
	}

	if ( handlerName == NXUniqueString("CP_FUNCTIONS") ) {
		return ( [ self cp_functions ] );
	}

	if ( handlerName == NXUniqueString("cp_infopanel") ) {
		return ( [ self cp_infoPanel ] );
	}

	if ( handlerName == NXUniqueString("CP_VERSION") ) {
		return ( [ self cp_version ] );
	}

	return ( nil ); // handlerName not recognized!
}


// ---------------------------------------------------------------
//   Useful methods for loading interface data
// ---------------------------------------------------------------


+ finishLoading:(struct mach_header *)header
{
	loadHeader = header; // Save this for later use
	return ( self );
}


- loadInterfaceData
{
	//[NXApp loadNibSection:"MyXModule.nib" owner:self
	//    withNames:NO fromHeader:loadHeader] )
		
	return ( self );
}


// ColorPanelXModule Handlers
- cp_alpha
{
	float	alpha;

	alpha = [[ NXColorPanel sharedInstance:YES ] alpha ];

	return ( [[ self getEmptyContainer ] setFloat:alpha ] );
}


- cp_color
{
	NXColor		currPanelColor;

	currPanelColor = [[ NXColorPanel sharedInstance:YES ] color ];

	return ( [[ self getEmptyContainer ] setColor:currPanelColor ] );
}


- cp_doesShowAlpha
{
	int		pCount = [ self paramCount ];
	BOOL		doesShowAlpha;

	doesShowAlpha = [[ NXColorPanel sharedInstance:YES ] doesShowAlpha ];

	return ( [[ self getEmptyContainer ] setBool:doesShowAlpha ] );
}


- cp_isContinuous
{
	int		pCount = [ self paramCount ];
	BOOL		continuousValue;

	continuousValue = [[ NXColorPanel sharedInstance:YES ] isContinuous ];

	return ( [[ self getEmptyContainer ] setBool:continuousValue ] );
}


- cp_mode
{
	int	mode;

	mode = [[ NXColorPanel sharedInstance:YES ] mode ];

	return ( [[ self getEmptyContainer ] setInt:mode ] );
}



// ColorPanelXModule Handlers
- cp_setColor
{
	int		pCount = [ self paramCount ];
	NXColor		colorValue, currPanelColor;

	if ( pCount != 1 ) {
		return ( [[ self getEmptyContainer ]
			setString: "Usage:  setColorPanelValue  <aColor>" ] );
	} 

	colorValue = [[ self getParam:1 ] colorVal ];
	[[ NXColorPanel sharedInstance:YES ] setColor:colorValue ];
	currPanelColor = [[ NXColorPanel sharedInstance:YES ] color ];

	return ( [[ self getEmptyContainer ] setColor:currPanelColor ] );
}


- cp_setContinuous
{
	int		pCount = [ self paramCount ];
	BOOL		contValue;

	if ( pCount != 1 ) {
		return ( [[ self getEmptyContainer ]
			setString: "Usage:  setTheContinuous  <aBoolVal>" ] );
	} 

	contValue = [[ self getParam:1 ] boolVal ];
	[[ NXColorPanel sharedInstance:YES ] setContinuous:contValue ];
	contValue = [[ NXColorPanel sharedInstance:YES ] isContinuous ];

	return ( [[ self getEmptyContainer ] setBool:contValue ] );
}


/*	These are the Modes, and their values may be seen in their
	defined .h files.

	Grayscale-Alpha			NX_GRAYMODE = 0
	Red-Green-Blue			NX_RGBMODE = 1
	Cyan-Yellow-Magenta-Black	NX_CMYKMODE = 2
	Hue-Saturation-Brightness	NX_HSBMODE = 3
	TIFF image			NX_CUSTOMPALETTEMODE = 4
	Color lists			NX_COLORLISTMODE = 5
	Color wheel			NX_BEGINMODE = 6
*/
- cp_setMode
{
	int	pCount = [ self paramCount ];
	int	newMode;

	if ( pCount != 1 ) {
		return ( [[ self getEmptyContainer ]
			setString: "Usage:  cp_setMode  <aModeInt>" ] );
	} 

	newMode = [[ self getParam:1 ] intVal ];
	[[ NXColorPanel sharedInstance:YES ] setMode:newMode ];
	newMode = [[ NXColorPanel sharedInstance:YES ] mode ];

	return ( [[ self getEmptyContainer ] setInt:newMode ] );
}


- cp_setShowAlpha
{
	int		pCount = [ self paramCount ];
	BOOL		showAlpha;

	if ( pCount != 1 ) {
		return ( [[ self getEmptyContainer ]
			setString: "Usage:  setShowAlpha  <aBoolVal>" ] );
	} 

	showAlpha = [[ self getParam:1 ] boolVal ];
	[[ NXColorPanel sharedInstance:YES ] setShowAlpha:showAlpha ];
	showAlpha = [[ NXColorPanel sharedInstance:YES ] isContinuous ];

	return ( [[ self getEmptyContainer ] setBool:showAlpha ] );
}


// Support functions
- cp_functions
{
	Value	*returnStr;

	returnStr = [[ self getEmptyContainer ] setString:"" ];
	
	[ returnStr appendString:"CP_ALPHA()\n" ];
	[ returnStr appendString:"CP_COLOR()\n" ];
	[ returnStr appendString:"CP_DOESSHOWALPHA()\n" ];
	[ returnStr appendString:"CP_ISCONTINUOUS()\n" ];
	[ returnStr appendString:"CP_MODE()\n" ];
	[ returnStr appendString:"cp_setcolor\n" ];
	[ returnStr appendString:"cp_setcontinuous\n" ];
	[ returnStr appendString:"cp_setmode\n" ];
	[ returnStr appendString:"cp_setshowalpha" ];

	return ( returnStr );
}


- cp_infoPanel
{
	char	*infoStrPtr, infoStr[1024];

	infoStrPtr = infoStr;

	strncpy(infoStr, "This XModule is designed to facilitate the use\n"
		"of the HyperSense color panel.  If you have any\n"
		"questions regarding this XModule or its distribution\n"
		"please consult the README files or write me a letter.\n\n"
		"\t\tTodd Anthony Nathan\n"
		"\t\t1325 Birch Street #14\n"
		"\t\tFort Collins, Colorado 80521\n"
		"\t\t+1.303.221.0559\n"
		"\t\t<todd@icebox.com>\n", 1023);


	[ self runAlertPanel:"Color Panel XModule Info" text: "%s", infoStr ];

	return ( self );
}


- cp_version
{
	id		version;

	version = [[ self getEmptyContainer ] setString:"r" ];
	[ version appendString:P_RELEASE ];
	[ version appendString:" v" ];
	[ version appendString:P_VERSION ];
	[ version appendString:"    " ];
	[ version appendString:P_RELTIME ];
	[ version appendString:"\n" ];
	[ version appendString:P_COPYRIGHT ];

	return ( version );
}

@end
