/*	MiscTDVDragging.h

	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 "MiscTreeDiagramView.h"
#import "MiscDiagramTree.h"
#import "MiscTreeDiagram.h"
#import "MiscUserPath.h"
#import "MiscHitPath.h"
#import "MiscTDVPrivate.h"
#import "MiscTDViewUtils.h"

@implementation MiscTreeDiagramView(Dragging)

+ (void)pasteboard:(NSPasteboard *)pb provideDataForType:(NSString *)type
{
	NSData *data;

    	if(([type isEqualToString:NSPostScriptPboardType]) || ([type isEqualToString:NSStringPboardType])){
		data = [pb dataForType:MiscTreeDiagramPboardType];
		if(data){
			if([type isEqualToString:NSPostScriptPboardType])
		    		[self _internalConvert:data toPostscriptPasteboard:pb];
			else 
		    		[self _internalConvert:data toAsciiPasteboard:pb];
		}
    } 
}

- (unsigned int)draggingEntered:sender
{
	NSPasteboard *pboard;
    	unsigned int sourceMask;
	NSPoint p;
	MiscDiagramTree *dragNode;
	NSPoint thePoint;
	BOOL onOutline;
	
    	sourceMask = [sender draggingSourceOperationMask];
    	pboard = [sender draggingPasteboard];
	p = [sender draggingLocation];
	p = [self convertPoint:p fromView:nil];
	thePoint.x = p.x;
	thePoint.y = p.y;
	[hitPath moveTo:thePoint];
	dragNode = [treeDiagram acceptsDraggingWithHitPath:hitPath onOutline:&onOutline];
	if(dragNode){
		if(miscTD_includesType([pboard types], NSColorPboardType) && opFlags.editable)
			return NSDragOperationGeneric;
		else if(miscTD_includesType([pboard types], MiscTreeDiagramPboardType) && opFlags.changeable)
			return NSDragOperationCopy;
		else if(miscTD_includesType([pboard types], MiscTreeDiagramStylePboardType) && opFlags.editable)
			return NSDragOperationGeneric;
	} else if(![treeDiagram root] && 
			miscTD_includesType([pboard types], MiscTreeDiagramPboardType) && opFlags.changeable)
		return NSDragOperationCopy;
	return NSDragOperationNone;
}

- (unsigned int)draggingUpdated:sender
{
	NSPasteboard *pboard;
    	unsigned int sourceMask;
	NSPoint p;
	MiscDiagramTree *dragNode;
	NSPoint thePoint;
	BOOL onOutline;
	
    	sourceMask = [sender draggingSourceOperationMask];
    	pboard = [sender draggingPasteboard];
	p = [sender draggingLocation];
	p = [self convertPoint:p fromView:nil];
	thePoint.x = p.x;
	thePoint.y = p.y;
	[hitPath moveTo:thePoint];
	dragNode = [treeDiagram acceptsDraggingWithHitPath:hitPath onOutline:&onOutline];
	if(dragNode){
		if(miscTD_includesType([pboard types], NSColorPboardType) && opFlags.editable)
			return NSDragOperationGeneric;
		else if(miscTD_includesType([pboard types], MiscTreeDiagramPboardType) && opFlags.changeable)
			return NSDragOperationCopy;
		else if(miscTD_includesType([pboard types], MiscTreeDiagramStylePboardType) && opFlags.editable)
			return NSDragOperationGeneric;
	} else if(![treeDiagram root] && 
			miscTD_includesType([pboard types], MiscTreeDiagramPboardType) && opFlags.changeable)
		return NSDragOperationCopy;
	return NSDragOperationNone;
}

- (BOOL)performDragOperation:sender
{
	NSPasteboard *pboard;
    	unsigned int sourceMask;
	NSPoint p;
	MiscDiagramTree *dragNode;
	NSPoint thePoint;
	BOOL success = NO;
	MiscDiagramTree *pasteTree = nil, *copyTree;
	NSData *data;
	BOOL onOutline;
	id pasteObject;
	int i, nrCopies;
	
    	sourceMask = [sender draggingSourceOperationMask];
    	pboard = [sender draggingPasteboard];
	p = [sender draggingLocation];
	p = [self convertPoint:p fromView:nil];
	thePoint.x = p.x;
	thePoint.y = p.y;
	[hitPath moveTo:thePoint];
	dragNode = [treeDiagram acceptsDraggingWithHitPath:hitPath onOutline:&onOutline];
	if(dragNode){
		if(miscTD_includesType([pboard types], NSColorPboardType) && opFlags.editable){
			NSColor *color = [NSColor colorFromPasteboard:pboard];
			if(onOutline)
				[treeDiagram setOutlineColor:color of:dragNode];
			else
				[treeDiagram setFillColor:color of:dragNode];
			success = YES;
		} else if(miscTD_includesType([pboard types], MiscTreeDiagramPboardType) && opFlags.changeable){
			data = [pboard dataForType:MiscTreeDiagramPboardType];
			if(data){
				pasteObject = [NSUnarchiver unarchiveObjectWithData:data];
				if([pasteObject isKindOfClass:[NSDictionary class]]){
					pasteTree = (MiscDiagramTree *)[pasteObject objectForKey:@"Template"];
					nrCopies = [[pasteObject objectForKey:@"NumberOfCopies"] intValue];
					[treeDiagram beginEditing];
					for(i = 0;i < nrCopies;i++){
						copyTree = (MiscDiagramTree *)[pasteTree copy];
						[treeDiagram addChild:copyTree to:dragNode];
						[copyTree release];
					}
					[treeDiagram endEditing];
					success = YES;
				} else if([pasteObject isKindOfClass:[MiscDiagramTree class]]){ 
					pasteTree = (MiscDiagramTree *)pasteObject;
					[treeDiagram addChild:pasteTree to:dragNode];
					success = YES;
				}
			}
		} else if(miscTD_includesType([pboard types], MiscTreeDiagramStylePboardType) && opFlags.editable){
			data = [pboard dataForType:MiscTreeDiagramStylePboardType];
			if(data){
				pasteObject = [NSUnarchiver unarchiveObjectWithData:data];
				[treeDiagram beginEditing];
				[dragNode setNodeStyle:[pasteObject objectForKey:@"NodeStyle"]];
				[dragNode setShapeType:[[pasteObject objectForKey:@"ShapeType"] intValue]];
				[treeDiagram endEditing];
				success = YES;
			}
		}
	} else if(![treeDiagram root] && 
		miscTD_includesType([pboard types], MiscTreeDiagramPboardType) && opFlags.changeable){
		data = [pboard dataForType:MiscTreeDiagramPboardType];
		if(data){
			pasteObject = [NSUnarchiver unarchiveObjectWithData:data];
			if([pasteObject isKindOfClass:[NSDictionary class]])
				pasteTree = (MiscDiagramTree *)[pasteObject objectForKey:@"Template"];	
			else if([pasteObject isKindOfClass:[MiscDiagramTree class]])
				pasteTree = (MiscDiagramTree *)pasteObject;
			[treeDiagram setRoot:pasteTree];
			success = YES;
		}
	}
	return success;
}

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

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

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

@end
