From a4294e4a94189dffb1fdf99c9a60d87d77272926 Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Sat, 13 Jul 2024 10:52:24 -0700 Subject: Restructure project. --- src/widget/table.c | 103 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 src/widget/table.c (limited to 'src/widget/table.c') diff --git a/src/widget/table.c b/src/widget/table.c new file mode 100644 index 0000000..7a0ea03 --- /dev/null +++ b/src/widget/table.c @@ -0,0 +1,103 @@ +#include "table.h" + +#include "widget.h" + +const uiCell* GetCell(const uiTable* table, int row, int col) { + assert(table); + return &table->cells[row][col]; +} + +uiCell* GetCellMut(uiTable* table, int row, int col) { + assert(table); + return (uiCell*)GetCell(table, row, col); +} + +uiCell** GetLastRow(uiTable* table) { + assert(table); + assert(table->rows > 0); + return &table->cells[table->rows - 1]; +} + +uiTable* uiMakeTable(int rows, int cols, const char** header) { + uiTable* table = UI_NEW(uiTable); + + *table = (uiTable){ + .widget = (uiWidget){.type = uiTypeTable}, + .rows = rows, + .cols = cols, + .widths = (cols > 0) ? calloc(cols, sizeof(int)) : 0, + .header = header ? calloc(cols, sizeof(uiCell)) : 0, + .cells = (rows * cols > 0) ? calloc(rows, sizeof(uiCell*)) : 0, + .flags = {0}, + }; + + if (header) { + for (int col = 0; col < cols; ++col) { + table->header[col].child = (uiWidget*)uiMakeLabel(header[col]); + } + } + + return table; +} + +void uiTableClear(uiTable* table) { + assert(table); + + // Free row data. + 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); + } + free(table->cells[row]); + } + free(table->cells); + table->cells = 0; + } + table->rows = 0; + + // Clear row widths. + for (int i = 0; i < table->cols; ++i) { + table->widths[i] = 0; + } + + table->offset = 0; + + table->flags.vertical_overflow = 0; +} + +void uiTableAddRow(uiTable* table, const char** row) { + assert(table); + + table->rows++; + + uiCell** cells = realloc(table->cells, table->rows * sizeof(uiCell*)); + ASSERT(cells); + table->cells = cells; + + uiCell** pLastRow = GetLastRow(table); + *pLastRow = calloc(table->cols, sizeof(uiCell)); + ASSERT(*pLastRow); + uiCell* lastRow = *pLastRow; + + for (int col = 0; col < table->cols; ++col) { + lastRow[col].child = (uiWidget*)uiMakeLabel(row[col]); + } +} + +void uiTableSet(uiTable* table, int row, int col, uiPtr child) { + 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; +} + +uiWidget* uiTableGetMut(uiTable* table, int row, int col) { + assert(table); + return GetCellMut(table, row, col)->child; +} -- cgit v1.2.3