From e0230d73a6e6fe22f5718108d17c625f16c82b39 Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Sat, 25 Jun 2016 23:04:49 -0400 Subject: [PATCH] Started editable uiTable elements. --- darwin/table.m | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ test/page16.c | 14 +++++++++++--- uitable.h | 4 +++- 3 files changed, 62 insertions(+), 4 deletions(-) diff --git a/darwin/table.m b/darwin/table.m index baa17999..e3b3686a 100644 --- a/darwin/table.m +++ b/darwin/table.m @@ -5,11 +5,15 @@ // - initial state of table view is off // - header cell seems off // - background color shows up for a line or two below selection +// - editable NSTextFields have no intrinsic width +// - changing a part property does not refresh views +// - the target/action thing is wrong; we need to pass the model column for col, not the view column @interface tableModel : NSObject { uiTableModel *libui_m; } - (id)initWithModel:(uiTableModel *)m; +- (IBAction)onAction:(id)sender; @end enum { @@ -23,6 +27,7 @@ enum { @property int textColorColumn; @property int imageColumn; @property int expand; +@property int editable; - (NSView *)mkView:(uiTableModel *)m row:(int)row; @end @@ -157,6 +162,36 @@ done: // TODO autorelease color? or release it? } +- (IBAction)onAction:(id)sender +{ + uiTableModel *m = self->libui_m; + NSView *view = (NSView *) sender; + NSTableView *tv; + NSInteger row; + const void *data; + + row = -1; + for (tv in m->tables) { + row = [tv rowForView:view]; + if (row != -1) + break; + } + if (row == -1) + implbug("table model action triggered on view with no associated table"); + + if ([view isKindOfClass:[NSTextField class]]) + data = [((NSTextField *) view) stringValue]; + else + implbug("table model editing action triggered on non-editable view"); + + (*(m->mh->SetCellValue))(m->mh, m, + row, [tv columnForView:view], + data); + // always refresh the value in case the model rejected it + // TODO only affect tv + uiTableModelRowChanged(m, row); +} + @end @implementation tablePart @@ -194,6 +229,11 @@ done: [tf setTextColor:color]; // TODO release color } + if (self.editable) { + [tf setEditable:YES]; + [tf setTarget:m->m]; + [tf setAction:@selector(onAction:)]; + } view = tf; break; case partImage: @@ -325,6 +365,14 @@ void uiTableColumnAppendImagePart(uiTableColumn *c, int modelColumn, int expand) [c->parts addObject:part]; } +void uiTableColumnPartSetEditable(uiTableColumn *c, int part, int editable) +{ + tablePart *p; + + p = (tablePart *) [c->parts objectAtIndex:part]; + p.editable = editable; +} + void uiTableColumnPartSetTextColor(uiTableColumn *c, int part, int modelColumn) { tablePart *p; diff --git a/test/page16.c b/test/page16.c index 4de9983e..890e2a3d 100644 --- a/test/page16.c +++ b/test/page16.c @@ -23,6 +23,7 @@ static int modelNumRows(uiTableModelHandler *mh, uiTableModel *m) } static uiImage *img[2]; +static char row9text[1024]; static void *modelCellValue(uiTableModelHandler *mh, uiTableModel *m, int row, int col) { @@ -49,17 +50,21 @@ static void *modelCellValue(uiTableModelHandler *mh, uiTableModel *m, int row, i case 0: sprintf(buf, "Row %d", row); break; - case 1: case 2: + if (row == 9) + return uiTableModelStrdup(row9text); + // fall through + case 1: strcpy(buf, "Part"); break; } return uiTableModelStrdup(buf); } -static void modelSetCellValue(uiTableModelHandler *mh, uiTableModel *m, int row, int col, void *val) +static void modelSetCellValue(uiTableModelHandler *mh, uiTableModel *m, int row, int col, const void *val) { - // not implemented yet + if (row == 9 && col == 2) + strcpy(row9text, (const char *) val); } uiBox *makePage16(void) @@ -76,6 +81,8 @@ uiBox *makePage16(void) appendImageNamed(img[1], "tango-icon-theme-0.8.90_16x16_x-office-spreadsheet.png"); appendImageNamed(img[1], "tango-icon-theme-0.8.90_32x32_x-office-spreadsheet.png"); + strcpy(row9text, "Part"); + page16 = newVerticalBox(); mh.NumColumns = modelNumColumns; @@ -95,6 +102,7 @@ uiBox *makePage16(void) uiTableColumnAppendTextPart(tc, 1, 0); uiTableColumnAppendTextPart(tc, 2, 1); uiTableColumnPartSetTextColor(tc, 1, 4); + uiTableColumnPartSetEditable(tc, 2, 1); uiTableSetRowBackgroundColorModelColumn(t, 3); diff --git a/uitable.h b/uitable.h index 508d3d8a..cc5b4d94 100644 --- a/uitable.h +++ b/uitable.h @@ -22,7 +22,7 @@ struct uiTableModelHandler { uiTableModelColumnType (*ColumnType)(uiTableModelHandler *, uiTableModel *, int); int (*NumRows)(uiTableModelHandler *, uiTableModel *); void *(*CellValue)(uiTableModelHandler *, uiTableModel *, int, int); - void (*SetCellValue)(uiTableModelHandler *, uiTableModel *, int, int, void *); + void (*SetCellValue)(uiTableModelHandler *, uiTableModel *, int, int, const void *); }; _UI_EXTERN void *uiTableModelStrdup(const char *str); @@ -41,6 +41,8 @@ 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); +// TODO Editable? +_UI_EXTERN void uiTableColumnPartSetEditable(uiTableColumn *c, int part, int editable); _UI_EXTERN void uiTableColumnPartSetTextColor(uiTableColumn *c, int part, int modelColumn); typedef struct uiTable uiTable;