/*
		Copyright (c) Uwe Hoffmann, 1995.
                  All Rights Reserved.

Filename: MiscDiagramTree.m
Author:   Uwe Hoffmann
Date:	  Sep 03, 1995

$Id: MiscDiagramTree.m,v 1.1 1995/09/03 11:22:00 Uwe Hoffmann Exp $
 $Log: MiscDiagramTree.m,v $
*/

#import <foundation/foundation.h>
#import <foundation/NSArchiver.h>
#import <appkit/appkit.h>

#import "uwFoundationAdds.h"

#import "MiscDiagramTree.h"
#import "MiscUserPath.h"

@implementation MiscDiagramTree

+ tree
/*"Creates, initializes and returns a MiscDiagramTree instance."*/
{
	return [[[self alloc] init] autorelease];
} 

+ treeWithLabel:(NSString *)aLabel shapeType:(MiscShapeType)aShapeType
/*"Creates, initializes and returns a MiscDiagramTree instance with size aSize."*/
{
	return [[[self alloc] initWithLabel:aLabel shapeType:aShapeType] autorelease];
} 

- initWithLabel:(NSString *)aLabel shapeType:(MiscShapeType)aShapeType
/*"Initializes a newly created instance of MiscDiagramTree with aSize. 
This is the designated initializer of this class."*/
{  
	NSRect r;
	
	labelTextCell = [[TextFieldCell allocWithZone:[self zone]] initTextCell:[aLabel cString]];
	[labelTextCell setBackgroundTransparent:YES];
	[labelTextCell setTextGray:NX_BLACK];
	[labelTextCell setAlignment:NX_CENTERED];
	[labelTextCell setFont:[Font newFont:"Times-Roman" size:10.0]];
	[labelTextCell calcCellSize:(NXSize *)&(r.size)];
	self = [super initWithSize:r.size];
	shape = [MiscDiagramShape newWithZone:[self zone] shapeType:aShapeType bounds:r]; 	
   	return self;
}

- init
{  
   return [self initWithLabel:@"Node" shapeType:MiscRectangleShapeType];  	
}
   
- (void)dealloc
{
	[labelTextCell release];
	[shape release];
   	return [super dealloc];
}

- copyNodeWithZone:(NSZone *)zone
{
	MiscDiagramTree *theCopy;
	
	theCopy = [super copyNodeWithZone:zone];
	theCopy->shape = [shape copyWithZone:zone];
	theCopy->labelTextCell = [labelTextCell copyWithZone:zone];
	return theCopy;
}

- (void)encodeWithCoder:(NSCoder *)coder
{
	[super encodeWithCoder:coder];
	[coder encodeObject:shape];
	[coder encodeNXObject:labelTextCell];
}

- initWithCoder:(NSCoder *)coder
{
	self = [super initWithCoder:coder];
	shape = [[coder decodeObject] retain];
	labelTextCell = [[coder decodeNXObject] retain];
	return self;
}

- (void)layoutNode
{
	[shape moveTo:[self pos]];
}

- (void)drawOutline
{
	MiscDiagramTree *theChild,*theSibling;
	
	theChild = (MiscDiagramTree *)[self child];
	theSibling = (MiscDiagramTree *)[self sibling];
	[shape drawOutline];
	if(theChild)
		[theChild drawOutline];
	if(theSibling)
		[theSibling drawOutline];
}

- (void)drawFill
{
	MiscDiagramTree *theChild,*theSibling;
	
	theChild = (MiscDiagramTree *)[self child];
	theSibling = (MiscDiagramTree *)[self sibling];
	[shape drawFill];
	if(theChild)
		[theChild drawFill];
	if(theSibling)
		[theSibling drawFill];
}

- (void)drawCellsInView:aView attachments:(BOOL)aBool
{
	MiscDiagramTree *theChild,*theSibling;
	NSRect r;
	
	theChild = (MiscDiagramTree *)[self child];
	theSibling = (MiscDiagramTree *)[self sibling];
	r = [self nodeBounds];
	[labelTextCell drawInside:(NXRect *)&r inView:aView];
	if(theChild)
		[theChild drawCellsInView:aView attachments:aBool];
	if(theSibling)
		[theSibling drawCellsInView:aView attachments:aBool];
}

- (void)fillLines:(MiscUserPath *)linesPath
{
	MiscDiagramTree *theChild,*theSibling, *theParent;
	
	theChild = (MiscDiagramTree *)[self child];
	theSibling = (MiscDiagramTree *)[self sibling];
	theParent = (MiscDiagramTree *)[self parent];
	if(theParent){
		[linesPath moveto:([self pos].x + [self size].width / 2) :([self pos].y + [self size].height / 2)];
		[linesPath lineto:([theParent pos].x + [theParent size].width / 2) :([theParent pos].y + [theParent size].height / 2)];
	}
	if(theChild)
		[theChild fillLines:linesPath];
	if(theSibling)
		[theSibling fillLines:linesPath];
}

- (MiscShapeType)shapeType
{
	return [shape shapeType];
}

- (void)setShapeType:(MiscShapeType)aShapeType
{
	NSRect b;
	
	b = [shape innerBounds];
	[shape release];
	shape = [MiscDiagramShape newWithZone:[self zone] shapeType:aShapeType bounds:b];
	[self setSize:[shape bounds].size];
}

- (NSString *)label
{
	return [NSString stringWithCString:[(TextFieldCell *)labelTextCell stringValue]];
}

- (void)setLabel:(NSString *)aLabel
{
	NSSize s;
	
	[labelTextCell setStringValue:[aLabel cString]];
	[labelTextCell calcCellSize:(NXSize *)&s];
	[shape sizeTo:s];
	[self setSize:s];
}
			              
@end

