Mercurial > dwindows
comparison ios/dw.m @ 2428:9c5b95d66fc8
iOS: Make spin buttons actually function. Fix layout and autosizing issues.
author | bsmith@81767d24-ef19-dc11-ae90-00e081727c95 |
---|---|
date | Mon, 05 Apr 2021 00:12:20 +0000 |
parents | 4f078d24fe83 |
children | 89f4aba2e387 |
comparison
equal
deleted
inserted
replaced
2427:4f078d24fe83 | 2428:9c5b95d66fc8 |
---|---|
2184 -(id)init; | 2184 -(id)init; |
2185 -(void *)userdata; | 2185 -(void *)userdata; |
2186 -(void)setUserdata:(void *)input; | 2186 -(void)setUserdata:(void *)input; |
2187 -(UITextField *)textfield; | 2187 -(UITextField *)textfield; |
2188 -(UIStepper *)stepper; | 2188 -(UIStepper *)stepper; |
2189 -(void)controlTextDidChange:(NSNotification *)aNotification; | 2189 -(void)textFieldDidEndEditing:(UITextField *)textField; |
2190 -(void)setClickDefault:(id)input; | 2190 -(void)setClickDefault:(id)input; |
2191 @end | 2191 @end |
2192 | 2192 |
2193 @implementation DWSpinButton | 2193 @implementation DWSpinButton |
2194 -(id)init | 2194 -(id)init |
2203 [self addSubview:stepper]; | 2203 [self addSubview:stepper]; |
2204 [stepper setParent:self]; | 2204 [stepper setParent:self]; |
2205 [stepper setTextfield:textfield]; | 2205 [stepper setTextfield:textfield]; |
2206 [textfield setText:[NSString stringWithFormat:@"%ld",(long)[stepper value]]]; | 2206 [textfield setText:[NSString stringWithFormat:@"%ld",(long)[stepper value]]]; |
2207 [textfield setDelegate:self]; | 2207 [textfield setDelegate:self]; |
2208 [stepper addTarget:self action:@selector(stepperChanged:) forControlEvents:UIControlEventValueChanged]; | |
2208 } | 2209 } |
2209 return self; | 2210 return self; |
2210 } | 2211 } |
2211 -(void *)userdata { return userdata; } | 2212 -(void *)userdata { return userdata; } |
2212 -(void)setUserdata:(void *)input { userdata = input; } | 2213 -(void)setUserdata:(void *)input { userdata = input; } |
2213 -(UITextField *)textfield { return textfield; } | 2214 -(UITextField *)textfield { return textfield; } |
2214 -(UIStepper *)stepper { return stepper; } | 2215 -(UIStepper *)stepper { return stepper; } |
2215 -(void)controlTextDidChange:(NSNotification *)aNotification | 2216 -(void)textFieldDidEndEditing:(UITextField *)textField |
2216 { | 2217 { |
2217 long val = [[textfield text] intValue]; | 2218 long val = [[textfield text] intValue]; |
2218 [stepper setValue:(float)val]; | 2219 [stepper setValue:(float)val]; |
2219 _dw_event_handler(self, DW_INT_TO_POINTER(val), 14); | 2220 [textfield setText:[NSString stringWithFormat:@"%d", (int)[stepper value]]]; |
2221 _dw_event_handler(self, DW_INT_TO_POINTER((int)[stepper value]), 14); | |
2222 } | |
2223 -(void)stepperChanged:(UIStepper*)theStepper | |
2224 { | |
2225 [textfield setText:[NSString stringWithFormat:@"%d", (int)[theStepper value]]]; | |
2220 } | 2226 } |
2221 -(void)setClickDefault:(id)input { clickDefault = input; } | 2227 -(void)setClickDefault:(id)input { clickDefault = input; } |
2222 -(void)dealloc { UserData *root = userdata; _dw_remove_userdata(&root, NULL, TRUE); dw_signal_disconnect_by_window(self); [super dealloc]; } | 2228 -(void)dealloc { UserData *root = userdata; _dw_remove_userdata(&root, NULL, TRUE); dw_signal_disconnect_by_window(self); [super dealloc]; } |
2223 @end | 2229 @end |
2224 | 2230 |
2641 else if([handle isMemberOfClass:[DWSpinButton class]]) | 2647 else if([handle isMemberOfClass:[DWSpinButton class]]) |
2642 { | 2648 { |
2643 DWSpinButton *spinbutton = (DWSpinButton *)handle; | 2649 DWSpinButton *spinbutton = (DWSpinButton *)handle; |
2644 UITextField *textfield = [spinbutton textfield]; | 2650 UITextField *textfield = [spinbutton textfield]; |
2645 UIStepper *stepper = [spinbutton stepper]; | 2651 UIStepper *stepper = [spinbutton stepper]; |
2646 [textfield setFrame:CGRectMake(0,0,rect.size.width-20,rect.size.height)]; | 2652 NSInteger stepperwidth = [stepper intrinsicContentSize].width; |
2647 [stepper setFrame:CGRectMake(rect.size.width-20,0,20,rect.size.height)]; | 2653 [textfield setFrame:CGRectMake(0,0,rect.size.width-stepperwidth,rect.size.height)]; |
2654 [stepper setFrame:CGRectMake(rect.size.width-stepperwidth,0,stepperwidth,rect.size.height)]; | |
2648 } | 2655 } |
2649 else if([handle isMemberOfClass:[DWSplitBar class]]) | 2656 else if([handle isMemberOfClass:[DWSplitBar class]]) |
2650 { | 2657 { |
2651 DWSplitBar *split = (DWSplitBar *)handle; | 2658 DWSplitBar *split = (DWSplitBar *)handle; |
2652 float percent = [split percent]; | 2659 float percent = [split percent]; |
3203 int thiswidth = 1, thisheight = 1, extrawidth = 0, extraheight = 0; | 3210 int thiswidth = 1, thisheight = 1, extrawidth = 0, extraheight = 0; |
3204 NSString *nsstr = nil; | 3211 NSString *nsstr = nil; |
3205 id object = _dw_text_handle(handle); | 3212 id object = _dw_text_handle(handle); |
3206 | 3213 |
3207 /* Handle all the different button types */ | 3214 /* Handle all the different button types */ |
3208 if([ object isMemberOfClass:[ DWButton class ] ]) | 3215 if([handle isMemberOfClass:[DWButton class]]) |
3209 { | 3216 { |
3210 UIImage *image = (UIImage *)[object imageForState:UIControlStateNormal]; | 3217 UIImage *image = [handle imageForState:UIControlStateNormal]; |
3211 | 3218 |
3212 if(image) | 3219 if(image) |
3213 { | 3220 { |
3214 /* Image button */ | 3221 /* Image button */ |
3215 CGSize size = [image size]; | 3222 CGSize size = [image size]; |
3216 extrawidth = (int)size.width + 1; | 3223 extrawidth = (int)size.width + 1; |
3217 /* Height isn't additive */ | 3224 /* Height isn't additive */ |
3218 thisheight = (int)size.height + 1; | 3225 thisheight = (int)size.height + 1; |
3219 } | 3226 } |
3220 /* Text button */ | 3227 /* Text button */ |
3221 nsstr = [[object titleLabel] text]; | 3228 nsstr = [[handle titleLabel] text]; |
3222 | 3229 |
3223 if(nsstr && [nsstr length] > 0) | 3230 if(nsstr && [nsstr length] > 0) |
3224 { | 3231 { |
3225 /* With combined text/image we seem to | 3232 /* With combined text/image we seem to |
3226 * need a lot of additional width space. | 3233 * need a lot of additional width space. |
3231 extrawidth += 8; | 3238 extrawidth += 8; |
3232 extraheight += 4; | 3239 extraheight += 4; |
3233 } | 3240 } |
3234 } | 3241 } |
3235 /* If the control is an entryfield set width to 150 */ | 3242 /* If the control is an entryfield set width to 150 */ |
3236 else if([object isKindOfClass:[ UITextField class ]]) | 3243 else if([object isKindOfClass:[UITextField class]]) |
3237 { | 3244 { |
3238 UIFont *font = [object font]; | 3245 UIFont *font = [object font]; |
3239 | 3246 |
3240 if([object isEditable]) | 3247 if([object isEditable]) |
3241 { | 3248 { |
3242 /* Spinbuttons don't need to be as wide */ | 3249 /* Spinbuttons don't need to be as wide */ |
3243 if([object isMemberOfClass:[ DWSpinButton class]]) | 3250 if([handle isMemberOfClass:[DWSpinButton class]]) |
3244 thiswidth = 50; | 3251 thiswidth = 100; |
3245 else | 3252 else |
3246 thiswidth = 150; | 3253 thiswidth = 150; |
3247 } | 3254 } |
3248 else | 3255 nsstr = [object text]; |
3249 nsstr = [object text]; | |
3250 | 3256 |
3251 if(font) | 3257 if(font) |
3252 thisheight = (int)[font lineHeight]; | 3258 thisheight = (int)[font lineHeight]; |
3253 } | 3259 } |
3254 /* Handle the ranged widgets */ | 3260 /* Handle the ranged widgets */ |
3255 else if([ object isMemberOfClass:[DWPercent class] ] || | 3261 else if([object isMemberOfClass:[DWPercent class]] || |
3256 [ object isMemberOfClass:[DWSlider class] ]) | 3262 [object isMemberOfClass:[DWSlider class]]) |
3257 { | 3263 { |
3258 thiswidth = 100; | 3264 thiswidth = 100; |
3259 thisheight = 20; | 3265 thisheight = 25; |
3260 } | 3266 } |
3261 /* Handle bitmap size */ | 3267 /* Handle bitmap size */ |
3262 else if([ object isMemberOfClass:[UIImageView class] ]) | 3268 else if([object isMemberOfClass:[UIImageView class]]) |
3263 { | 3269 { |
3264 UIImage *image = (UIImage *)[object image]; | 3270 UIImage *image = (UIImage *)[object image]; |
3265 | 3271 |
3266 if(image) | 3272 if(image) |
3267 { | 3273 { |
3269 thiswidth = (int)size.width; | 3275 thiswidth = (int)size.width; |
3270 thisheight = (int)size.height; | 3276 thisheight = (int)size.height; |
3271 } | 3277 } |
3272 } | 3278 } |
3273 /* Handle calendar */ | 3279 /* Handle calendar */ |
3274 else if([ object isMemberOfClass:[DWCalendar class] ]) | 3280 else if([object isMemberOfClass:[DWCalendar class]]) |
3275 { | 3281 { |
3276 CGSize size = [object intrinsicContentSize]; | 3282 CGSize size = [object intrinsicContentSize]; |
3277 | 3283 |
3278 thiswidth = size.width; | 3284 thiswidth = size.width; |
3279 thisheight = size.height; | 3285 thisheight = size.height; |
3280 } | 3286 } |
3281 /* MLE and Container */ | 3287 /* MLE and Container */ |
3282 else if([ object isMemberOfClass:[DWMLE class] ] || | 3288 else if([object isMemberOfClass:[DWMLE class]] || |
3283 [ object isMemberOfClass:[DWContainer class] ]) | 3289 [object isMemberOfClass:[DWContainer class]]) |
3284 { | 3290 { |
3285 CGSize size; | 3291 CGSize size; |
3286 | 3292 |
3287 if([ object isMemberOfClass:[DWMLE class] ]) | 3293 if([object isMemberOfClass:[DWMLE class]]) |
3288 size = [object contentSize]; | 3294 size = [object contentSize]; |
3289 else | 3295 else |
3290 size = [object getsize]; | 3296 size = [object getsize]; |
3291 | 3297 |
3292 thiswidth = size.width; | 3298 thiswidth = size.width; |
3299 if(thisheight < _DW_SCROLLED_MIN_HEIGHT) | 3305 if(thisheight < _DW_SCROLLED_MIN_HEIGHT) |
3300 thisheight = _DW_SCROLLED_MIN_HEIGHT; | 3306 thisheight = _DW_SCROLLED_MIN_HEIGHT; |
3301 if(thisheight > _DW_SCROLLED_MAX_HEIGHT) | 3307 if(thisheight > _DW_SCROLLED_MAX_HEIGHT) |
3302 thisheight = _DW_SCROLLED_MAX_HEIGHT; | 3308 thisheight = _DW_SCROLLED_MAX_HEIGHT; |
3303 } | 3309 } |
3304 else if([ object isKindOfClass:[UILabel class] ]) | 3310 else if([object isKindOfClass:[UILabel class]]) |
3305 { | 3311 { |
3306 nsstr = [object text]; | 3312 nsstr = [object text]; |
3307 extrawidth = extraheight = 2; | 3313 extrawidth = extraheight = 2; |
3308 } | 3314 } |
3315 #ifdef DW_INCLUDE_DEPRECATED | |
3309 /* Any other control type */ | 3316 /* Any other control type */ |
3310 else if([ object isKindOfClass:[ UIControl class ] ]) | 3317 else if([object isKindOfClass:[UIControl class]]) |
3311 nsstr = [object text]; | 3318 nsstr = [object text]; |
3319 #endif | |
3312 | 3320 |
3313 /* If we have a string... | 3321 /* If we have a string... |
3314 * calculate the size with the current font. | 3322 * calculate the size with the current font. |
3315 */ | 3323 */ |
3316 if(nsstr && [nsstr length]) | 3324 if(nsstr && [nsstr length]) |
3321 | 3329 |
3322 if(textheight > thisheight) | 3330 if(textheight > thisheight) |
3323 thisheight = textheight; | 3331 thisheight = textheight; |
3324 if(textwidth > thiswidth) | 3332 if(textwidth > thiswidth) |
3325 thiswidth = textwidth; | 3333 thiswidth = textwidth; |
3326 } | |
3327 | |
3328 /* Handle static text fields */ | |
3329 if([object isKindOfClass:[ UITextField class ]] && ![object isEditable]) | |
3330 { | |
3331 extrawidth = 10; | |
3332 } | 3334 } |
3333 | 3335 |
3334 /* Set the requested sizes */ | 3336 /* Set the requested sizes */ |
3335 if(width) | 3337 if(width) |
3336 *width = thiswidth + extrawidth; | 3338 *width = thiswidth + extrawidth; |