From 21b9d81d527f100e25a4c6dedaf423cdd8f6827c Mon Sep 17 00:00:00 2001 From: Pasi Pentikainen Date: Mon, 13 Feb 2012 19:59:23 +0200 Subject: Fix mouse wheel page-by-page scrolling on windows In windows, the page-by-page mouse wheel scrolling is configured with scroll lines value of -1, which maps to INT_MAX. The scroll calculations had an integer overflow issue which caused the wheel scrolling to scroll only downwards when configured with large enough value like this. Task-number: QTBUG-11336 Change-Id: Ib4440367ce2617f96797c1f8cc8ec9e6a2f8467c Reviewed-by: Friedemann Kleint Reviewed-by: Andrew Semenenko Reviewed-by: Shane Kearns --- src/gui/widgets/qabstractslider.cpp | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/gui/widgets/qabstractslider.cpp b/src/gui/widgets/qabstractslider.cpp index 17afa29..9919658 100644 --- a/src/gui/widgets/qabstractslider.cpp +++ b/src/gui/widgets/qabstractslider.cpp @@ -689,6 +689,16 @@ void QAbstractSlider::sliderChange(SliderChange) update(); } +/*! + \internal + + Truncate qreal to int without flipping on overflow. +*/ +static inline int clampScrollStep(qreal x) +{ + return int(qBound(qreal(INT_MIN), x, qreal(INT_MAX))); +} + bool QAbstractSliderPrivate::scrollByDelta(Qt::Orientation orientation, Qt::KeyboardModifiers modifiers, int delta) { Q_Q(QAbstractSlider); @@ -700,7 +710,7 @@ bool QAbstractSliderPrivate::scrollByDelta(Qt::Orientation orientation, Qt::Keyb if ((modifiers & Qt::ControlModifier) || (modifiers & Qt::ShiftModifier)) { // Scroll one page regardless of delta: - stepsToScroll = qBound(-pageStep, int(offset * pageStep), pageStep); + stepsToScroll = qBound(-pageStep, clampScrollStep(offset * pageStep), pageStep); offset_accumulated = 0; } else { // Calculate how many lines to scroll. Depending on what delta is (and @@ -718,14 +728,14 @@ bool QAbstractSliderPrivate::scrollByDelta(Qt::Orientation orientation, Qt::Keyb offset_accumulated += stepsToScrollF; #ifndef Q_WS_MAC // Don't scroll more than one page in any case: - stepsToScroll = qBound(-pageStep, int(offset_accumulated), pageStep); + stepsToScroll = qBound(-pageStep, clampScrollStep(offset_accumulated), pageStep); #else // Native UI-elements on Mac can scroll hundreds of lines at a time as // a result of acceleration. So keep the same behaviour in Qt, and // don't restrict stepsToScroll to certain maximum (pageStep): - stepsToScroll = int(offset_accumulated); + stepsToScroll = clampScrollStep(offset_accumulated); #endif - offset_accumulated -= int(offset_accumulated); + offset_accumulated -= clampScrollStep(offset_accumulated); if (stepsToScroll == 0) return false; } -- cgit v0.12