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 --- config.def.h | 16 ++++++++++++---- st.c | 1 + win.h | 1 + x.c | 31 ++++++++++++++++++++++++++++++- 4 files changed, 44 insertions(+), 5 deletions(-) diff --git a/config.def.h b/config.def.h index d7e1fe5..3daa8fc 100644 --- a/config.def.h +++ b/config.def.h @@ -8,6 +8,16 @@ static char *font = "Liberation Mono:pixelsize=14:antialias=true:autohint=true"; // slightly bigger font static int borderpx = 2; +/* + * scrollbar options + * scrollbarwidth: width of the scrollbar in pixels (0 to disable) + * scrollbarcolor: color of the scrollbar thumb + * scrollbarbg: color of the scrollbar background (use -1 for none) + */ +static int scrollbarwidth = 3; +static unsigned int scrollbarcolor = 8; +static int scrollbarbg = 257; + /* * What program is execed by st depends of these precedence rules: * 1: program passed with -e @@ -203,10 +213,8 @@ static uint forcemousemod = ShiftMask; static MouseShortcut mshortcuts[] = { /* mask button function argument release */ { XK_ANY_MOD, Button2, selpaste, {.i = 0}, 1 }, - { ShiftMask, Button4, ttysend, {.s = "\033[5;2~"} }, - { XK_ANY_MOD, Button4, ttysend, {.s = "\031"} }, - { ShiftMask, Button5, ttysend, {.s = "\033[6;2~"} }, - { XK_ANY_MOD, Button5, ttysend, {.s = "\005"} }, + { XK_ANY_MOD, Button4, kscrollup, {.i = 1} }, + { XK_ANY_MOD, Button5, kscrolldown, {.i = 1} }, }; /* Internal keyboard shortcuts. */ diff --git a/st.c b/st.c index 170a8a8..0f2ed03 100644 --- a/st.c +++ b/st.c @@ -2903,6 +2903,7 @@ draw(void) term.ocx, term.ocy, TLINE(term.ocy)[term.ocx]); term.ocx = cx; term.ocy = term.c.y; + xdrawscrollbar(TSCREEN.off, TSCREEN.size, term.row); xfinishdraw(); if (ocx != term.ocx || ocy != term.ocy) xximspot(term.ocx, term.ocy); diff --git a/win.h b/win.h index 6de960d..fbd3159 100644 --- a/win.h +++ b/win.h @@ -39,3 +39,4 @@ void xsetpointermotion(int); void xsetsel(char *); int xstartdraw(void); void xximspot(int, int); +void xdrawscrollbar(int, int, int); 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