comparison ios/dw.m @ 2432:5e0507e67c5d

iOS: Initial implementation of DWComboBox, not fully functional yet.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Mon, 05 Apr 2021 22:18:19 +0000
parents e08968d21d45
children 87669cfe3c92
comparison
equal deleted inserted replaced
2431:b0e6f8f0a1ff 2432:5e0507e67c5d
2281 } 2281 }
2282 completionHandler(); 2282 completionHandler();
2283 } 2283 }
2284 @end 2284 @end
2285 2285
2286 @interface DWComboBox : UITextField <UIPickerViewDelegate,UIPickerViewDataSource,UITextFieldDelegate>
2287 {
2288 UIPickerView* pickerView;
2289 NSMutableArray* dataArray;
2290 UIBarStyle toolbarStyle;
2291 int selectedIndex;
2292 }
2293 -(void)setToolbarStyle:(UIBarStyle)style;
2294 -(void)append:(NSString *)item;
2295 -(void)insert:(NSString *)item atIndex:(int)index;
2296 -(void)clear;
2297 -(int)count;
2298 -(NSString *)getTextAtIndex:(int)index;
2299 -(void)setText:(NSString *)item atIndex:(int)index;
2300 -(void)deleteAtIndex:(int)index;
2301 -(int)selectedIndex;
2302 @end
2303
2304 @implementation DWComboBox
2305 -(id)init
2306 {
2307 self = [super init];
2308 if(self)
2309 {
2310 [self setDelegate:self];
2311
2312 /* Set UI defaults */
2313 toolbarStyle = UIBarStyleDefault;
2314
2315 /* Hide the caret and its blinking */
2316 [[self valueForKey:@"textInputTraits"]
2317 setValue:[UIColor clearColor]
2318 forKey:@"insertionPointColor"];
2319
2320 /* Setup the arrow image */
2321 UIButton *imageButton = [UIButton buttonWithType:UIButtonTypeCustom];
2322 UIImage *image = [UIImage systemImageNamed:@"chevron.down"];
2323 [imageButton setImage:image forState:UIControlStateNormal];
2324 [self setRightView:imageButton];
2325 [self setRightViewMode:UITextFieldViewModeAlways];
2326 [imageButton addTarget:self action:@selector(showPicker:) forControlEvents:UIControlEventTouchUpInside];
2327 selectedIndex = -1;
2328 }
2329 return self;
2330 }
2331 -(void)setToolbarStyle:(UIBarStyle)style { toolbarStyle = style; }
2332 -(void)append:(NSString *)item { if(item) [dataArray addObject:item]; }
2333 -(void)insert:(NSString *)item atIndex:(int)index { if(item) [dataArray insertObject:item atIndex:index]; }
2334 -(void)clear { [dataArray removeAllObjects]; }
2335 -(int)count { return (int)[dataArray count]; }
2336 -(NSString *)getTextAtIndex:(int)index
2337 {
2338 if(index > -1 && index < [dataArray count])
2339 return [dataArray objectAtIndex:index];
2340 return nil;
2341 }
2342 -(void)setText:(NSString *)item atIndex:(int)index
2343 {
2344 if(item && index > -1 && index < [dataArray count])
2345 [dataArray replaceObjectAtIndex:index withObject:item];
2346 }
2347 -(void)deleteAtIndex:(int)index { if(index > -1 && index < [dataArray count]) [dataArray removeObjectAtIndex:index]; }
2348 -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView { return 1; }
2349 -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
2350 {
2351 selectedIndex = (int)row;
2352 [self setText:[dataArray objectAtIndex:row]];
2353 [self sendActionsForControlEvents:UIControlEventValueChanged];
2354 _dw_event_handler(self, DW_INT_TO_POINTER(selectedIndex), 11);
2355 }
2356 -(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component { return [dataArray count]; }
2357 -(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
2358 {
2359 return [dataArray objectAtIndex:row];
2360 }
2361 -(void)doneClicked:(id)sender
2362 {
2363 /* Hides the pickerView */
2364 [self resignFirstResponder];
2365
2366 if([[self text] length] == 0 || ![dataArray containsObject:[self text]])
2367 {
2368 selectedIndex = -1;
2369 }
2370 [self sendActionsForControlEvents:UIControlEventValueChanged];
2371 }
2372 -(void)cancelClicked:(id)sender
2373 {
2374 /* Hides the pickerView */
2375 [pickerView resignFirstResponder];
2376 }
2377 -(void)showPicker:(id)sender
2378 {
2379 pickerView = [[UIPickerView alloc] init];
2380 [pickerView setDataSource:self];
2381 [pickerView setDelegate:self];
2382
2383 /* If the text field is empty show the place holder otherwise show the last selected option */
2384 if([[self text] length] == 0 || ![dataArray containsObject:[self text]])
2385 {
2386 [pickerView selectRow:0 inComponent:0 animated:YES];
2387 }
2388 else
2389 {
2390 if([dataArray containsObject:[self text]])
2391 {
2392 [pickerView selectRow:[dataArray indexOfObject:[self text]] inComponent:0 animated:YES];
2393 }
2394 }
2395
2396 UIToolbar* toolbar = [[UIToolbar alloc] init];
2397 [toolbar setBarStyle:toolbarStyle];
2398 [toolbar sizeToFit];
2399
2400 /* Space between buttons */
2401 UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
2402 target:nil
2403 action:nil];
2404
2405 UIBarButtonItem *doneButton = [[UIBarButtonItem alloc]
2406 initWithTitle:@"Done"
2407 style:UIBarButtonItemStyleDone
2408 target:self
2409 action:@selector(doneClicked:)];
2410
2411 UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc]
2412 initWithTitle:@"Cancel"
2413 style:UIBarButtonItemStylePlain
2414 target:self
2415 action:@selector(cancelClicked:)];
2416
2417 [toolbar setItems:[NSArray arrayWithObjects:cancelButton, flexibleSpace, doneButton, nil]];
2418
2419 /* Custom input view */
2420 [self setInputView:pickerView];
2421 [self setInputAccessoryView:toolbar];
2422 }
2423 -(int)selectedIndex { return selectedIndex; }
2424 @end
2425
2286 /* Subclass for a MDI type 2426 /* Subclass for a MDI type
2287 * This is just a box for display purposes... but it is a 2427 * This is just a box for display purposes... but it is a
2288 * unique class so it can be identified when creating windows. 2428 * unique class so it can be identified when creating windows.
2289 */ 2429 */
2290 @interface DWMDI : DWBox {} 2430 @interface DWMDI : DWBox {}
4185 DW_FUNCTION_RESTORE_PARAM2(handle, HWND, text, char *) 4325 DW_FUNCTION_RESTORE_PARAM2(handle, HWND, text, char *)
4186 { 4326 {
4187 DW_FUNCTION_INIT; 4327 DW_FUNCTION_INIT;
4188 id object = handle; 4328 id object = handle;
4189 4329
4190 if([object isMemberOfClass:[DWContainer class]]) 4330 if([object isMemberOfClass:[DWComboBox class]])
4331 {
4332 DWComboBox *combo = handle;
4333
4334 [combo append:[NSString stringWithUTF8String:text]];
4335 }
4336 else if([object isMemberOfClass:[DWContainer class]])
4191 { 4337 {
4192 DWContainer *cont = handle; 4338 DWContainer *cont = handle;
4193 NSString *nstr = [NSString stringWithUTF8String:text]; 4339 NSString *nstr = [NSString stringWithUTF8String:text];
4194 NSArray *newrow = [NSArray arrayWithObject:_dw_table_cell_view_new(nil, nstr)]; 4340 NSArray *newrow = [NSArray arrayWithObject:_dw_table_cell_view_new(nil, nstr)];
4195 4341
4213 DW_FUNCTION_RESTORE_PARAM3(handle, HWND, text, const char *, pos, int) 4359 DW_FUNCTION_RESTORE_PARAM3(handle, HWND, text, const char *, pos, int)
4214 { 4360 {
4215 DW_FUNCTION_INIT; 4361 DW_FUNCTION_INIT;
4216 id object = handle; 4362 id object = handle;
4217 4363
4218 if([object isMemberOfClass:[DWContainer class]]) 4364 if([object isMemberOfClass:[DWComboBox class]])
4365 {
4366 DWComboBox *combo = handle;
4367
4368 [combo insert:[NSString stringWithUTF8String:text] atIndex:pos];
4369 }
4370 else if([object isMemberOfClass:[DWContainer class]])
4219 { 4371 {
4220 DWContainer *cont = handle; 4372 DWContainer *cont = handle;
4221 NSString *nstr = [NSString stringWithUTF8String:text]; 4373 NSString *nstr = [NSString stringWithUTF8String:text];
4222 NSArray *newrow = [NSArray arrayWithObject:_dw_table_cell_view_new(nil, nstr)]; 4374 NSArray *newrow = [NSArray arrayWithObject:_dw_table_cell_view_new(nil, nstr)];
4223 4375
4241 DW_FUNCTION_RESTORE_PARAM3(handle, HWND, text, char **, count, int) 4393 DW_FUNCTION_RESTORE_PARAM3(handle, HWND, text, char **, count, int)
4242 { 4394 {
4243 DW_FUNCTION_INIT; 4395 DW_FUNCTION_INIT;
4244 id object = handle; 4396 id object = handle;
4245 4397
4246 if([object isMemberOfClass:[DWContainer class]]) 4398 if([object isMemberOfClass:[DWComboBox class]])
4399 {
4400 DWComboBox *combo = handle;
4401 int z;
4402
4403 for(z=0;z<count;z++)
4404 {
4405 NSString *nstr = [NSString stringWithUTF8String:text[z]];
4406
4407 [combo append:nstr];
4408 }
4409 }
4410 else if([object isMemberOfClass:[DWContainer class]])
4247 { 4411 {
4248 DWContainer *cont = handle; 4412 DWContainer *cont = handle;
4249 int z; 4413 int z;
4250 4414
4251 for(z=0;z<count;z++) 4415 for(z=0;z<count;z++)
4272 DW_FUNCTION_RESTORE_PARAM1(handle, HWND) 4436 DW_FUNCTION_RESTORE_PARAM1(handle, HWND)
4273 { 4437 {
4274 DW_FUNCTION_INIT; 4438 DW_FUNCTION_INIT;
4275 id object = handle; 4439 id object = handle;
4276 4440
4441 if([object isMemberOfClass:[DWComboBox class]])
4442 {
4443 DWComboBox *combo = handle;
4444
4445 [combo clear];
4446 }
4277 if([object isMemberOfClass:[DWContainer class]]) 4447 if([object isMemberOfClass:[DWContainer class]])
4278 { 4448 {
4279 DWContainer *cont = handle; 4449 DWContainer *cont = handle;
4280 4450
4281 [cont clear]; 4451 [cont clear];
4297 { 4467 {
4298 DW_FUNCTION_INIT; 4468 DW_FUNCTION_INIT;
4299 id object = handle; 4469 id object = handle;
4300 int result = 0; 4470 int result = 0;
4301 4471
4302 if([object isMemberOfClass:[DWContainer class]]) 4472 if([object isMemberOfClass:[DWComboBox class]])
4473 {
4474 DWComboBox *combo = handle;
4475
4476 result = [combo count];
4477 }
4478 else if([object isMemberOfClass:[DWContainer class]])
4303 { 4479 {
4304 DWContainer *cont = handle; 4480 DWContainer *cont = handle;
4305 result = (int)[cont numberOfRowsInSection:0]; 4481 result = (int)[cont numberOfRowsInSection:0];
4306 } 4482 }
4307 DW_FUNCTION_RETURN_THIS(result); 4483 DW_FUNCTION_RETURN_THIS(result);
4345 DW_FUNCTION_RESTORE_PARAM4(handle, HWND, index, unsigned int, buffer, char *, length, unsigned int) 4521 DW_FUNCTION_RESTORE_PARAM4(handle, HWND, index, unsigned int, buffer, char *, length, unsigned int)
4346 { 4522 {
4347 DW_FUNCTION_INIT; 4523 DW_FUNCTION_INIT;
4348 id object = handle; 4524 id object = handle;
4349 4525
4350 if([object isMemberOfClass:[DWContainer class]]) 4526 if([object isMemberOfClass:[DWComboBox class]])
4527 {
4528 DWComboBox *combo = handle;
4529 NSString *nstr = [combo getTextAtIndex:index];
4530
4531 if(nstr)
4532 strncpy(buffer, [nstr UTF8String], length - 1);
4533 else
4534 *buffer = '\0';
4535 }
4536 else if([object isMemberOfClass:[DWContainer class]])
4351 { 4537 {
4352 DWContainer *cont = handle; 4538 DWContainer *cont = handle;
4353 int count = (int)[cont numberOfRowsInSection:0]; 4539 int count = (int)[cont numberOfRowsInSection:0];
4354 4540
4355 if(index > count) 4541 if(index > count)
4380 DW_FUNCTION_RESTORE_PARAM3(handle, HWND, index, unsigned int, buffer, char *) 4566 DW_FUNCTION_RESTORE_PARAM3(handle, HWND, index, unsigned int, buffer, char *)
4381 { 4567 {
4382 DW_FUNCTION_INIT; 4568 DW_FUNCTION_INIT;
4383 id object = handle; 4569 id object = handle;
4384 4570
4385 if([object isMemberOfClass:[DWContainer class]]) 4571 if([object isMemberOfClass:[DWComboBox class]])
4572 {
4573 DWComboBox *combo = handle;
4574
4575 [combo setText:[NSString stringWithUTF8String:buffer] atIndex:index];
4576 }
4577 else if([object isMemberOfClass:[DWContainer class]])
4386 { 4578 {
4387 DWContainer *cont = handle; 4579 DWContainer *cont = handle;
4388 int count = (int)[cont numberOfRowsInSection:0]; 4580 int count = (int)[cont numberOfRowsInSection:0];
4389 4581
4390 if(index <= count) 4582 if(index <= count)
4412 { 4604 {
4413 DW_FUNCTION_INIT; 4605 DW_FUNCTION_INIT;
4414 id object = handle; 4606 id object = handle;
4415 int result = -1; 4607 int result = -1;
4416 4608
4417 if([object isMemberOfClass:[DWContainer class]]) 4609 if([object isMemberOfClass:[DWComboBox class]])
4610 {
4611 DWComboBox *combo = handle;
4612
4613 result = [combo selectedIndex];
4614 }
4615 else if([object isMemberOfClass:[DWContainer class]])
4418 { 4616 {
4419 DWContainer *cont = handle; 4617 DWContainer *cont = handle;
4420 NSIndexPath *ip = [cont indexPathForSelectedRow]; 4618 NSIndexPath *ip = [cont indexPathForSelectedRow];
4421 4619
4422 if(ip) 4620 if(ip)
4491 DW_FUNCTION_RESTORE_PARAM2(handle, HWND, index, int) 4689 DW_FUNCTION_RESTORE_PARAM2(handle, HWND, index, int)
4492 { 4690 {
4493 DW_FUNCTION_INIT; 4691 DW_FUNCTION_INIT;
4494 id object = handle; 4692 id object = handle;
4495 4693
4496 if([object isMemberOfClass:[DWContainer class]]) 4694 if([object isMemberOfClass:[DWComboBox class]])
4695 {
4696 DWComboBox *combo = handle;
4697
4698 [combo deleteAtIndex:index];
4699 }
4700 else if([object isMemberOfClass:[DWContainer class]])
4497 { 4701 {
4498 DWContainer *cont = handle; 4702 DWContainer *cont = handle;
4499 4703
4500 [cont removeRow:index]; 4704 [cont removeRow:index];
4501 [cont reloadData]; 4705 [cont reloadData];
4508 * Create a new Combobox window (widget) to be packed. 4712 * Create a new Combobox window (widget) to be packed.
4509 * Parameters: 4713 * Parameters:
4510 * text: The default text to be in the combpbox widget. 4714 * text: The default text to be in the combpbox widget.
4511 * id: An ID to be used with dw_window_from_id() or 0L. 4715 * id: An ID to be used with dw_window_from_id() or 0L.
4512 */ 4716 */
4513 HWND API dw_combobox_new(const char *text, ULONG cid) 4717 DW_FUNCTION_DEFINITION(dw_combobox_new, HWND, const char *text, ULONG cid)
4514 { 4718 DW_FUNCTION_ADD_PARAM2(text, cid)
4515 /* TODO: Implment comboboxes. https://www.codeproject.com/Articles/301681/iPhone-ComboBox */ 4719 DW_FUNCTION_RETURN(dw_combobox_new, HWND)
4516 return 0; 4720 DW_FUNCTION_RESTORE_PARAM2(text, const char *, cid, ULONG)
4721 {
4722 DWComboBox *combo = [[DWComboBox alloc] init];
4723 if(text)
4724 [combo setText:[NSString stringWithUTF8String:text]];
4725 [combo setTag:cid];
4726 DW_FUNCTION_RETURN_THIS(combo);
4517 } 4727 }
4518 4728
4519 /* 4729 /*
4520 * Create a new Multiline Editbox window (widget) to be packed. 4730 * Create a new Multiline Editbox window (widget) to be packed.
4521 * Parameters: 4731 * Parameters: