2018-06-03 19:55:08 -05:00
|
|
|
// 3 june 2018
|
2018-06-03 18:59:05 -05:00
|
|
|
#import "uipriv_darwin.h"
|
2018-06-03 19:55:08 -05:00
|
|
|
#import "table.h"
|
2018-06-03 18:59:05 -05:00
|
|
|
|
|
|
|
@interface uiprivTableModel : NSObject<NSTableViewDataSource, NSTableViewDelegate> {
|
|
|
|
uiTableModel *m;
|
|
|
|
}
|
|
|
|
- (id)initWithModel:(uiTableModel *)model;
|
|
|
|
@end
|
|
|
|
|
2018-06-04 21:14:05 -05:00
|
|
|
// TODO we really need to clean up the sharing of the table and model variables...
|
|
|
|
@interface uiprivTableView : NSTableView {
|
|
|
|
uiTable *uiprivT;
|
|
|
|
uiTableModel *uiprivM;
|
|
|
|
}
|
|
|
|
- (id)initWithFrame:(NSRect)r uiprivT:(uiTable *)t uiprivM:(uiTableModel *)m;
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation uiprivTableView
|
|
|
|
|
|
|
|
- (id)initWithFrame:(NSRect)r uiprivT:(uiTable *)t uiprivM:(uiTableModel *)m
|
|
|
|
{
|
|
|
|
self = [super initWithFrame:r];
|
|
|
|
if (self) {
|
|
|
|
self->uiprivT = t;
|
|
|
|
self->uiprivM = m;
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO is this correct for overflow scrolling?
|
|
|
|
static void setBackgroundColor(uiprivTableView *t, NSTableRowView *rv, NSInteger row)
|
|
|
|
{
|
|
|
|
uiTableData *data;
|
|
|
|
NSColor *color;
|
|
|
|
double r, g, b, a;
|
|
|
|
|
|
|
|
if (t->uiprivT->backgroundColumn == -1)
|
|
|
|
return;
|
|
|
|
data = (*(t->uiprivM->mh->CellValue))(t->uiprivM->mh, t->uiprivM, row, t->uiprivT->backgroundColumn);
|
|
|
|
if (data != NULL) {
|
|
|
|
uiTableDataColor(data, &r, &g, &b, &a);
|
|
|
|
uiFreeTableData(data);
|
|
|
|
color = [NSColor colorWithSRGBRed:r green:g blue:b alpha:a];
|
|
|
|
} else {
|
|
|
|
NSArray *colors;
|
|
|
|
NSInteger index;
|
|
|
|
|
|
|
|
// this usage is primarily a guess; hopefully it is correct for the non-two color case... (TODO)
|
|
|
|
// it does seem to be correct for the two-color case, judging from comparing against the value of backgroundColor before changing it (and no, nil does not work; it just sets to white)
|
|
|
|
colors = [NSColor controlAlternatingRowBackgroundColors];
|
|
|
|
index = row % [colors count];
|
|
|
|
color = (NSColor *) [colors objectAtIndex:index];
|
|
|
|
}
|
|
|
|
[rv setBackgroundColor:color];
|
|
|
|
// color is autoreleased in all cases
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
2018-06-03 19:55:08 -05:00
|
|
|
@implementation uiprivTableModel
|
2018-06-03 18:59:05 -05:00
|
|
|
|
2018-06-03 22:06:44 -05:00
|
|
|
- (id)initWithModel:(uiTableModel *)model
|
2018-06-03 18:59:05 -05:00
|
|
|
{
|
|
|
|
self = [super init];
|
|
|
|
if (self)
|
2018-06-03 22:06:44 -05:00
|
|
|
self->m = model;
|
2018-06-03 18:59:05 -05:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSInteger)numberOfRowsInTableView:(NSTableView *)tv
|
|
|
|
{
|
|
|
|
return (*(self->m->mh->NumRows))(self->m->mh, self->m);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSView *)tableView:(NSTableView *)tv viewForTableColumn:(NSTableColumn *)cc row:(NSInteger)row
|
|
|
|
{
|
|
|
|
uiprivTableColumn *c = (uiprivTableColumn *) cc;
|
2018-06-03 20:39:49 -05:00
|
|
|
// TODO consider renaming this type to uiprivTableCellView
|
2018-06-03 22:06:44 -05:00
|
|
|
uiprivTableCellView *cv;
|
2018-06-03 18:59:05 -05:00
|
|
|
|
2018-06-03 22:06:44 -05:00
|
|
|
cv = (uiprivTableCellView *) [tv makeViewWithIdentifier:[c identifier] owner:self];
|
2018-06-03 18:59:05 -05:00
|
|
|
if (cv == nil)
|
|
|
|
cv = [c uiprivMakeCellView];
|
|
|
|
[cv uiprivUpdate:row];
|
|
|
|
return cv;
|
|
|
|
}
|
|
|
|
|
2018-06-04 21:14:05 -05:00
|
|
|
- (void)tableView:(NSTableView *)tv didAddRowView:(NSTableRowView *)rv forRow:(NSInteger)row
|
2018-06-03 18:59:05 -05:00
|
|
|
{
|
2018-06-04 21:14:05 -05:00
|
|
|
setBackgroundColor((uiprivTableView *) tv, rv, row);
|
2018-06-03 18:59:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
uiTableModel *uiNewTableModel(uiTableModelHandler *mh)
|
|
|
|
{
|
|
|
|
uiTableModel *m;
|
|
|
|
|
|
|
|
m = uiprivNew(uiTableModel);
|
|
|
|
m->mh = mh;
|
2018-06-03 20:39:49 -05:00
|
|
|
m->m = [[uiprivTableModel alloc] initWithModel:m];
|
2018-06-03 18:59:05 -05:00
|
|
|
m->tables = [NSMutableArray new];
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
|
|
|
void uiFreeTableModel(uiTableModel *m)
|
|
|
|
{
|
|
|
|
if ([m->tables count] != 0)
|
|
|
|
uiprivUserBug("You cannot free a uiTableModel while uiTables are using it.");
|
|
|
|
[m->tables release];
|
|
|
|
[m->m release];
|
|
|
|
uiprivFree(m);
|
|
|
|
}
|
|
|
|
|
|
|
|
void uiTableModelRowInserted(uiTableModel *m, int newIndex)
|
|
|
|
{
|
|
|
|
NSTableView *tv;
|
|
|
|
NSIndexSet *set;
|
|
|
|
|
|
|
|
set = [NSIndexSet indexSetWithIndex:newIndex];
|
|
|
|
for (tv in m->tables)
|
|
|
|
[tv insertRowsAtIndexes:set withAnimation:NSTableViewAnimationEffectNone];
|
|
|
|
// set is autoreleased
|
|
|
|
}
|
|
|
|
|
|
|
|
void uiTableModelRowChanged(uiTableModel *m, int index)
|
|
|
|
{
|
2018-06-04 21:14:05 -05:00
|
|
|
uiprivTableView *tv;
|
2018-06-03 20:39:49 -05:00
|
|
|
NSTableRowView *rv;
|
|
|
|
NSUInteger i, n;
|
2018-06-03 22:06:44 -05:00
|
|
|
uiprivTableCellView *cv;
|
2018-06-03 18:59:05 -05:00
|
|
|
|
|
|
|
for (tv in m->tables) {
|
2018-06-03 22:06:44 -05:00
|
|
|
rv = [tv rowViewAtRow:index makeIfNecessary:NO];
|
2018-06-04 21:14:05 -05:00
|
|
|
if (rv != nil)
|
|
|
|
setBackgroundColor(tv, rv, index);
|
2018-06-03 20:39:49 -05:00
|
|
|
n = [[tv tableColumns] count];
|
|
|
|
for (i = 0; i < n; i++) {
|
2018-06-03 22:06:44 -05:00
|
|
|
cv = (uiprivTableCellView *) [tv viewAtColumn:i row:index makeIfNecessary:NO];
|
2018-06-03 20:39:49 -05:00
|
|
|
if (cv != nil)
|
|
|
|
[cv uiprivUpdate:index];
|
|
|
|
}
|
2018-06-03 18:59:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void uiTableModelRowDeleted(uiTableModel *m, int oldIndex)
|
|
|
|
{
|
|
|
|
NSTableView *tv;
|
|
|
|
NSIndexSet *set;
|
|
|
|
|
|
|
|
set = [NSIndexSet indexSetWithIndex:oldIndex];
|
|
|
|
for (tv in m->tables)
|
|
|
|
[tv removeRowsAtIndexes:set withAnimation:NSTableViewAnimationEffectNone];
|
|
|
|
// set is autoreleased
|
|
|
|
}
|
|
|
|
|
|
|
|
uiDarwinControlAllDefaultsExceptDestroy(uiTable, sv)
|
|
|
|
|
|
|
|
static void uiTableDestroy(uiControl *c)
|
|
|
|
{
|
|
|
|
uiTable *t = uiTable(c);
|
|
|
|
|
|
|
|
// TODO
|
|
|
|
[t->sv release];
|
|
|
|
uiFreeControl(uiControl(t));
|
|
|
|
}
|
|
|
|
|
|
|
|
void uiTableSetRowBackgroundColorModelColumn(uiTable *t, int modelColumn)
|
|
|
|
{
|
|
|
|
t->backgroundColumn = modelColumn;
|
2018-06-03 21:37:36 -05:00
|
|
|
// TODO update all rows
|
2018-06-03 18:59:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
uiTable *uiNewTable(uiTableModel *model)
|
|
|
|
{
|
|
|
|
uiTable *t;
|
|
|
|
uiprivScrollViewCreateParams p;
|
|
|
|
|
|
|
|
uiDarwinNewControl(uiTable, t);
|
2018-06-03 21:37:36 -05:00
|
|
|
t->m = model;
|
2018-06-03 18:59:05 -05:00
|
|
|
|
2018-06-04 21:14:05 -05:00
|
|
|
t->tv = [[uiprivTableView alloc] initWithFrame:NSZeroRect uiprivT:t uiprivM:t->m];
|
2018-06-03 18:59:05 -05:00
|
|
|
|
|
|
|
[t->tv setDataSource:model->m];
|
|
|
|
[t->tv setDelegate:model->m];
|
|
|
|
[t->tv reloadData];
|
|
|
|
[model->tables addObject:t->tv];
|
|
|
|
|
|
|
|
// TODO is this sufficient?
|
|
|
|
[t->tv setAllowsColumnReordering:NO];
|
|
|
|
[t->tv setAllowsColumnResizing:YES];
|
|
|
|
[t->tv setAllowsMultipleSelection:NO];
|
|
|
|
[t->tv setAllowsEmptySelection:YES];
|
|
|
|
[t->tv setAllowsColumnSelection:NO];
|
|
|
|
[t->tv setUsesAlternatingRowBackgroundColors:YES];
|
|
|
|
[t->tv setSelectionHighlightStyle:NSTableViewSelectionHighlightStyleRegular];
|
|
|
|
[t->tv setGridStyleMask:NSTableViewGridNone];
|
|
|
|
[t->tv setAllowsTypeSelect:YES];
|
|
|
|
// TODO floatsGroupRows — do we even allow group rows?
|
|
|
|
|
|
|
|
memset(&p, 0, sizeof (uiprivScrollViewCreateParams));
|
|
|
|
p.DocumentView = t->tv;
|
|
|
|
// this is what Interface Builder sets it to
|
|
|
|
// TODO verify
|
|
|
|
p.BackgroundColor = [NSColor colorWithCalibratedWhite:1.0 alpha:1.0];
|
|
|
|
p.DrawsBackground = YES;
|
|
|
|
p.Bordered = YES;
|
|
|
|
p.HScroll = YES;
|
|
|
|
p.VScroll = YES;
|
|
|
|
t->sv = uiprivMkScrollView(&p, &(t->d));
|
|
|
|
|
2018-06-04 19:17:15 -05:00
|
|
|
// TODO WHY DOES THIS REMOVE ALL GRAPHICAL GLITCHES?
|
|
|
|
// I got the idea from http://jwilling.com/blog/optimized-nstableview-scrolling/ but that was on an unrelated problem I didn't seem to have (although I have small-ish tables to start with)
|
|
|
|
// I don't get layer-backing... am I supposed to layer-back EVERYTHING manually? I need to check Interface Builder again...
|
|
|
|
[t->sv setWantsLayer:YES];
|
|
|
|
|
2018-06-03 18:59:05 -05:00
|
|
|
t->backgroundColumn = -1;
|
|
|
|
|
|
|
|
return t;
|
|
|
|
}
|