diff --git a/AppKit/AppKit.j b/AppKit/AppKit.j index c48b219d3..27f3a3001 100644 --- a/AppKit/AppKit.j +++ b/AppKit/AppKit.j @@ -20,7 +20,9 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ +@import "_CPToolTip.j" @import "CALayer.j" +@import "CGGeometry.j" @import "CPAccordionView.j" @import "CPAlert.j" @import "CPAnimation.j" @@ -36,10 +38,10 @@ @import "CPCibBindingConnector.j" @import "CPCibConnector.j" @import "CPCibControlConnector.j" +@import "CPCibHelpConnector.j" @import "CPCibLoading.j" @import "CPCibOutletConnector.j" @import "CPCibRuntimeAttributesConnector.j" -@import "CPCibHelpConnector.j" @import "CPClipView.j" @import "CPCollectionView.j" @import "CPCollectionViewItem.j" @@ -60,7 +62,6 @@ @import "CPFlashView.j" @import "CPFont.j" @import "CPFontManager.j" -@import "CPGeometry.j" @import "CPGradient.j" @import "CPGraphics.j" @import "CPImage.j" @@ -99,13 +100,12 @@ @import "CPTokenField.j" @import "CPToolbar.j" @import "CPToolbarItem.j" -@import "_CPToolTip.j" @import "CPTreeNode.j" @import "CPUserDefaultsController.j" @import "CPView.j" @import "CPViewAnimation.j" @import "CPViewController.j" @import "CPWebView.j" -@import "CPWorkspace.j" @import "CPWindow.j" @import "CPWindowController.j" +@import "CPWorkspace.j" diff --git a/AppKit/CPImage.j b/AppKit/CPImage.j index d87c0b5d4..f19991f7a 100644 --- a/AppKit/CPImage.j +++ b/AppKit/CPImage.j @@ -21,13 +21,12 @@ */ @import +@import @import @import @import @import -@import "CPGeometry.j" - CPImageLoadStatusInitialized = 0; CPImageLoadStatusLoading = 1; diff --git a/AppKit/CPPopUpButton.j b/AppKit/CPPopUpButton.j index cbc011db0..43d5ac3dc 100644 --- a/AppKit/CPPopUpButton.j +++ b/AppKit/CPPopUpButton.j @@ -20,8 +20,9 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ +@import + @import "CPButton.j" -@import "CPGeometry.j" @import "CPMenu.j" @import "CPMenuItem.j" diff --git a/AppKit/CPView.j b/AppKit/CPView.j index eede974cc..8d4db509f 100644 --- a/AppKit/CPView.j +++ b/AppKit/CPView.j @@ -21,14 +21,14 @@ */ @import +@import @import @import @import "CGAffineTransform.j" -@import "CGGeometry.j" + @import "CPColor.j" -@import "CPGeometry.j" @import "CPGraphicsContext.j" @import "CPResponder.j" @import "CPTheme.j" diff --git a/AppKit/CoreGraphics/CGAffineTransform.h b/AppKit/CoreGraphics/CGAffineTransform.h index 1b3fc9266..4eea12e67 100644 --- a/AppKit/CoreGraphics/CGAffineTransform.h +++ b/AppKit/CoreGraphics/CGAffineTransform.h @@ -20,7 +20,7 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ -#include "CGGeometry.h" +#include "../../Foundation/_CGGeometry.h" #define _CGAffineTransformMake(a_, b_, c_, d_, tx_, ty_) { a:a_, b:b_, c:c_, d:d_, tx:tx_, ty:ty_ } #define _CGAffineTransformMakeIdentity() _CGAffineTransformMake(1.0, 0.0, 0.0, 1.0, 0.0, 0.0) diff --git a/AppKit/CoreGraphics/CGGeometry.j b/AppKit/CoreGraphics/CGGeometry.j index 957662a00..bdefc9bd6 100644 --- a/AppKit/CoreGraphics/CGGeometry.j +++ b/AppKit/CoreGraphics/CGGeometry.j @@ -2,8 +2,8 @@ * CGGeometry.j * AppKit * - * Created by Francisco Tolmasky. - * Copyright 2008, 280 North, Inc. + * Created by Alexander Ljungberg. + * Copyright 2012, SlevenBits Ltd. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -20,301 +20,4 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ -#define _function(inline) function inline { return _##inline; } - -_function(CGPointMake(x, y)) -_function(CGPointMakeZero()) -_function(CGPointMakeCopy(aPoint)) -_function(CGPointCreateCopy(aPoint)) - -_function(CGPointEqualToPoint(lhsPoint, rhsPoint)) -_function(CGStringFromPoint(aPoint)) - -_function(CGSizeMake(width, height)) -_function(CGSizeMakeZero()) -_function(CGSizeMakeCopy(aSize)) -_function(CGSizeCreateCopy(aSize)) - -_function(CGSizeEqualToSize(lhsSize, rhsSize)) -_function(CGStringFromSize(aSize)) - -_function(CGRectMake(x, y, width, height)) -_function(CGRectMakeZero()) -_function(CGRectMakeCopy(aRect)) -_function(CGRectCreateCopy(aRect)) - -_function(CGRectEqualToRect(lhsRect, rhsRect)) -_function(CGStringFromRect(aRect)) - -_function(CGRectOffset(aRect, dX, dY)) -_function(CGRectInset(aRect, dX, dY)) - -_function(CGRectGetHeight(aRect)) -_function(CGRectGetMaxX(aRect)) -_function(CGRectGetMaxY(aRect)) -_function(CGRectGetMidX(aRect)) -_function(CGRectGetMidY(aRect)) -_function(CGRectGetMinX(aRect)) -_function(CGRectGetMinY(aRect)) -_function(CGRectGetWidth(aRect)) - -_function(CGRectIsEmpty(aRect)) -_function(CGRectIsNull(aRect)) - -_function(CGRectContainsPoint(aRect, aPoint)) - -_function(CGInsetMake(top, right, bottom, left)) -_function(CGInsetMakeZero()) -_function(CGInsetMakeCopy(anInset)) -_function(CGInsetIsEmpty(anInset)) -_function(CGInsetEqualToInset(lhsInset, rhsInset)) - -CGMinXEdge = 0; -CGMinYEdge = 1; -CGMaxXEdge = 2; -CGMaxYEdge = 3; - -CGRectNull = _CGRectMake(Infinity, Infinity, 0.0, 0.0); - -/*! - @addtogroup appkit - @{ -*/ - -/*! - Creates two rectangles -- slice and rem -- from inRect, by dividing inRect - with a line that's parallel to the side of inRect specified by edge. - The size of slice is determined by amount, which specifies the distance from edge. - - slice and rem must not be NULL, must not be the same object, and must not be the - same object as inRect. - - @group CGRect -*/ -function CGRectDivide(inRect, slice, rem, amount, edge) -{ - slice.origin = _CGPointMakeCopy(inRect.origin); - slice.size = _CGSizeMakeCopy(inRect.size); - rem.origin = _CGPointMakeCopy(inRect.origin); - rem.size = _CGSizeMakeCopy(inRect.size); - - switch (edge) - { - case CGMinXEdge: - slice.size.width = amount; - rem.origin.x += amount; - rem.size.width -= amount; - break; - - case CGMaxXEdge: - slice.origin.x = _CGRectGetMaxX(slice) - amount; - slice.size.width = amount; - rem.size.width -= amount; - break; - - case CGMinYEdge: - slice.size.height = amount; - rem.origin.y += amount; - rem.size.height -= amount; - break; - - case CGMaxYEdge: - slice.origin.y = _CGRectGetMaxY(slice) - amount; - slice.size.height = amount; - rem.size.height -= amount; - } -} - -/*! - Returns a \c BOOL indicating whether CGRect \c lhsRect - contains CGRect \c rhsRect. - @group CGRect - @param lhsRect the CGRect to test if \c rhsRect is inside of - @param rhsRect the CGRect to test if it fits inside \c lhsRect. - @return BOOL \c YES if \c rhsRect fits inside \c lhsRect. -*/ -function CGRectContainsRect(lhsRect, rhsRect) -{ - var union = CGRectUnion(lhsRect, rhsRect); - - return _CGRectEqualToRect(union, lhsRect); -} - -/*! - Returns \c YES if the two rectangles intersect - @group CGRect - @param lhsRect the first CGRect - @param rhsRect the second CGRect - @return BOOL \c YES if the two rectangles have any common spaces, and \c NO, otherwise. -*/ -function CGRectIntersectsRect(lhsRect, rhsRect) -{ - var intersection = CGRectIntersection(lhsRect, rhsRect); - - return !_CGRectIsEmpty(intersection); -} - -/*! - Makes the origin and size of a CGRect all integers. Specifically, by making - the southwest corner the origin (rounded down), and the northeast corner a CGSize (rounded up). - @param aRect the rectangle to operate on - @return CGRect the modified rectangle (same as the input) - @group CGRect -*/ -function CGRectIntegral(aRect) -{ - aRect = CGRectStandardize(aRect); - - // Store these out separately, if not the GetMaxes will return incorrect values. - var x = FLOOR(_CGRectGetMinX(aRect)), - y = FLOOR(_CGRectGetMinY(aRect)); - - aRect.size.width = CEIL(_CGRectGetMaxX(aRect)) - x; - aRect.size.height = CEIL(_CGRectGetMaxY(aRect)) - y; - - aRect.origin.x = x; - aRect.origin.y = y; - - return aRect; -} - -/*! - Returns the intersection of the two provided rectangles as a new rectangle. - @param lhsRect the first rectangle used for calculation - @param rhsRect the second rectangle used for calculation - @return CGRect the intersection of the two rectangles - @group CGRect -*/ -function CGRectIntersection(lhsRect, rhsRect) -{ - var intersection = _CGRectMake( - MAX(_CGRectGetMinX(lhsRect), _CGRectGetMinX(rhsRect)), - MAX(_CGRectGetMinY(lhsRect), _CGRectGetMinY(rhsRect)), - 0, 0); - - intersection.size.width = MIN(_CGRectGetMaxX(lhsRect), _CGRectGetMaxX(rhsRect)) - _CGRectGetMinX(intersection); - intersection.size.height = MIN(_CGRectGetMaxY(lhsRect), _CGRectGetMaxY(rhsRect)) - _CGRectGetMinY(intersection); - - return _CGRectIsEmpty(intersection) ? _CGRectMakeZero() : intersection; -} - -/* - -*/ -function CGRectStandardize(aRect) -{ - var width = _CGRectGetWidth(aRect), - height = _CGRectGetHeight(aRect), - standardized = _CGRectMakeCopy(aRect); - - if (width < 0.0) - { - standardized.origin.x += width; - standardized.size.width = -width; - } - - if (height < 0.0) - { - standardized.origin.y += height; - standardized.size.height = -height; - } - - return standardized; -} - -function CGRectUnion(lhsRect, rhsRect) -{ - var lhsRectIsNull = !lhsRect || lhsRect === CGRectNull, - rhsRectIsNull = !rhsRect || rhsRect === CGRectNull; - - if (lhsRectIsNull) - return rhsRectIsNull ? CGRectNull : rhsRect; - - if (rhsRectIsNull) - return lhsRectIsNull ? CGRectNull : lhsRect; - - var minX = MIN(_CGRectGetMinX(lhsRect), _CGRectGetMinX(rhsRect)), - minY = MIN(_CGRectGetMinY(lhsRect), _CGRectGetMinY(rhsRect)), - maxX = MAX(_CGRectGetMaxX(lhsRect), _CGRectGetMaxX(rhsRect)), - maxY = MAX(_CGRectGetMaxY(lhsRect), _CGRectGetMaxY(rhsRect)); - - return _CGRectMake(minX, minY, maxX - minX, maxY - minY); -} - -/*! - Returns the specified rectangle inset by the given CGInset as a new rectangle. - @param aRect the rect to inset - @param anInset a CGInset to inset by - @return CGRect aRect inset by anInset -*/ -_function(CGRectInsetByInset(aRect, anInset)); - -function CGPointFromString(aString) -{ - var comma = aString.indexOf(','); - - return { x:parseFloat(aString.substr(1, comma - 1)), y:parseFloat(aString.substring(comma + 1, aString.length)) }; -} - -function CGSizeFromString(aString) -{ - var comma = aString.indexOf(','); - - return { width:parseFloat(aString.substr(1, comma - 1)), height:parseFloat(aString.substring(comma + 1, aString.length)) }; -} - -function CGRectFromString(aString) -{ - var comma = aString.indexOf(',', aString.indexOf(',') + 1); - - return { origin:CGPointFromString(aString.substr(1, comma - 1)), size:CGSizeFromString(aString.substring(comma + 2, aString.length)) }; -} - -function CGPointFromEvent(anEvent) -{ - return _CGPointMake(anEvent.clientX, anEvent.clientY); -} - -/*! - Combines two insets by adding their individual elements and returns the result. - - @group CGInset -*/ -function CGInsetUnion(lhsInset, rhsInset) -{ - return _CGInsetMake(lhsInset.top + rhsInset.top, - lhsInset.right + rhsInset.right, - lhsInset.bottom + rhsInset.bottom, - lhsInset.left + rhsInset.left); -} - -/*! - Subtract one inset from another by subtracting their individual elements and returns the result. - - @group CGInset -*/ -function CGInsetDifference(lhsInset, rhsInset) -{ - return _CGInsetMake(lhsInset.top - rhsInset.top, - lhsInset.right - rhsInset.right, - lhsInset.bottom - rhsInset.bottom, - lhsInset.left - rhsInset.left); -} - -function CGInsetFromString(aString) -{ - var numbers = aString.substr(1, aString.length - 2).split(','); - - return _CGInsetMake(parseFloat(numbers[0]), parseFloat(numbers[1]), parseFloat(numbers[2]), parseFloat(numbers[3])); -} - -CGInsetFromCPString = CGInsetFromString; - -function CPStringFromCGInset(anInset) -{ - return '{' + anInset.top + ", " + anInset.left + ", " + anInset.bottom + ", " + anInset.right + '}'; -} - -/*! - @} -*/ +@import diff --git a/AppKit/CPGeometry.j b/Foundation/CPGeometry.j similarity index 59% rename from AppKit/CPGeometry.j rename to Foundation/CPGeometry.j index 7e7fa2079..ba3c249f4 100644 --- a/AppKit/CPGeometry.j +++ b/Foundation/CPGeometry.j @@ -1,6 +1,6 @@ /* * CPGeometry.j - * AppKit + * Foundation * * Created by Francisco Tolmasky. * Copyright 2008, 280 North, Inc. @@ -20,102 +20,30 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ -@import "CGGeometry.j" - - +@import "_CGGeometry.j" +        CPMinXEdge = 0; CPMinYEdge = 1; CPMaxXEdge = 2; CPMaxYEdge = 3; -// FIXME: the rest! +/*! + Makes a CGPoint object out of two numbers provided as arguments + @group CGPoint + @param x the x-coordinate of the CGPoint + @param y the y-coordinate of the CGPoint + @return CGPoint a CGPoint with an X and Y coordinate equal to the function arguments +*/ CPMakePoint = CGPointMake; + +/*! + Creates and returns a new CGSize object from the provided dimensions. + @group CGSize + @param width the width for the new CGSize + @param height the height for the new CGSize + @return CGSize the new CGSize +*/ CPMakeSize = CGSizeMake; -CPMakeRect = CGRectMake; - -/*! - @addtogroup appkit - @{ -*/ - -/*! - Creates a copy of a specified point and returns the copy - @group CGPoint - @param the point to be copied - @return CGPoint the copy of the provided CGPoint - */ -function CPPointCreateCopy(aPoint) -{ - return { x: aPoint.x, y: aPoint.y }; -} - -/*! - Makes a CGPoint object out of two numbers provided as arguments - @group CGPoint - @param x the x-coordinate of the CGPoint - @param y the y-coordinate of the CGPoint - @return CGPoint a CGPoint with an X and Y coordinate equal to the function arguments - */ -function CPPointMake(x, y) -{ - return { x: x, y: y }; -} - -/*! - Makes a CGRect with an origin and size equal to \c aRect less the \c dX/dY insets specified. - @param dX the size of the inset in the x-axis - @param dY the size of the inset in the y-axis - @group CGRect - @return CGRect a rectangle like \c aRect with an inset -*/ -function CPRectInset(aRect, dX, dY) -{ - return CPRectMake( aRect.origin.x + dX, aRect.origin.y + dY, - aRect.size.width - 2 * dX, aRect.size.height - 2 * dY); -} - -/*! - @group CGRect - @ignore - @return void - @deprecated -*/ -function CPRectIntegral(aRect) -{ - // FIXME!!! - alert("CPRectIntegral unimplemented"); -} - -/*! - Returns the intersection of the two provided rectangles as a new rectangle - @group CGRect - @param lhsRect the first rectangle used for calculation - @param rhsRect the second rectangle used for calculation - @return CGRect the intersection of the two rectangles -*/ -function CPRectIntersection(lhsRect, rhsRect) -{ - var intersection = CPRectMake( - MAX(CPRectGetMinX(lhsRect), CPRectGetMinX(rhsRect)), - MAX(CPRectGetMinY(lhsRect), CPRectGetMinY(rhsRect)), - 0, 0); - - intersection.size.width = MIN(CPRectGetMaxX(lhsRect), CPRectGetMaxX(rhsRect)) - CPRectGetMinX(intersection); - intersection.size.height = MIN(CPRectGetMaxY(lhsRect), CPRectGetMaxY(rhsRect)) - CPRectGetMinY(intersection); - - return CPRectIsEmpty(intersection) ? CPRectMakeZero() : intersection; -} - -/*! - Creates a copy of the provided rectangle - @group CGRect - @param aRect the CGRect that will be copied - @return CGRect the rectangle copy -*/ -function CPRectCreateCopy(aRect) -{ - return { origin: CPPointCreateCopy(aRect.origin), size: CPSizeCreateCopy(aRect.size) }; -} /*! Returns a CGRect made of the specified arguments @@ -126,118 +54,15 @@ function CPRectCreateCopy(aRect) @param height the height of the new rectangle @return CGRect the new rectangle */ -function CPRectMake(x, y, width, height) -{ - return { origin: CPPointMake(x, y), size: CPSizeMake(width, height) }; -} +CPMakeRect = CGRectMake; /*! - Creates a new rectangle with its origin offset by \c dX and \c dY. - @group CGRect - @param aRect the rectangle to copy the origin and size from - @param dX the amount added to the x-size of the new rectangle - @param dY the amount added to the y-size of the new rectangle - @return CGRect the new rectangle with modified size + Creates a copy of a specified point and returns the copy + @group CGPoint + @param the point to be copied + @return CGPoint the copy of the provided CGPoint */ -function CPRectOffset(aRect, dX, dY) -{ - return CPRectMake(aRect.origin.x + dX, aRect.origin.y + dY, aRect.size.width, aRect.size.height); -} - -/*! - @group CGRect - @param aRect a CGRect - @return CGRect -*/ -function CPRectStandardize(aRect) -{ - var width = CPRectGetWidth(aRect), - height = CPRectGetHeight(aRect), - standardized = CPRectCreateCopy(aRect); - - if (width < 0.0) - { - standardized.origin.x += width; - standardized.size.width = -width; - } - - if (height < 0.0) - { - standardized.origin.y += height; - standardized.size.height = -height; - } - - return standardized; -} - -/*! - Returns the smallest rectangle that can contain the two argument \c CGRects. - @group CGRect - @param lhsRect the first CGRect to use for the union calculation - @param rhsRect the second CGRect to use for the union calculation - @return CGRect the union rectangle -*/ -function CPRectUnion(lhsRect, rhsRect) -{ - var minX = MIN(CPRectGetMinX(lhsRect), CPRectGetMinX(rhsRect)), - minY = MIN(CPRectGetMinY(lhsRect), CPRectGetMinY(rhsRect)), - maxX = MAX(CPRectGetMaxX(lhsRect), CPRectGetMaxX(rhsRect)), - maxY = MAX(CPRectGetMaxY(lhsRect), CPRectGetMaxY(rhsRect)); - - return CPRectMake(minX, minY, maxX - minX, maxY - minY); -} - -/*! - Creates and returns a copy of the provided CGSize - @group CGSize - @param aSize the CGSize to copy - @return CGSize the copy of the CGSize -*/ -function CPSizeCreateCopy(aSize) -{ - return { width: aSize.width, height: aSize.height }; -} - -/*! - Creates and returns a new CGSize object from the provided dimensions. - @group CGSize - @param width the width for the new CGSize - @param height the height for the new CGSize - @return CGSize the new CGSize -*/ -function CPSizeMake(width, height) -{ - return { width: width, height: height }; -} - -/*! - Returns \c YES if the CGRect, \c aRect, contains - the CGPoint, \c aPoint. - @param aRect the rectangle to test with - @param aPoint the point to test with - @group CGRect - @return BOOL \c YES if the rectangle contains the point, \c NO otherwise. -*/ -function CPRectContainsPoint(aRect, aPoint) -{ - return aPoint.x >= CPRectGetMinX(aRect) && - aPoint.y >= CPRectGetMinY(aRect) && - aPoint.x < CPRectGetMaxX(aRect) && - aPoint.y < CPRectGetMaxY(aRect); -} - -/*! - Returns a \c BOOL indicating whether CGRect \c possibleOuter - contains CGRect \c possibleInner. - @group CGRect - @param possibleOuter the CGRect to test if \c possibleInner is inside of - @param possibleInner the CGRect to test if it fits inside \c possibleOuter. - @return BOOL \c YES if \c possibleInner fits inside \c possibleOuter. -*/ -function CPRectContainsRect(possibleOuter, possibleInner) -{ - return CGRectContainsRect(possibleOuter, possibleInner); -} +CPPointCreateCopy = CGPointMakeCopy; /*! Tests whether the two CGPoints are equal to each other by comparing their @@ -247,10 +72,7 @@ function CPRectContainsRect(possibleOuter, possibleInner) @param rhsPoint the second CGPoint to check @return BOOL \c YES if the two points have the same x's, and the same y's. */ -function CPPointEqualToPoint(lhsPoint, rhsPoint) -{ - return lhsPoint.x == rhsPoint.x && lhsPoint.y == rhsPoint.y; -} +CPPointEqualToPoint = CGPointEqualToPoint; /*! Test whether the two CGRects have the same origin and size @@ -259,103 +81,7 @@ function CPPointEqualToPoint(lhsPoint, rhsPoint) @param rhsRect the second CGRect to compare @return BOOL \c YES if the two rectangles have the same origin and size. \c NO, otherwise. */ -function CPRectEqualToRect(lhsRect, rhsRect) -{ - return CPPointEqualToPoint(lhsRect.origin, rhsRect.origin) && - CPSizeEqualToSize(lhsRect.size, rhsRect.size); -} - -/*! - @group CGRect - @param aRect a CGRect - @return int -*/ -function CPRectGetHeight(aRect) -{ - return aRect.size.height; -} - -/*! - @group CGRect - @param aRect a CGRect - @return int -*/ -function CPRectGetMaxX(aRect) -{ - return aRect.origin.x + aRect.size.width; -} - -/*! - @group CGRect - @param aRect a CGRect - @return int -*/ -function CPRectGetMaxY(aRect) -{ - return aRect.origin.y + aRect.size.height; -} - -/*! - @group CGRect - @param aRect a CGRect - @return float -*/ -function CPRectGetMidX(aRect) -{ - return aRect.origin.x + (aRect.size.width) / 2.0; -} - -/*! - @group CGRect - @param aRect a CGRect - @return float -*/ -function CPRectGetMidY(aRect) -{ - return aRect.origin.y + (aRect.size.height) / 2.0; -} - -/*! - @group CGRect - @param aRect a CGRect - @return int -*/ -function CPRectGetMinX(aRect) -{ - return aRect.origin.x; -} - -/*! - @group CGRect - @param aRect a CGRect - @return int -*/ -function CPRectGetMinY(aRect) -{ - return aRect.origin.y; -} - -/*! - @group CGRect - @param aRect a CGRect - @return int -*/ -function CPRectGetWidth(aRect) -{ - return aRect.size.width; -} - -/*! - Returns \c YES if the two rectangles intersect - @group CGRect - @param lhsRect the first CGRect - @param rhsRect the second CGRect - @return BOOL \c YES if the two rectangles have any common spaces, and \c NO, otherwise. -*/ -function CPRectIntersectsRect(lhsRect, rhsRect) -{ - return !CPRectIsEmpty(CPRectIntersection(lhsRect, rhsRect)); -} +CPRectEqualToRect = CGRectEqualToRect; /*! Returns \c YES if the CGRect has no area. @@ -364,10 +90,189 @@ function CPRectIntersectsRect(lhsRect, rhsRect) @param aRect the CGRect to test @return BOOL \c YES if the CGRect has no area, and \c NO, otherwise. */ -function CPRectIsEmpty(aRect) -{ - return aRect.size.width <= 0.0 || aRect.size.height <= 0.0; -} +CPRectIsEmpty = CGRectIsEmpty; + +/*! + Returns a \c BOOL indicating whether CGRect \c possibleOuter + contains CGRect \c possibleInner. + @group CGRect + @param possibleOuter the CGRect to test if \c possibleInner is inside of + @param possibleInner the CGRect to test if it fits inside \c possibleOuter. + @return BOOL \c YES if \c possibleInner fits inside \c possibleOuter. +*/ +CPRectContainsRect = CGRectContainsRect; + +/*! + Returns the intersection of the two provided rectangles as a new rectangle + @group CGRect + @param lhsRect the first rectangle used for calculation + @param rhsRect the second rectangle used for calculation + @return CGRect the intersection of the two rectangles +*/ +CPRectIntersection = CGRectIntersection; + +/*! + Makes a CGPoint object out of two numbers provided as arguments + @group CGPoint + @param x the x-coordinate of the CGPoint + @param y the y-coordinate of the CGPoint + @return CGPoint a CGPoint with an X and Y coordinate equal to the function arguments +*/ +CPPointMake = CGPointMake; + +/*! + Makes a CGRect with an origin and size equal to \c aRect less the \c dX/dY insets specified. + @param dX the size of the inset in the x-axis + @param dY the size of the inset in the y-axis + @group CGRect + @return CGRect a rectangle like \c aRect with an inset +*/ +CPRectInset = CGRectInset; + +/*! + @group CGRect + @ignore + @return void + @deprecated +*/ +CPRectIntegral = CGRectIntegral; + +/*! + Creates a copy of the provided rectangle + @group CGRect + @param aRect the CGRect that will be copied + @return CGRect the rectangle copy +*/ +CPRectCreateCopy = CGRectCreateCopy; + +/*! + Returns a CGRect made of the specified arguments + @group CGRect + @param x the x-coordinate of the rectangle's origin + @param y the y-coordinate of the rectangle's origin + @param width the width of the new rectangle + @param height the height of the new rectangle + @return CGRect the new rectangle +*/ +CPRectMake = CGRectMake; + +/*! + Creates a new rectangle with its origin offset by \c dX and \c dY. + @group CGRect + @param aRect the rectangle to copy the origin and size from + @param dX the amount added to the x-size of the new rectangle + @param dY the amount added to the y-size of the new rectangle + @return CGRect the new rectangle with modified size +*/ +CPRectOffset = CGRectOffset; + +/*! + @group CGRect + @param aRect a CGRect + @return CGRect +*/ +CPRectStandardize = CGRectStandardize; + +/*! + Returns the smallest rectangle that can contain the two argument \c CGRects. + @group CGRect + @param lhsRect the first CGRect to use for the union calculation + @param rhsRect the second CGRect to use for the union calculation + @return CGRect the union rectangle +*/ +CPRectUnion = CGRectUnion; + +/*! + Creates and returns a copy of the provided CGSize + @group CGSize + @param aSize the CGSize to copy + @return CGSize the copy of the CGSize +*/ +CPSizeCreateCopy = CGSizeCreateCopy; + +/*! + Creates and returns a new CGSize object from the provided dimensions. + @group CGSize + @param width the width for the new CGSize + @param height the height for the new CGSize + @return CGSize the new CGSize +*/ +CPSizeMake = CGSizeMake; + +/*! + Returns \c YES if the CGRect, \c aRect, contains + the CGPoint, \c aPoint. + @param aRect the rectangle to test with + @param aPoint the point to test with + @group CGRect + @return BOOL \c YES if the rectangle contains the point, \c NO otherwise. +*/ +CPRectContainsPoint = CGRectContainsPoint; + +/*! + @group CGRect + @param aRect a CGRect + @return int +*/ +CPRectGetHeight = CGRectGetHeight; + +/*! + @group CGRect + @param aRect a CGRect + @return int +*/ +CPRectGetMaxX = CGRectGetMaxX; + +/*! + @group CGRect + @param aRect a CGRect + @return int +*/ +CPRectGetMaxY = CGRectGetMaxY; + +/*! + @group CGRect + @param aRect a CGRect + @return float +*/ +CPRectGetMidX = CGRectGetMidX; + +/*! + @group CGRect + @param aRect a CGRect + @return float +*/ +CPRectGetMidY = CGRectGetMidY; + +/*! + @group CGRect + @param aRect a CGRect + @return int +*/ +CPRectGetMinX = CGRectGetMinX; + +/*! + @group CGRect + @param aRect a CGRect + @return int +*/ +CPRectGetMinY = CGRectGetMinY; + +/*! + @group CGRect + @param aRect a CGRect + @return int +*/ +CPRectGetWidth = CGRectGetWidth; + +/*! + Returns \c YES if the two rectangles intersect + @group CGRect + @param lhsRect the first CGRect + @param rhsRect the second CGRect + @return BOOL \c YES if the two rectangles have any common spaces, and \c NO, otherwise. +*/ +CPRectIntersectsRect = CGRectIntersectsRect; /*! Returns \c YES if the CGRect has no area. @@ -375,10 +280,7 @@ function CPRectIsEmpty(aRect) @group CGRect @return BOOL \c YES if the CGRect has no area, and \c NO, otherwise. */ -function CPRectIsNull(aRect) -{ - return aRect.size.width <= 0.0 || aRect.size.height <= 0.0; -} +CPRectIsNull = CGRectIsNull; /*! Creates two rectangles -- slice and rem -- from inRect, by dividing inRect @@ -389,10 +291,7 @@ function CPRectIsNull(aRect) @group CGRect */ -function CPDivideRect(inRect, slice, rem, amount, edge) -{ - CGRectDivide(inRect, slice, rem, amount, edge); -} +CPDivideRect = CGRectDivide; /*! Returns \c YES if the two CGSizes are identical. @@ -401,10 +300,7 @@ function CPDivideRect(inRect, slice, rem, amount, edge) @param rhsSize the second CGSize to compare @return BOOL \c YES if the two sizes are identical. \c NO, otherwise. */ -function CPSizeEqualToSize(lhsSize, rhsSize) -{ - return lhsSize.width == rhsSize.width && lhsSize.height == rhsSize.height; -} +CPSizeEqualToSize = CGSizeEqualToSize; /*! Returns a human readable string of the provided CGPoint. @@ -412,10 +308,7 @@ function CPSizeEqualToSize(lhsSize, rhsSize) @param aPoint the point to represent @return CGPoint a string representation of the CGPoint */ -function CPStringFromPoint(aPoint) -{ - return "{" + aPoint.x + ", " + aPoint.y + "}"; -} +CPStringFromPoint = CGStringFromPoint; /*! Returns a human readable string of the provided CGSize. @@ -423,10 +316,7 @@ function CPStringFromPoint(aPoint) @param aSize the size to represent @return CGSize a string representation of the CGSize */ -function CPStringFromSize(aSize) -{ - return "{" + aSize.width + ", " + aSize.height + "}"; -} +CPStringFromSize = CGStringFromSize; /*! Returns a human readable string of the provided CGRect. @@ -434,10 +324,7 @@ function CPStringFromSize(aSize) @param aRect the rectangle to represent @return CGString the string representation of the rectangle */ -function CPStringFromRect(aRect) -{ - return "{" + CPStringFromPoint(aRect.origin) + ", " + CPStringFromSize(aRect.size) + "}"; -} +CPStringFromRect = CGStringFromRect; /*! Returns a CGPoint from a string with a comma separated pair of integers. @@ -445,12 +332,7 @@ function CPStringFromRect(aRect) @param aString a string containing two comma separated integers @return CGPoint the point object created from the string */ -function CPPointFromString(aString) -{ - var comma = aString.indexOf(','); - - return { x:parseFloat(aString.substr(1, comma - 1), 10), y:parseFloat(aString.substring(comma + 1, aString.length), 10) }; -} +CPPointFromString = CGPointFromString; /*! Returns a CGSize from a string containing a pair of comma separated integers. @@ -458,12 +340,7 @@ function CPPointFromString(aString) @param aString a string containing two comma separated integers @return CGSize the size object created from the string */ -function CPSizeFromString(aString) -{ - var comma = aString.indexOf(','); - - return { width:parseFloat(aString.substr(1, comma - 1), 10), height:parseFloat(aString.substring(comma + 1, aString.length), 10) }; -} +CPSizeFromString = CGSizeFromString; /*! Returns a CGRect created from a string. @@ -471,53 +348,32 @@ function CPSizeFromString(aString) @param aString a string in the form generated by \c CPStringFromRect @return CGRect the rectangle created from the string */ -function CPRectFromString(aString) -{ - var comma = aString.indexOf(',', aString.indexOf(',') + 1); - - return { origin:CPPointFromString(aString.substr(1, comma - 1)), size:CPSizeFromString(aString.substring(comma + 2, aString.length)) }; -} +CPRectFromString = CGRectFromString; /*! @group CGPoint @param anEvent @return CGPoint */ -function CPPointFromEvent(anEvent) -{ - return CPPointMake(anEvent.clientX, anEvent.clientY, 0); -} +CPPointFromEvent = CGPointFromEvent; /*! Returns a zero sized CGSize. @group CGSize @return CGSize a size object with zeros for \c width and \c height */ -function CPSizeMakeZero() -{ - return CPSizeMake(0, 0); -} +CPSizeMakeZero = CGSizeMakeZero; /*! Returns a rectangle at origin \c (0,0) and size of \c (0,0). @group CGRect @return CGRect a zeroed out CGRect */ -function CPRectMakeZero() -{ - return CPRectMake(0, 0, 0, 0); -} +CPRectMakeZero = CGRectMakeZero; /*! Returns a point located at \c (0, 0). @group CGPoint @return CGPoint a point located at \c (0, 0) */ -function CPPointMakeZero() -{ - return CPPointMake(0, 0, 0); -} - -/*! - @} -*/ +CPPointMakeZero = CGPointMakeZero; diff --git a/Foundation/Foundation.j b/Foundation/Foundation.j index d26bfbb1f..422501616 100755 --- a/Foundation/Foundation.j +++ b/Foundation/Foundation.j @@ -36,6 +36,7 @@ @import "CPException.j" @import "CPExpression.j" @import "CPFormatter.j" +@import "CPGeometry.j" @import "CPIndexSet.j" @import "CPIndexPath.j" @import "CPInvocation.j" diff --git a/Foundation/Jakefile b/Foundation/Jakefile index 90a5bdba9..20d218c9e 100644 --- a/Foundation/Jakefile +++ b/Foundation/Jakefile @@ -42,7 +42,13 @@ foundationTask = framework ("Foundation", function(foundationTask) foundationTask.setInfoPlistPath("Info.plist"); foundationTask.setEnvironments(require("objective-j/jake/environment").ObjJ); - var INCLUDES = "--include \"../AppKit/Platform/Platform.h\" --include \"Ref.h\""; + // Grab all the .h's and just include them in each file. + var INCLUDES = new FileList("**/*.h").map(function(aFilename) + { + return "--include \"" + aFilename + "\""; + }).join(" "); + + INCLUDES = "--include \"../AppKit/Platform/Platform.h\" " + INCLUDES; if ($CONFIGURATION === "Release") foundationTask.setCompilerFlags("-O " + INCLUDES); diff --git a/AppKit/CoreGraphics/CGGeometry.h b/Foundation/_CGGeometry.h similarity index 91% rename from AppKit/CoreGraphics/CGGeometry.h rename to Foundation/_CGGeometry.h index f66a82d32..5ea60e41c 100644 --- a/AppKit/CoreGraphics/CGGeometry.h +++ b/Foundation/_CGGeometry.h @@ -1,6 +1,6 @@ /* * CGGeometry.h - * AppKit + * Foundation * * Created by Francisco Tolmasky. * Copyright 2008, 280 North, Inc. @@ -20,6 +20,12 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ +/* + +CGGeometry is not a part of Foundation. The reason _CGGeometry.h exists, and is in Foundation, is that CPGeometry and CGGeometry both use the same code so the shared basis needs to be in the lowest layer. If for some reason Cappuccino was ever reimplemented such that CPRect !== CGRect etc, this class could be removed and CPGeometry.j and CGGeometry.j updated with relevant functions without breaking any client code. + +*/ + #define _CGPointMake(x_, y_) { x:x_, y:y_ } #define _CGPointMakeCopy(aPoint) _CGPointMake(aPoint.x, aPoint.y) #define _CGPointMakeZero() _CGPointMake(0.0, 0.0) diff --git a/Foundation/_CGGeometry.j b/Foundation/_CGGeometry.j new file mode 100644 index 000000000..bbb116d9b --- /dev/null +++ b/Foundation/_CGGeometry.j @@ -0,0 +1,326 @@ +/* + * _CGGeometry.j + * Foundation + * + * Created by Francisco Tolmasky. + * Copyright 2008, 280 North, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/* + +CGGeometry is not a part of Foundation. The reason _CGGeometry.j exists, and is in Foundation, is that CPGeometry and CGGeometry both use the same code so the shared basis needs to be in the lowest layer. If for some reason Cappuccino was ever reimplemented such that CPRect !== CGRect etc, this class could be removed and CPGeometry.j and CGGeometry.j updated with relevant functions without breaking any client code. + +*/ + +#define _function(inline) function inline { return _##inline; } + +_function(CGPointMake(x, y)) +_function(CGPointMakeZero()) +_function(CGPointMakeCopy(aPoint)) +_function(CGPointCreateCopy(aPoint)) + +_function(CGPointEqualToPoint(lhsPoint, rhsPoint)) +_function(CGStringFromPoint(aPoint)) + +_function(CGSizeMake(width, height)) +_function(CGSizeMakeZero()) +_function(CGSizeMakeCopy(aSize)) +_function(CGSizeCreateCopy(aSize)) + +_function(CGSizeEqualToSize(lhsSize, rhsSize)) +_function(CGStringFromSize(aSize)) + +_function(CGRectMake(x, y, width, height)) +_function(CGRectMakeZero()) +_function(CGRectMakeCopy(aRect)) +_function(CGRectCreateCopy(aRect)) + +_function(CGRectEqualToRect(lhsRect, rhsRect)) +_function(CGStringFromRect(aRect)) + +_function(CGRectOffset(aRect, dX, dY)) +_function(CGRectInset(aRect, dX, dY)) + +_function(CGRectGetHeight(aRect)) +_function(CGRectGetMaxX(aRect)) +_function(CGRectGetMaxY(aRect)) +_function(CGRectGetMidX(aRect)) +_function(CGRectGetMidY(aRect)) +_function(CGRectGetMinX(aRect)) +_function(CGRectGetMinY(aRect)) +_function(CGRectGetWidth(aRect)) + +_function(CGRectIsEmpty(aRect)) +_function(CGRectIsNull(aRect)) + +_function(CGRectContainsPoint(aRect, aPoint)) + +_function(CGInsetMake(top, right, bottom, left)) +_function(CGInsetMakeZero()) +_function(CGInsetMakeCopy(anInset)) +_function(CGInsetIsEmpty(anInset)) +_function(CGInsetEqualToInset(lhsInset, rhsInset)) + +CGMinXEdge = 0; +CGMinYEdge = 1; +CGMaxXEdge = 2; +CGMaxYEdge = 3; + +CGRectNull = _CGRectMake(Infinity, Infinity, 0.0, 0.0); + +/*! + @addtogroup appkit + @{ +*/ + +/*! + Creates two rectangles -- slice and rem -- from inRect, by dividing inRect + with a line that's parallel to the side of inRect specified by edge. + The size of slice is determined by amount, which specifies the distance from edge. + + slice and rem must not be NULL, must not be the same object, and must not be the + same object as inRect. + + @group CGRect +*/ +function CGRectDivide(inRect, slice, rem, amount, edge) +{ + slice.origin = _CGPointMakeCopy(inRect.origin); + slice.size = _CGSizeMakeCopy(inRect.size); + rem.origin = _CGPointMakeCopy(inRect.origin); + rem.size = _CGSizeMakeCopy(inRect.size); + + switch (edge) + { + case CGMinXEdge: + slice.size.width = amount; + rem.origin.x += amount; + rem.size.width -= amount; + break; + + case CGMaxXEdge: + slice.origin.x = _CGRectGetMaxX(slice) - amount; + slice.size.width = amount; + rem.size.width -= amount; + break; + + case CGMinYEdge: + slice.size.height = amount; + rem.origin.y += amount; + rem.size.height -= amount; + break; + + case CGMaxYEdge: + slice.origin.y = _CGRectGetMaxY(slice) - amount; + slice.size.height = amount; + rem.size.height -= amount; + } +} + +/*! + Returns a \c BOOL indicating whether CGRect \c lhsRect + contains CGRect \c rhsRect. + @group CGRect + @param lhsRect the CGRect to test if \c rhsRect is inside of + @param rhsRect the CGRect to test if it fits inside \c lhsRect. + @return BOOL \c YES if \c rhsRect fits inside \c lhsRect. +*/ +function CGRectContainsRect(lhsRect, rhsRect) +{ + var union = CGRectUnion(lhsRect, rhsRect); + + return _CGRectEqualToRect(union, lhsRect); +} + +/*! + Returns \c YES if the two rectangles intersect + @group CGRect + @param lhsRect the first CGRect + @param rhsRect the second CGRect + @return BOOL \c YES if the two rectangles have any common spaces, and \c NO, otherwise. +*/ +function CGRectIntersectsRect(lhsRect, rhsRect) +{ + var intersection = CGRectIntersection(lhsRect, rhsRect); + + return !_CGRectIsEmpty(intersection); +} + +/*! + Makes the origin and size of a CGRect all integers. Specifically, by making + the southwest corner the origin (rounded down), and the northeast corner a CGSize (rounded up). + @param aRect the rectangle to operate on + @return CGRect the modified rectangle (same as the input) + @group CGRect +*/ +function CGRectIntegral(aRect) +{ + aRect = CGRectStandardize(aRect); + + // Store these out separately, if not the GetMaxes will return incorrect values. + var x = FLOOR(_CGRectGetMinX(aRect)), + y = FLOOR(_CGRectGetMinY(aRect)); + + aRect.size.width = CEIL(_CGRectGetMaxX(aRect)) - x; + aRect.size.height = CEIL(_CGRectGetMaxY(aRect)) - y; + + aRect.origin.x = x; + aRect.origin.y = y; + + return aRect; +} + +/*! + Returns the intersection of the two provided rectangles as a new rectangle. + @param lhsRect the first rectangle used for calculation + @param rhsRect the second rectangle used for calculation + @return CGRect the intersection of the two rectangles + @group CGRect +*/ +function CGRectIntersection(lhsRect, rhsRect) +{ + var intersection = _CGRectMake( + MAX(_CGRectGetMinX(lhsRect), _CGRectGetMinX(rhsRect)), + MAX(_CGRectGetMinY(lhsRect), _CGRectGetMinY(rhsRect)), + 0, 0); + + intersection.size.width = MIN(_CGRectGetMaxX(lhsRect), _CGRectGetMaxX(rhsRect)) - _CGRectGetMinX(intersection); + intersection.size.height = MIN(_CGRectGetMaxY(lhsRect), _CGRectGetMaxY(rhsRect)) - _CGRectGetMinY(intersection); + + return _CGRectIsEmpty(intersection) ? _CGRectMakeZero() : intersection; +} + +/* + +*/ +function CGRectStandardize(aRect) +{ + var width = _CGRectGetWidth(aRect), + height = _CGRectGetHeight(aRect), + standardized = _CGRectMakeCopy(aRect); + + if (width < 0.0) + { + standardized.origin.x += width; + standardized.size.width = -width; + } + + if (height < 0.0) + { + standardized.origin.y += height; + standardized.size.height = -height; + } + + return standardized; +} + +function CGRectUnion(lhsRect, rhsRect) +{ + var lhsRectIsNull = !lhsRect || lhsRect === CGRectNull, + rhsRectIsNull = !rhsRect || rhsRect === CGRectNull; + + if (lhsRectIsNull) + return rhsRectIsNull ? CGRectNull : rhsRect; + + if (rhsRectIsNull) + return lhsRectIsNull ? CGRectNull : lhsRect; + + var minX = MIN(_CGRectGetMinX(lhsRect), _CGRectGetMinX(rhsRect)), + minY = MIN(_CGRectGetMinY(lhsRect), _CGRectGetMinY(rhsRect)), + maxX = MAX(_CGRectGetMaxX(lhsRect), _CGRectGetMaxX(rhsRect)), + maxY = MAX(_CGRectGetMaxY(lhsRect), _CGRectGetMaxY(rhsRect)); + + return _CGRectMake(minX, minY, maxX - minX, maxY - minY); +} + +/*! + Returns the specified rectangle inset by the given CGInset as a new rectangle. + @param aRect the rect to inset + @param anInset a CGInset to inset by + @return CGRect aRect inset by anInset +*/ +_function(CGRectInsetByInset(aRect, anInset)); + +function CGPointFromString(aString) +{ + var comma = aString.indexOf(','); + + return { x:parseFloat(aString.substr(1, comma - 1)), y:parseFloat(aString.substring(comma + 1, aString.length)) }; +} + +function CGSizeFromString(aString) +{ + var comma = aString.indexOf(','); + + return { width:parseFloat(aString.substr(1, comma - 1)), height:parseFloat(aString.substring(comma + 1, aString.length)) }; +} + +function CGRectFromString(aString) +{ + var comma = aString.indexOf(',', aString.indexOf(',') + 1); + + return { origin:CGPointFromString(aString.substr(1, comma - 1)), size:CGSizeFromString(aString.substring(comma + 2, aString.length)) }; +} + +function CGPointFromEvent(anEvent) +{ + return _CGPointMake(anEvent.clientX, anEvent.clientY); +} + +/*! + Combines two insets by adding their individual elements and returns the result. + + @group CGInset +*/ +function CGInsetUnion(lhsInset, rhsInset) +{ + return _CGInsetMake(lhsInset.top + rhsInset.top, + lhsInset.right + rhsInset.right, + lhsInset.bottom + rhsInset.bottom, + lhsInset.left + rhsInset.left); +} + +/*! + Subtract one inset from another by subtracting their individual elements and returns the result. + + @group CGInset +*/ +function CGInsetDifference(lhsInset, rhsInset) +{ + return _CGInsetMake(lhsInset.top - rhsInset.top, + lhsInset.right - rhsInset.right, + lhsInset.bottom - rhsInset.bottom, + lhsInset.left - rhsInset.left); +} + +function CGInsetFromString(aString) +{ + var numbers = aString.substr(1, aString.length - 2).split(','); + + return _CGInsetMake(parseFloat(numbers[0]), parseFloat(numbers[1]), parseFloat(numbers[2]), parseFloat(numbers[3])); +} + +CGInsetFromCPString = CGInsetFromString; + +function CPStringFromCGInset(anInset) +{ + return '{' + anInset.top + ", " + anInset.left + ", " + anInset.bottom + ", " + anInset.right + '}'; +} + +/*! + @} +*/ diff --git a/Tests/AppKit/CPGeometryTest.j b/Tests/AppKit/CPGeometryTest.j deleted file mode 100644 index 50f91828e..000000000 --- a/Tests/AppKit/CPGeometryTest.j +++ /dev/null @@ -1,17 +0,0 @@ -@import - -@implementation CPGeometryTest : OJTestCase -{ -} - -- (void)testCPRectContainsRect -{ - [self assertTrue:CPRectContainsRect(CGRectMake(0, 0, 500, 500), CGRectMake(0, 0, 50, 50))]; - [self assertTrue:CPRectContainsRect(CGRectMake(0, 0, 500, 500), CGRectMake(50, 50, 50, 50))]; - [self assertTrue:CPRectContainsRect(CGRectMake(0, 0, 500, 500), CGRectMake(450, 450, 50, 50))]; - [self assertTrue:CPRectContainsRect(CGRectMake(0, 0, 500, 500), CGRectMake(500, 500, 0, 0))]; - [self assertFalse:CPRectContainsRect(CGRectMake(0, 0, 500, 500), CGRectMake(50, 50, 500, 500))]; - [self assertFalse:CPRectContainsRect(CGRectMake(0, 0, 500, 500), CGRectMake(500, 500, 1, 1))]; -} - -@end diff --git a/Tests/Foundation/CPGeometryTest.j b/Tests/Foundation/CPGeometryTest.j new file mode 100644 index 000000000..2aa9fb909 --- /dev/null +++ b/Tests/Foundation/CPGeometryTest.j @@ -0,0 +1,324 @@ +@import + +@implementation CPGeometryTest : OJTestCase +{ +} + +- (void)testCPRectContainsRect +{ + [self assertTrue:CPRectContainsRect(CGRectMake(0, 0, 500, 500), CGRectMake(0, 0, 50, 50))]; + [self assertTrue:CPRectContainsRect(CGRectMake(0, 0, 500, 500), CGRectMake(50, 50, 50, 50))]; + [self assertTrue:CPRectContainsRect(CGRectMake(0, 0, 500, 500), CGRectMake(450, 450, 50, 50))]; + [self assertTrue:CPRectContainsRect(CGRectMake(0, 0, 500, 500), CGRectMake(500, 500, 0, 0))]; + [self assertFalse:CPRectContainsRect(CGRectMake(0, 0, 500, 500), CGRectMake(50, 50, 500, 500))]; + [self assertFalse:CPRectContainsRect(CGRectMake(0, 0, 500, 500), CGRectMake(500, 500, 1, 1))]; +} + +- (void)testCPPointEqualToPoint +{ + [self assertTrue:CPPointEqualToPoint(CGPointMake(10, 10), CGPointMake(10, 10))]; + [self assertFalse:CPPointEqualToPoint(CGPointMake(0, 10), CGPointMake(10, 10))]; + [self assertFalse:CPPointEqualToPoint(CGPointMake(10, 10), CGPointMake(0, 10))]; + [self assertFalse:CPPointEqualToPoint(CGPointMake(10, 0), CGPointMake(10, 10))]; + [self assertFalse:CPPointEqualToPoint(CGPointMake(10, 10), CGPointMake(10, 0))]; +} + +- (void)testCPRectEqualToRect +{ + [self assertTrue:CPRectEqualToRect(CGRectMake(0, 0, 100, 100), CGRectMake(0, 0, 100, 100))]; + [self assertFalse:CPRectEqualToRect(CGRectMake(50, 0, 100, 100), CGRectMake(0, 0, 100, 100))]; + [self assertFalse:CPRectEqualToRect(CGRectMake(0, 0, 100, 100), CGRectMake(50, 0, 100, 100))]; + [self assertFalse:CPRectEqualToRect(CGRectMake(0, 50, 100, 100), CGRectMake(0, 0, 100, 100))]; + [self assertFalse:CPRectEqualToRect(CGRectMake(0, 0, 100, 100), CGRectMake(0, 50, 100, 100))]; + [self assertFalse:CPRectEqualToRect(CGRectMake(0, 0, 50, 100), CGRectMake(0, 0, 100, 100))]; + [self assertFalse:CPRectEqualToRect(CGRectMake(0, 0, 100, 100), CGRectMake(0, 0, 50, 100))]; + [self assertFalse:CPRectEqualToRect(CGRectMake(0, 0, 100, 50), CGRectMake(0, 0, 100, 100))]; + [self assertFalse:CPRectEqualToRect(CGRectMake(0, 0, 100, 100), CGRectMake(0, 0, 100, 50))]; +} + +- (void)textCPRectIsEmpty +{ + [self assertTrue:CPRectIsEmpty(CGRectMake(0, 0, 0, 10))]; + [self assertTrue:CPRectIsEmpty(CGRectMake(0, 0, 10, 0))]; + [self assertTrue:CPRectIsEmpty(CGRectMake(0, 0, -10, 10))]; + [self assertTrue:CPRectIsEmpty(CGRectMake(0, 0, 10, -10))]; + [self assertFalse:CPRectIsEmpty(CGRectMake(0, 0, 10, 10))]; +} + +- (void)testCPRectIntersection +{ + var lhsRect = CGRectMake(0, 0, 20, 20), + rhsRect = CGRectMake(10, 10, 20, 20), + intersectionResultRect = CGRectMake(10, 10, 10, 10); + + [self assertTrue:CPRectEqualToRect(CPRectIntersection(lhsRect, rhsRect), intersectionResultRect)]; + intersectionResultRect.origin.x = 30; + intersectionResultRect.origin.y = 30; + [self assertFalse:CPRectEqualToRect(CPRectIntersection(lhsRect, rhsRect), intersectionResultRect)]; +} + +- (void)testCPPointCreateCopy +{ + var point = CGPointMake(1, 1), + copiedPoint = CPPointCreateCopy(point); + [self assertTrue:CPPointEqualToPoint(point, copiedPoint)]; + point.x = 10; + [self assertFalse:CPPointEqualToPoint(point, copiedPoint)]; +} + +- (void)testCPPointMake +{ + var testPoint = CPPointMake(1, 2); + [self assert:1 equals:testPoint.x message:"point x coordinate failed"]; + [self assert:2 equals:testPoint.y message:"point y coordinate failed"]; +} + +- (void)testCPRectInset +{ + var testRect = CGRectMake(0, 0, 100, 100); + [self assertTrue:CGRectEqualToRect(CGRectMake(10, 10, 80, 80), CPRectInset(testRect, 10, 10))]; +} + +- (void)testCPRectIntegral +{ + var rect = CGRectMake(0.1, 0.2, 10.3, 10.4); + [self assertTrue:CGRectEqualToRect(CGRectMake(0, 0, 11, 11), CPRectIntegral(rect))]; +} + +- (void)testCPRectCreateCopy +{ + var rect = CGRectMake(0, 0, 100, 100), + copiedRect = CPRectCreateCopy(rect); + [self assertTrue:CGRectEqualToRect(rect, copiedRect)]; + rect.origin.x = 10; + [self assertFalse:CGRectEqualToRect(rect, copiedRect)]; +} + +- (void)testCPRectMake +{ + var rect = CPRectMake(10, 20, 30, 40); + [self assert:10 equals:rect.origin.x message:"Rect x coordinate failed"]; + [self assert:20 equals:rect.origin.y message:"Rect x coordinate failed"]; + [self assert:30 equals:rect.size.width message:"Rect width failed"]; + [self assert:40 equals:rect.size.height message:"Rect height failed"]; +} + +- (void)testCPRectOffset +{ + var initialRect = CGRectMake(10, 10, 100, 100), + offsetRect = CGRectMake(20, 20, 100, 100); + [self assertTrue:CGRectEqualToRect(offsetRect, CPRectOffset(initialRect, 10, 10))]; + [self assertFalse:CGRectEqualToRect(initialRect, CPRectOffset(initialRect, 10, 10))]; +} + +- (void)testCPRectStandardize +{ + var initialRect = CGRectMake(10, 10, -10, -10), + standardizedRect = CGRectMake(0, 0, 10, 10); + [self assertTrue:CGRectEqualToRect(standardizedRect,CPRectStandardize(initialRect))]; + [self assertFalse:CGRectEqualToRect(initialRect,CPRectStandardize(initialRect))]; +} + +- (void)testCPRectUnion +{ + var firstRect = CGRectMake(0, 0, 10, 10), + secondRect = CGRectMake(20, 20, 10, 10), + unitedRect = CGRectMake(0, 0, 30, 30); + [self assertTrue:CGRectEqualToRect(unitedRect, CPRectUnion(firstRect, secondRect))]; + [self assertFalse:CGRectEqualToRect(firstRect, CPRectUnion(firstRect, secondRect))]; +} + +- (void)testCPSizeCreateCopy +{ + var initialSize = CGSizeMake(100, 200), + copiedSize = CPSizeCreateCopy(initialSize); + [self assertTrue:CGSizeEqualToSize(initialSize, copiedSize)]; + initialSize.width = 10; + [self assertFalse:CGSizeEqualToSize(initialSize, copiedSize)]; +} + +- (void)testCPSizeMake +{ + var size = CPSizeMake(10, 20); + [self assert:10 equals:size.width message:"Size width failed"]; + [self assert:20 equals:size.height message:"Size height failed"]; +} + +- (void)testCPRectContainsPoint +{ + var rect = CGRectMake(10, 10, 10, 10); + [self assertTrue:CPRectContainsPoint(rect, CGPointMake(15, 15))]; + [self assertFalse:CPRectContainsPoint(rect, CGPointMake(5, 5))]; + [self assertFalse:CPRectContainsPoint(rect, CGPointMake(15, 5))]; + [self assertFalse:CPRectContainsPoint(rect, CGPointMake(25, 5))]; + [self assertFalse:CPRectContainsPoint(rect, CGPointMake(5, 15))]; + [self assertFalse:CPRectContainsPoint(rect, CGPointMake(5, 25))]; + [self assertFalse:CPRectContainsPoint(rect, CGPointMake(15, 25))]; + [self assertFalse:CPRectContainsPoint(rect, CGPointMake(25, 15))]; + [self assertFalse:CPRectContainsPoint(rect, CGPointMake(25, 25))]; +} + +- (void)testCPRectGetHeight +{ + var rect = CGRectMake(10, 20, 30, 40); + [self assert:40 equals:CPRectGetHeight(rect)]; +} + +- (void)testCPRectGetMaxX +{ + var rect = CGRectMake(10, 20, 40, 80); + [self assert:50 equals:CPRectGetMaxX(rect)]; +} + +- (void)testCPRectGetMaxY +{ + var rect = CGRectMake(10, 20, 40, 80); + [self assert:100 equals:CPRectGetMaxY(rect)]; +} + +- (void)testCPRectGetMidX +{ + var rect = CGRectMake(10, 20, 40, 80); + [self assert:30 equals:CPRectGetMidX(rect)]; +} + +- (void)testCPRectGetMidY +{ + var rect = CGRectMake(10, 20, 40, 80); + [self assert:60 equals:CPRectGetMidY(rect)]; +} + +- (void)testCPRectGetMinX +{ + var rect = CGRectMake(10, 20, 40, 80); + [self assert:10 equals:CPRectGetMinX(rect)]; +} + +- (void)testCPRectGetMinY +{ + var rect = CGRectMake(10, 20, 40, 80); + [self assert:20 equals:CPRectGetMinY(rect)]; +} + +- (void)testCPRectGetWidth +{ + var rect = CGRectMake(10, 20, 40, 80); + [self assert:40 equals:CPRectGetWidth(rect)]; +} + +- (void)testCPRectIntersectsRect +{ + var firstRect = CGRectMake(10, 10, 10, 10), + secondRect = CGRectMake(15, 15, 10, 10); + [self assertTrue:CPRectIntersectsRect(firstRect, secondRect)]; + secondRect.origin = CGPointMake(25, 25); + [self assertFalse:CPRectIntersectsRect(firstRect, secondRect)]; +} + +- (void)testCPRectIsNull +{ + [self assertTrue:CPRectIsNull(CGRectMake(0, 0, 0, 10))]; + [self assertTrue:CPRectIsNull(CGRectMake(0, 0, 10, 0))]; + [self assertTrue:CPRectIsNull(CGRectMake(0, 0, -10, 10))]; + [self assertTrue:CPRectIsNull(CGRectMake(0, 0, 10, -10))]; + [self assertFalse:CPRectIsNull(CGRectMake(0, 0, 10, 10))]; +} + +- (void)testCPDivideRect +{ + var initialRect = CGRectMake(0, 0, 10, 10), + slice = CGRectMake(0, 0, 0, 0), + rem = CGRectMake(0, 0, 0, 0); + + CPDivideRect(initialRect, slice, rem, 3, CGMinXEdge); + [self assertTrue:CGRectEqualToRect(slice, CGRectMake(0, 0, 3, 10))]; + [self assertTrue:CGRectEqualToRect(rem, CGRectMake(3, 0, 7, 10))]; + + CPDivideRect(initialRect, slice, rem, 3, CGMinYEdge); + [self assertTrue:CGRectEqualToRect(slice, CGRectMake(0, 0, 10, 3))]; + [self assertTrue:CGRectEqualToRect(rem, CGRectMake(0, 3, 10, 7))]; + + CPDivideRect(initialRect, slice, rem, 3, CGMaxXEdge); + [self assertTrue:CGRectEqualToRect(slice, CGRectMake(7, 0, 3, 10))]; + [self assertTrue:CGRectEqualToRect(rem, CGRectMake(0, 0, 7, 10))]; + + CPDivideRect(initialRect, slice, rem, 3, CGMaxYEdge); + [self assertTrue:CGRectEqualToRect(slice, CGRectMake(0, 7, 10, 3))]; + [self assertTrue:CGRectEqualToRect(rem, CGRectMake(0, 0, 10, 7))]; +} + +- (void)testCPSizeEqualToSize +{ + [self assertTrue:CPSizeEqualToSize(CGSizeMake(10, 10), CGSizeMake(10, 10))]; + [self assertFalse:CPSizeEqualToSize(CGSizeMake(0, 10), CGSizeMake(10, 10))]; + [self assertFalse:CPSizeEqualToSize(CGSizeMake(10, 10), CGSizeMake(0, 10))]; + [self assertFalse:CPSizeEqualToSize(CGSizeMake(10, 0), CGSizeMake(10, 10))]; + [self assertFalse:CPSizeEqualToSize(CGSizeMake(10, 10), CGSizeMake(10, 0))]; +} + +- (void)testCPStringFromPoint +{ + var point = CGPointMake(0, 1); + [self assert:"{0, 1}" equals: CPStringFromPoint(point)]; +} + +- (void)testCPStringFromSize +{ + var size = CGSizeMake(10, 20); + [self assert:"{10, 20}" equals: CPStringFromSize(size)]; +} + +- (void)testCPStringFromRect +{ + var rect = CGRectMake(10, 20, 30, 40); + [self assert:"{{10, 20}, {30, 40}}" equals:CPStringFromRect(rect)]; +} + +- (void)testCPPointFromString +{ + var point = CGPointMake(0,1); + [self assertTrue:CGPointEqualToPoint(point , CPPointFromString("{0, 1}"))]; +} + +- (void)testCPSizeFromString +{ + var size = CGSizeMake(10, 20); + [self assertTrue:CGSizeEqualToSize(size, CPSizeFromString("{10, 20}"))]; +} + +- (void)testCPRectFromString +{ + var rect = CGRectMake(10, 20, 30, 40); + [self assertTrue:CGRectEqualToRect(rect, CPRectFromString("{{10, 20}, {30, 40}}"))]; +} + +- (void)testCPSizeMakeZero +{ + var size = CGSizeMake(10, 20); + [self assertTrue:CGSizeEqualToSize(CGSizeMake(0, 0), CPSizeMakeZero(size))]; +} + +- (void)testCPRectMakeZero +{ + var rect = CGRectMake(10, 20, 30, 40); + [self assertTrue:CGRectEqualToRect(CGRectMake(0, 0, 0, 0), CPRectMakeZero(rect))]; +} + +- (void)testCPPointMakeZero +{ + var point = CGPointMake(10, 20); + [self assertTrue:CGPointEqualToPoint(CGPointMake(0, 0), CPPointMakeZero(0, 0))]; +} + +- (void)testCPPointFromEvent +{ + var anEvent = [CPEvent mouseEventWithType:CPLeftMouseDownMask location:CGPointMake(5.0, 5.0) modifierFlags:0 + timestamp:0 windowNumber:0 context:nil eventNumber:0 clickCount:1 pressure:1]; + anEvent.clientX = 1.0; + anEvent.clientY = 2.0; + var point = CPPointFromEvent(anEvent); + [self assert:1.0 equals:point.x message:"Point x coordinate failed"]; + [self assert:2.0 equals:point.y message:"Point y coordinate failed"]; +} + +@end