diff options
author | 3gg <3gg@shellblade.net> | 2024-07-13 10:52:24 -0700 |
---|---|---|
committer | 3gg <3gg@shellblade.net> | 2024-07-13 10:52:24 -0700 |
commit | a4294e4a94189dffb1fdf99c9a60d87d77272926 (patch) | |
tree | 2e92f7c95116861bc39f4dae1d0ab5d388550000 /src/widget/table.c | |
parent | cf9579d7546c04dbc708bd8719e3f935a28088bd (diff) |
Restructure project.
Diffstat (limited to 'src/widget/table.c')
-rw-r--r-- | src/widget/table.c | 103 |
1 files changed, 103 insertions, 0 deletions
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 @@ | |||
1 | #include "table.h" | ||
2 | |||
3 | #include "widget.h" | ||
4 | |||
5 | const uiCell* GetCell(const uiTable* table, int row, int col) { | ||
6 | assert(table); | ||
7 | return &table->cells[row][col]; | ||
8 | } | ||
9 | |||
10 | uiCell* GetCellMut(uiTable* table, int row, int col) { | ||
11 | assert(table); | ||
12 | return (uiCell*)GetCell(table, row, col); | ||
13 | } | ||
14 | |||
15 | uiCell** GetLastRow(uiTable* table) { | ||
16 | assert(table); | ||
17 | assert(table->rows > 0); | ||
18 | return &table->cells[table->rows - 1]; | ||
19 | } | ||
20 | |||
21 | uiTable* uiMakeTable(int rows, int cols, const char** header) { | ||
22 | uiTable* table = UI_NEW(uiTable); | ||
23 | |||
24 | *table = (uiTable){ | ||
25 | .widget = (uiWidget){.type = uiTypeTable}, | ||
26 | .rows = rows, | ||
27 | .cols = cols, | ||
28 | .widths = (cols > 0) ? calloc(cols, sizeof(int)) : 0, | ||
29 | .header = header ? calloc(cols, sizeof(uiCell)) : 0, | ||
30 | .cells = (rows * cols > 0) ? calloc(rows, sizeof(uiCell*)) : 0, | ||
31 | .flags = {0}, | ||
32 | }; | ||
33 | |||
34 | if (header) { | ||
35 | for (int col = 0; col < cols; ++col) { | ||
36 | table->header[col].child = (uiWidget*)uiMakeLabel(header[col]); | ||
37 | } | ||
38 | } | ||
39 | |||
40 | return table; | ||
41 | } | ||
42 | |||
43 | void uiTableClear(uiTable* table) { | ||
44 | assert(table); | ||
45 | |||
46 | // Free row data. | ||
47 | if (table->cells) { | ||
48 | for (int row = 0; row < table->rows; ++row) { | ||
49 | for (int col = 0; col < table->cols; ++col) { | ||
50 | DestroyWidget(&table->cells[row][col].child); | ||
51 | } | ||
52 | free(table->cells[row]); | ||
53 | } | ||
54 | free(table->cells); | ||
55 | table->cells = 0; | ||
56 | } | ||
57 | table->rows = 0; | ||
58 | |||
59 | // Clear row widths. | ||
60 | for (int i = 0; i < table->cols; ++i) { | ||
61 | table->widths[i] = 0; | ||
62 | } | ||
63 | |||
64 | table->offset = 0; | ||
65 | |||
66 | table->flags.vertical_overflow = 0; | ||
67 | } | ||
68 | |||
69 | void uiTableAddRow(uiTable* table, const char** row) { | ||
70 | assert(table); | ||
71 | |||
72 | table->rows++; | ||
73 | |||
74 | uiCell** cells = realloc(table->cells, table->rows * sizeof(uiCell*)); | ||
75 | ASSERT(cells); | ||
76 | table->cells = cells; | ||
77 | |||
78 | uiCell** pLastRow = GetLastRow(table); | ||
79 | *pLastRow = calloc(table->cols, sizeof(uiCell)); | ||
80 | ASSERT(*pLastRow); | ||
81 | uiCell* lastRow = *pLastRow; | ||
82 | |||
83 | for (int col = 0; col < table->cols; ++col) { | ||
84 | lastRow[col].child = (uiWidget*)uiMakeLabel(row[col]); | ||
85 | } | ||
86 | } | ||
87 | |||
88 | void uiTableSet(uiTable* table, int row, int col, uiPtr child) { | ||
89 | assert(table); | ||
90 | assert(child.widget); | ||
91 | |||
92 | GetCellMut(table, row, col)->child = child.widget; | ||
93 | } | ||
94 | |||
95 | const uiWidget* uiTableGet(const uiTable* table, int row, int col) { | ||
96 | assert(table); | ||
97 | return GetCell(table, row, col)->child; | ||
98 | } | ||
99 | |||
100 | uiWidget* uiTableGetMut(uiTable* table, int row, int col) { | ||
101 | assert(table); | ||
102 | return GetCellMut(table, row, col)->child; | ||
103 | } | ||