diff --git a/ChangeLog b/ChangeLog index 55c3860b4..f54b43217 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,15 @@ +2011-07-12 Eric Wasylishen + + * Source/GSGuiPrivate.h: New rounding rounding function + GSRoundTowardsInfinity which is simply floor(x+0.5) + * Source/NSBrowser.m: + * Source/NSSliderCell.m: + * Source/NSButtonCell.m: + * Source/NSClipView.m: + * Source/NSOutlineView.m: + * Source/NSRulerView.m: + Switch rint[f] to GSRoundTowardsInfinity + 2011-07-11 Eric Wasylishen * Source/GSGhostscriptImageRep.m: Only invoke 'which' once. diff --git a/Source/GSGuiPrivate.h b/Source/GSGuiPrivate.h index dd97a3a97..95484beca 100644 --- a/Source/GSGuiPrivate.h +++ b/Source/GSGuiPrivate.h @@ -32,6 +32,7 @@ #import #include "GNUstepBase/preface.h" +#include /* * Return the gnustep-gui bundle used to load gnustep-gui resources. @@ -71,5 +72,21 @@ static inline NSString *GSGuiLocalizedString (NSString *key, NSString *comment) #endif #define NSLocalizedString(key,comment) GSGuiLocalizedString (key, comment) +/** + * Rounds to the nearest integer, and in the case of ties, round to the + * larger integer. This is the recommended rounding function for rounding + * graphics points. + * + * For example: + * GSRoundTowardsInfinity(0.8) == 1.0 + * GSRoundTowardsInfinity(0.5) == 1.0 + * GSRoundTowardsInfinity(0.1) == 0.0 + * GSRoundTowardsInfinity(-2.5) == -2.0 + */ +static inline CGFloat GSRoundTowardsInfinity(CGFloat x) +{ + return floor(x + 0.5); +} + #endif /* _GNUstep_H_GSGuiPrivate */ diff --git a/Source/NSBrowser.m b/Source/NSBrowser.m index e38ce1b71..aa9e1af8c 100644 --- a/Source/NSBrowser.m +++ b/Source/NSBrowser.m @@ -56,7 +56,7 @@ #import "AppKit/NSBezierPath.h" #import "GNUstepGUI/GSTheme.h" -DEFINE_RINT_IF_MISSING +#import "GSGuiPrivate.h" /* Cache */ static float scrollerWidth; // == [NSScroller scrollerWidth] @@ -1667,7 +1667,7 @@ static NSTextFieldCell *titleCell; { float f = [sender floatValue]; - [self scrollColumnToVisible: rintf(f * _lastColumnLoaded)]; + [self scrollColumnToVisible: GSRoundTowardsInfinity(f * _lastColumnLoaded)]; } break; diff --git a/Source/NSButtonCell.m b/Source/NSButtonCell.m index b0e55667b..8d1147fe5 100644 --- a/Source/NSButtonCell.m +++ b/Source/NSButtonCell.m @@ -59,8 +59,6 @@ #include -DEFINE_RINT_IF_MISSING - typedef struct _GSButtonCellFlags { #if GS_WORDS_BIGENDIAN == 1 @@ -969,7 +967,7 @@ typedef struct _GSButtonCellFlags { position = [controlView convertPointToBase: position]; } - position = NSMakePoint(rint(position.x), rint(position.y)); + position = NSMakePoint(GSRoundTowardsInfinity(position.x), GSRoundTowardsInfinity(position.y)); if (controlView) { position = [controlView convertPointFromBase: position]; diff --git a/Source/NSClipView.m b/Source/NSClipView.m index 6bf8c7fb3..5e0a87ed4 100644 --- a/Source/NSClipView.m +++ b/Source/NSClipView.m @@ -44,8 +44,6 @@ #include -DEFINE_RINT_IF_MISSING - @interface NSClipView (Private) - (void) _scrollToPoint: (NSPoint)aPoint; @end @@ -444,8 +442,8 @@ static inline NSRect integralRect (NSRect rect, NSView *view) */ if (_copiesOnScroll) { - new.x = _bounds.origin.x + (rint(new.x - _bounds.origin.x)); - new.y = _bounds.origin.y + (rint(new.y - _bounds.origin.y)); + new.x = _bounds.origin.x + (GSRoundTowardsInfinity(new.x - _bounds.origin.x)); + new.y = _bounds.origin.y + (GSRoundTowardsInfinity(new.y - _bounds.origin.y)); } return new; diff --git a/Source/NSOutlineView.m b/Source/NSOutlineView.m index a698647ba..dd296d7ae 100644 --- a/Source/NSOutlineView.m +++ b/Source/NSOutlineView.m @@ -59,6 +59,7 @@ #import "AppKit/NSTextFieldCell.h" #import "AppKit/NSWindow.h" +#import "GSGuiPrivate.h" #include static NSNotificationCenter *nc = nil; @@ -1282,9 +1283,9 @@ Also returns the child index relative to this parent. */ /* _bounds.origin is (0, 0) when the outline view is not clipped. * When the view is scrolled, _bounds.origin.y returns the scrolled height. */ verticalQuarterPosition = - rint(((p.y + _bounds.origin.y) / _rowHeight) * 4.); + GSRoundTowardsInfinity(((p.y + _bounds.origin.y) / _rowHeight) * 4.); horizontalHalfPosition = - rint(((p.x + _bounds.origin.y) / _indentationPerLevel) * 2.); + GSRoundTowardsInfinity(((p.x + _bounds.origin.y) / _indentationPerLevel) * 2.); /* We add an extra quarter to shift the insertion row below the hovered row. */ row = (verticalQuarterPosition + 1) / 4; @@ -1323,7 +1324,7 @@ Also returns the child index relative to this parent. */ { NSInteger minInsertionLevel = levelAfter; NSInteger maxInsertionLevel = levelBefore; - NSInteger pointerInsertionLevel = rint((float)horizontalHalfPosition / 2.); + NSInteger pointerInsertionLevel = GSRoundTowardsInfinity((float)horizontalHalfPosition / 2.); /* Save positions to avoid executing this code when the general * position of the mouse is unchanged. diff --git a/Source/NSRulerView.m b/Source/NSRulerView.m index 73b334fda..0b7554cac 100644 --- a/Source/NSRulerView.m +++ b/Source/NSRulerView.m @@ -42,8 +42,7 @@ #import "AppKit/NSRulerView.h" #import "AppKit/NSScrollView.h" #import "AppKit/NSStringDrawing.h" - -DEFINE_RINT_IF_MISSING +#import "GSGuiPrivate.h" #define MIN_LABEL_DISTANCE 40 #define MIN_MARK_DISTANCE 5 @@ -581,9 +580,9 @@ static NSMutableDictionary *units = nil; } /* calculate number of small marks in each bigger mark */ - _marksToMidMark = rint([self _stepForIndex: convIndex + 1]); + _marksToMidMark = GSRoundTowardsInfinity([self _stepForIndex: convIndex + 1]); _marksToBigMark = _marksToMidMark - * rint([self _stepForIndex: convIndex + 2]); + * GSRoundTowardsInfinity([self _stepForIndex: convIndex + 2]); /* Calculate distance between labels. It must not be less than MIN_LABEL_DISTANCE. */ @@ -595,7 +594,7 @@ static NSMutableDictionary *units = nil; } /* number of small marks between two labels */ - _marksToLabel = rint(_labelDistance / _markDistance); + _marksToLabel = GSRoundTowardsInfinity(_labelDistance / _markDistance); /* format of labels */ if (_labelDistance / _unitToRuler >= 1) diff --git a/Source/NSSliderCell.m b/Source/NSSliderCell.m index c56d99215..6dc631676 100644 --- a/Source/NSSliderCell.m +++ b/Source/NSSliderCell.m @@ -44,7 +44,8 @@ #import "AppKit/NSWindow.h" #import -DEFINE_RINT_IF_MISSING +#import "GSGuiPrivate.h" + #ifndef HAVE_ATAN2F #define atan2f atan2 #endif @@ -689,7 +690,7 @@ float _floatValueForMousePoint (NSPoint point, NSRect knobRect, d = _maxValue - _minValue; f = ((aValue - _minValue) * (effectiveTicks - 1)) / d; - f = ((rint(f) * d) / (effectiveTicks - 1)) + _minValue; + f = ((GSRoundTowardsInfinity(f) * d) / (effectiveTicks - 1)) + _minValue; /* never return the maximum value, tested on Apple */ if (_type == NSCircularSlider && (f >= _maxValue))