#import "MFClickThroughDisabler.h"


@interface NSControl ( MFPrivateSimplifiedEnableRoutines )

- (void)mf_enable;
- (void)mf_disable;

@end


@implementation MFClickThroughDisabler

+ (id)clickThroughDisablerWithControl:(NSControl *)aControl {
	return [[[self alloc] initWithControl:aControl] autorelease];
}

- (id)initWithControl:(NSControl *)aControl {
        if (!aControl) return nil;
	self = [super init];
	[self setControl:aControl];
        myArray = nil;
        myWindow = nil;
	return self;
}

- (id)initWithControl:(NSControl *)aControl window:(NSWindow *)window
{
    if (!aControl || !window) return nil;
    self = [super init];
    myArray = nil;
    myWindow = [window retain];
    [self setControl:aControl];
    return self;
}

- (id)initWithArray:(NSArray *)anArray window:(NSWindow *)aWindow
{
    if (!anArray) return nil;
    self = [super init];
    control = nil;
    myWindow = [aWindow retain];
    [self setArray:anArray];
    return self;
}

- (void)dealloc
{
    if (control) {
        [control setEnabled:YES];
        [control release];
    }
    if (myArray) {
        int i, n = [myArray count];
        for (i=0;i<n;i++)
            [[myArray objectAtIndex:i] setEnabled:YES];
        [myArray release];
    }
    if (myWindow)
        [myWindow release];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [super dealloc];
}

- (NSControl *)control {
	return control;
}

- (NSArray *)array {
        return myArray;
}

- (NSWindow *)window {
        return myWindow;
}

- (void)setControl:(NSControl *)aControl {
	[control setEnabled:YES];
	[[NSNotificationCenter defaultCenter] removeObserver:self];
	[aControl retain];
	[control release];
	control = aControl;
        NSWindow *window = myWindow;
        if (!window)
            window = [control window];
	if ( control != nil ) {
		[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(windowDidBecomeMain:) name:NSWindowDidBecomeMainNotification object:window];
		[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(windowDidResignMain:) name:NSWindowDidResignMainNotification object:window];
		[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(toolbarItemWasRemoved:) name:NSToolbarDidRemoveItemNotification object:nil];
                
		[control setEnabled:([window isMainWindow])];
	}
}

- (void)setArray:(NSArray *)anArray {
        int i, n = [myArray count];
        for (i=0;i<n;i++)
            [[myArray objectAtIndex:i] setEnabled:YES];
        [[NSNotificationCenter defaultCenter] removeObserver:self];
        [anArray retain];
        [myArray release];
        myArray = anArray;
        if (myArray && myWindow) {
                [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(windowDidBecomeMain:) name:NSWindowDidBecomeMainNotification object:myWindow];
                [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(windowDidResignMain:) name:NSWindowDidResignMainNotification object:myWindow];
                n = [myArray count];
                for (i=0;i<n;i++)
                    [[myArray objectAtIndex:i] setEnabled:([myWindow isMainWindow])];
        }
}

- (void)windowDidBecomeMain:(NSNotification *)notification {
    if (control)
	// Put it on the run loop so that it enable itself after receiving the click
	[[self control] performSelector:@selector(mf_enable) withObject:[self control] afterDelay:0];
    else if (myArray) {
        int i, n = [myArray count];
        for (i=0;i<n;i++)
            [[myArray objectAtIndex:i] performSelector:@selector(mf_enable) withObject:[myArray objectAtIndex:i] afterDelay:0];
    }

}

- (void)windowDidResignMain:(NSNotification *)notification {
    if (control)
        [[self control] setEnabled:NO];
    else if (myArray) {
        int i, n = [myArray count];
        for (i=0;i<n;i++)
            [[myArray objectAtIndex:i] setEnabled:NO];
    }
}

- (void)toolbarItemWasRemoved:(NSNotification *)notification
{
    NSToolbarItem *item = [[notification userInfo] objectForKey:@"item"];
    if ((id)item == (id)control)
        [self release];
}

@end


@implementation NSObject ( MFPrivateSimplifiedEnableRoutines )

- (void)mf_enable {
	[(NSControl *)self setEnabled:YES];
}
- (void)mf_disable {
	[(NSControl *)self setEnabled:NO];
}

@end