diff --git a/darwin/table.m b/darwin/table.m index 213bc644..178e3cd3 100644 --- a/darwin/table.m +++ b/darwin/table.m @@ -18,10 +18,10 @@ enum { partText, partImage, + partButton, }; @interface tablePart : NSObject -@property int index; @property int type; @property int textColumn; @property int textColorColumn; @@ -153,6 +153,7 @@ done: uiTable *t = tv.libui_t; NSColor *color; + [rv setBackgroundColor:nil]; // make default by default if (t->backgroundColumn == -1) return; color = (NSColor *) ((*(m->mh->CellValue))(m->mh, m, row, t->backgroundColumn)); @@ -181,6 +182,8 @@ done: if ([view isKindOfClass:[NSTextField class]]) data = [[((NSTextField *) view) stringValue] UTF8String]; + else if ([view isKindOfClass:[NSButton class]]) + data = NULL; else implbug("table model editing action triggered on non-editable view"); @@ -189,7 +192,7 @@ done: row, [view tag], data); // always refresh the value in case the model rejected it - // TODO only affect tv + // TODO only affect tv? uiTableModelRowChanged(m, row); } @@ -214,6 +217,7 @@ done: NSView *view; NSTextField *tf; NSImageView *iv; + NSButton *b; switch (self.type) { case partText: @@ -235,7 +239,7 @@ done: [tf setTarget:m->m]; [tf setAction:@selector(onAction:)]; } - [tf setTag:self.index]; + [tf setTag:self.textColumn]; view = tf; break; case partImage: @@ -252,9 +256,27 @@ done: iv, NSLayoutAttributeHeight, 1, 0, @"uiTable image squareness constraint")]; - [iv setTag:self.index]; + [iv setTag:self.imageColumn]; view = iv; break; + case partButton: + // TODO buttons get clipped + data = (*(m->mh->CellValue))(m->mh, m, row, self.textColumn); + str = toNSString((char *) data); + b = [[NSButton alloc] initWithFrame:NSZeroRect]; + [b setTitle:str]; + [b setButtonType:NSMomentaryPushInButton]; + [b setBordered:YES]; + [b setBezelStyle:NSRoundRectBezelStyle]; + uiDarwinSetControlFont(b, NSRegularControlSize); + if (self.editable) { + [b setTarget:m->m]; + [b setAction:@selector(onAction:)]; + } else + [b setEnabled:NO]; + [b setTag:self.textColumn]; + view = b; + break; } // if stretchy, don't hug, otherwise hug forcibly @@ -330,6 +352,7 @@ void uiTableModelRowChanged(uiTableModel *m, int index) for (tv in m->tables) { cols = [[NSIndexSet alloc] initWithIndexesInRange:NSMakeRange(0, [[tv tableColumns] count])]; [tv reloadDataForRowIndexes:set columnIndexes:cols]; + // TODO this isn't enough [cols release]; } // set is autoreleased @@ -351,7 +374,6 @@ void uiTableColumnAppendTextPart(uiTableColumn *c, int modelColumn, int expand) tablePart *part; part = [tablePart new]; - part.index = [c->parts count]; part.type = partText; part.textColumn = modelColumn; part.expand = expand; @@ -363,13 +385,24 @@ void uiTableColumnAppendImagePart(uiTableColumn *c, int modelColumn, int expand) tablePart *part; part = [tablePart new]; - part.index = [c->parts count]; part.type = partImage; part.imageColumn = modelColumn; part.expand = expand; [c->parts addObject:part]; } +void uiTableColumnAppendButtonPart(uiTableColumn *c, int modelColumn, int expand) +{ + tablePart *part; + + part = [tablePart new]; + part.type = partButton; + part.textColumn = modelColumn; + part.expand = expand; + part.editable = 1; // editable by default + [c->parts addObject:part]; +} + void uiTableColumnPartSetEditable(uiTableColumn *c, int part, int editable) { tablePart *p; diff --git a/test/page16.c b/test/page16.c index 890e2a3d..35239d0c 100644 --- a/test/page16.c +++ b/test/page16.c @@ -5,7 +5,7 @@ static uiTableModelHandler mh; static int modelNumColumns(uiTableModelHandler *mh, uiTableModel *m) { - return 6; + return 7; } static uiTableModelColumnType modelColumnType(uiTableModelHandler *mh, uiTableModel *m, int column) @@ -24,12 +24,15 @@ static int modelNumRows(uiTableModelHandler *mh, uiTableModel *m) static uiImage *img[2]; static char row9text[1024]; +static int yellowRow = -1; static void *modelCellValue(uiTableModelHandler *mh, uiTableModel *m, int row, int col) { char buf[256]; if (col == 3) { + if (row == yellowRow) + return uiTableModelGiveColor(1, 1, 0, 1); if (row == 3) return uiTableModelGiveColor(1, 0, 0, 1); if (row == 11) @@ -57,6 +60,9 @@ static void *modelCellValue(uiTableModelHandler *mh, uiTableModel *m, int row, i case 1: strcpy(buf, "Part"); break; + case 6: + strcpy(buf, "Make Yellow"); + break; } return uiTableModelStrdup(buf); } @@ -65,6 +71,8 @@ static void modelSetCellValue(uiTableModelHandler *mh, uiTableModel *m, int row, { if (row == 9 && col == 2) strcpy(row9text, (const char *) val); + if (col == 6) + yellowRow = row; } uiBox *makePage16(void) @@ -106,5 +114,8 @@ uiBox *makePage16(void) uiTableSetRowBackgroundColorModelColumn(t, 3); + tc = uiTableAppendColumn(t, "Buttons"); + uiTableColumnAppendButtonPart(tc, 6, 1); + return page16; } diff --git a/uitable.h b/uitable.h index cc5b4d94..df87addb 100644 --- a/uitable.h +++ b/uitable.h @@ -41,6 +41,7 @@ typedef struct uiTableColumn uiTableColumn; _UI_EXTERN void uiTableColumnAppendTextPart(uiTableColumn *c, int modelColumn, int expand); // TODO images shouldn't expand... _UI_EXTERN void uiTableColumnAppendImagePart(uiTableColumn *c, int modelColumn, int expand); +_UI_EXTERN void uiTableColumnAppendButtonPart(uiTableColumn *c, int modelColumn, int expand); // TODO Editable? _UI_EXTERN void uiTableColumnPartSetEditable(uiTableColumn *c, int part, int editable); _UI_EXTERN void uiTableColumnPartSetTextColor(uiTableColumn *c, int part, int modelColumn);