From 166f7a2fe015dda37b3b1fa7a7070189493cde05 Mon Sep 17 00:00:00 2001 From: Jens Arnold Date: Sun, 16 Jan 2005 00:14:55 +0000 Subject: scrollbar(): Code size optimisation; fixed overflow with large parameters (fixes seek position display in video.rock with large files). git-svn-id: svn://svn.rockbox.org/rockbox/trunk@5566 a1c6a512-1295-4272-9138-f99709370657 --- apps/recorder/widgets.c | 55 +++++++++++++++++++++++-------------------------- 1 file changed, 26 insertions(+), 29 deletions(-) (limited to 'apps/recorder') diff --git a/apps/recorder/widgets.c b/apps/recorder/widgets.c index 7ac647024a..5c608bcb6b 100644 --- a/apps/recorder/widgets.c +++ b/apps/recorder/widgets.c @@ -17,6 +17,7 @@ * ****************************************************************************/ #include +#include #include "widgets.h" @@ -42,6 +43,7 @@ void scrollbar(int x, int y, int width, int height, int items, int min_shown, { int min; int max; + int inner_len; int start; int size; @@ -82,42 +84,37 @@ void scrollbar(int x, int y, int width, int height, int items, int min_shown, if(max > items) max = items; + if (orientation == VERTICAL) + inner_len = height - 2; + else + inner_len = width - 2; + + /* avoid overflows */ + while (items > (INT_MAX / inner_len)) { + items >>= 1; + min >>= 1; + max >>= 1; + } + /* calc start and end of the knob */ if(items > 0 && items > (max - min)) { - if(orientation == VERTICAL) { - size = (height - 2) * (max - min) / items; - start = (height - 2 - size) * min / (items - (max - min)); - } - else { - size = (width - 2) * (max - min) / items; - start = (width - 2 - size) * min / (items - (max - min)); - } + size = inner_len * (max - min) / items; + start = (inner_len - size) * min / (items - (max - min)); } else { /* if null draw a full bar */ + size = inner_len; start = 0; - if(orientation == VERTICAL) - size = (height - 2); - else - size = (width - 2); } - - /* knob has a width */ - if(size != 0) { - if(orientation == VERTICAL) - lcd_fillrect(x + 1, y + start + 1, width - 2, size); - else - lcd_fillrect(x + start + 1, y + 1, size, height - 2); - } - else { /* width of knob is null */ - if(orientation == VERTICAL) { - start = (height - 2 - 1) * min / items; - lcd_fillrect(x + 1, y + start + 1, width - 2, 1); - } - else { - start = (width - 2 - 1) * min / items; - lcd_fillrect(x + start + 1, y + 1, 1, height - 2); - } + /* width of knob is null */ + if(size == 0) { + start = (inner_len - 1) * min / items; + size = 1; } + + if(orientation == VERTICAL) + lcd_fillrect(x + 1, y + start + 1, width - 2, size); + else + lcd_fillrect(x + start + 1, y + 1, size, height - 2); } /* -- cgit v1.2.3