From 380697b895e4e96a32b11d84a70afc3c47b0f370 Mon Sep 17 00:00:00 2001 From: Dave Davenport Date: Fri, 4 Sep 2015 08:26:57 +0200 Subject: [PATCH] scrollbar: Handle mouse clicks, fix width, remove border. --- include/scrollbar.h | 1 + source/rofi.c | 9 +++++++-- source/scrollbar.c | 17 ++++++++++++----- 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/include/scrollbar.h b/include/scrollbar.h index 37a4663f..afc6a718 100644 --- a/include/scrollbar.h +++ b/include/scrollbar.h @@ -21,4 +21,5 @@ void scrollbar_set_pos_length ( scrollbar *sb, unsigned int pos_length ); void scrollbar_set_pos ( scrollbar *sb, unsigned int pos ); void scrollbar_set_length ( scrollbar *sb, unsigned int length ); void scrollbar_draw ( scrollbar *sb ); +unsigned int scrollbar_clicked ( scrollbar *sb, int y ); #endif // ROFI_SCROLLBAR_H diff --git a/source/rofi.c b/source/rofi.c index 778b50f9..c5cd25cb 100644 --- a/source/rofi.c +++ b/source/rofi.c @@ -662,6 +662,11 @@ static void menu_mouse_navigation ( MenuState *state, XButtonEvent *xbe ) return; } else { + if ( state->scrollbar && state->scrollbar->window == xbe->window ) { + state->selected = scrollbar_clicked ( state->scrollbar, xbe->y ); + state->update = TRUE; + return; + } for ( unsigned int i = 0; config.sidebar_mode == TRUE && i < num_switchers; i++ ) { if ( switchers[i]->tb->window == ( xbe->window ) ) { *( state->selected_line ) = 0; @@ -1017,8 +1022,8 @@ MenuReturn menu ( char **lines, unsigned int num_lines, char **input, char *prom x_offset, y_offset, state.element_width, element_height, NORMAL, "" ); } - state.scrollbar = scrollbar_create ( main_window, &vinfo, map, state.w - config.padding - 10, state.top_offset, - 10, ( state.max_rows - 1 ) * ( element_height + config.line_margin ) + element_height - 2 ); + state.scrollbar = scrollbar_create ( main_window, &vinfo, map, state.w - config.padding - 8, state.top_offset, + 8, ( state.max_rows - 1 ) * ( element_height + config.line_margin ) + element_height ); scrollbar_set_length ( state.scrollbar, state.num_lines ); diff --git a/source/scrollbar.c b/source/scrollbar.c index 977aa1a0..bec570b9 100644 --- a/source/scrollbar.c +++ b/source/scrollbar.c @@ -37,8 +37,6 @@ extern Display *display; -#define SCROLLBAR_WIDTH 10 - scrollbar *scrollbar_create ( Window parent, XVisualInfo *vinfo, Colormap map, short x, short y, short w, short h ) { @@ -50,7 +48,7 @@ scrollbar *scrollbar_create ( Window parent, XVisualInfo *vinfo, Colormap map, sb->w = MAX ( 1, w ); sb->h = MAX ( 1, h ); - sb->length = SCROLLBAR_WIDTH; + sb->length = 10; sb->pos = 0; sb->pos_length = 4; @@ -58,9 +56,10 @@ scrollbar *scrollbar_create ( Window parent, XVisualInfo *vinfo, Colormap map, attr.colormap = map; attr.border_pixel = color_border ( display ); attr.background_pixel = color_background ( display ); - sb->window = XCreateWindow ( display, sb->parent, sb->x, sb->y, sb->w, sb->h, 1, vinfo->depth, + sb->window = XCreateWindow ( display, sb->parent, sb->x, sb->y, sb->w, sb->h, 0, vinfo->depth, InputOutput, vinfo->visual, CWColormap | CWBorderPixel | CWBackPixel, &attr ); + XSelectInput ( display, sb->window, ExposureMask | ButtonPressMask ); sb->gc = XCreateGC ( display, sb->window, 0, 0 ); XSetForeground ( display, sb->gc, color_separator ( display ) ); //XSetFillStyle ( display, sb->gc, FillSolid); @@ -116,5 +115,13 @@ void scrollbar_draw ( scrollbar *sb ) // Redraw base window XClearWindow ( display, sb->window ); // Paint the handle. - XFillRectangle ( display, sb->window, sb->gc, 1, y, SCROLLBAR_WIDTH - 2, height ); + XFillRectangle ( display, sb->window, sb->gc, 1, y, sb->w, height ); +} + +unsigned int scrollbar_clicked ( scrollbar *sb, int y ) +{ + const short bh = sb->h - 2; + float sec = ( ( bh ) / (float) sb->length ); + unsigned int sel = y / sec; + return sel; }