From 1c9fbd0059d93b6dd57837020f3ef951a729acbb Mon Sep 17 00:00:00 2001 From: salmonik Date: Tue, 28 Apr 2026 17:51:33 +0200 Subject: scrollbar + scroll on mousescroll --- x.c | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) (limited to 'x.c') diff --git a/x.c b/x.c index 8c9a74e..9c0ddc5 100644 --- a/x.c +++ b/x.c @@ -749,7 +749,7 @@ cresize(int width, int height) if (height != 0) win.h = height; - col = (win.w - 2 * borderpx) / win.cw; + col = (win.w - 2 * borderpx - scrollbarwidth) / win.cw; row = (win.h - 2 * borderpx) / win.ch; col = MAX(1, col); row = MAX(1, row); @@ -1750,6 +1750,35 @@ xdrawline(Line line, int x1, int y1, int x2) xdrawglyphfontspecs(specs, base, i, ox, y1); } +void +xdrawscrollbar(int offset, int total, int visible) +{ + int sb_width = scrollbarwidth; + int sb_x = win.w - sb_width; + int max_off = total - visible; + + if (max_off <= 0 || sb_width == 0) + return; + + /* draw scrollbar background - full height from top to bottom */ + if (scrollbarbg >= 0) { + XSetForeground(xw.dpy, dc.gc, dc.col[scrollbarbg].pixel); + XFillRectangle(xw.dpy, xw.buf, dc.gc, sb_x, 0, + sb_width, win.h); + } + + /* calculate thumb position and height */ + int sb_height = win.h; + int thumb_h = MAX(2, ((float)visible / total) * sb_height); + int thumb_y = ((float)(max_off - offset) / max_off) * + (sb_height - thumb_h); + + /* draw scrollbar thumb */ + XSetForeground(xw.dpy, dc.gc, dc.col[scrollbarcolor].pixel); + XFillRectangle(xw.dpy, xw.buf, dc.gc, sb_x, + thumb_y, sb_width, thumb_h); +} + void xfinishdraw(void) { -- cgit v1.2.3