#pragma .h #import <AppKit/NSBrowserCell.h>
#pragma .h 
#pragma .h #import "IKDependency.h"
#pragma .h 
#pragma .h @interface NSObject (IKBrowserCellDelegate) <IKDependency>
#pragma .h - (NSString *)name;
#pragma .h - (BOOL)isLeaf;
#pragma .h - (BOOL)respondsToSelector:(SEL)message;
#pragma .h @end
#pragma .h

#import <Foundation/NSString.h>
#import <Foundation/NSUtilities.h>

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

@implementation IKBrowserCell:NSBrowserCell
{
    id		delegate;
    id		matrix;
    BOOL	needsErase;
}



- (void)dealloc
{
    [delegate removeUser: self];
    return [super dealloc];
}


- (void)setDelegate:(id)theDelegate
{
    id	     old = delegate;
    NSString *name;

    if (!theDelegate  || [theDelegate  conformsToProtocol: @protocol(IKDependency)]) {
        delegate = theDelegate;
    } else {
        NSLog (@"%@ does not conform to IKDependency", [(IKFolder *)theDelegate name]);
        delegate = nil;
    }
    [self setLoaded:YES];
    if(delegate) {
        [delegate addUser: self];
        [self setStringValue: (name = [(IKFolder *)delegate name]) ? name : @""];
        [self setLeaf: [delegate respondsToSelector:@selector(isLeaf)] ? [delegate isLeaf] : YES];
    } else {
        [self setStringValue:@""];
        [self setLeaf:YES];
    }
    if(old) {
        [old removeUser: self];
    }
}

- delegate
{
    return delegate;
}


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


- (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
{
    NSString *title;
    id       branchIcon = [NSBrowserCell branchImage];
    NSSize   size;

    matrix = controlView;
    if (needsErase && (![matrix drawsCellBackground])) {
        PSsetgray (NSLightGray);
        NSRectFill(cellFrame);
    }

    needsErase = NO;

    title = [[self stringValue] copy];
    size = [branchIcon size];
    IKShortenTitle (self, cellFrame.size.width - size.width);
    [super drawInteriorWithFrame:cellFrame inView:matrix];
    [self setStringValue:title];
    [title release];
}


- didChangeName: sender
{
    if ((sender == delegate) && [self  isLoaded]) {
        NSString	*name = [(IKFolder *)delegate name];
        [self  setStringValue: name ? 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
