summaryrefslogtreecommitdiff
path: root/src/widget/table.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/widget/table.c')
-rw-r--r--src/widget/table.c58
1 files changed, 40 insertions, 18 deletions
diff --git a/src/widget/table.c b/src/widget/table.c
index 76a0413..e7d412e 100644
--- a/src/widget/table.c
+++ b/src/widget/table.c
@@ -1,20 +1,7 @@
1#include "widget.h" 1#include "widget.h"
2 2
3const uiCell* GetCell(const uiTable* table, int row, int col) { 3#define Min(a, b) ((a) < (b) ? (a) : (b))
4 assert(table); 4#define Max(a, b) ((a) > (b) ? (a) : (b))
5 return &table->cells[row][col];
6}
7
8uiCell* GetCellMut(uiTable* table, int row, int col) {
9 assert(table);
10 return (uiCell*)GetCell(table, row, col);
11}
12
13uiCell** GetLastRow(uiTable* table) {
14 assert(table);
15 assert(table->rows > 0);
16 return &table->cells[table->rows - 1];
17}
18 5
19uiTable* uiMakeTable(int rows, int cols, const char** header) { 6uiTable* uiMakeTable(int rows, int cols, const char** header) {
20 uiTable* table = UI_NEW(uiTable); 7 uiTable* table = UI_NEW(uiTable);
@@ -73,7 +60,7 @@ void uiTableAddRow(uiTable* table, const char** row) {
73 ASSERT(cells); 60 ASSERT(cells);
74 table->cells = cells; 61 table->cells = cells;
75 62
76 uiCell** pLastRow = GetLastRow(table); 63 uiCell** pLastRow = TableGetLastRow(table);
77 *pLastRow = calloc(table->cols, sizeof(uiCell)); 64 *pLastRow = calloc(table->cols, sizeof(uiCell));
78 ASSERT(*pLastRow); 65 ASSERT(*pLastRow);
79 uiCell* lastRow = *pLastRow; 66 uiCell* lastRow = *pLastRow;
@@ -86,10 +73,45 @@ void uiTableAddRow(uiTable* table, const char** row) {
86void uiTableSet(uiTable* table, int row, int col, const char* text) { 73void uiTableSet(uiTable* table, int row, int col, const char* text) {
87 assert(table); 74 assert(table);
88 assert(text); 75 assert(text);
89 GetCellMut(table, row, col)->text = string_new(text); 76 TableGetCellMut(table, row, col)->text = string_new(text);
90} 77}
91 78
92const char* uiTableGet(const uiTable* table, int row, int col) { 79const char* uiTableGet(const uiTable* table, int row, int col) {
93 assert(table); 80 assert(table);
94 return string_data(GetCell(table, row, col)->text); 81 return string_data(TableGetCell(table, row, col)->text);
82}
83
84void uiTableScroll(uiTable* table, int row) {
85 assert(table);
86 table->offset = Min(table->rows - table->num_visible_rows, Max(0, row));
87 SyncScrollbarToTable(table);
88}
89
90void SyncScrollbarToTable(uiTable* table) {
91 assert(table);
92 ScrollbarScroll(
93 &table->scrollbar, (int)((double)table->offset / (double)table->rows *
94 (double)table->height));
95}
96
97void SyncTableToScrollbar(uiTable* table) {
98 assert(table);
99 table->offset = (int)((double)table->scrollbar.handle_y /
100 (double)table->height * (double)table->rows);
101}
102
103const uiCell* TableGetCell(const uiTable* table, int row, int col) {
104 assert(table);
105 return &table->cells[row][col];
106}
107
108uiCell* TableGetCellMut(uiTable* table, int row, int col) {
109 assert(table);
110 return (uiCell*)TableGetCell(table, row, col);
111}
112
113uiCell** TableGetLastRow(uiTable* table) {
114 assert(table);
115 assert(table->rows > 0);
116 return &table->cells[table->rows - 1];
95} 117}