NSGeometry: create NSEdgeInsets struct

Create the NSEdgeInsets struct in NSGeometry, introduced in OSX 10.7.
Add helper functions for creating these insets.

This struct is not used in Base. Rather, it is used as part of newer
APIs in GUI.
This commit is contained in:
Daniel Ferreira 2017-06-21 09:40:26 +10:00 committed by Ivan Vučica
parent 47ecf6a90b
commit a9df1b104a
3 changed files with 78 additions and 0 deletions

View file

@ -130,6 +130,24 @@ enum
*/
typedef NSUInteger NSRectEdge;
/**
<example>{
CGFloat top;
CGFloat left;
CGFloat bottom;
CGFloat right;
}</example>
<p>A description of the distance between the edges of two rectangles.</p> */
#if OS_API_VERSION(MAC_OS_X_VERSION_10_7, GS_API_LATEST)
typedef struct NSEdgeInsets {
CGFloat top;
CGFloat left;
CGFloat bottom;
CGFloat right;
} NSEdgeInsets;
#endif
/** Point at 0,0 */
static const NSPoint NSZeroPoint __attribute__((unused)) = {0.0,0.0};
/** Zero-size rectangle at 0,0 */
@ -137,6 +155,11 @@ static const NSRect NSZeroRect __attribute__((unused)) = {{0.0,0.0},{0.0,0.0}};
/** Zero size */
static const NSSize NSZeroSize __attribute__((unused)) = {0.0,0.0};
#if OS_API_VERSION(MAC_OS_X_VERSION_10_7, GS_API_LATEST)
/** Zero edge insets **/
static const NSEdgeInsets NSEdgeInsetsZero __attribute__((unused)) = {0.0,0.0,0.0,0.0};
#endif
/**** Function Prototypes ****************************************************/
/*
@ -200,6 +223,33 @@ NSMakeRect(CGFloat x, CGFloat y, CGFloat w, CGFloat h)
return rect;
}
/** Constructs NSEdgeInsets. **/
#if OS_API_VERSION(MAC_OS_X_VERSION_10_7, GS_API_LATEST)
GS_GEOM_SCOPE NSEdgeInsets
NSEdgeInsetsMake(CGFloat top, CGFloat left,
CGFloat bottom, CGFloat right) GS_GEOM_ATTR;
GS_GEOM_SCOPE NSEdgeInsets
NSEdgeInsetsMake(CGFloat top, CGFloat left, CGFloat bottom, CGFloat right)
{
NSEdgeInsets edgeInsets;
edgeInsets.top = top;
edgeInsets.left = left;
edgeInsets.bottom = bottom;
edgeInsets.right = right;
return edgeInsets;
}
#if OS_API_VERSION(MAC_OS_X_VERSION_10_10, GS_API_LATEST)
/** Compares two edge insets for equality. **/
GS_EXPORT BOOL
NSEdgeInsetsEqual(NSEdgeInsets e1, NSEdgeInsets e2);
#endif
#endif
/** Get a Rectangle's Coordinates... **/
GS_GEOM_SCOPE CGFloat

View file

@ -499,3 +499,14 @@ NSEqualPoints(NSPoint aPoint, NSPoint bPoint)
&& almostEqual(aPoint.y, bPoint.y)) ? YES : NO;
}
BOOL
NSEdgeInsetsEqual(NSEdgeInsets e1, NSEdgeInsets e2)
{
return (
almostEqual(e1.top, e2.top)
&& almostEqual(e1.left, e2.left)
&& almostEqual(e1.bottom, e2.bottom)
&& almostEqual(e1.right, e2.right)
);
}

View file

@ -21,11 +21,13 @@ geom_string()
NSPoint p, p2;
NSRect r, r2;
NSSize s, s2;
NSEdgeInsets ei;
NSString *sp, *sr, *ss;
p = NSMakePoint(23.45, -3.45);
r = NSMakeRect(23.45, -3.45, 2044.3, 2033);
s = NSMakeSize(0.5, 0.22);
ei = NSEdgeInsetsMake(23.45, -3.45, 2044.3, 2033);
PASS(NSEqualPoints(p, NSMakePoint(23.45, -3.45)),
"identical points are equal");
@ -79,6 +81,21 @@ geom_string()
"an empty rect does not intersect with one touching it");
PASS(!NSIntersectsRect(NSMakeRect(1,1,0,0), NSMakeRect(1,1,0,0)),
"identical empty rects do not intersec");
PASS(NSEdgeInsetsEqual(ei, NSEdgeInsetsMake(23.45, -3.45, 2044.3, 2033)),
"identical rects are equal");
if (sizeof(CGFloat) == sizeof(float))
{
PASS(NSEdgeInsetsEqual(ei, NSEdgeInsetsMake(23.45, -3.45, 2044.3, 2033.00001)),
"near identical rects are equal");
}
else
{
PASS(NSEdgeInsetsEqual(ei, NSEdgeInsetsMake(23.45, -3.45, 2044.3, 2033.0000000000001)),
"near identical rects are equal");
}
PASS(!NSEdgeInsetsEqual(ei, NSEdgeInsetsMake(23.45, -3.45, 2044.3, 2033.0001)),
"moderately similar rects are not equal");
#if defined(GNUSTEP_BASE_LIBRARY)
if (compat_mode == YES)