// MORegexPalette.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 "MORegexPalette.h"
#import "MOKit/MORegexFormCell.h"
#import "MOKit/MORegexTextCell.h"
#import <appkit/appkit.h>
#import <objc/objc-runtime.h>

#define CLASS_VERSION	0
#define CLASS_NAME 		"MORegexPalette"

#define BUNDLE_TYPE							"bundle"

#define MOSTRING_CLASS_NAME					"MOString"
#define MOREGEXFORMCELL_CLASS_NAME			"MORegexFormCell"
#define MOREGEXTEXTCELL_CLASS_NAME			"MORegexTextCell"
#define MODATEFORMCELL_CLASS_NAME			"MODateFormCell"
#define MODATETEXTCELL_CLASS_NAME			"MODateTextCell"


#define FORM_GENERIC_NAME					"Generic:"
#define FORM_INT_NAME						"Integer:"
#define FORM_FLOAT_NAME						"Float:"
#define FORM_TIME_NAME						"Time:"
#define FORM_DATE_NAME						"Date:"

#define INT_REGEX		"^[ ]*([+-]?[0-9]+)[ ]*$"

#define FLOAT_REGEX		"^[ ]*(([+-]?[0-9]*)\\.?([0-9]*)[eE]?([0-9]*))[ ]*$"

#define TIME_REGEX_1	"^[ ]*(([0]?[0-9]|[1][0-2]):([0-5][0-9])[ ]*([aA][mM]?|[pP][mM]?))[ ]*$"
#define TIME_REGEX_2	"^[ ]*(([0]?[0-9]|[1][0-9]|[2][0-3]):([0-5][0-9])())[ ]*$"


@interface MORegexPalette(Private)

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

@end

@implementation  MORegexPalette

static id MOStringClass;
static id MORegexFormCellClass;
static id MORegexTextCellClass;
static id MODateFormCellClass;
static id MODateTextCellClass;

+ (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 all needed classes
		MOStringClass = [self MO_loadClassBundle:MOSTRING_CLASS_NAME];
		MORegexFormCellClass = 
					[self MO_loadClassBundle:MOREGEXFORMCELL_CLASS_NAME];
		MORegexTextCellClass = 
					[self MO_loadClassBundle:MOREGEXTEXTCELL_CLASS_NAME];
		MODateFormCellClass = 
					[self MO_loadClassBundle:MODATEFORMCELL_CLASS_NAME];
		MODateTextCellClass = 
					[self MO_loadClassBundle:MODATETEXTCELL_CLASS_NAME];
	}
	return self;
}

- finishInstantiate
// First load up the class bundles, one for each included class.
// Then set up all the palette controls.
{
	id protoCell;
	NXSize size;
		
	// Set up the generic MORegexFormCell
	protoCell = [[MORegexFormCellClass allocFromZone:[self zone]] 
						initTextCell:NULL];
	[genericForm setPrototype:protoCell];
	[genericForm addEntry:FORM_GENERIC_NAME];
	[genericForm sizeToFit];

	// Set up the integer MORegexFormCell
	protoCell = [[MORegexFormCellClass allocFromZone:[self zone]] 
						initTextCell:NULL];
	[protoCell addRegexStr:INT_REGEX];
	[intForm setPrototype:protoCell];
	[intForm addEntry:FORM_INT_NAME];
	[intForm sizeToFit];

	// Set up the float MORegexFormCell
	protoCell = [[MORegexFormCellClass allocFromZone:[self zone]] 
						initTextCell:NULL];
	[protoCell addRegexStr:FLOAT_REGEX];
	[floatForm setPrototype:protoCell];
	[floatForm addEntry:FORM_FLOAT_NAME];
	[floatForm sizeToFit];

	// Set up the time MORegexFormCell
	protoCell = [[MORegexFormCellClass allocFromZone:[self zone]] 
						initTextCell:NULL];
	[protoCell addRegexStr:TIME_REGEX_1];
	[protoCell addRegexStr:TIME_REGEX_2];
	[timeForm setPrototype:protoCell];
	[timeForm addEntry:FORM_TIME_NAME];
	[timeForm sizeToFit];

	// Set up the date MODateFormCell
	protoCell = [[MODateFormCellClass allocFromZone:[self zone]] 
						initTextCell:NULL];
	[dateForm setPrototype:protoCell];
	[dateForm addEntry:FORM_DATE_NAME];
	[dateForm sizeToFit];

	// Set up the generic MORegexTextCell
	protoCell = [[MORegexTextCellClass allocFromZone:[self zone]] 
						initTextCell:NULL];
	[protoCell setBezeled:YES];
	[protoCell setBackgroundGray:NX_WHITE];
	[protoCell setTextGray:NX_BLACK];
	[protoCell setEditable:YES];
	[genericMatrix setPrototype:protoCell];
	size.width = 100; size.height = 21;
	[genericMatrix setCellSize:&size];
	size.width = 4; size.height = 4;
	[genericMatrix setIntercell:&size];
	[genericMatrix renewRows:1 cols:1];
	[genericMatrix sizeToCells];
	
	// Set up the integer MORegexTextCell
	protoCell = [[MORegexTextCellClass allocFromZone:[self zone]] 
						initTextCell:NULL];
	[protoCell setBezeled:YES];
	[protoCell setBackgroundGray:NX_WHITE];
	[protoCell setTextGray:NX_BLACK];
	[protoCell setEditable:YES];
	[protoCell addRegexStr:INT_REGEX];
	[intMatrix setPrototype:protoCell];
	size.width = 100; size.height = 21;
	[intMatrix setCellSize:&size];
	size.width = 4; size.height = 4;
	[intMatrix setIntercell:&size];
	[intMatrix renewRows:1 cols:1];
	[intMatrix sizeToCells];
	
	// Set up the float MORegexTextCell
	protoCell = [[MORegexTextCellClass allocFromZone:[self zone]] 
						initTextCell:NULL];
	[protoCell setBezeled:YES];
	[protoCell setBackgroundGray:NX_WHITE];
	[protoCell setTextGray:NX_BLACK];
	[protoCell setEditable:YES];
	[protoCell addRegexStr:FLOAT_REGEX];
	[floatMatrix setPrototype:protoCell];
	size.width = 100; size.height = 21;
	[floatMatrix setCellSize:&size];
	size.width = 4; size.height = 4;
	[floatMatrix setIntercell:&size];
	[floatMatrix renewRows:1 cols:1];
	[floatMatrix sizeToCells];
	
	// Set up the time MORegexTextCell
	protoCell = [[MORegexTextCellClass allocFromZone:[self zone]] 
						initTextCell:NULL];
	[protoCell setBezeled:YES];
	[protoCell setBackgroundGray:NX_WHITE];
	[protoCell setTextGray:NX_BLACK];
	[protoCell setEditable:YES];
	[protoCell addRegexStr:TIME_REGEX_1];
	[protoCell addRegexStr:TIME_REGEX_2];
	[timeMatrix setPrototype:protoCell];
	size.width = 100; size.height = 21;
	[timeMatrix setCellSize:&size];
	size.width = 4; size.height = 4;
	[timeMatrix setIntercell:&size];
	[timeMatrix renewRows:1 cols:1];
	[timeMatrix sizeToCells];
	
	// Set up the time MORegexTextCell
	protoCell = [[MODateTextCellClass allocFromZone:[self zone]] 
						initTextCell:NULL];
	[protoCell setBezeled:YES];
	[protoCell setBackgroundGray:NX_WHITE];
	[protoCell setTextGray:NX_BLACK];
	[protoCell setEditable:YES];
	[dateMatrix setPrototype:protoCell];
	size.width = 100; size.height = 21;
	[dateMatrix setCellSize:&size];
	size.width = 4; size.height = 4;
	[dateMatrix setIntercell:&size];
	[dateMatrix renewRows:1 cols:1];
	[dateMatrix sizeToCells];
	
	return self;
}

@end
