From 92978a10576d52a0f6c9983d3b6afae7c40eff40 Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Thu, 12 Mar 2026 15:29:23 -0700 Subject: Support scrolling by dragging scrollbars --- src/widget/table.c | 58 +++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 40 insertions(+), 18 deletions(-) (limited to 'src/widget/table.c') 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 @@ #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]; -} +#define Min(a, b) ((a) < (b) ? (a) : (b)) +#define Max(a, b) ((a) > (b) ? (a) : (b)) uiTable* uiMakeTable(int rows, int cols, const char** header) { uiTable* table = UI_NEW(uiTable); @@ -73,7 +60,7 @@ void uiTableAddRow(uiTable* table, const char** row) { ASSERT(cells); table->cells = cells; - uiCell** pLastRow = GetLastRow(table); + uiCell** pLastRow = TableGetLastRow(table); *pLastRow = calloc(table->cols, sizeof(uiCell)); ASSERT(*pLastRow); uiCell* lastRow = *pLastRow; @@ -86,10 +73,45 @@ void uiTableAddRow(uiTable* table, const char** row) { void uiTableSet(uiTable* table, int row, int col, const char* text) { assert(table); assert(text); - GetCellMut(table, row, col)->text = string_new(text); + TableGetCellMut(table, row, col)->text = string_new(text); } const char* uiTableGet(const uiTable* table, int row, int col) { assert(table); - return string_data(GetCell(table, row, col)->text); + return string_data(TableGetCell(table, row, col)->text); +} + +void uiTableScroll(uiTable* table, int row) { + assert(table); + table->offset = Min(table->rows - table->num_visible_rows, Max(0, row)); + SyncScrollbarToTable(table); +} + +void SyncScrollbarToTable(uiTable* table) { + assert(table); + ScrollbarScroll( + &table->scrollbar, (int)((double)table->offset / (double)table->rows * + (double)table->height)); +} + +void SyncTableToScrollbar(uiTable* table) { + assert(table); + table->offset = (int)((double)table->scrollbar.handle_y / + (double)table->height * (double)table->rows); +} + +const uiCell* TableGetCell(const uiTable* table, int row, int col) { + assert(table); + return &table->cells[row][col]; +} + +uiCell* TableGetCellMut(uiTable* table, int row, int col) { + assert(table); + return (uiCell*)TableGetCell(table, row, col); +} + +uiCell** TableGetLastRow(uiTable* table) { + assert(table); + assert(table->rows > 0); + return &table->cells[table->rows - 1]; } -- cgit v1.2.3