From f3882d6124adada301f29733cb5bc20a5c8ccc27 Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Sun, 24 Jun 2018 14:22:05 -0400 Subject: [PATCH] Cleaned up color access across platforms. --- common/table.h | 1 + common/tablemodel.c | 14 ++++++++++++++ darwin/table.m | 10 +++------- darwin/tablecolumn.m | 18 ++++-------------- windows/tabledraw.cpp | 43 ++++++++++++++++--------------------------- 5 files changed, 38 insertions(+), 48 deletions(-) diff --git a/common/table.h b/common/table.h index a7e2d8ff..f83205df 100644 --- a/common/table.h +++ b/common/table.h @@ -13,6 +13,7 @@ extern uiTableValue *uiprivTableModelCellValue(uiTableModel *m, int row, int col extern void uiprivTableModelSetCellValue(uiTableModel *m, int row, int column, const uiTableValue *value); extern const uiTableTextColumnOptionalParams uiprivDefaultTextColumnOptionalParams; extern int uiprivTableModelCellEditable(uiTableModel *m, int row, int column); +extern int uiprivTableModelColorIfProvided(uiTableModel *m, int row, int column, double *r, double *g, double *b, double *a); #ifdef __cplusplus } diff --git a/common/tablemodel.c b/common/tablemodel.c index e1de4896..dbda4a81 100644 --- a/common/tablemodel.c +++ b/common/tablemodel.c @@ -63,3 +63,17 @@ int uiprivTableModelCellEditable(uiTableModel *m, int row, int column) uiFreeTableValue(value); return editable; } + +int uiprivTableModelColorIfProvided(uiTableModel *m, int row, int column, double *r, double *g, double *b, double *a) +{ + uiTableValue *value; + + if (column == -1) + return 0; + value = uiprivTableModelCellValue(m, row, column); + if (value == NULL) + return 0; + uiTableValueColor(value, r, g, b, a); + uiFreeTableValue(value); + return 1; +} diff --git a/darwin/table.m b/darwin/table.m index 41726b6b..b23ed47d 100644 --- a/darwin/table.m +++ b/darwin/table.m @@ -31,18 +31,14 @@ // TODO is this correct for overflow scrolling? static void setBackgroundColor(uiprivTableView *t, NSTableRowView *rv, NSInteger row) { - uiTableValue *value; NSColor *color; double r, g, b, a; if (t->uiprivT->backgroundColumn == -1) - return; - value = uiprivTableModelCellValue(t->uiprivM, row, t->uiprivT->backgroundColumn); - if (value != NULL) { - uiTableValueColor(value, &r, &g, &b, &a); - uiFreeTableValue(value); + return; // let Cocoa do its default thing + if (uiprivTableModelColorIfProvided(t->uiprivM, row, t->uiprivT->backgroundColumn, &r, &g, &b, &a)) color = [NSColor colorWithSRGBRed:r green:g blue:b alpha:a]; - } else { + else { NSArray *colors; NSInteger index; diff --git a/darwin/tablecolumn.m b/darwin/tablecolumn.m index 4b3eb11d..b918c50c 100644 --- a/darwin/tablecolumn.m +++ b/darwin/tablecolumn.m @@ -249,6 +249,7 @@ struct textColumnCreateParams { if (self->tf != nil) { NSString *str; NSColor *color; + double r, g, b, a; value = uiprivTableModelCellValue(self->m, row, self->textModelColumn); str = uiprivToNSString(uiTableValueString(value)); @@ -257,20 +258,9 @@ struct textColumnCreateParams { [self->tf setEditable:uiprivTableModelCellEditable(self->m, row, self->textEditableModelColumn)]; - color = nil; - if (self->textParams.ColorModelColumn != -1) { - double r, g, b, a; - - value = uiprivTableModelCellValue(self->m, row, self->textParams.ColorModelColumn); - // TODO document this is allowed - if (value != NULL) { - uiTableValueColor(value, &r, &g, &b, &a); - uiFreeTableValue(value); - color = [NSColor colorWithSRGBRed:r green:g blue:b alpha:a]; - } - } - if (color == nil) - color = [NSColor controlTextColor]; + color = [NSColor controlTextColor]; + if (uiprivTableModelColorIfProvided(self->m, row, self->textParams.ColorModelColumn, &r, &g, &b, &a)) + color = [NSColor colorWithSRGBRed:r green:g blue:b alpha:a]; [self->tf setTextColor:color]; // we don't own color in ether case; don't release } diff --git a/windows/tabledraw.cpp b/windows/tabledraw.cpp index 33eeb700..eb50f28a 100644 --- a/windows/tabledraw.cpp +++ b/windows/tabledraw.cpp @@ -549,42 +549,31 @@ static HRESULT fillDrawState(struct drawState *s, uiTable *t, NMLVCUSTOMDRAW *nm s->textColor = GetSysColor(COLOR_HIGHLIGHTTEXT); s->textBrush = GetSysColorBrush(COLOR_HIGHLIGHTTEXT); } else { - uiTableValue *value; double r, g, b, a; s->bgColor = GetSysColor(COLOR_WINDOW); s->bgBrush = GetSysColorBrush(COLOR_WINDOW); - if (t->backgroundColumn != -1) { - value = uiprivTableModelCellValue(s->model, s->iItem, t->backgroundColumn); - if (value != NULL) { - uiTableValueColor(value, &r, &g, &b, &a); - uiFreeTableValue(value); - s->bgColor = blend(s->bgColor, r, g, b, a); - s->bgBrush = CreateSolidBrush(s->bgColor); - if (s->bgBrush == NULL) { - logLastError(L"CreateSolidBrush()"); - hr = E_FAIL; - goto fail; - } - s->freeBgBrush = TRUE; + if (uiprivTableModelColorIfProvided(s->model, s->iItem, t->backgroundColumn, &r, &g, &b, &a)) { + s->bgColor = blend(s->bgColor, r, g, b, a); + s->bgBrush = CreateSolidBrush(s->bgColor); + if (s->bgBrush == NULL) { + logLastError(L"CreateSolidBrush()"); + hr = E_FAIL; + goto fail; } + s->freeBgBrush = TRUE; } s->textColor = GetSysColor(COLOR_WINDOWTEXT); s->textBrush = GetSysColorBrush(COLOR_WINDOWTEXT); - if (p->textParams.ColorModelColumn != -1) { - value = uiprivTableModelCellValue(s->model, s->iItem, p->textParams.ColorModelColumn); - if (value != NULL) { - uiTableValueColor(value, &r, &g, &b, &a); - uiFreeTableValue(value); - s->textColor = blend(s->bgColor, r, g, b, a); - s->textBrush = CreateSolidBrush(s->textColor); - if (s->textBrush == NULL) { - logLastError(L"CreateSolidBrush()"); - hr = E_FAIL; - goto fail; - } - s->freeTextBrush = TRUE; + if (uiprivTableModelColorIfProvided(s->model, s->iItem, p->textParams.ColorModelColumn, &r, &g, &b, &a)) { + s->textColor = blend(s->bgColor, r, g, b, a); + s->textBrush = CreateSolidBrush(s->textColor); + if (s->textBrush == NULL) { + logLastError(L"CreateSolidBrush()"); + hr = E_FAIL; + goto fail; } + s->freeTextBrush = TRUE; } }