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


// ABOUT MOKit
// by Mike Ferris (mike@lorax.com)
//
// MOKit is a collection of useful and general objects.  Permission is 
// granted by the author to use MOKit in your own programs in any way 
// you see fit.  All other rights pertaining to the kit are reserved by the 
// author including the right to sell these objects as objects,  as part 
// of a LIBRARY, or as SOURCE CODE.  In plain English, I wish to retain 
// rights to these objects as objects, but allow the use of the objects 
// as pieces in a fully functional program.  Permission is also granted to 
// redistribute the source code of MOKit for FREE as long as this copyright 
// notice is left intact and unchanged.  NO WARRANTY is expressed or implied.  
// The author will under no circumstances be held responsible for ANY 
// consequences from the use of these objects.  Since you don't have to pay 
// for them, and full source is provided, I think this is perfectly fair.

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

#define CLASS_VERSION	0
#define CLASS_NAME		"MODateFormCellInspector"

#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

@interface MODateFormCellInspector(Private)

+ (Class)MO_loadClassBundle:(const char *)className;

@end

@implementation MODateFormCellInspector

static id MODateFormCellClass;

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

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

		// Load the MODateFormCell class if necessary
		MODateFormCellClass = 
					[self MO_loadClassBundle:MODATEFORMCELL_CLASS_NAME];
	}
	return self;
}

- init
// Load our nib file.
{
	char buf[MAXPATHLEN+1];
	id bundle;
	
	[super init];
	
	// load our nib file.
	bundle = [NXBundle bundleForClass:[self 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
