/*	TDStyleWell.m

	Copyright 1996 Uwe Hoffmann.

	This notice may not be removed from this source code.
	The use and distribution of this software is governed by the
	terms of the MiscKit license agreement.  Refer to the license
	document included with the MiscKit distribution for the terms.

	Version 2 (August 1996)
*/

#import "TDStyleWell.h"
#import <MiscAppKit/MiscDiagramTree.h>
#import <MiscAppKit/MiscTreeDiagram.h>
#import <MiscAppKit/MiscTreeDiagramView.h>
#import <MiscAppKit/MiscNodeStyle.h>
#import <MiscAppKit/MiscTreeStyle.h>

extern BOOL miscTD_includesType(NSArray *types, NSString *type);

@interface TDStyleWell(PrivateMethods)

- (BOOL)_internalWriteToPasteboard:(NSPasteboard *)pb;

@end

@implementation TDStyleWell

- initWithFrame:(NSRect)theFrame
{        
    	[super initWithFrame:theFrame];	
	dragImage = [[NSImage imageNamed:@"style"] retain];
	dragRect.size = [dragImage size];
	dragRect.origin.x = ([self bounds].size.width - dragRect.size.width) / 2;
	dragRect.origin.y = ([self bounds].size.height - dragRect.size.height) / 2;
	wellClient = nil;
        isEnabled = NO;
    	return self;
}

- (void)dealloc
{
	[dragImage release];
	return [super dealloc];
}

- (void)mouseDown:(NSEvent *)event 
{
   	NSPasteboard *dragPboard;
	BOOL error;
	NSSize zero = {0.0,0.0};	
	
	if(!isEnabled)
		return;
    	[NSApp preventWindowOrdering];
    	dragPboard = [NSPasteboard pasteboardWithName:NSDragPboard];
	error = [self _internalWriteToPasteboard:dragPboard];
	if(error)
		return;
    	[self dragImage:dragImage at:dragRect.origin 
		offset:zero 
		event:event pasteboard:dragPboard source:self slideBack:YES];
}

- (void)draggedImage:(NSImage *)image beganAt:(NSPoint)screenPoint
{
	
}

- (unsigned int)draggingSourceOperationMaskForLocal:(BOOL)flag
{
    return NSDragOperationGeneric;
}

- (void)draggedImage:(NSImage *)image endedAt:(NSPoint)screenPoint deposited:(BOOL)flag
{
    
}

- (BOOL)acceptsFirstMouse:(NSEvent *)theEvent
{
    return YES;
}

- (BOOL)shouldDelayWindowOrderingForEvent:(NSEvent *)theEvent 
{
    return YES;
}

- (void)setFrameSize:(NSSize)aSize
{
    	[super setFrameSize:aSize];
	dragRect.origin.x = ([self bounds].size.width - dragRect.size.width) / 2;
	dragRect.origin.y = ([self bounds].size.height - dragRect.size.height) / 2;
}


- (void)drawRect:(NSRect)r
{	
	PSsetgray(NSDarkGray); 
	NSRectFill([self bounds]);
	if(isEnabled)
		[dragImage compositeToPoint:dragRect.origin operation:NSCompositeSourceOver];
}

- (void)setEnabled:(BOOL)aBOOL
{
	isEnabled = aBOOL;
	[self display];
}

- (void)setWellClient:(id <TDStyleWellAPI>)aClient
{
    	wellClient = aClient;
}

- (id <TDStyleWellAPI>)wellClient
{
    	return wellClient;
}

@end

@implementation TDStyleWell(PrivateMethods)

- (BOOL)_internalWriteToPasteboard:(NSPasteboard *)pb
{
	NSData *data = nil;
	BOOL error = YES;

	if(wellClient && [wellClient hasSelection])
		data = [NSArchiver archivedDataWithRootObject:[wellClient styleSelection]];
	if(data){
		[pb declareTypes:[NSArray arrayWithObject:MiscTreeDiagramStylePboardType] owner:[self class]];
		[pb setData:data forType:MiscTreeDiagramStylePboardType];
		error = NO;
	}
	return error;
}


@end


