Added row background colors.

This commit is contained in:
Pietro Gagliardi 2016-06-23 11:29:43 -04:00
parent f02fbd2ecf
commit 67e8db9efd
3 changed files with 56 additions and 3 deletions

View File

@ -26,6 +26,10 @@ enum {
@property uiTableColumn *libui_col; @property uiTableColumn *libui_col;
@end @end
@interface tableView : NSTableView
@property uiTable *libui_t;
@end
struct uiTableModel { struct uiTableModel {
uiTableModelHandler *mh; uiTableModelHandler *mh;
tableModel *m; tableModel *m;
@ -40,8 +44,9 @@ struct uiTableColumn {
struct uiTable { struct uiTable {
uiDarwinControl c; uiDarwinControl c;
NSScrollView *sv; NSScrollView *sv;
NSTableView *tv; tableView *tv;
struct scrollViewData *d; struct scrollViewData *d;
int backgroundColumn;
}; };
@implementation tableModel @implementation tableModel
@ -124,6 +129,22 @@ done:
return v; 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 @end
@implementation tablePart @implementation tablePart
@ -161,6 +182,9 @@ done:
@implementation tableColumn @implementation tableColumn
@end @end
@implementation tableView
@end
void *uiTableModelStrdup(const char *str) void *uiTableModelStrdup(const char *str)
{ {
// TODO don't we have this already? // TODO don't we have this already?
@ -182,6 +206,11 @@ uiTableModel *uiNewTableModel(uiTableModelHandler *mh)
return m; 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) void uiFreeTableModel(uiTableModel *m)
{ {
if ([m->tables count] != 0) if ([m->tables count] != 0)
@ -266,6 +295,11 @@ uiTableColumn *uiTableAppendColumn(uiTable *t, const char *name)
return c; return c;
} }
void uiTableSetRowBackgroundColorModelColumn(uiTable *t, int modelColumn)
{
t->backgroundColumn = modelColumn;
}
uiTable *uiNewTable(uiTableModel *model) uiTable *uiNewTable(uiTableModel *model)
{ {
uiTable *t; uiTable *t;
@ -273,7 +307,8 @@ uiTable *uiNewTable(uiTableModel *model)
uiDarwinNewControl(uiTable, t); 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 setDataSource:model->m];
[t->tv setDelegate:model->m]; [t->tv setDelegate:model->m];
@ -303,5 +338,7 @@ uiTable *uiNewTable(uiTableModel *model)
p.VScroll = YES; p.VScroll = YES;
t->sv = mkScrollView(&p, &(t->d)); t->sv = mkScrollView(&p, &(t->d));
t->backgroundColumn = -1;
return t; return t;
} }

View File

@ -5,11 +5,13 @@ static uiTableModelHandler mh;
static int modelNumColumns(uiTableModelHandler *mh, uiTableModel *m) static int modelNumColumns(uiTableModelHandler *mh, uiTableModel *m)
{ {
return 3; return 4;
} }
static uiTableModelColumnType modelColumnType(uiTableModelHandler *mh, uiTableModel *m, int column) static uiTableModelColumnType modelColumnType(uiTableModelHandler *mh, uiTableModel *m, int column)
{ {
if (column == 3)
return uiTableModelColumnColor;
return uiTableModelColumnString; return uiTableModelColumnString;
} }
@ -22,6 +24,13 @@ static void *modelCellValue(uiTableModelHandler *mh, uiTableModel *m, int row, i
{ {
char buf[256]; 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) { switch (col) {
case 0: case 0:
sprintf(buf, "Row %d", row); sprintf(buf, "Row %d", row);
@ -64,5 +73,7 @@ uiBox *makePage16(void)
uiTableColumnAppendTextPart(tc, 1, 0); uiTableColumnAppendTextPart(tc, 1, 0);
uiTableColumnAppendTextPart(tc, 2, 1); uiTableColumnAppendTextPart(tc, 2, 1);
uiTableSetRowBackgroundColorModelColumn(t, 3);
return page16; return page16;
} }

View File

@ -6,6 +6,7 @@ typedef struct uiTableModelHandler uiTableModelHandler;
_UI_ENUM(uiTableModelColumnType) { _UI_ENUM(uiTableModelColumnType) {
uiTableModelColumnString, uiTableModelColumnString,
uiTableModelColumnColor,
}; };
struct uiTableModelHandler { struct uiTableModelHandler {
@ -17,6 +18,8 @@ struct uiTableModelHandler {
}; };
_UI_EXTERN void *uiTableModelStrdup(const char *str); _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 uiTableModel *uiNewTableModel(uiTableModelHandler *mh);
_UI_EXTERN void uiFreeTableModel(uiTableModel *m); _UI_EXTERN void uiFreeTableModel(uiTableModel *m);
@ -33,4 +36,6 @@ typedef struct uiTable uiTable;
#define uiTable(this) ((uiTable *) (this)) #define uiTable(this) ((uiTable *) (this))
_UI_EXTERN uiTableColumn *uiTableAppendColumn(uiTable *t, const char *name); _UI_EXTERN uiTableColumn *uiTableAppendColumn(uiTable *t, const char *name);
_UI_EXTERN uiTableColumn *uiTableAppendTextColumn(uiTable *t, const char *name, int modelColumn); _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); _UI_EXTERN uiTable *uiNewTable(uiTableModel *model);