#import "PAScrollViewDeluxeInspector.h"
#import "PAScrollViewDeluxe.h"


typedef struct { @defs (ScrollView) } *scrollViewId;

typedef struct { @defs (Scroller) } *scrollerId;

#define NONE	0
#define LINE	1
#define	BEZEL	2
#define	RIDGE	3

#define VERTIC	0
#define HORIZ	1


#define BLACK	0
#define DKGRAY	1
#define LTGRAY	2
#define WHITE	3

@implementation PAScrollViewDeluxeInspector

- init
{
	char path[MAXPATHLEN];
    NXBundle	*bundle = [NXBundle bundleForClass:[self class]];

	[super init];
    [bundle getPath:path forResource:"PAScrollViewDeluxeInspector"
		ofType:"nib"];
	[NXApp loadNibFile:path owner:self withNames:NO];
    return self;
}

- (BOOL)wantsButtons { return NO; }

- revert:sender
{
    int		i=0;
    id		scroller;
    float	gray;
    
    scroller = [object vertScroller];
    if (!scroller) scroller = [object horizScroller];
    switch ([object borderType]) {
        case NX_NONE:
	i = NONE;
	break;
        case NX_LINE:
	i = LINE;
	break;
        case NX_BEZEL:
	i = BEZEL;
	break;
        case NX_RIDGE:
	i = RIDGE;
	break;
    }
    [borderMatrix selectCellWithTag:i];
    gray = [object backgroundGray];
    i = ((gray == NX_BLACK)? 0 : ((gray < 0.5) ? 1 : ((gray < 0.8) ? 2 : 3)));
    [backGrayMatrix selectCellWithTag:i];
    if (((scrollViewId) object)->_sFlags.vScrollerRequired) 
    	[[scrollerMatrix findCellWithTag:VERTIC] setIntValue:1];
    else [[scrollerMatrix findCellWithTag:VERTIC] setIntValue:0];
    if (((scrollViewId) object)->_sFlags.hScrollerRequired) 
    	[[scrollerMatrix findCellWithTag:HORIZ] setIntValue:1];
    else [[scrollerMatrix findCellWithTag:HORIZ] setIntValue:0];
    [lineCell setFloatValue:((scrollViewId) object)->lineAmount];
    [pageCell setFloatValue:((scrollViewId) object)->pageContext];

	[pageUpDownSwitch setState:[object pageUpDownButtonsVisible]];
	[pageLeftRightSwitch setState:[object pageLeftRightButtonsVisible]];
	[zoomSwitch setState:[object zoomButtonVisible]];
	[topRulerSwitch setState:[object topViewVisible]];
	[leftRulerSwitch setState:[object leftViewVisible]];
	[deluxeMatrix display];
	return [super revert:sender];
}

- ok:sender
{
    int	i=0;
    float gray= 0.0;
    switch ([borderMatrix selectedTag]) {
        case NONE:
	i = NX_NONE;
	break;
        case LINE:
	i = NX_LINE;
	break;
        case BEZEL:
	i = NX_BEZEL;
	break;
        case RIDGE:
	i = NX_RIDGE;
	break;
    }
    [object setBorderType:i];
    switch ([backGrayMatrix selectedTag]) {
        case 0:
	gray = NX_BLACK;
	break;
        case 1:
	gray = NX_DKGRAY;
	break;
        case 2:
	gray = NX_LTGRAY;
	break;
        case 3:
	gray = NX_WHITE;
	break;
    }
    [object setBackgroundGray:gray];
    [object setVertScrollerRequired:
    		[[scrollerMatrix findCellWithTag:VERTIC] intValue]];
    [object setHorizScrollerRequired:
    		[[scrollerMatrix findCellWithTag:HORIZ] intValue]];
    if ([lineCell floatValue] < 1.0) [lineCell setFloatValue:1.0];
    [lineCell setFloatValue:ceil([lineCell floatValue])];
    ((scrollViewId) object)->lineAmount = [lineCell floatValue];
    if ([pageCell floatValue] < 1.0) [pageCell setFloatValue:1.0];
    [pageCell setFloatValue:ceil([pageCell floatValue])];
    ((scrollViewId) object)->pageContext = [pageCell floatValue];
	[object setPageLeftRightButtonsVisible:[pageLeftRightSwitch state]];
	[object setPageUpDownButtonsVisible:[pageUpDownSwitch state]];
	[object setZoomButtonVisible:[zoomSwitch state]];
	[object setTopViewVisible:[topRulerSwitch state]];
	[object setLeftViewVisible:[leftRulerSwitch state]];
	return [super ok:sender];
}

@end

@interface Menu(attachedMenu)
@end
@implementation Menu(attachedMenu)
- attachedMenu { return attachedMenu; }
@end

@interface DummyScrollView : ScrollView
@end

@implementation DummyScrollView : ScrollView
+ finishLoading:(struct mach_header *)header
{
	[self poseAs:[ScrollView class]];
	return self;
}

+ allocFromZone:(NXZone *)zone 
{
	id menuMatrix =[[[[NXApp mainMenu] attachedMenu] attachedMenu] itemList];
	id menuCell = [menuMatrix selectedCell];

	// If the menu cell exists, is enabled & has the correct title, use Deluxe
	if(([NXApp currentEvent]->flags&NX_COMMANDMASK) && menuCell && 
		[menuCell isEnabled] && ([menuMatrix selectedRow] == 5))
			return [[PAScrollViewDeluxe class] alloc];
	return [super allocFromZone:zone];
}

@end


