views:

109

answers:

1

I'm quite new to objective-j and the framework so I was attempting to drill down and understand some code, but I'm getting lost with the object (or struct) CGRect.

Here is the full documentation: Cappuccino

Ok, now I'm drilling down and I try to learn how the CPView class works: CPView.j

Now, I see CGRect objects (or structures) everywhere and I simply CANNOT for the life me figure out where CGRect is defined. How does this work? I feel like I am missing something.

The closest thing I've found is CGGeometry.j and CGGeometry.h, but still no CGRect structure or object is defined.

Can someone help me out? How is it possible to instantiate a CGRect struct or object and where is it defined? What does it contain? etc etc...

I know that the main way to create a CGRect is to use the CGRectMake function as stated here.

I just don't feel like I have a grasp on how this is working.

Thank you.

Update:

After looking at the code some more: line 23, line 30, and line 37, seem to look kinda like JavaScript object descriptions. Is this correct? I'm not sure... is whatever that is translating these .j files aware of what a CGRect is already? Is this the reason we can use (CGRect) type castes and objects? CGRects seem like they are not the same as any other object I've seen used so far, and I'd like to know why :).

Thanks, again.

00023 #define _CGPointMake(x_, y_) { x:x_, y:y_ }
00024 #define _CGPointMakeCopy(aPoint) _CGPointMake(aPoint.x, aPoint.y)
00025 #define _CGPointMakeZero() _CGPointMake(0.0, 0.0)
00026 
00027 #define _CGPointEqualToPoint(lhsPoint, rhsPoint) (lhsPoint.x == rhsPoint.x && lhsPoint.y == rhsPoint.y)
00028 #define _CGStringFromPoint(aPoint) ("{" + aPoint.x + ", " + aPoint.y + "}")
00029 
00030 #define _CGSizeMake(width_, height_) { width:width_, height:height_ }
00031 #define _CGSizeMakeCopy(aSize) _CGSizeMake(aSize.width, aSize.height)
00032 #define _CGSizeMakeZero() _CGSizeMake(0.0, 0.0)
00033 
00034 #define _CGSizeEqualToSize(lhsSize, rhsSize) (lhsSize.width == rhsSize.width && lhsSize.height == rhsSize.height)
00035 #define _CGStringFromSize(aSize) ("{" + aSize.width + ", " + aSize.height + "}")
00036 
00037 #define _CGRectMake(x, y, width, height) { origin: _CGPointMake(x, y), size: _CGSizeMake(width, height) }
+1  A: 

The short answer is yes, CGRects are unlike other Cappuccino objects. CGRects are not CPObjects but simply Javascript objects with x, y, width and height members. When you create one, such as with CGRectMake, you're actually invoking a simple function which looks something like this:

function CGRectMake(_x, _y, _width, _height) 
{ 
    return { 
        origin: { x:_x, y:_y }, 
        size: { width: _width, height: _height } 
    };
}

The declarations you found are the macros which Cappuccino itself uses to inline the CGRectMake function for better performance. The macros are used at the end of the day to actually define the exported CGRectMake function used in your own code (which is why my snippet above is merely an approximation.)

Alexander Ljungberg