diff --git a/include/widgets/scrollbar.h b/include/widgets/scrollbar.h index c58ab5de..7bf97cc1 100644 --- a/include/widgets/scrollbar.h +++ b/include/widgets/scrollbar.h @@ -88,7 +88,7 @@ void scrollbar_set_max_value ( scrollbar *sb, unsigned int max ); * * Calculate the position of the click relative to the max value of bar */ -unsigned int scrollbar_scroll ( const scrollbar *sb, int y ); +guint scrollbar_scroll_get_line ( const scrollbar *sb, int y ); /*@}*/ #endif // ROFI_SCROLLBAR_H diff --git a/source/widgets/scrollbar.c b/source/widgets/scrollbar.c index b21066f7..f7d54877 100644 --- a/source/widgets/scrollbar.c +++ b/source/widgets/scrollbar.c @@ -46,22 +46,26 @@ static int scrollbar_get_desired_height ( widget *wid ) // TODO // This should behave more like a real scrollbar. -unsigned int scrollbar_scroll ( const scrollbar *sb, int y ) +guint scrollbar_scroll_get_line ( const scrollbar *sb, int y ) { - if ( sb != NULL ) { - if ( y >= sb->widget.y && y <= ( sb->widget.y + sb->widget.h ) ) { - short r = ( sb->length * sb->widget.h ) / ( (double) ( sb->length + sb->pos_length ) ); - short handle = sb->widget.h - r; - double sec = ( ( r ) / (double) ( sb->length - 1 ) ); - short half_handle = handle / 2; - y -= sb->widget.y + half_handle; - y = MIN ( MAX ( 0, y ), sb->widget.h - 2 * half_handle ); - - unsigned int sel = ( ( y ) / sec ); - return MIN ( sel, sb->length - 1 ); - } + y -= sb->widget.border.top.distance; + if ( y < 0 ) { + return 0; } - return 0; + + if ( y > sb->widget.h ) { + return sb->length - 1; + } + + short r = ( sb->length * sb->widget.h ) / ( (double) ( sb->length + sb->pos_length ) ); + short handle = sb->widget.h - r; + double sec = ( ( r ) / (double) ( sb->length - 1 ) ); + short half_handle = handle / 2; + y -= half_handle; + y = MIN ( MAX ( 0, y ), sb->widget.h - 2 * half_handle ); + + unsigned int sel = ( ( y ) / sec ); + return MIN ( sel, sb->length - 1 ); } static gboolean scrollbar_trigger_action ( widget *wid, MouseBindingMouseDefaultAction action, G_GNUC_UNUSED gint x, gint y, G_GNUC_UNUSED void *user_data ) diff --git a/test/scrollbar-test.c b/test/scrollbar-test.c index 5a1ae3f8..bd3881f0 100644 --- a/test/scrollbar-test.c +++ b/test/scrollbar-test.c @@ -103,22 +103,22 @@ int main ( G_GNUC_UNUSED int argc, G_GNUC_UNUSED char **argv ) TASSERTE ( sb->pos_length, 1 ); - unsigned int cl = scrollbar_scroll ( sb, 10 ); + guint cl = scrollbar_scroll_get_line ( sb, 10 ); TASSERTE ( cl, 1010); - cl = scrollbar_scroll ( sb, 20 ); + cl = scrollbar_scroll_get_line ( sb, 20 ); TASSERTE ( cl, 2020); - cl = scrollbar_scroll ( sb, 0 ); + cl = scrollbar_scroll_get_line ( sb, 0 ); TASSERTE ( cl, 0); - cl = scrollbar_scroll ( sb, 99 ); + cl = scrollbar_scroll_get_line ( sb, 99 ); TASSERTE ( cl, 9999); scrollbar_set_handle_length ( sb, 1000); - cl = scrollbar_scroll ( sb, 10 ); + cl = scrollbar_scroll_get_line ( sb, 10 ); TASSERTE ( cl, 555); - cl = scrollbar_scroll ( sb, 20 ); + cl = scrollbar_scroll_get_line ( sb, 20 ); TASSERTE ( cl, 1666); - cl = scrollbar_scroll ( sb, 0 ); + cl = scrollbar_scroll_get_line ( sb, 0 ); TASSERTE ( cl, 0); - cl = scrollbar_scroll ( sb, 99 ); + cl = scrollbar_scroll_get_line ( sb, 99 ); TASSERTE ( cl, 9999); widget_free( WIDGET (sb ) );