diff --git a/darwin/table.m b/darwin/table.m index 4e046a86..0fa8ac86 100644 --- a/darwin/table.m +++ b/darwin/table.m @@ -26,6 +26,10 @@ enum { @property uiTableColumn *libui_col; @end +@interface tableView : NSTableView +@property uiTable *libui_t; +@end + struct uiTableModel { uiTableModelHandler *mh; tableModel *m; @@ -40,8 +44,9 @@ struct uiTableColumn { struct uiTable { uiDarwinControl c; NSScrollView *sv; - NSTableView *tv; + tableView *tv; struct scrollViewData *d; + int backgroundColumn; }; @implementation tableModel @@ -124,6 +129,22 @@ done: return v; } +- (void)tableView:(NSTableView *)nstv didAddRowView:(NSTableRowView *)rv forRow:(NSInteger)row +{ + uiTableModel *m = self->libui_m; + tableView *tv = (tableView *) nstv; + uiTable *t = tv.libui_t; + NSColor *color; + + if (t->backgroundColumn == -1) + return; + color = (NSColor *) ((*(m->mh->CellValue))(m->mh, m, row, t->backgroundColumn)); + if (color == nil) + return; + [rv setBackgroundColor:color]; + // TODO autorelease color? or release it? +} + @end @implementation tablePart @@ -161,6 +182,9 @@ done: @implementation tableColumn @end +@implementation tableView +@end + void *uiTableModelStrdup(const char *str) { // TODO don't we have this already? @@ -182,6 +206,11 @@ uiTableModel *uiNewTableModel(uiTableModelHandler *mh) return m; } +void *uiTableModelGiveColor(double r, double g, double b, double a) +{ + return [[NSColor colorWithSRGBRed:r green:g blue:b alpha:a] retain]; +} + void uiFreeTableModel(uiTableModel *m) { if ([m->tables count] != 0) @@ -266,6 +295,11 @@ uiTableColumn *uiTableAppendColumn(uiTable *t, const char *name) return c; } +void uiTableSetRowBackgroundColorModelColumn(uiTable *t, int modelColumn) +{ + t->backgroundColumn = modelColumn; +} + uiTable *uiNewTable(uiTableModel *model) { uiTable *t; @@ -273,7 +307,8 @@ uiTable *uiNewTable(uiTableModel *model) uiDarwinNewControl(uiTable, t); - t->tv = [[NSTableView alloc] initWithFrame:NSZeroRect]; + t->tv = [[tableView alloc] initWithFrame:NSZeroRect]; + t->tv.libui_t = t; [t->tv setDataSource:model->m]; [t->tv setDelegate:model->m]; @@ -303,5 +338,7 @@ uiTable *uiNewTable(uiTableModel *model) p.VScroll = YES; t->sv = mkScrollView(&p, &(t->d)); + t->backgroundColumn = -1; + return t; } diff --git a/test/page16.c b/test/page16.c index f210f89b..66df42b1 100644 --- a/test/page16.c +++ b/test/page16.c @@ -5,11 +5,13 @@ static uiTableModelHandler mh; static int modelNumColumns(uiTableModelHandler *mh, uiTableModel *m) { - return 3; + return 4; } static uiTableModelColumnType modelColumnType(uiTableModelHandler *mh, uiTableModel *m, int column) { + if (column == 3) + return uiTableModelColumnColor; return uiTableModelColumnString; } @@ -22,6 +24,13 @@ static void *modelCellValue(uiTableModelHandler *mh, uiTableModel *m, int row, i { char buf[256]; + if (col == 3) { + if (row == 3) + return uiTableModelGiveColor(1, 0, 0, 1); + if (row == 11) + return uiTableModelGiveColor(0, 0.5, 1, 0.5); + return NULL; + } switch (col) { case 0: sprintf(buf, "Row %d", row); @@ -64,5 +73,7 @@ uiBox *makePage16(void) uiTableColumnAppendTextPart(tc, 1, 0); uiTableColumnAppendTextPart(tc, 2, 1); + uiTableSetRowBackgroundColorModelColumn(t, 3); + return page16; } diff --git a/uitable.h b/uitable.h index fc0e5ad5..05c5acd2 100644 --- a/uitable.h +++ b/uitable.h @@ -6,6 +6,7 @@ typedef struct uiTableModelHandler uiTableModelHandler; _UI_ENUM(uiTableModelColumnType) { uiTableModelColumnString, + uiTableModelColumnColor, }; struct uiTableModelHandler { @@ -17,6 +18,8 @@ struct uiTableModelHandler { }; _UI_EXTERN void *uiTableModelStrdup(const char *str); +// TODO rename the strdup one to this too +_UI_EXTERN void *uiTableModelGiveColor(double r, double g, double b, double a); _UI_EXTERN uiTableModel *uiNewTableModel(uiTableModelHandler *mh); _UI_EXTERN void uiFreeTableModel(uiTableModel *m); @@ -33,4 +36,6 @@ typedef struct uiTable uiTable; #define uiTable(this) ((uiTable *) (this)) _UI_EXTERN uiTableColumn *uiTableAppendColumn(uiTable *t, const char *name); _UI_EXTERN uiTableColumn *uiTableAppendTextColumn(uiTable *t, const char *name, int modelColumn); +// TODO getter? +_UI_EXTERN void uiTableSetRowBackgroundColorModelColumn(uiTable *t, int modelColumn); _UI_EXTERN uiTable *uiNewTable(uiTableModel *model);