PageRenderTime 23ms CodeModel.GetById 14ms app.highlight 3ms RepoModel.GetById 2ms app.codeStats 0ms

/Frameworks/Debug/AppKit/browser.platform/CALayer.j

http://github.com/jfahrenkrug/MapKit-HelloWorld
Unknown | 764 lines | 751 code | 13 blank | 0 comment | 0 complexity | f4cf7c0559379c3a303a4cbf5c0b2884 MD5 | raw file
  1I;21;Foundation/CPObject.jI;23;AppKit/CABackingStore.jI;18;AppKit/CGContext.jI;19;AppKit/CGGeometry.ji;12;CGGeometry.jc;36412;
  2CGAffineTransformMake= function(a, b, c, d, tx, ty) { return { a:a, b:b, c:c, d:d, tx:tx, ty:ty }; }
  3CGAffineTransformMakeIdentity= function() { return { a:1.0, b:0.0, c:0.0, d:1.0, tx:0.0, ty:0.0 }; }
  4CGAffineTransformMakeCopy= function(anAffineTransform) { return { a:anAffineTransform.a, b:anAffineTransform.b, c:anAffineTransform.c, d:anAffineTransform.d, tx:anAffineTransform.tx, ty:anAffineTransform.ty }; }
  5CGAffineTransformMakeScale= function(sx, sy) { return { a:sx, b:0.0, c:0.0, d:sy, tx:0.0, ty:0.0 }; }
  6CGAffineTransformMakeTranslation= function(tx, ty) { return { a:1.0, b:0.0, c:0.0, d:1.0, tx:tx, ty:ty }; }
  7CGAffineTransformTranslate= function(aTransform, tx, ty) { return { a:aTransform.a, b:aTransform.b, c:aTransform.c, d:aTransform.d, tx:aTransform.tx + aTransform.a * tx + aTransform.c * ty, ty:aTransform.ty + aTransform.b * tx + aTransform.d * ty }; }
  8CGAffineTransformScale= function(aTransform, sx, sy) { return { a:aTransform.a * sx, b:aTransform.b * sx, c:aTransform.c * sy, d:aTransform.d * sy, tx:aTransform.tx, ty:aTransform.ty }; }
  9CGAffineTransformConcat= function(lhs, rhs) { return { a:lhs.a * rhs.a + lhs.b * rhs.c, b:lhs.a * rhs.b + lhs.b * rhs.d, c:lhs.c * rhs.a + lhs.d * rhs.c, d:lhs.c * rhs.b + lhs.d * rhs.d, tx:lhs.tx * rhs.a + lhs.ty * rhs.c + rhs.tx, ty:lhs.tx * rhs.b + lhs.ty * rhs.d + rhs.ty }; }
 10CGPointApplyAffineTransform= function(aPoint, aTransform) { return { x:aPoint.x * aTransform.a + aPoint.y * aTransform.c + aTransform.tx, y:aPoint.x * aTransform.b + aPoint.y * aTransform.d + aTransform.ty }; }
 11CGSizeApplyAffineTransform= function(aSize, aTransform) { return { width:aSize.width * aTransform.a + aSize.height * aTransform.c, height:aSize.width * aTransform.b + aSize.height * aTransform.d }; }
 12CGAffineTransformIsIdentity= function(aTransform) { return (aTransform.a == 1 && aTransform.b == 0 && aTransform.c == 0 && aTransform.d == 1 && aTransform.tx == 0 && aTransform.ty == 0); }
 13CGAffineTransformEqualToTransform= function(lhs, rhs) { return (lhs.a == rhs.a && lhs.b == rhs.b && lhs.c == rhs.c && lhs.d == rhs.d && lhs.tx == rhs.tx && lhs.ty == rhs.ty); }
 14CGStringCreateWithCGAffineTransform= function(aTransform) { return (" [[ " + aTransform.a + ", " + aTransform.b + ", 0 ], [ " + aTransform.c + ", " + aTransform.d + ", 0 ], [ " + aTransform.tx + ", " + aTransform.ty + ", 1]]"); }
 15CGAffineTransformCreateCopy= function(aTransform)
 16{
 17    return { a:aTransform.a, b:aTransform.b, c:aTransform.c, d:aTransform.d, tx:aTransform.tx, ty:aTransform.ty };
 18}
 19CGAffineTransformMakeRotation= function(anAngle)
 20{
 21    var sin = SIN(anAngle),
 22        cos = COS(anAngle);
 23    return { a:cos, b:sin, c:-sin, d:cos, tx:0.0, ty:0.0 };
 24}
 25CGAffineTransformRotate= function(aTransform, anAngle)
 26{
 27    var sin = SIN(anAngle),
 28        cos = COS(anAngle);
 29    return {
 30            a:aTransform.a * cos + aTransform.c * sin,
 31            b:aTransform.b * cos + aTransform.d * sin,
 32            c:aTransform.c * cos - aTransform.a * sin,
 33            d:aTransform.d * cos - aTransform.b * sin,
 34            tx:aTransform.tx,
 35            ty:aTransform.ty
 36        };
 37}
 38CGAffineTransformInvert= function(aTransform)
 39{
 40    var determinant = 1 / (aTransform.a * aTransform.d - aTransform.b * aTransform.c);
 41    return {
 42        a:determinant * aTransform.d,
 43        b:-determinant * aTransform.b,
 44        c:-determinant * aTransform.c,
 45        d:determinant * aTransform.a,
 46        tx:determinant * (aTransform.c * aTransform.ty - aTransform.d * aTransform.tx),
 47        ty:determinant * (aTransform.b * aTransform.tx - aTransform.a * aTransform.ty)
 48    };
 49}
 50CGRectApplyAffineTransform= function(aRect, anAffineTransform)
 51{
 52    var top = (aRect.origin.y),
 53        left = (aRect.origin.x),
 54        right = (aRect.origin.x + aRect.size.width),
 55        bottom = (aRect.origin.y + aRect.size.height),
 56        topLeft = CGPointApplyAffineTransform({ x:left, y:top }, anAffineTransform),
 57        topRight = CGPointApplyAffineTransform({ x:right, y:top }, anAffineTransform),
 58        bottomLeft = CGPointApplyAffineTransform({ x:left, y:bottom }, anAffineTransform),
 59        bottomRight = CGPointApplyAffineTransform({ x:right, y:bottom }, anAffineTransform),
 60        minX = MIN(topLeft.x, topRight.x, bottomLeft.x, bottomRight.x),
 61        maxX = MAX(topLeft.x, topRight.x, bottomLeft.x, bottomRight.x),
 62        minY = MIN(topLeft.y, topRight.y, bottomLeft.y, bottomRight.y),
 63        maxY = MAX(topLeft.y, topRight.y, bottomLeft.y, bottomRight.y);
 64    return { origin: { x:minX, y:minY }, size: { width:(maxX - minX), height:(maxY - minY) } };
 65}
 66CPStringFromCGAffineTransform= function(anAffineTransform)
 67{
 68    return '{' + anAffineTransform.a + ", " + anAffineTransform.b + ", " + anAffineTransform.c + ", " + anAffineTransform.d + ", " + anAffineTransform.tx + ", " + anAffineTransform.ty + '}';
 69}
 70var CALayerGeometryBoundsMask = 1,
 71    CALayerGeometryPositionMask = 2,
 72    CALayerGeometryAnchorPointMask = 4,
 73    CALayerGeometryAffineTransformMask = 8,
 74    CALayerGeometryParentSublayerTransformMask = 16;
 75var USE_BUFFER = NO;
 76var CALayerFrameOriginUpdateMask = 1,
 77    CALayerFrameSizeUpdateMask = 2,
 78    CALayerZPositionUpdateMask = 4,
 79    CALayerDisplayUpdateMask = 8,
 80    CALayerCompositeUpdateMask = 16,
 81    CALayerDOMUpdateMask = CALayerZPositionUpdateMask | CALayerFrameOriginUpdateMask | CALayerFrameSizeUpdateMask;
 82var CALayerRegisteredRunLoopUpdates = nil;
 83{var the_class = objj_allocateClassPair(CPObject, "CALayer"),
 84meta_class = the_class.isa;class_addIvars(the_class, [new objj_ivar("_frame"), new objj_ivar("_bounds"), new objj_ivar("_position"), new objj_ivar("_zPosition"), new objj_ivar("_anchorPoint"), new objj_ivar("_affineTransform"), new objj_ivar("_sublayerTransform"), new objj_ivar("_sublayerTransformForSublayers"), new objj_ivar("_backingStoreFrame"), new objj_ivar("_standardBackingStoreFrame"), new objj_ivar("_hasSublayerTransform"), new objj_ivar("_hasCustomBackingStoreFrame"), new objj_ivar("_opacity"), new objj_ivar("_isHidden"), new objj_ivar("_backgroundColor"), new objj_ivar("_superlayer"), new objj_ivar("_sublayers"), new objj_ivar("_runLoopUpdateMask"), new objj_ivar("_needsDisplayOnBoundsChange"), new objj_ivar("_delegate"), new objj_ivar("_delegateRespondsToDisplayLayerSelector"), new objj_ivar("_delegateRespondsToDrawLayerInContextSelector"), new objj_ivar("_DOMElement"), new objj_ivar("_DOMContentsElement"), new objj_ivar("_contents"), new objj_ivar("_context"), new objj_ivar("_owningView"), new objj_ivar("_transformToLayer"), new objj_ivar("_transformFromLayer")]);
 85objj_registerClassPair(the_class);
 86objj_addClassForBundle(the_class, objj_getBundleWithPath(OBJJ_CURRENT_BUNDLE.path));
 87class_addMethods(the_class, [new objj_method(sel_getUid("init"), function $CALayer__init(self, _cmd)
 88{ with(self)
 89{
 90    self = objj_msgSendSuper({ receiver:self, super_class:objj_getClass("CPObject") }, "init");
 91
 92    if (self)
 93    {
 94        _frame = CGRectMakeZero();
 95
 96        _backingStoreFrame = CGRectMakeZero();
 97        _standardBackingStoreFrame = CGRectMakeZero();
 98
 99        _bounds = CGRectMakeZero();
100        _position = CGPointMakeZero();
101        _zPosition = 0.0;
102        _anchorPoint = CGPointMake(0.5, 0.5);
103        _affineTransform = CGAffineTransformMakeIdentity();
104        _sublayerTransform = CGAffineTransformMakeIdentity();
105
106        _transformToLayer = CGAffineTransformMakeIdentity();
107        _transformFromLayer = CGAffineTransformMakeIdentity();
108
109        _opacity = 1.0;
110        _isHidden = NO;
111        _masksToBounds = NO;
112
113        _sublayers = [];
114
115        _DOMElement = document.createElement("div");
116
117        _DOMElement.style.overflow = "visible";
118        _DOMElement.style.position = "absolute";
119        _DOMElement.style.visibility = "visible";
120        _DOMElement.style.top = "0px";
121        _DOMElement.style.left = "0px";
122        _DOMElement.style.zIndex = 0;
123        _DOMElement.style.width = "0px";
124        _DOMElement.style.height = "0px";
125    }
126
127    return self;
128}
129}), new objj_method(sel_getUid("setBounds:"), function $CALayer__setBounds_(self, _cmd, aBounds)
130{ with(self)
131{
132    if (CGRectEqualToRect(_bounds, aBounds))
133        return;
134
135    var oldOrigin = _bounds.origin;
136
137    _bounds = { origin: { x:aBounds.origin.x, y:aBounds.origin.y }, size: { width:aBounds.size.width, height:aBounds.size.height } };
138
139    if (_hasSublayerTransform)
140        _CALayerUpdateSublayerTransformForSublayers(self);
141    _CALayerRecalculateGeometry(self, CALayerGeometryBoundsMask);
142}
143}), new objj_method(sel_getUid("bounds"), function $CALayer__bounds(self, _cmd)
144{ with(self)
145{
146    return _bounds;
147}
148}), new objj_method(sel_getUid("setPosition:"), function $CALayer__setPosition_(self, _cmd, aPosition)
149{ with(self)
150{
151    if (CGPointEqualToPoint(_position, aPosition))
152        return;
153    _position = { x:aPosition.x, y:aPosition.y };
154    _CALayerRecalculateGeometry(self, CALayerGeometryPositionMask);
155}
156}), new objj_method(sel_getUid("position"), function $CALayer__position(self, _cmd)
157{ with(self)
158{
159    return _position;
160}
161}), new objj_method(sel_getUid("setZPosition:"), function $CALayer__setZPosition_(self, _cmd, aZPosition)
162{ with(self)
163{
164    if (_zPosition == aZPosition)
165        return;
166    _zPosition = aZPosition;
167    objj_msgSend(self, "registerRunLoopUpdateWithMask:", CALayerZPositionUpdateMask);
168}
169}), new objj_method(sel_getUid("setAnchorPoint:"), function $CALayer__setAnchorPoint_(self, _cmd, anAnchorPoint)
170{ with(self)
171{
172    anAnchorPoint = { x:anAnchorPoint.x, y:anAnchorPoint.y };
173    anAnchorPoint.x = MIN(1.0, MAX(0.0, anAnchorPoint.x));
174    anAnchorPoint.y = MIN(1.0, MAX(0.0, anAnchorPoint.y));
175    if (CGPointEqualToPoint(_anchorPoint, anAnchorPoint))
176        return;
177    _anchorPoint = anAnchorPoint;
178    if (_hasSublayerTransform)
179        _CALayerUpdateSublayerTransformForSublayers(self);
180    if (_owningView)
181        _position = CGPointMake((_bounds.size.width) * _anchorPoint.x, (_bounds.size.height) * _anchorPoint.y);
182    _CALayerRecalculateGeometry(self, CALayerGeometryAnchorPointMask);
183}
184}), new objj_method(sel_getUid("anchorPoint"), function $CALayer__anchorPoint(self, _cmd)
185{ with(self)
186{
187    return _anchorPoint;
188}
189}), new objj_method(sel_getUid("setAffineTransform:"), function $CALayer__setAffineTransform_(self, _cmd, anAffineTransform)
190{ with(self)
191{
192    if (CGAffineTransformEqualToTransform(_affineTransform, anAffineTransform))
193        return;
194    _affineTransform = { a:anAffineTransform.a, b:anAffineTransform.b, c:anAffineTransform.c, d:anAffineTransform.d, tx:anAffineTransform.tx, ty:anAffineTransform.ty };
195    _CALayerRecalculateGeometry(self, CALayerGeometryAffineTransformMask);
196}
197}), new objj_method(sel_getUid("affineTransform"), function $CALayer__affineTransform(self, _cmd)
198{ with(self)
199{
200    return _affineTransform;
201}
202}), new objj_method(sel_getUid("setSublayerTransform:"), function $CALayer__setSublayerTransform_(self, _cmd, anAffineTransform)
203{ with(self)
204{
205    if (CGAffineTransformEqualToTransform(_sublayerTransform, anAffineTransform))
206        return;
207    var hadSublayerTransform = _hasSublayerTransform;
208    _sublayerTransform = { a:anAffineTransform.a, b:anAffineTransform.b, c:anAffineTransform.c, d:anAffineTransform.d, tx:anAffineTransform.tx, ty:anAffineTransform.ty };
209    _hasSublayerTransform = !(_sublayerTransform.a == 1 && _sublayerTransform.b == 0 && _sublayerTransform.c == 0 && _sublayerTransform.d == 1 && _sublayerTransform.tx == 0 && _sublayerTransform.ty == 0);
210    if (_hasSublayerTransform)
211    {
212        _CALayerUpdateSublayerTransformForSublayers(self);
213        var index = _sublayers.length;
214        while (index--)
215            _CALayerRecalculateGeometry(_sublayers[index], CALayerGeometryParentSublayerTransformMask);
216    }
217}
218}), new objj_method(sel_getUid("sublayerTransform"), function $CALayer__sublayerTransform(self, _cmd)
219{ with(self)
220{
221    return _sublayerTransform;
222}
223}), new objj_method(sel_getUid("transformToLayer"), function $CALayer__transformToLayer(self, _cmd)
224{ with(self)
225{
226    return _transformToLayer;
227}
228}), new objj_method(sel_getUid("setFrame:"), function $CALayer__setFrame_(self, _cmd, aFrame)
229{ with(self)
230{
231    alert("FIXME IMPLEMENT");
232}
233}), new objj_method(sel_getUid("frame"), function $CALayer__frame(self, _cmd)
234{ with(self)
235{
236    if (!_frame)
237        _frame = objj_msgSend(self, "convertRect:toLayer:", _bounds, _superlayer);
238    return _frame;
239}
240}), new objj_method(sel_getUid("backingStoreFrame"), function $CALayer__backingStoreFrame(self, _cmd)
241{ with(self)
242{
243    return _backingStoreFrame;
244}
245}), new objj_method(sel_getUid("setBackingStoreFrame:"), function $CALayer__setBackingStoreFrame_(self, _cmd, aFrame)
246{ with(self)
247{
248    _hasCustomBackingStoreFrame = (aFrame != nil);
249    if (aFrame == nil)
250        aFrame = CGRectMakeCopy(_standardBackingStoreFrame);
251    else
252    {
253        if (_superlayer)
254        {
255            aFrame = objj_msgSend(_superlayer, "convertRect:toLayer:", aFrame, nil);
256            var bounds = objj_msgSend(_superlayer, "bounds"),
257                frame = objj_msgSend(_superlayer, "convertRect:toLayer:", bounds, nil);
258            aFrame.origin.x -= (frame.origin.x);
259            aFrame.origin.y -= (frame.origin.y);
260        }
261        else
262            aFrame = CGRectMakeCopy(aFrame);
263    }
264    if (!CGPointEqualToPoint(_backingStoreFrame.origin, aFrame.origin))
265        objj_msgSend(self, "registerRunLoopUpdateWithMask:", CALayerFrameOriginUpdateMask);
266    if (!CGSizeEqualToSize(_backingStoreFrame.size, aFrame.size))
267        objj_msgSend(self, "registerRunLoopUpdateWithMask:", CALayerFrameSizeUpdateMask);
268    _backingStoreFrame = aFrame;
269}
270}), new objj_method(sel_getUid("contents"), function $CALayer__contents(self, _cmd)
271{ with(self)
272{
273    return _contents;
274}
275}), new objj_method(sel_getUid("setContents:"), function $CALayer__setContents_(self, _cmd, contents)
276{ with(self)
277{
278    if (_contents == contents)
279        return;
280    _contents = contents;
281    objj_msgSend(self, "composite");
282}
283}), new objj_method(sel_getUid("composite"), function $CALayer__composite(self, _cmd)
284{ with(self)
285{
286    if (USE_BUFFER && !_contents || !_context)
287        return;
288    CGContextClearRect(_context, { origin: { x:0.0, y:0.0 }, size: { width:(_backingStoreFrame.size.width), height:(_backingStoreFrame.size.height) } });
289    var transform = _transformFromLayer;
290    if (_superlayer)
291    {
292        var superlayerTransform = _CALayerGetTransform(_superlayer, nil),
293            superlayerOrigin = CGPointApplyAffineTransform(_superlayer._bounds.origin, superlayerTransform);
294        transform = CGAffineTransformConcat(transform, superlayerTransform);
295        transform.tx -= superlayerOrigin.x;
296        transform.ty -= superlayerOrigin.y;
297    }
298    transform.tx -= (_backingStoreFrame.origin.x);
299    transform.ty -= (_backingStoreFrame.origin.y);
300    CGContextSaveGState(_context);
301    CGContextConcatCTM(_context, transform);
302    if (USE_BUFFER)
303    {
304        _context.drawImage(_contents.buffer, (_bounds.origin.x), (_bounds.origin.y));
305    }
306    else
307        objj_msgSend(self, "drawInContext:", _context);
308    CGContextRestoreGState(_context);
309}
310}), new objj_method(sel_getUid("display"), function $CALayer__display(self, _cmd)
311{ with(self)
312{
313    if (!_context)
314    {
315        _context = CGBitmapGraphicsContextCreate();
316        _DOMContentsElement = _context.DOMElement;
317        _DOMContentsElement.style.zIndex = -100;
318        _DOMContentsElement.style.overflow = "hidden";
319        _DOMContentsElement.style.position = "absolute";
320        _DOMContentsElement.style.visibility = "visible";
321        _DOMContentsElement.width = ROUND((_backingStoreFrame.size.width));
322        _DOMContentsElement.height = ROUND((_backingStoreFrame.size.height));
323        _DOMContentsElement.style.top = "0px";
324        _DOMContentsElement.style.left = "0px";
325        _DOMContentsElement.style.width = ROUND((_backingStoreFrame.size.width)) + "px";
326        _DOMContentsElement.style.height = ROUND((_backingStoreFrame.size.height)) + "px";
327        _DOMElement.appendChild(_DOMContentsElement);
328    }
329    if (USE_BUFFER)
330    {
331        if (_delegateRespondsToDisplayLayerSelector)
332            return objj_msgSend(_delegate, "displayInLayer:", self);
333        if ((_backingStoreFrame.size.width) == 0.0 || (_backingStoreFrame.size.height) == 0.0)
334            return;
335        if (!_contents)
336            _contents = CABackingStoreCreate();
337        CABackingStoreSetSize(_contents, _bounds.size);
338        objj_msgSend(self, "drawInContext:", CABackingStoreGetContext(_contents));
339    }
340    objj_msgSend(self, "composite");
341}
342}), new objj_method(sel_getUid("drawInContext:"), function $CALayer__drawInContext_(self, _cmd, aContext)
343{ with(self)
344{
345    if (_backgroundColor)
346    {
347        CGContextSetFillColor(aContext, _backgroundColor);
348        CGContextFillRect(aContext, _bounds);
349    }
350    if (_delegateRespondsToDrawLayerInContextSelector)
351        objj_msgSend(_delegate, "drawLayer:inContext:", self, aContext);
352}
353}), new objj_method(sel_getUid("opacity"), function $CALayer__opacity(self, _cmd)
354{ with(self)
355{
356    return _opacity;
357}
358}), new objj_method(sel_getUid("setOpacity:"), function $CALayer__setOpacity_(self, _cmd, anOpacity)
359{ with(self)
360{
361    if (_opacity == anOpacity)
362        return;
363    _opacity = anOpacity;
364    _DOMElement.style.opacity = anOpacity;
365    _DOMElement.style.filter = "alpha(opacity=" + anOpacity * 100 + ")";
366}
367}), new objj_method(sel_getUid("setHidden:"), function $CALayer__setHidden_(self, _cmd, isHidden)
368{ with(self)
369{
370    _isHidden = isHidden;
371    _DOMElement.style.display = isHidden ? "none" : "block";
372}
373}), new objj_method(sel_getUid("hidden"), function $CALayer__hidden(self, _cmd)
374{ with(self)
375{
376    return _isHidden;
377}
378}), new objj_method(sel_getUid("isHidden"), function $CALayer__isHidden(self, _cmd)
379{ with(self)
380{
381    return _isHidden;
382}
383}), new objj_method(sel_getUid("setMasksToBounds:"), function $CALayer__setMasksToBounds_(self, _cmd, masksToBounds)
384{ with(self)
385{
386    if (_masksToBounds == masksToBounds)
387        return;
388    _masksToBounds = masksToBounds;
389    _DOMElement.style.overflow = _masksToBounds ? "hidden" : "visible";
390}
391}), new objj_method(sel_getUid("setBackgroundColor:"), function $CALayer__setBackgroundColor_(self, _cmd, aColor)
392{ with(self)
393{
394    _backgroundColor = aColor;
395    objj_msgSend(self, "setNeedsDisplay");
396}
397}), new objj_method(sel_getUid("backgroundColor"), function $CALayer__backgroundColor(self, _cmd)
398{ with(self)
399{
400    return _backgroundColor;
401}
402}), new objj_method(sel_getUid("sublayers"), function $CALayer__sublayers(self, _cmd)
403{ with(self)
404{
405    return _sublayers;
406}
407}), new objj_method(sel_getUid("superlayer"), function $CALayer__superlayer(self, _cmd)
408{ with(self)
409{
410    return _superlayer;
411}
412}), new objj_method(sel_getUid("addSublayer:"), function $CALayer__addSublayer_(self, _cmd, aLayer)
413{ with(self)
414{
415    objj_msgSend(self, "insertSublayer:atIndex:", aLayer, _sublayers.length);
416    return;
417    if (_DOMContentsElement && aLayer._zPosition > _DOMContentsElement.style.zIndex) _DOMContentsElement.style.zIndex -= 100.0;;
418    objj_msgSend(_sublayers, "addObject:", aLayer);
419    _DOMElement.appendChild(aLayer._DOMElement);
420}
421}), new objj_method(sel_getUid("removeFromSuperlayer"), function $CALayer__removeFromSuperlayer(self, _cmd)
422{ with(self)
423{
424    if (_owningView)
425        objj_msgSend(_owningView, "setLayer:", nil);
426    if (!_superlayer)
427        return;
428    _superlayer._DOMElement.removeChild(_DOMElement);
429    objj_msgSend(_superlayer._sublayers, "removeObject:", self);
430    _superlayer = nil;
431}
432}), new objj_method(sel_getUid("insertSublayer:atIndex:"), function $CALayer__insertSublayer_atIndex_(self, _cmd, aLayer, anIndex)
433{ with(self)
434{
435    if (!aLayer)
436        return;
437    var superlayer = objj_msgSend(aLayer, "superlayer");
438    if (superlayer == self)
439    {
440        var index = objj_msgSend(_sublayers, "indexOfObjectIdenticalTo:", aLayer);
441        if (index == anIndex)
442            return;
443        objj_msgSend(_sublayers, "removeObjectAtIndex:", index);
444        if (index < anIndex)
445            --anIndex;
446    }
447    else if (superlayer != nil)
448        objj_msgSend(aLayer, "removeFromSuperlayer");
449    if (_DOMContentsElement && aLayer._zPosition > _DOMContentsElement.style.zIndex) _DOMContentsElement.style.zIndex -= 100.0;;
450    objj_msgSend(_sublayers, "insertObject:atIndex:", aLayer, anIndex);
451    if (anIndex >= _sublayers.length - 1)
452        _DOMElement.appendChild(aLayer._DOMElement);
453    else
454        _DOMElement.insertBefore(aLayer._DOMElement, _sublayers[anIndex + 1]._DOMElement);
455    aLayer._superlayer = self;
456    if (self != superlayer)
457        _CALayerRecalculateGeometry(aLayer, 0xFFFFFFF);
458}
459}), new objj_method(sel_getUid("insertSublayer:below:"), function $CALayer__insertSublayer_below_(self, _cmd, aLayer, aSublayer)
460{ with(self)
461{
462    var index = aSublayer ? objj_msgSend(_sublayers, "indexOfObjectIdenticalTo:", aSublayer) : 0;
463    objj_msgSend(self, "insertSublayer:atIndex:", aLayer, index == CPNotFound ? _sublayers.length : index);
464}
465}), new objj_method(sel_getUid("insertSublayer:above:"), function $CALayer__insertSublayer_above_(self, _cmd, aLayer, aSublayer)
466{ with(self)
467{
468    var index = aSublayer ? objj_msgSend(_sublayers, "indexOfObjectIdenticalTo:", aSublayer) : _sublayers.length;
469    if (index == CPNotFound)
470        objj_msgSend(CPException, "raise:reason:", "CALayerNotFoundException", "aSublayer is not a sublayer of this layer");
471    objj_msgSend(_sublayers, "insertObject:atIndex:", aLayer, index == CPNotFound ? _sublayers.length : index + 1);
472}
473}), new objj_method(sel_getUid("replaceSublayer:with:"), function $CALayer__replaceSublayer_with_(self, _cmd, aSublayer, aLayer)
474{ with(self)
475{
476    if (aSublayer == aLayer)
477        return;
478    if (aSublayer._superlayer != self)
479    {
480        alert("EXCEPTION");
481        return;
482    }
483    if (_DOMContentsElement && aLayer._zPosition > _DOMContentsElement.style.zIndex) _DOMContentsElement.style.zIndex -= 100.0;;
484    objj_msgSend(_sublayers, "replaceObjectAtIndex:withObject:", objj_msgSend(_sublayers, "indexOfObjectIdenticalTo:", aSublayer), aLayer);
485    _DOMElement.replaceChild(aSublayer._DOMElement, aLayer._DOMElement);
486}
487}), new objj_method(sel_getUid("registerRunLoopUpdateWithMask:"), function $CALayer__registerRunLoopUpdateWithMask_(self, _cmd, anUpdateMask)
488{ with(self)
489{
490    if (CALayerRegisteredRunLoopUpdates == nil)
491    {
492        CALayerRegisteredRunLoopUpdates = {};
493        objj_msgSend(objj_msgSend(CPRunLoop, "currentRunLoop"), "performSelector:target:argument:order:modes:", sel_getUid("runLoopUpdateLayers"), CALayer, nil, 0, [CPDefaultRunLoopMode]);
494    }
495    _runLoopUpdateMask |= anUpdateMask;
496    CALayerRegisteredRunLoopUpdates[objj_msgSend(self, "hash")] = self;
497}
498}), new objj_method(sel_getUid("setNeedsComposite"), function $CALayer__setNeedsComposite(self, _cmd)
499{ with(self)
500{
501    objj_msgSend(self, "registerRunLoopUpdateWithMask:", CALayerCompositeUpdateMask);
502}
503}), new objj_method(sel_getUid("setNeedsDisplay"), function $CALayer__setNeedsDisplay(self, _cmd)
504{ with(self)
505{
506    objj_msgSend(self, "registerRunLoopUpdateWithMask:", CALayerDisplayUpdateMask);
507}
508}), new objj_method(sel_getUid("setNeedsDisplayOnBoundsChange:"), function $CALayer__setNeedsDisplayOnBoundsChange_(self, _cmd, needsDisplayOnBoundsChange)
509{ with(self)
510{
511    _needsDisplayOnBoundsChange = needsDisplayOnBoundsChange;
512}
513}), new objj_method(sel_getUid("needsDisplayOnBoundsChange"), function $CALayer__needsDisplayOnBoundsChange(self, _cmd)
514{ with(self)
515{
516    return _needsDisplayOnBoundsChange;
517}
518}), new objj_method(sel_getUid("setNeedsDisplayInRect:"), function $CALayer__setNeedsDisplayInRect_(self, _cmd, aRect)
519{ with(self)
520{
521    _dirtyRect = aRect;
522    objj_msgSend(self, "display");
523}
524}), new objj_method(sel_getUid("convertPoint:fromLayer:"), function $CALayer__convertPoint_fromLayer_(self, _cmd, aPoint, aLayer)
525{ with(self)
526{
527    return CGPointApplyAffineTransform(aPoint, _CALayerGetTransform(aLayer, self));
528}
529}), new objj_method(sel_getUid("convertPoint:toLayer:"), function $CALayer__convertPoint_toLayer_(self, _cmd, aPoint, aLayer)
530{ with(self)
531{
532    return CGPointApplyAffineTransform(aPoint, _CALayerGetTransform(self, aLayer));
533}
534}), new objj_method(sel_getUid("convertRect:fromLayer:"), function $CALayer__convertRect_fromLayer_(self, _cmd, aRect, aLayer)
535{ with(self)
536{
537    return CGRectApplyAffineTransform(aRect, _CALayerGetTransform(aLayer, self));
538}
539}), new objj_method(sel_getUid("convertRect:toLayer:"), function $CALayer__convertRect_toLayer_(self, _cmd, aRect, aLayer)
540{ with(self)
541{
542    return CGRectApplyAffineTransform(aRect, _CALayerGetTransform(self, aLayer));
543}
544}), new objj_method(sel_getUid("containsPoint:"), function $CALayer__containsPoint_(self, _cmd, aPoint)
545{ with(self)
546{
547    return (aPoint.x >= (_bounds.origin.x) && aPoint.y >= (_bounds.origin.y) && aPoint.x < (_bounds.origin.x + _bounds.size.width) && aPoint.y < (_bounds.origin.y + _bounds.size.height));
548}
549}), new objj_method(sel_getUid("hitTest:"), function $CALayer__hitTest_(self, _cmd, aPoint)
550{ with(self)
551{
552    if (_isHidden)
553        return nil;
554    var point = CGPointApplyAffineTransform(aPoint, _transformToLayer);
555    if (!(point.x >= (_bounds.origin.x) && point.y >= (_bounds.origin.y) && point.x < (_bounds.origin.x + _bounds.size.width) && point.y < (_bounds.origin.y + _bounds.size.height)))
556        return nil;
557    var layer = nil,
558        index = _sublayers.length;
559    while (index--)
560        if (layer = objj_msgSend(_sublayers[index], "hitTest:", point))
561            return layer;
562    return self;
563}
564}), new objj_method(sel_getUid("setDelegate:"), function $CALayer__setDelegate_(self, _cmd, aDelegate)
565{ with(self)
566{
567    if (_delegate == aDelegate)
568        return;
569    _delegate = aDelegate;
570    _delegateRespondsToDisplayLayerSelector = objj_msgSend(_delegate, "respondsToSelector:", sel_getUid("displayLayer:"));
571    _delegateRespondsToDrawLayerInContextSelector = objj_msgSend(_delegate, "respondsToSelector:", sel_getUid("drawLayer:inContext:"));
572    if (_delegateRespondsToDisplayLayerSelector || _delegateRespondsToDrawLayerInContextSelector)
573        objj_msgSend(self, "setNeedsDisplay");
574}
575}), new objj_method(sel_getUid("delegate"), function $CALayer__delegate(self, _cmd)
576{ with(self)
577{
578    return _delegate;
579}
580}), new objj_method(sel_getUid("_setOwningView:"), function $CALayer___setOwningView_(self, _cmd, anOwningView)
581{ with(self)
582{
583    _owningView = anOwningView;
584    if (_owningView)
585    {
586        _owningView = anOwningView;
587        _bounds.size = CGSizeMakeCopy(objj_msgSend(_owningView, "bounds").size);
588        _position = CGPointMake((_bounds.size.width) * _anchorPoint.x, (_bounds.size.height) * _anchorPoint.y);
589    }
590    _CALayerRecalculateGeometry(self, CALayerGeometryPositionMask | CALayerGeometryBoundsMask);
591}
592}), new objj_method(sel_getUid("_owningViewBoundsChanged"), function $CALayer___owningViewBoundsChanged(self, _cmd)
593{ with(self)
594{
595    _bounds.size = CGSizeMakeCopy(objj_msgSend(_owningView, "bounds").size);
596    _position = CGPointMake((_bounds.size.width) * _anchorPoint.x, (_bounds.size.height) * _anchorPoint.y);
597    _CALayerRecalculateGeometry(self, CALayerGeometryPositionMask | CALayerGeometryBoundsMask);
598}
599}), new objj_method(sel_getUid("_update"), function $CALayer___update(self, _cmd)
600{ with(self)
601{
602    window.loop = true;
603    var mask = _runLoopUpdateMask;
604    if (mask & CALayerDOMUpdateMask)
605        _CALayerUpdateDOM(self, mask);
606    if (mask & CALayerDisplayUpdateMask)
607        objj_msgSend(self, "display");
608    else if (mask & CALayerFrameSizeUpdateMask || mask & CALayerCompositeUpdateMask)
609        objj_msgSend(self, "composite");
610    _runLoopUpdateMask = 0;
611    window.loop = false;
612}
613})]);
614class_addMethods(meta_class, [new objj_method(sel_getUid("layer"), function $CALayer__layer(self, _cmd)
615{ with(self)
616{
617    return objj_msgSend(objj_msgSend(objj_msgSend(self, "class"), "alloc"), "init");
618}
619}), new objj_method(sel_getUid("runLoopUpdateLayers"), function $CALayer__runLoopUpdateLayers(self, _cmd)
620{ with(self)
621{if (window.oops) {alert(window.latest); objj_debug_print_backtrace();}
622    window.loop = true;
623    for (hash in CALayerRegisteredRunLoopUpdates)
624    {
625        var layer = CALayerRegisteredRunLoopUpdates[hash],
626            mask = layer._runLoopUpdateMask;
627        if (mask & CALayerDOMUpdateMask)
628            _CALayerUpdateDOM(layer, mask);
629        if (mask & CALayerDisplayUpdateMask)
630            objj_msgSend(layer, "display");
631        else if (mask & CALayerFrameSizeUpdateMask || mask & CALayerCompositeUpdateMask)
632            objj_msgSend(layer, "composite");
633        layer._runLoopUpdateMask = 0;
634    }
635    window.loop= false;
636    CALayerRegisteredRunLoopUpdates = nil;
637}
638})]);
639}
640_CALayerUpdateSublayerTransformForSublayers= function(aLayer)
641{
642    var bounds = aLayer._bounds,
643        anchorPoint = aLayer._anchorPoint,
644        translateX = (bounds.size.width) * anchorPoint.x,
645        translateY = (bounds.size.height) * anchorPoint.y;
646    aLayer._sublayerTransformForSublayers = CGAffineTransformConcat(
647        CGAffineTransformMakeTranslation(-translateX, -translateY),
648        CGAffineTransformConcat(aLayer._sublayerTransform,
649        CGAffineTransformMakeTranslation(translateX, translateY)));
650}
651_CALayerUpdateDOM= function(aLayer, aMask)
652{
653    var DOMElementStyle = aLayer._DOMElement.style;
654    if (aMask & CALayerZPositionUpdateMask)
655        DOMElementStyle.zIndex = aLayer._zPosition;
656    var frame = aLayer._backingStoreFrame;
657    if (aMask & CALayerFrameOriginUpdateMask)
658    {
659        DOMElementStyle.top = ROUND((frame.origin.y)) + "px";
660        DOMElementStyle.left = ROUND((frame.origin.x)) + "px";
661    }
662    if (aMask & CALayerFrameSizeUpdateMask)
663    {
664        var width = MAX(0.0, ROUND((frame.size.width))),
665            height = MAX(0.0, ROUND((frame.size.height))),
666            DOMContentsElement = aLayer._DOMContentsElement;
667        DOMElementStyle.width = width + "px";
668        DOMElementStyle.height = height + "px";
669        if (DOMContentsElement)
670        {
671            DOMContentsElement.width = width;
672            DOMContentsElement.height = height;
673            DOMContentsElement.style.width = width + "px";
674            DOMContentsElement.style.height = height + "px";
675        }
676    }
677}
678_CALayerRecalculateGeometry= function(aLayer, aGeometryChange)
679{
680    var bounds = aLayer._bounds,
681        superlayer = aLayer._superlayer,
682        width = (bounds.size.width),
683        height = (bounds.size.height),
684        position = aLayer._position,
685        anchorPoint = aLayer._anchorPoint,
686        affineTransform = aLayer._affineTransform,
687        backingStoreFrameSize = { width:aLayer._backingStoreFrame.width, height:aLayer._backingStoreFrame.height },
688        hasCustomBackingStoreFrame = aLayer._hasCustomBackingStoreFrame;
689    aLayer._transformFromLayer = CGAffineTransformConcat(
690        CGAffineTransformMakeTranslation(-width * anchorPoint.x - (aLayer._bounds.origin.x), -height * anchorPoint.y - (aLayer._bounds.origin.y)),
691        CGAffineTransformConcat(affineTransform,
692        CGAffineTransformMakeTranslation(position.x, position.y)));
693    if (superlayer && superlayer._hasSublayerTransform)
694    {
695        var tx = aLayer._transformFromLayer.tx * superlayer._sublayerTransformForSublayers.a + aLayer._transformFromLayer.ty * superlayer._sublayerTransformForSublayers.c + superlayer._sublayerTransformForSublayers.tx;aLayer._transformFromLayer.ty = aLayer._transformFromLayer.tx * superlayer._sublayerTransformForSublayers.b + aLayer._transformFromLayer.ty * superlayer._sublayerTransformForSublayers.d + superlayer._sublayerTransformForSublayers.ty;aLayer._transformFromLayer.tx = tx;var a = aLayer._transformFromLayer.a * superlayer._sublayerTransformForSublayers.a + aLayer._transformFromLayer.b * superlayer._sublayerTransformForSublayers.c, b = aLayer._transformFromLayer.a * superlayer._sublayerTransformForSublayers.b + aLayer._transformFromLayer.b * superlayer._sublayerTransformForSublayers.d, c = aLayer._transformFromLayer.c * superlayer._sublayerTransformForSublayers.a + aLayer._transformFromLayer.d * superlayer._sublayerTransformForSublayers.c;aLayer._transformFromLayer.d = aLayer._transformFromLayer.c * superlayer._sublayerTransformForSublayers.b + aLayer._transformFromLayer.d * superlayer._sublayerTransformForSublayers.d;aLayer._transformFromLayer.a = a;aLayer._transformFromLayer.b = b;aLayer._transformFromLayer.c = c;;
696    }
697    aLayer._transformToLayer = CGAffineTransformInvert(aLayer._transformFromLayer);
698    aLayer._frame = nil;
699    aLayer._standardBackingStoreFrame = objj_msgSend(aLayer, "convertRect:toLayer:", bounds, nil);
700    if (superlayer)
701    {
702        var bounds = objj_msgSend(superlayer, "bounds"),
703            frame = objj_msgSend(superlayer, "convertRect:toLayer:", bounds, nil);
704        aLayer._standardBackingStoreFrame.origin.x -= (frame.origin.x);
705        aLayer._standardBackingStoreFrame.origin.y -= (frame.origin.y);
706    }
707    var origin = aLayer._standardBackingStoreFrame.origin,
708        size = aLayer._standardBackingStoreFrame.size;
709    origin.x = FLOOR(origin.x);
710    origin.y = FLOOR(origin.y);
711    size.width = CEIL(size.width) + 1.0;
712    size.height = CEIL(size.height) + 1.0;
713    if (!hasCustomBackingStoreFrame)
714    {
715        var backingStoreFrame = CGRectMakeCopy(aLayer._standardBackingStoreFrame);
716        if (ROUND((backingStoreFrame.origin.x)) != ROUND((aLayer._backingStoreFrame.origin.x)) ||
717            ROUND((backingStoreFrame.origin.y)) != ROUND((aLayer._backingStoreFrame.origin.y)))
718            objj_msgSend(aLayer, "registerRunLoopUpdateWithMask:", CALayerFrameOriginUpdateMask);
719        if (((backingStoreFrame.size.width) != ROUND((aLayer._backingStoreFrame.size.width)) ||
720            (backingStoreFrame.size.height) != ROUND((aLayer._backingStoreFrame.size.height))))
721            objj_msgSend(aLayer, "registerRunLoopUpdateWithMask:", CALayerFrameSizeUpdateMask);
722        aLayer._backingStoreFrame = backingStoreFrame;
723    }
724    if (aGeometryChange & CALayerGeometryBoundsMask && aLayer._needsDisplayOnBoundsChange)
725        objj_msgSend(aLayer, "setNeedsDisplay");
726    else if (hasCustomBackingStoreFrame || (aGeometryChange & ~(CALayerGeometryPositionMask | CALayerGeometryAnchorPointMask)))
727        objj_msgSend(aLayer, "setNeedsComposite");
728    var sublayers = aLayer._sublayers,
729        index = 0,
730        count = sublayers.length;
731    for (; index < count; ++index)
732        _CALayerRecalculateGeometry(sublayers[index], aGeometryChange);
733}
734_CALayerGetTransform= function(fromLayer, toLayer)
735{
736    var transform = CGAffineTransformMakeIdentity();
737    if (fromLayer)
738    {
739        var layer = fromLayer;
740        while (layer && layer != toLayer)
741        {
742            var transformFromLayer = layer._transformFromLayer;
743            var tx = transform.tx * transformFromLayer.a + transform.ty * transformFromLayer.c + transformFromLayer.tx;transform.ty = transform.tx * transformFromLayer.b + transform.ty * transformFromLayer.d + transformFromLayer.ty;transform.tx = tx;var a = transform.a * transformFromLayer.a + transform.b * transformFromLayer.c, b = transform.a * transformFromLayer.b + transform.b * transformFromLayer.d, c = transform.c * transformFromLayer.a + transform.d * transformFromLayer.c;transform.d = transform.c * transformFromLayer.b + transform.d * transformFromLayer.d;transform.a = a;transform.b = b;transform.c = c;;
744            layer = layer._superlayer;
745        }
746        if (layer == toLayer)
747            return transform;
748    }
749    var layers = [],
750        layer = toLayer;
751    while (layer)
752    {
753        layers.push(layer);
754        layer = layer._superlayer;
755    }
756    var index = layers.length;
757    while (index--)
758    {
759        var transformToLayer = layers[index]._transformToLayer;
760        var tx = transform.tx * transformToLayer.a + transform.ty * transformToLayer.c + transformToLayer.tx;transform.ty = transform.tx * transformToLayer.b + transform.ty * transformToLayer.d + transformToLayer.ty;transform.tx = tx;var a = transform.a * transformToLayer.a + transform.b * transformToLayer.c, b = transform.a * transformToLayer.b + transform.b * transformToLayer.d, c = transform.c * transformToLayer.a + transform.d * transformToLayer.c;transform.d = transform.c * transformToLayer.b + transform.d * transformToLayer.d;transform.a = a;transform.b = b;transform.c = c;;
761    }
762    return transform;
763}
764