/*
 *	MiscTabButtonCellInspector.m --
 *		Written by Stephane Corthesy Copyright 1997 by Stephane Corthesy.
 *				Version 1.0.  All rights reserved.
 *
 *		This notice may not be removed from this source code.
 *
 *	This object is included in the MiscKit by permission from the author
 *	and its use is governed by the MiscKit license, found in the file
 *	"License.rtf" in the MiscKit distribution.  Please refer to that file
 *	for a list of all applicable permissions and restrictions.
 */	

#ifdef MISCKIT
#import <misckit/MiscTabButtonCellInspector.h>
#import <misckit/MiscTabButtonCell.subproj/MiscTabButtonCell.h>
#else
#import "MiscTabButtonCellInspector.h"
#import "MiscTabButtonCell.subproj/MiscTabButtonCell.h"
#endif
#import "MiscTabButtonCellInspector.h"

@implementation MiscTabButtonCellInspector

- init
{
    self = [super init];
    if(![NSBundle loadNibNamed:@"MiscTabButtonCellInspector.nib" owner:self])
        NSLog(@"MiscTabButtonCellInspector:: Could not load MiscTabButtonCellInspector.nib");
	return self;
}

- (BOOL) wantsButtons
{
	return NO;
}

- (void) revert:(id)sender
{
#ifndef STRICT_OPENSTEP
    [titleCell setObjectValue:[[self object] title]];
    [imageCell setObjectValue:[[[self object] image] name]];
#else
    if([[self object] title])
        [titleCell setStringValue:[[self object] title]];
    else
        [titleCell setStringValue:@""];
    if([[[self object] image] name])
        [imageCell setStringValue:[[[self object] image] name]];
    else
        [imageCell setStringValue:@""];
#endif
    [tagCell setIntValue:[[self object] tag]];
#ifndef STRICT_OPENSTEP
    [keyCell setObjectValue:[[self object] keyEquivalent]];
#else
    if([[self object] keyEquivalent])
        [keyCell setStringValue:[[self object] keyEquivalent]];
    else
        [keyCell setStringValue:@""];
#endif
    [alignMatrix selectCellWithTag:[[self object] alignment]];
    [orientationMatrix selectCellWithTag:[[self object] orientation]];
    [positionMatrix selectCellWithTag:[[self object] imagePosition]];
    [optionMatrix setState:![[self object] isEnabled] atRow:0 column:0];
    if([[[self object] controlView] isKindOfClass:[NSMatrix class]]){
        [[optionMatrix cellWithTag:1] setEnabled:YES];
        [optionMatrix setState:[[(NSMatrix *)[[self object] controlView] selectedCell] isEqual:[self object]] atRow:1 column:0];
    }
    else{
        [[optionMatrix cellWithTag:1] setState:NO];
        [[optionMatrix cellWithTag:1] setEnabled:NO];
    }
    [optionMatrix setState:[[self object] doesRotateText] atRow:2 column:0];
    [optionMatrix setState:[[self object] doesRotateImage] atRow:3 column:0];
    [super revert:sender];
}

- (void)ok:(id)sender
{
    if([sender isEqual:alignMatrix])
        [[self object] setAlignment:[[sender selectedCell] tag]];
    else if([sender isEqual:optionMatrix]){
        switch([[sender selectedCell] tag]){
            case 0:
                [[self object] setEnabled:![[sender selectedCell] state]];
                break;
            case 1:
                if([[sender selectedCell] state] && [[[self object] controlView] isKindOfClass:[NSMatrix class]]){
                    int	row, column;

                    [(NSMatrix *)[[self object] controlView] getRow:&row column:&column ofCell:[self object]];
                    [(NSMatrix *)[[self object] controlView] selectCellAtRow:row column:column];
                }
                break;
            case 2:
                [[self object] setRotateText:[[sender selectedCell] state]];
                break;
            case 3:
                [[self object] setRotateImage:[[sender selectedCell] state]];
                break;
        }
    }
    else{//Do test sender!
        NSCell	*aCell = [sender cell];

        if([aCell isEqual:titleCell])
            [[self object] setTitle:[aCell stringValue]];
        else if([aCell isEqual:imageCell])
            [[self object] setImage:[NSImage imageNamed:[aCell stringValue]]];
        else if([aCell isEqual:tagCell])
            [[self object] setTag:[aCell intValue]];
        else if([aCell isEqual:keyCell])
            [[self object] setKeyEquivalent:[aCell stringValue]];
    }
    [super ok:sender];
}

- (void)validateImagePosition:(id)sender
{
    [[self object] setImagePosition:[[sender selectedCell] tag]];
    [super ok:sender];//should call [self ok:sender]!!
}

- (void)validateOrientation:(id)sender
{
    [[self object] setOrientation:[[sender selectedCell] tag]];
    [super ok:sender];
}

@end
