Stored table column numbers more efficiently on Mac OS X.

This commit is contained in:
Pietro Gagliardi 2014-08-10 22:10:47 -04:00
parent 5ec2c768fa
commit 37b1c3309b
3 changed files with 20 additions and 7 deletions

View File

@ -81,7 +81,7 @@ extern struct xsize tabPreferredSize(id);
/* table_darwin.m */
extern id newTable(void);
extern void tableAppendColumn(id, char *);
extern void tableAppendColumn(id, intptr_t, char *);
extern void tableUpdate(id);
extern void tableMakeDataSource(id, void *);
extern struct xsize tablePreferredSize(id);

View File

@ -28,7 +28,7 @@ func finishNewTable(b *tablebase, ty reflect.Type) Table {
C.tableMakeDataSource(t._id, unsafe.Pointer(t))
for i := 0; i < ty.NumField(); i++ {
cname := C.CString(ty.Field(i).Name)
C.tableAppendColumn(t._id, cname)
C.tableAppendColumn(t._id, C.intptr_t(i), cname)
C.free(unsafe.Pointer(cname)) // free now (not deferred) to conserve memory
}
return t

View File

@ -6,6 +6,17 @@
#define toNSTableView(x) ((NSTableView *) (x))
// NSTableColumn provides no provision to store an integer data
// it does provide an identifier tag, but that's a NSString, and I'd rather not risk the conversion overhead
@interface goTableColumn : NSTableColumn {
@public
intptr_t gocolnum;
}
@end
@implementation goTableColumn
@end
@interface goTableDataSource : NSObject <NSTableViewDataSource> {
@public
void *gotable;
@ -23,9 +34,10 @@
{
char *str;
NSString *s;
intptr_t colnum;
// TODO there has to be a better way to get the column index
str = goTableDataSource_getValue(self->gotable, (intptr_t) row, (intptr_t) [[view tableColumns] indexOfObject:col]);
colnum = ((goTableColumn *) col)->gocolnum;
str = goTableDataSource_getValue(self->gotable, (intptr_t) row, colnum);
s = [NSString stringWithUTF8String:str];
free(str); // allocated with C.CString() on the Go side
return s;
@ -46,11 +58,12 @@ id newTable(void)
return (id) t;
}
void tableAppendColumn(id t, char *name)
void tableAppendColumn(id t, intptr_t colnum, char *name)
{
NSTableColumn *c;
goTableColumn *c;
c = [[NSTableColumn alloc] initWithIdentifier:nil];
c = [[goTableColumn alloc] initWithIdentifier:nil];
c->gocolnum = colnum;
[c setEditable:NO];
[[c headerCell] setStringValue:[NSString stringWithUTF8String:name]];
setSmallControlFont((id) [c headerCell]);