comparison ios/dw.m @ 2825:69e144d81f19

iOS: Rewrite DWContainer to use a single DWTableViewCell per row. Previously a cell was allocated for each row's column. Instead save an array of NSString and UIImages which can be displayed in the custom UITableViewCell based on the DW_FEATURE_CONTAINER_MODE setting.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Wed, 17 Aug 2022 11:36:34 +0000
parents 0adf73d5d8c2
children 455d539ac555
comparison
equal deleted inserted replaced
2824:22327163b35c 2825:69e144d81f19
2331 @end 2331 @end
2332 2332
2333 /* Subclass UITableViewCell so we can support multiple columns... 2333 /* Subclass UITableViewCell so we can support multiple columns...
2334 * Enabled via the DW_FEATURE_CONTAINER_MODE feature setting. 2334 * Enabled via the DW_FEATURE_CONTAINER_MODE feature setting.
2335 */ 2335 */
2336 @interface DWTableViewCell : UITableViewCell {} 2336 @interface DWTableViewCell : UITableViewCell
2337 {
2338 NSMutableArray *columndata;
2339 }
2340 -(void)setColumnData:(NSMutableArray *)input;
2341 -(NSMutableArray *)columnData;
2337 @end 2342 @end
2338 2343
2339 @implementation DWTableViewCell 2344 @implementation DWTableViewCell
2340 -(void)dealloc { [super dealloc]; } 2345 -(void)setColumnData:(NSMutableArray *)input { [columndata release]; columndata = input; [columndata retain]; }
2346 -(NSMutableArray *)columnData { return columndata; }
2347 -(void)dealloc { [columndata release]; [super dealloc]; }
2341 @end 2348 @end
2342 2349
2343 /* Create a tableview cell for containers using DW_CONTAINER_MODE_* or listboxes */ 2350 /* Create a tableview cell for containers using DW_CONTAINER_MODE_* or listboxes */
2344 DWTableViewCell *_dw_table_cell_view_new(UIImage *icon, NSString *text) 2351 DWTableViewCell *_dw_table_cell_view_new(UIImage *icon, NSString *text, NSMutableArray *columndata)
2345 { 2352 {
2346 DWTableViewCell *browsercell = [[DWTableViewCell alloc] init]; 2353 DWTableViewCell *browsercell = [[DWTableViewCell alloc] init];
2347 [browsercell setAutoresizesSubviews:YES]; 2354 [browsercell setAutoresizesSubviews:YES];
2348 2355
2349 if(icon) 2356 if(icon)
2350 [[browsercell imageView] setImage:icon]; 2357 [[browsercell imageView] setImage:icon];
2351 if(text) 2358 if(text)
2352 [[browsercell textLabel] setText:text]; 2359 [[browsercell textLabel] setText:text];
2360 if(columndata)
2361 [browsercell setColumnData:columndata];
2353 return browsercell; 2362 return browsercell;
2354 } 2363 }
2355 2364
2356 /* Subclass for a Container/List type */ 2365 /* Subclass for a Container/List type */
2357 @interface DWContainer : UITableView <UITableViewDataSource,UITableViewDelegate> 2366 @interface DWContainer : UITableView <UITableViewDataSource,UITableViewDelegate>
2378 -(NSString *)getColumn:(int)col; 2387 -(NSString *)getColumn:(int)col;
2379 -(void *)userdata; 2388 -(void *)userdata;
2380 -(void)setUserdata:(void *)input; 2389 -(void)setUserdata:(void *)input;
2381 -(void)setFilesystem:(int)input; 2390 -(void)setFilesystem:(int)input;
2382 -(int)filesystem; 2391 -(int)filesystem;
2383 -(int)addRow:(NSArray *)input; 2392 -(int)addRow:(DWTableViewCell *)input;
2384 -(int)addRows:(int)number; 2393 -(int)addRows:(int)number;
2385 -(void)editCell:(id)input at:(int)row and:(int)col; 2394 -(void)editCell:(id)input at:(int)row;
2386 -(void)removeRow:(int)row; 2395 -(void)removeRow:(int)row;
2387 -(void)setRow:(int)row title:(const char *)input; 2396 -(void)setRow:(int)row title:(const char *)input;
2388 -(void *)getRowTitle:(int)row; 2397 -(void *)getRowTitle:(int)row;
2389 -(id)getRow:(int)row and:(int)col; 2398 -(id)getRow:(int)row;
2399 -(NSMutableArray *)getColumns;
2390 -(int)cellType:(int)col; 2400 -(int)cellType:(int)col;
2391 -(int)lastAddPoint; 2401 -(int)lastAddPoint;
2392 -(int)lastQueryPoint; 2402 -(int)lastQueryPoint;
2393 -(void)setLastQueryPoint:(int)input; 2403 -(void)setLastQueryPoint:(int)input;
2394 -(void)clear; 2404 -(void)clear;
2400 @implementation DWContainer 2410 @implementation DWContainer
2401 -(NSInteger)tableView:(UITableView *)aTable numberOfRowsInSection:(NSInteger)section 2411 -(NSInteger)tableView:(UITableView *)aTable numberOfRowsInSection:(NSInteger)section
2402 { 2412 {
2403 /* Ignoring section for now, everything in one section */ 2413 /* Ignoring section for now, everything in one section */
2404 if(tvcols && data) 2414 if(tvcols && data)
2405 { 2415 return [data count];
2406 int cols = (int)[tvcols count];
2407 int total = (int)[data count];
2408 if(cols && total)
2409 return total / cols;
2410 }
2411 return 0; 2416 return 0;
2412 } 2417 }
2413 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 2418 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
2414 { 2419 {
2415 /* Not reusing cell views, so get the cell from our array */ 2420 /* Not reusing cell views, so get the cell from our array */
2416 int index = (int)(indexPath.row * [tvcols count]); 2421 int index = (int)indexPath.row;
2417 id celldata = [data objectAtIndex:index]; 2422 id celldata = [data objectAtIndex:index];
2418 2423
2419 /* The data is already a NSTableCellView so just return that */ 2424 /* The data is already a NSTableCellView so just return that */
2420 if([celldata isMemberOfClass:[DWTableViewCell class]]) 2425 if([celldata isMemberOfClass:[DWTableViewCell class]])
2421 { 2426 {
2518 /* Only refresh if we've been setup already */ 2523 /* Only refresh if we've been setup already */
2519 if(titles) 2524 if(titles)
2520 [self refreshColors]; 2525 [self refreshColors];
2521 } 2526 }
2522 -(void)viewDidChangeEffectiveAppearance { [self checkDark]; } 2527 -(void)viewDidChangeEffectiveAppearance { [self checkDark]; }
2523 -(int)insertRow:(NSArray *)input at:(int)index 2528 -(int)insertRow:(DWTableViewCell *)input at:(int)index
2524 { 2529 {
2525 if(data) 2530 if(data)
2526 { 2531 {
2527 unsigned long start = [tvcols count] * index;
2528 NSIndexSet *set = [[NSIndexSet alloc] initWithIndexesInRange:NSMakeRange(start, start + [tvcols count])];
2529 if(index < iLastAddPoint) 2532 if(index < iLastAddPoint)
2530 { 2533 {
2531 iLastAddPoint++; 2534 iLastAddPoint++;
2532 } 2535 }
2533 [data insertObjects:input atIndexes:set]; 2536 [data insertObject:input atIndex:index];
2534 [titles insertPointer:NULL atIndex:index]; 2537 [titles insertPointer:NULL atIndex:index];
2535 [rowdatas insertPointer:NULL atIndex:index]; 2538 [rowdatas insertPointer:NULL atIndex:index];
2536 [set release];
2537 return (int)[titles count]; 2539 return (int)[titles count];
2538 } 2540 }
2539 return 0; 2541 return 0;
2540 } 2542 }
2541 -(int)addRow:(NSArray *)input 2543 -(int)addRow:(DWTableViewCell *)input
2542 { 2544 {
2543 if(data) 2545 if(data)
2544 { 2546 {
2545 iLastAddPoint = (int)[titles count]; 2547 iLastAddPoint = (int)[titles count];
2546 [data addObjectsFromArray:input]; 2548 [data addObject:input];
2547 [titles addPointer:NULL]; 2549 [titles addPointer:NULL];
2548 [rowdatas addPointer:NULL]; 2550 [rowdatas addPointer:NULL];
2549 return (int)[titles count]; 2551 return (int)[titles count];
2550 } 2552 }
2551 return 0; 2553 return 0;
2552 } 2554 }
2553 -(int)addRows:(int)number 2555 -(int)addRows:(int)number
2554 { 2556 {
2555 if(tvcols) 2557 if(tvcols)
2556 { 2558 {
2557 int count = (int)(number * [tvcols count]);
2558 int z; 2559 int z;
2559 2560
2560 iLastAddPoint = (int)[titles count]; 2561 iLastAddPoint = (int)[titles count];
2561 2562
2562 for(z=0;z<count;z++) 2563 for(z=0;z<number;z++)
2563 { 2564 {
2564 [data addObject:[NSNull null]]; 2565 [data addObject:[NSNull null]];
2565 }
2566 for(z=0;z<number;z++)
2567 {
2568 [titles addPointer:NULL]; 2566 [titles addPointer:NULL];
2569 [rowdatas addPointer:NULL]; 2567 [rowdatas addPointer:NULL];
2570 } 2568 }
2571 return (int)[titles count]; 2569 return (int)[titles count];
2572 } 2570 }
2573 return 0; 2571 return 0;
2574 } 2572 }
2575 -(void)editCell:(id)input at:(int)row and:(int)col 2573 -(void)editCell:(id)input at:(int)row
2576 { 2574 {
2577 if(tvcols) 2575 if(tvcols)
2578 { 2576 {
2579 int index = (int)(row * [tvcols count]) + col; 2577 if(row < [data count])
2580 if(index < [data count])
2581 { 2578 {
2582 if(!input) 2579 if(!input)
2583 input = [NSNull null]; 2580 input = [NSNull null];
2584 [data replaceObjectAtIndex:index withObject:input]; 2581 [data replaceObjectAtIndex:row withObject:input];
2585 } 2582 }
2586 } 2583 }
2587 } 2584 }
2588 -(void)removeRow:(int)row 2585 -(void)removeRow:(int)row
2589 { 2586 {
2590 if(tvcols) 2587 if(tvcols)
2591 { 2588 {
2592 int z, start, end;
2593 int count = (int)[tvcols count];
2594 void *oldtitle; 2589 void *oldtitle;
2595 2590
2596 start = (count * row); 2591 [data removeObjectAtIndex:row];
2597 end = start + count;
2598
2599 for(z=start;z<end;z++)
2600 {
2601 [data removeObjectAtIndex:start];
2602 }
2603 oldtitle = [titles pointerAtIndex:row]; 2592 oldtitle = [titles pointerAtIndex:row];
2604 [titles removePointerAtIndex:row]; 2593 [titles removePointerAtIndex:row];
2605 [rowdatas removePointerAtIndex:row]; 2594 [rowdatas removePointerAtIndex:row];
2606 if(iLastAddPoint > 0 && iLastAddPoint > row) 2595 if(iLastAddPoint > 0 && iLastAddPoint > row)
2607 { 2596 {
2623 } 2612 }
2624 } 2613 }
2625 -(void)setRowData:(int)row title:(void *)input { if(rowdatas && input) { [rowdatas replacePointerAtIndex:row withPointer:input]; } } 2614 -(void)setRowData:(int)row title:(void *)input { if(rowdatas && input) { [rowdatas replacePointerAtIndex:row withPointer:input]; } }
2626 -(void *)getRowTitle:(int)row { if(titles && row > -1) { return [titles pointerAtIndex:row]; } return NULL; } 2615 -(void *)getRowTitle:(int)row { if(titles && row > -1) { return [titles pointerAtIndex:row]; } return NULL; }
2627 -(void *)getRowData:(int)row { if(rowdatas && row > -1) { return [rowdatas pointerAtIndex:row]; } return NULL; } 2616 -(void *)getRowData:(int)row { if(rowdatas && row > -1) { return [rowdatas pointerAtIndex:row]; } return NULL; }
2628 -(id)getRow:(int)row and:(int)col { if(data && [data count]) { int index = (int)(row * [tvcols count]) + col; return [data objectAtIndex:index]; } return nil; } 2617 -(id)getRow:(int)row { if(data && [data count]) { return [data objectAtIndex:row]; } return nil; }
2618 -(NSMutableArray *)getColumns { return tvcols; }
2629 -(int)cellType:(int)col { return [[types objectAtIndex:col] intValue]; } 2619 -(int)cellType:(int)col { return [[types objectAtIndex:col] intValue]; }
2630 -(int)lastAddPoint { return iLastAddPoint; } 2620 -(int)lastAddPoint { return iLastAddPoint; }
2631 -(int)lastQueryPoint { return iLastQueryPoint; } 2621 -(int)lastQueryPoint { return iLastQueryPoint; }
2632 -(void)setLastQueryPoint:(int)input { iLastQueryPoint = input; } 2622 -(void)setLastQueryPoint:(int)input { iLastQueryPoint = input; }
2633 -(void)clear 2623 -(void)clear
5542 } 5532 }
5543 else if([object isMemberOfClass:[DWContainer class]]) 5533 else if([object isMemberOfClass:[DWContainer class]])
5544 { 5534 {
5545 DWContainer *cont = handle; 5535 DWContainer *cont = handle;
5546 NSString *nstr = [NSString stringWithUTF8String:text]; 5536 NSString *nstr = [NSString stringWithUTF8String:text];
5547 NSArray *newrow = [NSArray arrayWithObject:_dw_table_cell_view_new(nil, nstr)]; 5537
5548 5538 [cont addRow:_dw_table_cell_view_new(nil, nstr, nil)];
5549 [cont addRow:newrow];
5550 [cont reloadData]; 5539 [cont reloadData];
5551 [cont setNeedsDisplay]; 5540 [cont setNeedsDisplay];
5552 } 5541 }
5553 DW_FUNCTION_RETURN_NOTHING; 5542 DW_FUNCTION_RETURN_NOTHING;
5554 } 5543 }
5576 } 5565 }
5577 else if([object isMemberOfClass:[DWContainer class]]) 5566 else if([object isMemberOfClass:[DWContainer class]])
5578 { 5567 {
5579 DWContainer *cont = handle; 5568 DWContainer *cont = handle;
5580 NSString *nstr = [NSString stringWithUTF8String:text]; 5569 NSString *nstr = [NSString stringWithUTF8String:text];
5581 NSArray *newrow = [NSArray arrayWithObject:_dw_table_cell_view_new(nil, nstr)]; 5570
5582 5571 [cont insertRow:_dw_table_cell_view_new(nil, nstr, nil) at:pos];
5583 [cont insertRow:newrow at:pos];
5584 [cont reloadData]; 5572 [cont reloadData];
5585 [cont setNeedsDisplay]; 5573 [cont setNeedsDisplay];
5586 } 5574 }
5587 DW_FUNCTION_RETURN_NOTHING; 5575 DW_FUNCTION_RETURN_NOTHING;
5588 } 5576 }
5620 int z; 5608 int z;
5621 5609
5622 for(z=0;z<count;z++) 5610 for(z=0;z<count;z++)
5623 { 5611 {
5624 NSString *nstr = [NSString stringWithUTF8String:text[z]]; 5612 NSString *nstr = [NSString stringWithUTF8String:text[z]];
5625 NSArray *newrow = [NSArray arrayWithObjects:_dw_table_cell_view_new(nil, nstr),nil]; 5613
5626 5614 [cont addRow:_dw_table_cell_view_new(nil, nstr, nil)];
5627 [cont addRow:newrow];
5628 } 5615 }
5629 [cont reloadData]; 5616 [cont reloadData];
5630 [cont setNeedsDisplay]; 5617 [cont setNeedsDisplay];
5631 } 5618 }
5632 DW_FUNCTION_RETURN_NOTHING; 5619 DW_FUNCTION_RETURN_NOTHING;
5749 { 5736 {
5750 *buffer = '\0'; 5737 *buffer = '\0';
5751 } 5738 }
5752 else 5739 else
5753 { 5740 {
5754 DWTableViewCell *cell = [cont getRow:index and:0]; 5741 DWTableViewCell *cell = [cont getRow:index];
5755 NSString *nstr = [[cell textLabel] text]; 5742 NSString *nstr = [[cell textLabel] text];
5756 5743
5757 strncpy(buffer, [nstr UTF8String], length - 1); 5744 strncpy(buffer, [nstr UTF8String], length - 1);
5758 } 5745 }
5759 } 5746 }
5787 int count = (int)[cont numberOfRowsInSection:0]; 5774 int count = (int)[cont numberOfRowsInSection:0];
5788 5775
5789 if(index <= count) 5776 if(index <= count)
5790 { 5777 {
5791 NSString *nstr = [NSString stringWithUTF8String:buffer]; 5778 NSString *nstr = [NSString stringWithUTF8String:buffer];
5792 DWTableViewCell *cell = [cont getRow:index and:0]; 5779 DWTableViewCell *cell = [cont getRow:index];
5793 5780
5794 [[cell textLabel] setText:nstr]; 5781 [[cell textLabel] setText:nstr];
5795 [cont reloadData]; 5782 [cont reloadData];
5796 [cont setNeedsDisplay]; 5783 [cont setNeedsDisplay];
5797 } 5784 }
7180 icon = *((UIImage **)data); 7167 icon = *((UIImage **)data);
7181 } 7168 }
7182 else if(type & DW_CFA_STRING) 7169 else if(type & DW_CFA_STRING)
7183 { 7170 {
7184 char *str = *((char **)data); 7171 char *str = *((char **)data);
7185 text = [ NSString stringWithUTF8String:str ]; 7172 text = [NSString stringWithUTF8String:str];
7186 } 7173 }
7187 else 7174 else
7188 { 7175 {
7189 char textbuffer[101] = {0}; 7176 char textbuffer[101] = {0};
7190 7177
7197 else if(type & DW_CFA_DATE) 7184 else if(type & DW_CFA_DATE)
7198 { 7185 {
7199 struct tm curtm; 7186 struct tm curtm;
7200 CDATE cdate = *((CDATE *)data); 7187 CDATE cdate = *((CDATE *)data);
7201 7188
7202 memset( &curtm, 0, sizeof(curtm) ); 7189 memset(&curtm, 0, sizeof(curtm));
7203 curtm.tm_mday = cdate.day; 7190 curtm.tm_mday = cdate.day;
7204 curtm.tm_mon = cdate.month - 1; 7191 curtm.tm_mon = cdate.month - 1;
7205 curtm.tm_year = cdate.year - 1900; 7192 curtm.tm_year = cdate.year - 1900;
7206 7193
7207 strftime(textbuffer, 100, "%x", &curtm); 7194 strftime(textbuffer, 100, "%x", &curtm);
7217 curtm.tm_sec = ctime.seconds; 7204 curtm.tm_sec = ctime.seconds;
7218 7205
7219 strftime(textbuffer, 100, "%X", &curtm); 7206 strftime(textbuffer, 100, "%X", &curtm);
7220 } 7207 }
7221 if(textbuffer[0]) 7208 if(textbuffer[0])
7222 text = [ NSString stringWithUTF8String:textbuffer ]; 7209 text = [NSString stringWithUTF8String:textbuffer];
7223 } 7210 }
7224 } 7211 }
7225 7212
7226 id object = [cont getRow:(row+lastadd) and:column]; 7213 id object = [cont getRow:(row+lastadd)];
7227 7214 NSUInteger capacity = [[cont getColumns] count];
7215
7228 /* If it is a cell, change the content of the cell */ 7216 /* If it is a cell, change the content of the cell */
7229 if([object isMemberOfClass:[DWTableViewCell class]]) 7217 if([object isMemberOfClass:[DWTableViewCell class]])
7230 { 7218 {
7231 DWTableViewCell *cell = object; 7219 DWTableViewCell *cell = object;
7232 7220
7233 if(icon) 7221 if(column == 0)
7234 [[cell imageView] setImage:icon]; 7222 {
7235 else 7223 if(icon)
7236 [[cell textLabel] setText:text]; 7224 [[cell imageView] setImage:icon];
7237 } 7225 else
7238 else /* Otherwise replace it with a new cell */ 7226 [[cell textLabel] setText:text];
7239 [cont editCell:_dw_table_cell_view_new(icon, text) at:(row+lastadd) and:column]; 7227 }
7228 else if(icon || text)
7229 {
7230 NSMutableArray *cd = [cell columnData];
7231
7232 if(column >= capacity)
7233 capacity = column + 1;
7234
7235 if(!cd)
7236 {
7237 cd = [NSMutableArray arrayWithCapacity:capacity];
7238 [cell setColumnData:cd];
7239 }
7240 if(cd)
7241 {
7242 while([cd count] <= column)
7243 [cd addObject:[NSNull null]];
7244 [cd replaceObjectAtIndex:column withObject:(text ? text : icon)];
7245 }
7246 }
7247 }
7248 else
7249 {
7250 NSMutableArray *cd = [NSMutableArray arrayWithCapacity:(capacity > column ? capacity : column + 1)];
7251 if(cd)
7252 [cd replaceObjectAtIndex:column withObject:(text ? text : icon)];
7253 /* Otherwise replace it with a new cell */
7254 [cont editCell:_dw_table_cell_view_new(icon, text, cd) at:(row+lastadd)];
7255 }
7240 [cont setNeedsDisplay]; 7256 [cont setNeedsDisplay];
7241 DW_FUNCTION_RETURN_NOTHING; 7257 DW_FUNCTION_RETURN_NOTHING;
7242 } 7258 }
7243 7259
7244 /* 7260 /*
7302 7318
7303 /* If pointer is NULL we are getting a change request instead of set */ 7319 /* If pointer is NULL we are getting a change request instead of set */
7304 if(pointer) 7320 if(pointer)
7305 lastadd = [cont lastAddPoint]; 7321 lastadd = [cont lastAddPoint];
7306 7322
7307 id object = [cont getRow:(row+lastadd) and:0]; 7323 id object = [cont getRow:(row+lastadd)];
7308 7324
7309 /* If it is a cell, change the content of the cell */ 7325 /* If it is a cell, change the content of the cell */
7310 if([object isMemberOfClass:[DWTableViewCell class]]) 7326 if([object isMemberOfClass:[DWTableViewCell class]])
7311 { 7327 {
7312 DWTableViewCell *cell = object; 7328 DWTableViewCell *cell = object;
7315 [[cell imageView] setImage:icon]; 7331 [[cell imageView] setImage:icon];
7316 if(text) 7332 if(text)
7317 [[cell textLabel] setText:text]; 7333 [[cell textLabel] setText:text];
7318 } 7334 }
7319 else /* Otherwise replace it with a new cell */ 7335 else /* Otherwise replace it with a new cell */
7320 [cont editCell:_dw_table_cell_view_new(icon, text) at:(row+lastadd) and:0]; 7336 [cont editCell:_dw_table_cell_view_new(icon, text, nil) at:(row+lastadd)];
7321 [cont setNeedsDisplay]; 7337 [cont setNeedsDisplay];
7322 DW_FUNCTION_RETURN_NOTHING; 7338 DW_FUNCTION_RETURN_NOTHING;
7323 } 7339 }
7324 7340
7325 /* 7341 /*