diff options
| author | 3gg <3gg@shellblade.net> | 2026-03-12 15:29:23 -0700 |
|---|---|---|
| committer | 3gg <3gg@shellblade.net> | 2026-03-12 15:29:23 -0700 |
| commit | 92978a10576d52a0f6c9983d3b6afae7c40eff40 (patch) | |
| tree | bf73faed8aa1ecd71b9f61c37a549faf4cd30372 /src/widget/table.c | |
| parent | 58c0f40df5947b3933bf7b6564b2ba5dc39fbd92 (diff) | |
Support scrolling by dragging scrollbars
Diffstat (limited to 'src/widget/table.c')
| -rw-r--r-- | src/widget/table.c | 58 |
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 | ||
| 3 | const 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 | |||
| 8 | uiCell* GetCellMut(uiTable* table, int row, int col) { | ||
| 9 | assert(table); | ||
| 10 | return (uiCell*)GetCell(table, row, col); | ||
| 11 | } | ||
| 12 | |||
| 13 | uiCell** GetLastRow(uiTable* table) { | ||
| 14 | assert(table); | ||
| 15 | assert(table->rows > 0); | ||
| 16 | return &table->cells[table->rows - 1]; | ||
| 17 | } | ||
| 18 | 5 | ||
| 19 | uiTable* uiMakeTable(int rows, int cols, const char** header) { | 6 | uiTable* 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) { | |||
| 86 | void uiTableSet(uiTable* table, int row, int col, const char* text) { | 73 | void 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 | ||
| 92 | const char* uiTableGet(const uiTable* table, int row, int col) { | 79 | const 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 | |||
| 84 | void 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 | |||
| 90 | void SyncScrollbarToTable(uiTable* table) { | ||
| 91 | assert(table); | ||
| 92 | ScrollbarScroll( | ||
| 93 | &table->scrollbar, (int)((double)table->offset / (double)table->rows * | ||
| 94 | (double)table->height)); | ||
| 95 | } | ||
| 96 | |||
| 97 | void SyncTableToScrollbar(uiTable* table) { | ||
| 98 | assert(table); | ||
| 99 | table->offset = (int)((double)table->scrollbar.handle_y / | ||
| 100 | (double)table->height * (double)table->rows); | ||
| 101 | } | ||
| 102 | |||
| 103 | const uiCell* TableGetCell(const uiTable* table, int row, int col) { | ||
| 104 | assert(table); | ||
| 105 | return &table->cells[row][col]; | ||
| 106 | } | ||
| 107 | |||
| 108 | uiCell* TableGetCellMut(uiTable* table, int row, int col) { | ||
| 109 | assert(table); | ||
| 110 | return (uiCell*)TableGetCell(table, row, col); | ||
| 111 | } | ||
| 112 | |||
| 113 | uiCell** TableGetLastRow(uiTable* table) { | ||
| 114 | assert(table); | ||
| 115 | assert(table->rows > 0); | ||
| 116 | return &table->cells[table->rows - 1]; | ||
| 95 | } | 117 | } |
