# HG changeset patch # User bsmith@81767d24-ef19-dc11-ae90-00e081727c95 # Date 1302201847 0 # Node ID 29d8ae25a78ccee412a6d2adc7cea28b05f8b948 # Parent d94a4fa0359e01bb5ad79c16f9a1acb24598c41e Implemented dw_entryfield_set_limit() on the Mac using a custom formatter class. diff -r d94a4fa0359e -r 29d8ae25a78c mac/dw.m --- a/mac/dw.m Thu Apr 07 18:20:04 2011 +0000 +++ b/mac/dw.m Thu Apr 07 18:44:07 2011 +0000 @@ -761,6 +761,43 @@ -(void)setVCenter:(BOOL)input { vcenter = input; } @end +@interface DWEntryFieldFormatter : NSFormatter +{ + int maxLength; +} +- (void)setMaximumLength:(int)len; +- (int)maximumLength; +@end + +/* This formatter subclass will allow us to limit + * the text length in an entryfield. + */ +@implementation DWEntryFieldFormatter +-(id)init +{ + [super init]; + maxLength = INT_MAX; + return self; +} +-(void)setMaximumLength:(int)len { maxLength = len; } +-(int)maximumLength { return maxLength; } +-(NSString *)stringForObjectValue:(id)object { return (NSString *)object; } +-(BOOL)getObjectValue:(id *)object forString:(NSString *)string errorDescription:(NSString **)error { *object = string; return YES; } +-(BOOL)isPartialStringValid:(NSString **)partialStringPtr + proposedSelectedRange:(NSRangePointer)proposedSelRangePtr + originalString:(NSString *)origString + originalSelectedRange:(NSRange)origSelRange + errorDescription:(NSString **)error +{ + if([*partialStringPtr length] > maxLength) + { + return NO; + } + return YES; +} +-(NSAttributedString *)attributedStringForObjectValue:(id)anObject withDefaultAttributes:(NSDictionary *)attributes { return nil; } +@end + /* Subclass for a entryfield type */ @interface DWEntryField : NSTextField { @@ -3107,7 +3144,11 @@ */ void API dw_entryfield_set_limit(HWND handle, ULONG limit) { - NSLog(@"dw_entryfield_set_limit() unimplemented\n"); + DWEntryField *entry = handle; + DWEntryFieldFormatter *formatter = [[DWEntryFieldFormatter alloc] init]; + + [formatter setMaximumLength:(int)limit]; + [[entry cell] setFormatter:formatter]; } /*