Skip to content

Commit

Permalink
Modernized CPView drawing.
Browse files Browse the repository at this point in the history
  • Loading branch information
Francisco Ryan Tolmasky I committed Sep 11, 2008
1 parent 558bcc1 commit 5618b62
Show file tree
Hide file tree
Showing 4 changed files with 247 additions and 125 deletions.
152 changes: 101 additions & 51 deletions AppKit/CPView.j
Expand Up @@ -55,14 +55,14 @@ var _DOMOriginUpdateMask = 1 << 0,
var _CPViewNotificationCenter = nil;

#if PLATFORM(DOM)
var DOMCanvasElementZIndex = -1,
DOMBackgroundElementZIndex = -2,
DOMElementPrototype = nil,
var DOMElementPrototype = nil,

BackgroundTrivialColor = 0,
BackgroundVerticalThreePartImage = 1,
BackgroundHorizontalThreePartImage = 2,
BackgroundNinePartImage = 3;
BackgroundNinePartImage = 3,

CustomDrawRectViews = {};
#endif

@implementation CPView : CPResponder
Expand Down Expand Up @@ -92,14 +92,16 @@ var DOMCanvasElementZIndex = -1,

#if PLATFORM(DOM)
DOMElement _DOMElement;
DOMElement _DOMContentsElement;

CPArray _DOMImageParts;
CPArray _DOMImageSizes;

unsigned _backgroundType;

DOMElement _DOMGraphicsElement;
#endif

CGRect _dirtyRect;

float _opacity;
CPColor _backgroundColor;

Expand All @@ -117,6 +119,9 @@ var DOMCanvasElementZIndex = -1,

+ (void)initialize
{
if ([self instanceMethodForSelector:@selector(drawRect:)] != [CPView instanceMethodForSelector:@selector(drawRect:)])
CustomDrawRectViews[[self hash]] = YES;

if (self != [CPView class])
return;

Expand Down Expand Up @@ -333,7 +338,7 @@ var DOMCanvasElementZIndex = -1,

- (void)viewDidMoveToSuperview
{
if (_graphicsContext)
// if (_graphicsContext)
[self setNeedsDisplay:YES];
}

Expand Down Expand Up @@ -443,10 +448,18 @@ var DOMCanvasElementZIndex = -1,

if (_autoresizesSubviews)
[self resizeSubviewsWithOldSize:oldSize];

[self setNeedsDisplay:YES];

#if PLATFORM(DOM)
CPDOMDisplayServerSetStyleSize(_DOMElement, size.width, size.height);

if (_DOMContentsElement)
{
CPDOMDisplayServerSetSize(_DOMContentsElement, size.width, size.height);
CPDOMDisplayServerSetStyleSize(_DOMContentsElement, size.width, size.height);
}

if (_backgroundType == BackgroundTrivialColor)
return;

Expand Down Expand Up @@ -984,78 +997,115 @@ var DOMCanvasElementZIndex = -1,

}

// Focus
// Displaying

- (void)lockFocus
- (void)setNeedsDisplay:(BOOL)aFlag
{
// If we don't yet have a graphics context, then we must first create a
// canvas element, then use its 2d context.
if (!_graphicsContext)
{
var context = CGBitmapGraphicsContextCreate();

if (aFlag)
[self setNeedsDisplayInRect:[self bounds]];
#if PLATFORM(DOM)
_DOMGraphicsElement = context.DOMElement;

_DOMGraphicsElement.style.position = "absolute";
_DOMGraphicsElement.style.top = "0px";
_DOMGraphicsElement.style.left = "0px";
_DOMGraphicsElement.style.zIndex = DOMCanvasElementZIndex;

_DOMGraphicsElement.width = CPRectGetWidth(_frame);
_DOMGraphicsElement.height = CPRectGetHeight(_frame);

_DOMGraphicsElement.style.width = CPRectGetWidth(_frame) + "px";
_DOMGraphicsElement.style.height = CPRectGetHeight(_frame) + "px";

_DOMElement.appendChild(_DOMGraphicsElement);
else
CPDOMDisplayServerRemoveView(self);
#endif
_graphicsContext = [CPGraphicsContext graphicsContextWithGraphicsPort:context flipped:YES];
}

[CPGraphicsContext setCurrentContext:_graphicsContext];

CGContextSaveGState([_graphicsContext graphicsPort]);
}

- (void)unlockFocus
- (void)setNeedsDisplayInRect:(CPRect)aRect
{
var graphicsPort = [_graphicsContext graphicsPort];

CGContextRestoreGState(graphicsPort);
if (!CustomDrawRectViews[[[self class] hash]])
return;

if (_CGRectIsEmpty(aRect))
return;

[CPGraphicsContext setCurrentContext:nil];
}

// Displaying
if (_dirtyRect && !_CGRectIsEmpty(_dirtyRect))
_dirtyRect = CGRectUnion(aRect, _dirtyRect);
else
_dirtyRect = _CGRectMakeCopy(aRect);

- (void)setNeedsDisplay:(BOOL)aFlag
{
if (aFlag)
[self display];
#if PLATFORM(DOM)
CPDOMDisplayServerAddView(self);
#endif
}

- (void)setNeedsDisplayInRect:(CPRect)aRect
- (BOOL)needsDisplay
{
[self displayRect:aRect];
return _dirtyRect && !_CGRectIsEmpty(_dirtyRect);
}

- (void)displayIfNeeded
{
if ([self needsDisplay])
[self displayRect:_dirtyRect];
}

- (void)display
{
[self displayRect:_bounds];
[self displayRect:[self visibleRect]];
}

- (void)displayIfNeededInRect:(CGRect)aRect
{
if ([self needsDisplay])
[self displayRect:aRect];
}

- (void)displayRect:(CPRect)aRect
{
{
[self displayRectIgnoringOpacity:aRect inContext:nil];

_dirtyRect = NULL;
}

- (void)displayRectIgnoringOpacity:(CGRect)aRect inContext:(CPGraphicsContext)aGraphicsContext
{
[self lockFocus];

CGContextClearRect([[CPGraphicsContext currentContext] graphicsPort], aRect);

[self drawRect:aRect];
[self unlockFocus];
}


- (void)lockFocus
{
if (!_graphicsContext)
{
var graphicsPort = CGBitmapGraphicsContextCreate();

_DOMContentsElement = graphicsPort.DOMElement;

_DOMContentsElement.style.zIndex = -100;

_DOMContentsElement.style.overflow = "hidden";
_DOMContentsElement.style.position = "absolute";
_DOMContentsElement.style.visibility = "visible";

_DOMContentsElement.width = ROUND(_CGRectGetWidth(_frame));
_DOMContentsElement.height = ROUND(_CGRectGetHeight(_frame));

_DOMContentsElement.style.top = "0px";
_DOMContentsElement.style.left = "0px";
_DOMContentsElement.style.width = ROUND(_CGRectGetWidth(_frame)) + "px";
_DOMContentsElement.style.height = ROUND(_CGRectGetHeight(_frame)) + "px";

CPDOMDisplayServerAppendChild(_DOMElement, _DOMContentsElement);

_graphicsContext = [CPGraphicsContext graphicsContextWithGraphicsPort:graphicsPort flipped:YES];
}

[CPGraphicsContext setCurrentContext:_graphicsContext];

CGContextSaveGState([_graphicsContext graphicsPort]);
}

- (void)unlockFocus
{
CGContextRestoreGState([_graphicsContext graphicsPort]);

[CPGraphicsContext setCurrentContext:nil];
}

- (BOOL)isOpaque
{
return NO;
Expand Down
43 changes: 40 additions & 3 deletions AppKit/Platform/DOM/CPDOMDisplayServer.h
Expand Up @@ -26,9 +26,10 @@
#define SetStyleLeftBottom 2
#define SetStyleRightBottom 3
#define SetStyleSize 4
#define AppendChild 5
#define InsertBefore 6
#define RemoveChild 7
#define SetSize 5
#define AppendChild 6
#define InsertBefore 7
#define RemoveChild 8

#define CPDOMDisplayServerSetStyleOrigin(anInstruction, aDOMElement, aTransform, x, y)\
if (!aDOMElement.CPDOMDisplayContext)\
Expand Down Expand Up @@ -67,6 +68,20 @@
CPDOMDisplayServerInstructions[__index + 2] = aWidth;\
CPDOMDisplayServerInstructions[__index + 3] = aHeight;

#define CPDOMDisplayServerSetSize(aDOMElement, aWidth, aHeight)\
if (!aDOMElement.CPDOMDisplayContext)\
aDOMElement.CPDOMDisplayContext = [];\
var __index = aDOMElement.CPDOMDisplayContext[SetSize];\
if (!(__index >= 0))\
{\
__index = aDOMElement.CPDOMDisplayContext[SetSize] = CPDOMDisplayServerInstructionCount;\
CPDOMDisplayServerInstructionCount += 4;\
}\
CPDOMDisplayServerInstructions[__index] = SetSize;\
CPDOMDisplayServerInstructions[__index + 1] = aDOMElement;\
CPDOMDisplayServerInstructions[__index + 2] = aWidth;\
CPDOMDisplayServerInstructions[__index + 3] = aHeight;

#define CPDOMDisplayServerAppendChild(aParentElement, aChildElement)\
if (aChildElement.CPDOMDisplayContext) aChildElement.CPDOMDisplayContext[SetStyleOrigin] = -1;\
CPDOMDisplayServerInstructions[CPDOMDisplayServerInstructionCount++] = AppendChild;\
Expand All @@ -86,3 +101,25 @@
CPDOMDisplayServerInstructions[CPDOMDisplayServerInstructionCount++] = aChildElement;

//#dfeine CPDOMDisplayServerCustomAction()

#define CPDOMDisplayServerAddView(aView)\
{\
var ___hash = [aView hash];\
if (typeof (CPDOMDisplayServerViewsContext[___hash]) == "undefined")\
{\
CPDOMDisplayServerViews[CPDOMDisplayServerViewsCount++] = aView;\
CPDOMDisplayServerViewsContext[___hash] = aView;\
}\
}\

#define CPDOMDisplayServerRemoveView(aView)\
{\
var index = CPDOMDisplayServerViewsContext[[aView hash]];\
if (typeof index != "undefined") \
{\
CPDOMDisplayServerViewsContext[[aView hash]];\
CPDOMDisplayServerViews[index] = NULL;\
}\
}\


0 comments on commit 5618b62

Please sign in to comment.