comparison ios/dw.m @ 2704:ae846e9f1ead

iOS: Implement dw_entryfield_set_limit() using delegate method.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Wed, 17 Nov 2021 22:12:59 +0000
parents 246f23670e3b
children ddcbed595a84
comparison
equal deleted inserted replaced
2703:321e2cf1282a 2704:ae846e9f1ead
1802 -(void)setBox:(void *)input { box = input; } 1802 -(void)setBox:(void *)input { box = input; }
1803 -(id)box { return box; } 1803 -(id)box { return box; }
1804 -(void)dealloc { UserData *root = userdata; _dw_remove_userdata(&root, NULL, TRUE); dw_signal_disconnect_by_window(self); [super dealloc]; } 1804 -(void)dealloc { UserData *root = userdata; _dw_remove_userdata(&root, NULL, TRUE); dw_signal_disconnect_by_window(self); [super dealloc]; }
1805 @end 1805 @end
1806 1806
1807 @interface DWEntryFieldFormatter : NSFormatter
1808 {
1809 int maxLength;
1810 }
1811 - (void)setMaximumLength:(int)len;
1812 - (int)maximumLength;
1813 @end
1814
1815 /* This formatter subclass will allow us to limit
1816 * the text length in an entryfield.
1817 */
1818 @implementation DWEntryFieldFormatter
1819 -(id)init
1820 {
1821 self = [super init];
1822 maxLength = INT_MAX;
1823 return self;
1824 }
1825 -(void)setMaximumLength:(int)len { maxLength = len; }
1826 -(int)maximumLength { return maxLength; }
1827 -(NSString *)stringForObjectValue:(id)object { return (NSString *)object; }
1828 -(BOOL)getObjectValue:(id *)object forString:(NSString *)string errorDescription:(NSString **)error { *object = string; return YES; }
1829 -(BOOL)isPartialStringValid:(NSString **)partialStringPtr
1830 proposedSelectedRange:(NSRangePointer)proposedSelRangePtr
1831 originalString:(NSString *)origString
1832 originalSelectedRange:(NSRange)origSelRange
1833 errorDescription:(NSString **)error
1834 {
1835 if([*partialStringPtr length] > maxLength)
1836 {
1837 return NO;
1838 }
1839 return YES;
1840 }
1841 -(NSAttributedString *)attributedStringForObjectValue:(id)anObject withDefaultAttributes:(NSDictionary *)attributes { return nil; }
1842 @end
1843
1844 /* Subclass for a entryfield type */ 1807 /* Subclass for a entryfield type */
1845 @interface DWEntryField : UITextField 1808 @interface DWEntryField : UITextField <UITextFieldDelegate>
1846 { 1809 {
1847 void *userdata; 1810 void *userdata;
1848 id clickDefault; 1811 id clickDefault;
1812 unsigned long limit;
1849 } 1813 }
1850 -(void *)userdata; 1814 -(void *)userdata;
1851 -(void)setUserdata:(void *)input; 1815 -(void)setUserdata:(void *)input;
1852 -(void)setClickDefault:(id)input; 1816 -(void)setClickDefault:(id)input;
1853 @end 1817 @end
1855 @implementation DWEntryField 1819 @implementation DWEntryField
1856 -(void *)userdata { return userdata; } 1820 -(void *)userdata { return userdata; }
1857 -(void)setUserdata:(void *)input { userdata = input; } 1821 -(void)setUserdata:(void *)input { userdata = input; }
1858 -(void)setClickDefault:(id)input { clickDefault = input; } 1822 -(void)setClickDefault:(id)input { clickDefault = input; }
1859 -(void)dealloc { UserData *root = userdata; _dw_remove_userdata(&root, NULL, TRUE); dw_signal_disconnect_by_window(self); [super dealloc]; } 1823 -(void)dealloc { UserData *root = userdata; _dw_remove_userdata(&root, NULL, TRUE); dw_signal_disconnect_by_window(self); [super dealloc]; }
1824 -(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
1825 {
1826 // Prevent crashing undo bug – see note below.
1827 if(range.length + range.location > textField.text.length)
1828 return NO;
1829
1830 if(limit > 0)
1831 {
1832 NSUInteger newLength = [textField.text length] + [string length] - range.length;
1833 return newLength <= limit;
1834 }
1835 return YES;
1836 }
1837 -(void)setMaximumLength:(unsigned long)length { limit = length; }
1838 -(unsigned long)maximumLength { return limit; }
1860 @end 1839 @end
1861 1840
1862 /* Subclass for a text and status text type */ 1841 /* Subclass for a text and status text type */
1863 @interface DWText : UILabel 1842 @interface DWText : UILabel
1864 { 1843 {
4288 DW_FUNCTION_RESTORE_PARAM2(text, const char *, cid, ULONG) 4267 DW_FUNCTION_RESTORE_PARAM2(text, const char *, cid, ULONG)
4289 { 4268 {
4290 DWEntryField *entry = [[[DWEntryField alloc] init] retain]; 4269 DWEntryField *entry = [[[DWEntryField alloc] init] retain];
4291 [entry setText:[ NSString stringWithUTF8String:text ]]; 4270 [entry setText:[ NSString stringWithUTF8String:text ]];
4292 [entry setTag:cid]; 4271 [entry setTag:cid];
4272 [entry setDelegate:entry];
4293 DW_FUNCTION_RETURN_THIS(entry); 4273 DW_FUNCTION_RETURN_THIS(entry);
4294 } 4274 }
4295 4275
4296 /* 4276 /*
4297 * Create a new Entryfield (password) window (widget) to be packed. 4277 * Create a new Entryfield (password) window (widget) to be packed.
4304 DW_FUNCTION_RETURN(dw_entryfield_password_new, HWND) 4284 DW_FUNCTION_RETURN(dw_entryfield_password_new, HWND)
4305 DW_FUNCTION_RESTORE_PARAM2(text, const char *, cid, ULONG) 4285 DW_FUNCTION_RESTORE_PARAM2(text, const char *, cid, ULONG)
4306 { 4286 {
4307 DWEntryField *entry = dw_entryfield_new(text, cid); 4287 DWEntryField *entry = dw_entryfield_new(text, cid);
4308 [entry setSecureTextEntry:YES]; 4288 [entry setSecureTextEntry:YES];
4289 [entry setDelegate:entry];
4309 DW_FUNCTION_RETURN_THIS(entry); 4290 DW_FUNCTION_RETURN_THIS(entry);
4310 } 4291 }
4311 4292
4312 /* 4293 /*
4313 * Sets the entryfield character limit. 4294 * Sets the entryfield character limit.
4314 * Parameters: 4295 * Parameters:
4315 * handle: Handle to the spinbutton to be set. 4296 * handle: Handle to the spinbutton to be set.
4316 * limit: Number of characters the entryfield will take. 4297 * limit: Number of characters the entryfield will take.
4317 */ 4298 */
4318 void API dw_entryfield_set_limit(HWND handle, ULONG limit) 4299 DW_FUNCTION_DEFINITION(dw_entryfield_set_limit, void, HWND handle, unsigned long limit)
4319 { 4300 DW_FUNCTION_ADD_PARAM2(handle, limit)
4320 #if 0 /* TODO: Implment this via textField:shouldChangeCharactersInRange:replacementString: */ 4301 DW_FUNCTION_NO_RETURN(dw_entryfield_set_limit)
4302 DW_FUNCTION_RESTORE_PARAM2(handle, HWND, limit, unsigned long)
4303 {
4321 DWEntryField *entry = handle; 4304 DWEntryField *entry = handle;
4322 DWEntryFieldFormatter *formatter = [[[DWEntryFieldFormatter alloc] init] autorelease]; 4305
4323 4306 [entry setMaximumLength:limit];
4324 [formatter setMaximumLength:(int)limit]; 4307 DW_FUNCTION_RETURN_NOTHING;
4325 [entry setFormatter:formatter];
4326 #endif
4327 } 4308 }
4328 4309
4329 /* 4310 /*
4330 * Create a new bitmap button window (widget) to be packed. 4311 * Create a new bitmap button window (widget) to be packed.
4331 * Parameters: 4312 * Parameters: