#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; }