//
//	MiscScreenColor.m -- a class for color "tints"  (to specify a halftone)
//		Written by Don Yacktman (c) 1994 by Don Yacktman.
//				Version 1.0.  All rights reserved.
//
//		This notice may not be removed from this source code.
//
//	This object is included in the MiscKit by permission from the author
//	and its use is governed by the MiscKit license, found in the file
//	"LICENSE.rtf" in the MiscKit distribution.  Please refer to that file
//	for a list of all applicable permissions and restrictions.
//	

#import <misckit/misckit.h>

@implementation MiscScreenColor

+ (BOOL)canInitFromPasteboard:(Pasteboard *)pasteboard
{ // ***** need version to handle object type
	return [super canInitFromPasteboard:pasteboard];
}

- setFromPasteBoard:(Pasteboard *)pasteboard
{ // ***** need version to handle object type
	return [super setFromPasteBoard:pasteboard];
}

- writeToPasteBoard:(Pasteboard *)pasteboard
{ // ***** need version to handle object type
	return [super writeToPasteBoard:pasteboard];
}

- initColor:(NXColor)color
{
	return [self initColorFromPixel:NULL inView:nil];
}

- initColorFromPixel:(NXPoint *)location
{
	return [self initColorFromPixel:location inView:nil];
}

- initColorFromPixel:(NXPoint *)location inView:view
{
	[self setLocation:location];
	[self setView:view];
	[self setDelegate:nil];
	[self updateColor];
	return self;
}

- (NXColor)actualColor
{ // keep us up to date
	[self updateColor];
	return [super actualColor];
}

- (NXPoint)location { return theLocation; }
- view
{
	return theView;
}

- setView:aView
{
	id ret = [self view];
	theView = aView;
	return ret;
}

- setLocation:(NXPoint *)location
{
	if (!location) {
		theLocation.x = 0.0;
		theLocation.y = 0.0;
		return nil;
	}
	theLocation.x = location->x;
	theLocation.y = location->y;
	return self;
}

- updateColor
{
	id view = [self view];
	NXPoint location = [self location];
	if (view && ![view isFocusView]) [view lockFocus];
	[super setColorFromPixel:&location];
	if (view && ![view isFocusView]) [view lockFocus];
	return self;
}

- read:(NXTypedStream *)stream
{
	NXPoint newPoint;
	id newView;
	[super read:stream];
	NXReadPoint(stream, &newPoint);
	newView = NXReadObject(stream);
	[self setLocation:&newPoint];
	[self setView:newView];
	return self;
}

- write:(NXTypedStream *)stream
{
	id view = [self view];
	NXPoint point = [self location];
	[super write:stream];
	NXWritePoint(stream, &point);
	NXWriteObject(stream, view);
	return self;
}

- encodeUsing:(id <NXEncoding>)portal
{
	id view = [self view];
	NXPoint point = [self location];
	[super encodeUsing:portal];
	[portal encodeData:&(point.x) ofType:"f"];
	[portal encodeData:&(point.y) ofType:"f"];
	[portal encodeData:&view ofType:"@"];
	return self;
}

- decodeUsing:(id <NXDecoding>)portal
{
	NXPoint newPoint;
	id newView;
	[super decodeUsing:portal];
	[portal decodeData:&(newPoint.x) ofType:"f"];
	[portal decodeData:&(newPoint.y) ofType:"f"];
	[portal decodeData:&newView ofType:"@"];
	[self setLocation:&newPoint];
	[self setView:newView];
	return self;
}

- encodeRemotelyFor:(NXConnection *)connection
	freeAfterEncoding:(BOOL *)flagp isBycopy:(BOOL)isByCopy
{
	return [super encodeRemotelyFor:connection
				freeAfterEncoding:flagp isBycopy:isByCopy];
}

- (const char *)getInspectorClassName
{
	return "MiscScreenColorInspector";
}

- (NXImage *)getIBImage
{
	return [NXImage findImageNamed:"MiscScreenColorObj"];
}

@end