
/*
	NSComboBoxCell subclass to do auto-complete case-insensitively
	Original Source: <http://cocoa.karelia.com/AppKit_Classes/NSComboBoxCell_subc.m>
	(See copyright notice at <http://cocoa.karelia.com>)
*/

/*
	This is the method you need to override in your subclass of NSComboBoxCell so that autocompletion will be done case-insensitively.    To force your subclass to be instantiated for all combo boxes in  your application, you can use class posing like this at startup of your application:
	
	[[MyComboBoxCell class] poseAsClass:[NSComboBoxCell class]];

*/

@interface MyComboBoxCell : NSComboBoxCell
- (NSString *)completedString:(NSString *)substring;
@end

@implementation MyComboBoxCell

- (NSString *)completedString:(NSString *)substring
{
	if ([self usesDataSource])
	{
		return [super completedString:substring];
	}
	else	// basically do what complete should do -- be case insensitive.
	{
		NSArray *currentList = [self objectValues];
		NSEnumerator *theEnum = [currentList objectEnumerator];
		id eachString;
		int maxLength = 0;
		NSString *bestMatch = @"";
	
		while (nil != (eachString = [theEnum nextObject]) )
		{
			NSString *commonPrefix = [eachString commonPrefixWithString:substring options:NSCaseInsensitiveSearch];
			if ([commonPrefix length] >= [substring length] && [commonPrefix length] > maxLength)
			{
				maxLength = [commonPrefix length];
				// bestMatch = eachString;

				// Build match string based on what user has typed so far, to show changes in capitalization.
				bestMatch = [NSString stringWithFormat:@"%@%@",substring, [eachString substringFromIndex:[substring length]]];
			}
		}
		return bestMatch;
	}
}

@end