From 89188be968ac39e0e12077e6b596efaf00d06986 Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Sun, 8 Mar 2026 10:12:57 -0700 Subject: Store strings inside table cells for simplicity --- src/layout.c | 14 +++++--------- src/render.c | 6 ++---- src/widget/table.c | 22 ++++++++-------------- src/widget/widget.h | 2 +- 4 files changed, 16 insertions(+), 28 deletions(-) (limited to 'src') diff --git a/src/layout.c b/src/layout.c index 62e1876..2b43ce8 100644 --- a/src/layout.c +++ b/src/layout.c @@ -44,22 +44,18 @@ static void ResizeTable(uiTable* table, int width, int height) { int* widths = table->widths; // Header. for (int col = 0; col < table->cols; ++col) { - const uiCell* cell = &table->header[col]; - const uiLabel* label = (uiLabel*)cell->child; - const int length = (int)string_length(label->text); + const uiCell* cell = &table->header[col]; + const int length = (int)string_length(cell->text); widths[col] = length; } // Table contents. for (int row = 0; row < table->rows; ++row) { for (int col = 0; col < table->cols; ++col) { - const uiCell* cell = GetCell(table, row, col); - if (cell->child) { - const uiLabel* label = (uiLabel*)cell->child; - const int length = (int)string_length(label->text); + const uiCell* cell = GetCell(table, row, col); + const int length = (int)string_length(cell->text); - widths[col] = length > widths[col] ? length : widths[col]; - } + widths[col] = length > widths[col] ? length : widths[col]; } } // Multiply string lengths times glyph width to compute pixel size. diff --git a/src/render.c b/src/render.c index f716e1a..2d3764a 100644 --- a/src/render.c +++ b/src/render.c @@ -163,7 +163,6 @@ static void RenderText(const char* text, size_t length, RenderState* state) { /// Render a frame. static void RenderFrame(const uiFrame* frame, RenderState* state) { assert(frame); - FillRect(&frame->widget.rect, uiBlack, state); } @@ -171,7 +170,6 @@ static void RenderFrame(const uiFrame* frame, RenderState* state) { static void RenderLabel(const uiLabel* label, RenderState* state) { assert(label); assert(state); - RenderText(string_data(label->text), string_length(label->text), state); } @@ -198,7 +196,7 @@ static void RenderTable(const uiTable* table, RenderState* state) { &original_subsurface, &original_pen); const uiCell* cell = &table->header[col]; - RenderWidget(state, cell->child); + RenderText(string_data(cell->text), string_length(cell->text), state); // Reset the original subsurface and pen for subsequent columns. PopSubsurface(state, &original_subsurface, &original_pen); @@ -228,7 +226,7 @@ static void RenderTable(const uiTable* table, RenderState* state) { state->pen.x = 0; const uiCell* cell = GetCell(table, row, col); - RenderWidget(state, cell->child); + RenderText(string_data(cell->text), string_length(cell->text), state); // Reset the original subsurface and pen for subsequent columns. PopSubsurface(state, &original_subsurface, &original_pen); diff --git a/src/widget/table.c b/src/widget/table.c index 7a0ea03..dfb2e69 100644 --- a/src/widget/table.c +++ b/src/widget/table.c @@ -33,7 +33,7 @@ uiTable* uiMakeTable(int rows, int cols, const char** header) { if (header) { for (int col = 0; col < cols; ++col) { - table->header[col].child = (uiWidget*)uiMakeLabel(header[col]); + table->header[col].text = string_new(header[col]); } } @@ -47,7 +47,7 @@ void uiTableClear(uiTable* table) { if (table->cells) { for (int row = 0; row < table->rows; ++row) { for (int col = 0; col < table->cols; ++col) { - DestroyWidget(&table->cells[row][col].child); + string_del(&table->cells[row][col].text); } free(table->cells[row]); } @@ -81,23 +81,17 @@ void uiTableAddRow(uiTable* table, const char** row) { uiCell* lastRow = *pLastRow; for (int col = 0; col < table->cols; ++col) { - lastRow[col].child = (uiWidget*)uiMakeLabel(row[col]); + lastRow[col].text = string_new(row[col]); } } -void uiTableSet(uiTable* table, int row, int col, uiPtr child) { +void uiTableSet(uiTable* table, int row, int col, const char* text) { assert(table); - assert(child.widget); - - GetCellMut(table, row, col)->child = child.widget; -} - -const uiWidget* uiTableGet(const uiTable* table, int row, int col) { - assert(table); - return GetCell(table, row, col)->child; + assert(text); + GetCellMut(table, row, col)->text = string_new(text); } -uiWidget* uiTableGetMut(uiTable* table, int row, int col) { +const char* uiTableGet(const uiTable* table, int row, int col) { assert(table); - return GetCellMut(table, row, col)->child; + return string_data(GetCell(table, row, col)->text); } diff --git a/src/widget/widget.h b/src/widget/widget.h index 79a72f3..38acb9c 100644 --- a/src/widget/widget.h +++ b/src/widget/widget.h @@ -35,7 +35,7 @@ typedef struct uiLabel { /// Table cell. typedef struct uiCell { - uiWidget* child; + string text; } uiCell; /// Table. -- cgit v1.2.3