
/*
 *      You may freely copy, distribute and reuse the code
 *      in this example.  NeXT disclaims any warranty of
 *      any kind, expressed or implied, as to its fitness
 *      for any particular use.
 *
 *      Written by: Susan Rayl
 *
 *      Created 21-Mar-91
 */

#import "DateTextField.h"
#import "DateTFCell.h"
#import <strings.h>

@implementation DateTextField

/* Set the Cell for this class of Control to be a DateTFCell.  The DateTFCell
 * will change the text filter function of the Text object acting as the 
 * fieldEditor for this DateTextField.  Do this in the Cell since there are 
 * methods there specifically for editing the text in the Cell and thus for 
 * managing the Text object which supports the Cell.
 * 
 * This is a better design . . . isn't it?  Well, it works.  But there's a 
 * BETTER design.  The functionality in the DateTFCell class should be 
 * placed in an abstract class, called FilterTextCell, and subclasses 
 * should be made from this class for specific filtering functionality.  See 
 * the implementation of the TimeTFCell and SSTFCell classes for this preferred 
 * solution.
 */
+ initialize
{
	/* do whatever the superclass would do */
	[super initialize];
	
	/* set a custom Cell class for this Control subclass */
	[DateTextField setCellClass:[DateTFCell class]];
	return self;
}

- textDidEnd:anObject endChar:(unsigned short)whyEnd
{
	/* textDidEnd:endChar: should call endEditing: on the Control's cell to 
	 * remove the fieldEditor from the View hierarchy.
	 */
	[[self cell] endEditing:anObject];
	
	/* do whatever the superclass does but do it AFTER you have changed the
	 * Text object.
	 */
	[super textDidEnd:anObject endChar:whyEnd];
	return self;
}

- textWillEnd:textObject
{
	char *currVal;
	
	/* get the current value in the DateTextField */
	currVal = (char *)[self stringValue];
	
	/* if the string is 8 chars long then, because it has been filtered, it 
	 * must completely and correctly specify a date string.  Else, the user 
	 * has terminated the editing of the field before completing the date.  
	 * By returning nil, the DateTextField will remain the firstResponder 
	 * and the user must finish entering the date.
	 */
	if (strlen(currVal) == 8) {
		return nil;
	} else {
		[super textWillEnd:textObject];
		return self;
	}
}

@end
