// MODateFormCellInspector.h
//
// by Mike Ferris
// Part of MOKit - MORegexPalette
// Copyright 1993, all rights reserved.

#import "MODateFormCellInspector.h"
#import "MOKit/MODateFormCell.h"
#import "MOKit/MOString.h"
#import <objc/objc-runtime.h>

#define CLASS_VERSION	0

#define BUNDLE_TYPE							"bundle"
#define MODATEFORMCELL_CLASS_NAME			"MODateFormCell"

#define NIB_TYPE							"nib"
#define NIB_NAME							"MODateFormCellInspector"

#define AMERICAN_TAG		0
#define EUROPEAN_TAG		1

@implementation MODateFormCellInspector

static id MODateFormCellClass;

+ MO_loadClassBundle:(const char *)className
// Finds the bundle of the same name as the class, grabs it and loads the
// class from it.
{
	char pathBuff[MAXPATHLEN+1];
	
	// Load the bundle
	if (objc_lookUpClass(className) == nil)  {
		// class is not already loaded... load it.
		id classBundle;
		
		// Look for the bundle in the main bundle first, 
		// else try in this classes bundle.
		if (![[NXBundle mainBundle] getPath:pathBuff forResource:className 
					ofType:BUNDLE_TYPE])  {
			if (![[NXBundle bundleForClass:[self class]] getPath:pathBuff 
						forResource:className ofType:BUNDLE_TYPE])  {
				NXLogError("[MORegexPalette loadClassBundle] failed to "
						"find %s class bundle.", className);
				return nil;
			}
		}
		classBundle = [[NXBundle allocFromZone:[self zone]] 
					initForDirectory:pathBuff];
		if (!classBundle)  {
			NXLogError("[MORegexPalette loadClassBundle] failed to "
						"create bundle for class %s.", className);
			return nil;
		}
		if (![classBundle classNamed:className])  {
			NXLogError("[MORegexPalette loadClassBundle] failed to "
						"load %s class from bundle.", className);
			return nil;
		}
	}
	
	return self;
}

+ initialize
// Set the version.
{
	if (self == [MODateFormCellInspector class])  {
		[self setVersion:CLASS_VERSION];

		// Load the MODateFormCell class if necessary
		[self MO_loadClassBundle:MODATEFORMCELL_CLASS_NAME];
		MODateFormCellClass = 
					objc_lookUpClass(MODATEFORMCELL_CLASS_NAME);
		if (!MODateFormCellClass)  {
			NXLogError("[MODateFormCellInspector initialize]: failed to find "
						"MODateFormCell class.");
		}
	}
	return self;
}

- init
// Load our nib file.
{
	char buf[MAXPATHLEN+1];
	id bundle;
	
	[super init];
	
	// load our nib file.
	bundle = [NXBundle bundleForClass:[MODateFormCellInspector class]];
	[bundle getPath:buf forResource:NIB_NAME ofType:NIB_TYPE];
	[NXApp loadNibFile:buf owner:self withNames:NO fromZone:[self zone]];
	
	return self;
}

- ok:sender
// set the tag and enabled flag.  The patternButtonAction takes care of the
// other stuff.
{
	[object setTag:[tagForm intValueAt:0]];
	[object setEnabled:![[checkboxMatrix cellAt:0:0] state]];
	[object setAllowEmptyString:[[checkboxMatrix cellAt:1:0] state]];
	[object setEuropeanStyle:[styleRadioMatrix selectedTag]];
	[object setFormatDates:[formatCheckbox state]];
	[object setFormatString:[formatForm stringValueAt:0]];
	return [super ok:sender];
}

- revert:sender
// fill in the inspector with the attributes of "object"
{
	[checkboxMatrix setState:![object isEnabled] at:0:0];
	[checkboxMatrix setState:[object doesAllowEmptyString] at:1:0];
	[tagForm setIntValue:[object tag] at:0];
	
	if ([object isEuropeanStyle])  {
		[styleRadioMatrix selectCellWithTag:EUROPEAN_TAG];
	}  else  {
		[styleRadioMatrix selectCellWithTag:AMERICAN_TAG];
	}
	
	[formatCheckbox setState:[object doesFormatDates]];
	[formatForm setStringValue:[object formatString] at:0];
	
	return [super revert:sender];
}

- (BOOL)wantsButtons
// Our inspector does not have OK or Revert buttons.
{
	return NO;
}


@end
