Added uiTable buttons. Not fully working on OS X.

This commit is contained in:
Pietro Gagliardi 2016-06-26 13:06:33 -04:00
parent 15eca1372e
commit 44a723b314
3 changed files with 52 additions and 7 deletions

View File

@ -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;

View File

@ -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;
}

View File

@ -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);