/*

File IKBrowserCell.m

Release 1.2, 7 June 1994
Copyright (C) 1994 by H. Scott Roy

This code is part of IconKit, a general toolbox for drag-and-drop applications.  IconKit is free for noncommercial use, but costs money for a commercial license.  You should have received a copy of the license agreement with this file.  If not, a copy of the license and the complete source of IconKit can be obtained from the author:

		H. Scott Roy
		2573 Stowe Ct.
		Northbrook, IL  60062-8103
		iconkit@cs.stanford.edu

For your editing convenience, this file is best viewed using an editor that automatically wraps long lines, in a fixed point font at 80 columns, with tabs every 4 spaces.

*/


/* ========================================================================== */


/*

This file defines an NXBrowser cell subclass that can keep track of an IKIconObject.  Such a cell greatly simplifies the process of finding out what's been selected.

*/

#import <appkit/appkit.h>
#import "iconkit.h"

@implementation IKBrowserCell


/* ========================================================================== */


/*

An IKBrowserCell registers itself as a user of its delegate, so that it can properly respond to changes.  An IKBrowserCell takes all its vital statistics directly from its delegate.

*/


- free
{
	[delegate  removeUser: self];
	return self = [super  free];
}


- setDelegate: theDelegate
{
	id
		old = delegate;
	const char
		* name;
	
	if (!theDelegate  || [theDelegate  conformsTo: @protocol(IKDependency)])
			delegate = theDelegate;
	else
	{
		NXLogError ("%s does not conform to IKDependency", [theDelegate  name]);
		delegate = nil;
	}
	
	[delegate  addUser: self];
	[[[self	setStringValue: (name = [delegate  name]) ? name : ""]
			setLoaded: YES]
			setLeaf: [delegate  respondsTo:@selector(isLeaf)] ?
							[delegate  isLeaf]:
							YES];
		
	return [old  removeUser: self];
}


- delegate
{
	return delegate;
}


- willFree: sender
{
	if (delegate == sender) delegate = nil;
	return self;
}


/* ========================================================================== */


/*

An IKBrowserCell automatically redraws itself when its delegate takes on a new name or changes its leaf status.  Accordingly, it remembers its parent matrix so that it can send it drawing requests.

Redrawing is complicated slightly by the fact that the NXBrowser, rather than the parent matrix, takes care of erasing the cell background.  An IKBrowserCell therefore erases things itself when it must.

*/


- drawInside: (const NXRect *) cellFrame  inView: controlView;
{
	char
		title [1000];
	id
		branchIcon = [NXBrowserCell  branchIcon];
	NXSize
		size;
	
	matrix = controlView;
	if (needsErase && ([matrix  cellBackgroundGray] == -1.0))
	{
		PSsetgray (NX_LTGRAY);
		NXRectFill (cellFrame);
	}
	
	needsErase = NO;
	
	strcpy (title, contents);
	[branchIcon  getSize: &size];
	IKShortenTitle (self, cellFrame->size.width - size.width);
	[super  drawInside: cellFrame  inView: matrix];
	strcpy (contents, title);
	
	return self;
}


- didChangeName: sender
{
	if ((sender == delegate) && [self  isLoaded])
	{
		[self  setStringValue: [delegate  name]];
		needsErase = ![self  state];
		[matrix  updateCell: self];
	}
	
	return self;
}


- didChangeProperties: sender
{
	if ((sender == delegate) && ([delegate  isLeaf] != [self  isLeaf]))
	{
		[self  setLeaf: [delegate  isLeaf]];
		needsErase = ![self  state];
		[matrix  updateCell: self];
	}
	
	return self;
}


@end
