
/*
 *      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 "TimeTextField.h"
#import "TimeTFCell.h"
#import <strings.h>

@implementation TimeTextField


/* set the custom Cell class as the Cell class for this TextField class */
+ initialize
{
	[super initialize];
	[TimeTextField setCellClass:[TimeTFCell class]];
	return self;
}

/* override to call endEditing: on the Cell so that the text filter function of 
 * the fieldEditor will be reset to the original. 
 */
- textDidEnd:anObject endChar:(unsigned short)whyEnd
{
	[[self cell] endEditing:anObject];
	[super textDidEnd:anObject endChar:whyEnd];
	return self;
}

/* override to check that when the user presses return to stop editing the 
 * field, the entry is complete and correct.  Don't accept the value until it 
 * has the right length.  Remember, the format of the entry has been validated 
 * by the text filter function so just check the length here. 
 */
- textWillEnd:textObject
{
	char *currVal;
	
	/* get the current value in the DateTextField */
	currVal = (char *)[self stringValue];
	
	/* if the string is 5 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 time.  
	 * By returning nil, the TimeTextField will remain the firstResponder 
	 * and the user must finish entering the date.
	 */
	if (strlen(currVal) == 5) {
		return nil;
	} else {
		[super textWillEnd:textObject];
		return self;
	}
}

@end
