From d88f4d22e011deedc5e9e6f10913b161a05c7795 Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Tue, 25 Dec 2018 22:19:59 -0500 Subject: [PATCH] added basic mouse handling and scrollbar to picker --- src/pick.c | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/src/pick.c b/src/pick.c index 5f59fe4..1850a94 100644 --- a/src/pick.c +++ b/src/pick.c @@ -3,6 +3,7 @@ #include #include +#define INCLUDE_DEFS #include "config.h" typedef struct { @@ -135,12 +136,22 @@ static void xkeypress(XConf* x, XEvent* e) { } else if (key >= 0x20 && key <= 0x7F) { if (QueryIdx < sizeof(Query)-1) Query[QueryIdx++] = key; + Offset = ChoiceIdx = 0; } score(); } static void xbtnpress(XConf* x, XEvent* e) { - (void)x, (void)e; + (void)x; + if (e->xbutton.button == Button1) { + int starty = x->font->height + 4; + e->xbutton.y = (e->xbutton.y < starty ? starty : e->xbutton.y - starty); + ChoiceIdx = Offset + (e->xbutton.y / x->font->height); + } else if (e->xbutton.button == Button4) { + if (Offset > 0) Offset--; + } else if (e->xbutton.button == Button5) { + if (Offset < vec_size(&Choices)) Offset++; + } } static void xresize(XConf* x, XEvent* e) { @@ -155,9 +166,16 @@ static void xresize(XConf* x, XEvent* e) { static void redraw(XConf* x) { /* draw the background colors and border */ size_t fheight = x->font->height; + size_t nlines = ((x->height - x->font->height - 4) / x->font->height) - 1; + size_t scroll_height = x->height - fheight - 4; + size_t start = (size_t)((float)Offset / (float)vec_size(&Choices) * scroll_height); + size_t size = (size_t)((float)nlines / (float)vec_size(&Choices) * scroll_height); + x11_draw_rect(x, Palette[EditBg], 0, 0, x->width, x->height); x11_draw_rect(x, Palette[TagsBg], 0, 0, x->width, fheight + 4); x11_draw_rect(x, Palette[HorBdr], 0, fheight + 4, x->width, 1); + x11_draw_rect(x, Palette[ScrollBg], 0, fheight + 5, ScrollWidth+1, scroll_height); + x11_draw_rect(x, Palette[ScrollFg], 0, fheight + 5 + start, ScrollWidth, (size ? size : 5)); /* get size of the query when printed */ XGlyphInfo glyphinfo; @@ -168,19 +186,16 @@ static void redraw(XConf* x) { /* draw the query and the cursor to the query region */ int posx = 2 + glyphinfo.width + offset; - x11_draw_rect(x, Palette[TagsCsr], posx-1, 2, 3, 3); - x11_draw_rect(x, Palette[TagsCsr], posx, 2, 1, fheight); + x11_draw_rect(x, Palette[TagsCsr], posx-1, 2, 3, 3); + x11_draw_rect(x, Palette[TagsCsr], posx, 2, 1, fheight); x11_draw_rect(x, Palette[TagsCsr], posx-1, 2+fheight-3, 3, 3); x11_draw_string(x, x->font, offset + 2, fheight, Palette[TagsFg], Query); /* draw the scored and sorted results */ - size_t nlines = ((x->height - x->font->height - 4) / x->font->height) - 1; for (int i = Offset, y = 2 * fheight + 4; ((size_t)i < vec_size(&Choices)) && ((size_t)i <= Offset+nlines); i++, y += fheight) { - bool selected = ((size_t)i == ChoiceIdx); - Choice* choice = (Choice*)vec_at(&Choices, i); - if (selected) - x11_draw_rect(x, Palette[EditSel], 0, y - x->font->ascent, x->width, fheight); - x11_draw_string(x, x->font, 2, y, Palette[TagsFg], choice->string); + if ((size_t)i == ChoiceIdx) + x11_draw_rect(x, Palette[EditSel], ScrollWidth+3, y - x->font->ascent, x->width, fheight); + x11_draw_string(x, x->font, ScrollWidth+3, y, Palette[TagsFg], ((Choice*)vec_at(&Choices, i))->string); } x11_flip(x); } -- 2.49.0