From 5ec2c768face785c502ee6508b5ed5ed0124b307 Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Sun, 10 Aug 2014 15:56:59 -0400 Subject: [PATCH] Made a more intelligent Table.preferredSize() for Mac OS X. --- redo/control_darwin.m | 2 +- redo/objc_darwin.h | 1 + redo/table_darwin.go | 3 ++- redo/table_darwin.m | 31 +++++++++++++++++++++++++++++++ 4 files changed, 35 insertions(+), 2 deletions(-) diff --git a/redo/control_darwin.m b/redo/control_darwin.m index 8765e4c..ce1eca7 100644 --- a/redo/control_darwin.m +++ b/redo/control_darwin.m @@ -28,7 +28,7 @@ void setSmallControlFont(id control) [toNSControl(control) setFont:[NSFont systemFontOfSize:[NSFont systemFontSizeForControlSize:NSSmallControlSize]]]; } -// also good for NSTableView (TODO might not do what we want) and NSProgressIndicator +// also good for NSProgressIndicator struct xsize controlPreferredSize(id control) { NSControl *c; diff --git a/redo/objc_darwin.h b/redo/objc_darwin.h index 3a42017..de04f26 100644 --- a/redo/objc_darwin.h +++ b/redo/objc_darwin.h @@ -84,6 +84,7 @@ extern id newTable(void); extern void tableAppendColumn(id, char *); extern void tableUpdate(id); extern void tableMakeDataSource(id, void *); +extern struct xsize tablePreferredSize(id); /* control_darwin.m */ extern void parent(id, id); diff --git a/redo/table_darwin.go b/redo/table_darwin.go index 14b2c8d..37fcdf4 100644 --- a/redo/table_darwin.go +++ b/redo/table_darwin.go @@ -76,7 +76,8 @@ func (t *table) allocate(x int, y int, width int, height int, d *sizing) []*allo } func (t *table) preferredSize(d *sizing) (width, height int) { - return basepreferredSize(t, d) + s := C.tablePreferredSize(t._id) + return int(s.width), int(s.height) } func (t *table) commitResize(c *allocation, d *sizing) { diff --git a/redo/table_darwin.m b/redo/table_darwin.m index 9a16082..e679c1d 100644 --- a/redo/table_darwin.m +++ b/redo/table_darwin.m @@ -72,3 +72,34 @@ void tableMakeDataSource(id table, void *gotable) model->gotable = gotable; [toNSTableView(table) setDataSource:model]; } + +// -[NSTableView sizeToFit] does not actually size to fit +// -[NSTableColumn sizeToFit] is just for the header +// -[NSTableColumn sizeToFit] can work for guessing but overrides user settings +// -[[NSTableColumn headerCell] cellSize] does NOT (despite being documented as returning the minimum needed size) +// Let's write our own to prefer: +// - width of the sum of all columns's current widths +// - height of 5 rows (arbitrary count; seems reasonable) + header view height +// Hopefully this is reasonable. +struct xsize tablePreferredSize(id control) +{ + NSTableView *t; + NSArray *columns; + struct xsize s; + NSUInteger i, n; + NSTableColumn *c; + + t = toNSTableView(control); + columns = [t tableColumns]; + n = [columns count]; + s.width = 0; + for (i = 0; i < n; i++) { + CGFloat width; + + c = (NSTableColumn *) [columns objectAtIndex:i]; + s.width += (intptr_t) [c width]; + } + s.height = 5 * (intptr_t) [t rowHeight]; + s.height += (intptr_t) [[t headerView] frame].size.height; + return s; +}