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/widget.h | 49 +++++++++++++++++++++++++++++++------------------ 1 file changed, 31 insertions(+), 18 deletions(-) (limited to 'src/widget/widget.h') diff --git a/src/widget/widget.h b/src/widget/widget.h index 63f3d94..db11164 100644 --- a/src/widget/widget.h +++ b/src/widget/widget.h @@ -16,39 +16,42 @@ typedef struct uiWidget { Widget_list children; } uiWidget; -/// Button. typedef struct uiButton { uiWidget widget; string text; } uiButton; -/// Frame. typedef struct uiFrame { uiWidget widget; } uiFrame; -/// Label. typedef struct uiLabel { uiWidget widget; string text; } uiLabel; -/// Table cell. +typedef struct uiScrollbar { + int width; + int height; // Total height: handle plus scrollable area. + int handle_height; // Height of the scroll handle. + int handle_y; // Starting y-coordinate of the handle. +} uiScrollbar; + typedef struct uiCell { string text; } uiCell; -/// Table. typedef struct uiTable { - uiWidget widget; - int rows; - int cols; - int height; // Height in pixels. - int* widths; // Width, in pixels, for each column. - uiCell* header; // If non-null, row of 'cols' header cells. - uiCell** cells; // Array of 'rows' rows, each of 'cols' cells. - int offset; // Offset into the rows of the table. Units: rows. - int num_visible_rows; // The number of rows that are visible at once. + uiWidget widget; + int rows; + int cols; + int height; // Height in pixels. + int* widths; // Width, in pixels, for each column. + uiCell* header; // If non-null, row of 'cols' header cells. + uiCell** cells; // Array of 'rows' rows, each of 'cols' cells. + int offset; // Offset into the rows of the table. Units: rows. + int num_visible_rows; // The number of rows that are visible at once. + uiScrollbar scrollbar; struct { bool vertical_overflow : 1; // True if contents overflow vertically. } flags; @@ -56,14 +59,24 @@ typedef struct uiTable { void DestroyWidget(uiWidget** ppWidget); -const uiCell* GetCell(const uiTable* table, int row, int col); -uiCell* GetCellMut(uiTable* table, int row, int col); -uiCell** GetLastRow(uiTable* table); +/// Set the scrollbar handle's y-coordinate, which is clipped to the scrollbar's +/// rectangle. +/// +/// Return the handle's y-coordinate after clipping. +int ScrollbarScroll(uiScrollbar*, int y); -#define UI_NEW(TYPE) (TYPE*)uiAlloc(1, sizeof(TYPE)) +const uiCell* TableGetCell(const uiTable*, int row, int col); +uiCell* TableGetCellMut(uiTable*, int row, int col); +uiCell** TableGetLastRow(uiTable*); +void SyncScrollbarToTable(uiTable*); +void SyncTableToScrollbar(uiTable*); + +static inline int Min(int a, int b) { return a < b ? a : b; } +static inline int Max(int a, int b) { return a > b ? a : b; } static inline void* uiAlloc(size_t count, size_t size) { void* mem = calloc(count, size); ASSERT(mem); return mem; } +#define UI_NEW(TYPE) (TYPE*)uiAlloc(1, sizeof(TYPE)) -- cgit v1.2.3