/*
* CPView.j
* AppKit
*
* 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
*/
import
In order to display itself, a view must be placed in a window (represented by an
Subclasses can override -drawRect: in order to implement their
appearance. Other methods of aSubview a subview of the receiver. It is positioned relative to anotherView
@param aSubview the view to add as a subview
@param anOrderingMode specifies aSubview's ordering relative to anotherView
@param anotherView aSubview will be positioned relative to this argument
*/
- (void)addSubview:(CPView)aSubview positioned:(CPWindowOrderingMode)anOrderingMode relativeTo:(CPView)anotherView
{
var index = anotherView ? [_subviews indexOfObjectIdenticalTo:anotherView] : CPNotFound;
// In other words, if no view, then either all the way at the bottom or all the way at the top.
if (index == CPNotFound)
index = (anOrderingMode == CPWindowAbove) ? [_subviews count] : 0;
// else, if we have a view, above if above.
else if (anOrderingMode == CPWindowAbove)
++index;
[self _insertSubview:aSubview atIndex:index];
}
/* @ignore */
- (void)_insertSubview:(CPView)aSubview atIndex:(int)anIndex
{
// We will have to adjust the z-index of all views starting at this index.
var count = _subviews.length;
// If this is already one of our subviews, remove it.
if (aSubview._superview == self)
{
var index = [_subviews indexOfObjectIdenticalTo:aSubview];
if (index == anIndex || index == count - 1 && anIndex == count)
return;
[_subviews removeObjectAtIndex:index];
#if PLATFORM(DOM)
CPDOMDisplayServerRemoveChild(_DOMElement, aSubview._DOMElement);
#endif
if (anIndex > index)
--anIndex;
}
else
{
// Remove the view from its previous superview.
[aSubview removeFromSuperview];
// Set the subview's window to our own.
[aSubview _setWindow:_window];
// Notify the subview that it will be moving.
[aSubview viewWillMoveToSuperview:self];
// Set ourselves as the superview.
aSubview._superview = self;
}
if (anIndex == CPNotFound || anIndex >= count)
{
_subviews.push(aSubview);
#if PLATFORM(DOM)
// Attach the actual node.
CPDOMDisplayServerAppendChild(_DOMElement, aSubview._DOMElement);
#endif
}
else
{
_subviews.splice(anIndex, 0, aSubview);
#if PLATFORM(DOM)
// Attach the actual node.
CPDOMDisplayServerInsertBefore(_DOMElement, aSubview._DOMElement, _subviews[anIndex + 1]._DOMElement);
#endif
}
[aSubview setNextResponder:self];
[aSubview viewDidMoveToSuperview];
[self didAddSubview:aSubview];
}
/*!
Called when the receiver has added aSubview to it's child views.
@param aSubview the view that was added
*/
- (void)didAddSubview:(CPView)aSubview
{
}
/*!
Removes the receiver from it's container view and window.
Does nothing if there's no container view.
*/
- (void)removeFromSuperview
{
if (!_superview)
return;
[_superview willRemoveSubview:self];
[[_superview subviews] removeObject:self];
#if PLATFORM(DOM)
CPDOMDisplayServerRemoveChild(_superview._DOMElement, _DOMElement);
#endif
_superview = nil;
[self _setWindow:nil];
}
/*!
Replaces the specified child view with another view
@param aSubview the view to replace
@param aView the replacement view
*/
- (void)replaceSubview:(CPView)aSubview with:(CPView)aView
{
if (aSubview._superview != self)
return;
var index = [_subviews indexOfObjectIdenticalTo:aSubview];
[aSubview removeFromSuperview];
[self _insertSubview:aView atIndex:index];
}
/* @ignore */
- (void)_setWindow:(CPWindow)aWindow
{
// FIXME: check _window == aWindow? If not, comment why!
if ([_window firstResponder] == self)
[_window makeFirstResponder:nil];
// Notify the view and its subviews
[self viewWillMoveToWindow:aWindow];
[_subviews makeObjectsPerformSelector:@selector(_setWindow:) withObject:aWindow];
_window = aWindow;
[self viewDidMoveToWindow];
}
/*!
Returns YES if the receiver is, or is a descendant of, aView.
@param aView the view to test for ancestry
*/
- (BOOL)isDescendantOf:(CPView)aView
{
var view = self;
do
{
if (view == aView)
return YES;
} while(view = [view superview])
return NO;
}
/*!
Called when the receiver's superview has changed.
*/
- (void)viewDidMoveToSuperview
{
// if (_graphicsContext)
[self setNeedsDisplay:YES];
}
/*!
Called when the receiver has been moved to a new nil if the view or one of its ancestors wasn't found
*/
- (CPMenuItem)enclosingMenuItem
{
var view = self;
while (![view isKindOfClass:[_CPMenuItemView class]])
view = [view superview];
if (view)
return view._menuItem;
return nil;
/* var view = self,
enclosingMenuItem = _enclosingMenuItem;
while (!enclosingMenuItem && (view = view._enclosingMenuItem))
view = [view superview];
return enclosingMenuItem;*/
}
- (int)tag
{
return _tag;
}
/*!
Returns whether the view is flipped.
@return YES if the view is flipped. NO, otherwise.
*/
- (BOOL)isFlipped
{
return YES;
}
/*!
Sets the frame size of the receiver to the dimensions and origin of the provided rectangle in the coordinate system
of the superview. The method also posts an aSize is the same as the frame's current dimensions, this
method simply returns. The method posts a superviewSizeChanged: messages to subviews.
@param aSize the size for the subviews
*/
- (void)resizeSubviewsWithOldSize:(CGSize)aSize
{
var count = _subviews.length;
while (count--)
[_subviews[count] resizeWithOldSuperviewSize:aSize];
}
/*!
Specifies whether the receiver view should automatically resize its
subviews when its setFrameSize: method receives a change.
@param aFlag If YES, then subviews will automatically be resized
when this view is resized. NO means the views will not
be resized automatically.
*/
- (void)setAutoresizesSubviews:(BOOL)aFlag
{
_autoresizesSubviews = aFlag;
}
/*!
Reports whether the receiver automatically resizes its subviews when its frame size changes.
@return YES means it resizes its subviews on a frame size change.
*/
- (BOOL)autoresizesSubviews
{
return _autoresizesSubviews;
}
/*!
Determines automatic resizing behavior.
@param aMask a bit mask with options
*/
- (void)setAutoresizingMask:(unsigned)aMask
{
_autoresizingMask = aMask;
}
/*!
Returns the bit mask options for resizing behavior
*/
- (unsigned)autoresizingMask
{
return _autoresizingMask;
}
// Fullscreen Mode
/*!
Puts the receiver into full screen mode.
@param aScreen the that should be used
@param options configuration options
*/
- (BOOL)enterFullScreenMode:(CPScreen)aScreen withOptions:(CPDictionary)options
{
_fullScreenModeState = _CPViewFullScreenModeStateMake(self);
var fullScreenWindow = [[CPWindow alloc] initWithContentRect:[[CPDOMWindowBridge sharedDOMWindowBridge] contentBounds] styleMask:CPBorderlessWindowMask];
[fullScreenWindow setLevel:CPScreenSaverWindowLevel];
[fullScreenWindow setAutoresizingMask:CPViewWidthSizable | CPViewHeightSizable];
var contentView = [fullScreenWindow contentView];
[contentView setBackgroundColor:[CPColor blackColor]];
[contentView addSubview:self];
[self setAutoresizingMask:CPViewWidthSizable | CPViewHeightSizable];
[self setFrame:CGRectMakeCopy([contentView bounds])];
[fullScreenWindow makeKeyAndOrderFront:self];
[fullScreenWindow makeFirstResponder:self];
_isInFullScreenMode = YES;
return YES;
}
/*!
The receiver should exit full screen mode.
@param options configurations options
*/
- (void)exitFullScreenModeWithOptions:(CPDictionary)options
{
if (!_isInFullScreenMode)
return;
_isInFullScreenMode = NO;
[self setFrame:_fullScreenModeState.frame];
[self setAutoresizingMask:_fullScreenModeState.autoresizingMask];
[_fullScreenModeState.superview _insertSubview:self atIndex:_fullScreenModeState.index];
[[self window] orderOut:self];
}
/*!
Returns YES if the receiver is currently in full screen mode.
*/
- (BOOL)isInFullScreenMode
{
return _isInFullScreenMode;
}
/*!
Sets whether the receiver should be hidden.
@param aFlag YES makes the receiver hidden.
*/
- (void)setHidden:(BOOL)aFlag
{
if(_isHidden == aFlag)
return;
// FIXME: Should we return to visibility? This breaks in FireFox, Opera, and IE.
// _DOMElement.style.visibility = (_isHidden = aFlag) ? "hidden" : "visible";
_isHidden = aFlag;
#if PLATFORM(DOM)
_DOMElement.style.display = _isHidden ? "none" : "block";
#endif
}
/*!
Returns YES if the receiver is hidden.
*/
- (BOOL)isHidden
{
return _isHidden;
}
/*!
Sets the opacity of the receiver. The value must be in the range of 0.0 to 1.0, where 0.0 is
completely transparent and 1.0 is completely opaque.
@param anAlphaValue an alpha value ranging from 0.0 to 1.0.
*/
- (void)setAlphaValue:(float)anAlphaValue
{
if (_opacity == anAlphaValue)
return;
_opacity = anAlphaValue;
#if PLATFORM(DOM)
if (CPFeatureIsCompatible(CPOpacityRequiresFilterFeature))
{
if (anAlphaValue == 1.0)
try { _DOMElement.style.removeAttribute("filter") } catch (anException) { }
else
_DOMElement.style.filter = "alpha(opacity=" + anAlphaValue * 100 + ")";
}
else
_DOMElement.style.opacity = anAlphaValue;
#endif
}
/*!
Returns the alpha value of the receiver. Ranges from 0.0 to
1.0, where 0.0 is completely transparent and 1.0 is completely opaque.
*/
- (float)alphaValue
{
return _opacity;
}
/*!
Returns YES if the receiver is hidden, or one
of it's ancestor views is hidden. NO, otherwise.
*/
- (BOOL)isHiddenOrHasHiddenAncestor
{
var view = self;
while (![view isHidden])
view = [view superview];
return view != nil;
}
/*!
Returns whether the receiver should be sent a mouseDown: message for anEvent.
Returns YES by default.
@return YES, if the view object accepts first mouse-down event. NO, otherwise.
*/
//FIXME: should be NO by default?
- (BOOL)acceptsFirstMouse:(CPEvent)anEvent
{
return YES;
}
/*!
Returns whether or not the view responds to hit tests.
@return YES if this view listens to hitTest messages, NO otherwise.
*/
- (BOOL)hitTests
{
return YES;
}
/*!
Set whether or not the view should respond to hit tests.
@param shouldHitTest should be YES if this view should respond to hit tests, NO otherwise.
*/
- (void)setHitTests:(BOOL)shouldHitTest
{
_hitTests = shouldHitTest;
}
/*!
Tests whether a point is contained within this view, or one of its subviews.
@param aPoint the point to test
@return returns the containing view, or nil if the point is not contained
*/
- (CPView)hitTest:(CPPoint)aPoint
{
if(_isHidden || !_hitTests || !CPRectContainsPoint(_frame, aPoint))
return nil;
var view = nil,
i = _subviews.length,
adjustedPoint = _CGPointMake(aPoint.x - _CGRectGetMinX(_frame), aPoint.y - _CGRectGetMinY(_frame));
if (_inverseBoundsTransform)
adjustedPoint = _CGPointApplyAffineTransform(adjustedPoint, _inverseBoundsTransform);
while (i--)
if (view = [_subviews[i] hitTest:adjustedPoint])
return view;
return self;
}
/*!
Returns YES if mouse events aren't needed by the receiver and can be sent to the superview. The
default implementation returns NO if the view is opaque.
*/
- (BOOL)mouseDownCanMoveWindow
{
return ![self isOpaque];
}
- (void)mouseDown:(CPEvent)anEvent
{
if ([self mouseDownCanMoveWindow])
[super mouseDown:anEvent];
}
/*!
Sets the background color of the receiver.
@param aColor the new color for the receiver's background
*/
- (void)setBackgroundColor:(CPColor)aColor
{
if (_backgroundColor == aColor)
return;
_backgroundColor = aColor;
#if PLATFORM(DOM)
var patternImage = [_backgroundColor patternImage],
amount = 0;
if ([patternImage isThreePartImage])
{
_backgroundType = [patternImage isVertical] ? BackgroundVerticalThreePartImage : BackgroundHorizontalThreePartImage;
amount = 3 - _DOMImageParts.length;
}
else if ([patternImage isNinePartImage])
{
_backgroundType = BackgroundNinePartImage;
amount = 9 - _DOMImageParts.length;
}
else
{
_backgroundType = BackgroundTrivialColor;
amount = 0 - _DOMImageParts.length;
}
if (amount > 0)
while (amount--)
{
var DOMElement = DOMElementPrototype.cloneNode(false);
DOMElement.style.zIndex = -1000;
_DOMImageParts.push(DOMElement);
_DOMElement.appendChild(DOMElement);
}
else
{
amount = -amount;
while (amount--)
_DOMElement.removeChild(_DOMImageParts.pop());
}
if (_backgroundType == BackgroundTrivialColor)
// Opera doesn't like DOM properties set to nil.
// https://trac.280north.com/ticket/7
_DOMElement.style.background = _backgroundColor ? [_backgroundColor cssString] : "";
else
{
var slices = [patternImage imageSlices],
count = slices.length,
frameSize = _frame.size;
while (count--)
{
var image = slices[count],
size = _DOMImageSizes[count] = image ? [image size] : _CGSizeMakeZero();
CPDOMDisplayServerSetStyleSize(_DOMImageParts[count], size.width, size.height);
_DOMImageParts[count].style.background = image ? "url(\"" + [image filename] + "\")" : "";
}
if (_backgroundType == BackgroundNinePartImage)
{
var width = frameSize.width - _DOMImageSizes[0].width - _DOMImageSizes[2].width,
height = frameSize.height - _DOMImageSizes[0].height - _DOMImageSizes[6].height;
CPDOMDisplayServerSetStyleSize(_DOMImageParts[1], width, _DOMImageSizes[0].height);
CPDOMDisplayServerSetStyleSize(_DOMImageParts[3], _DOMImageSizes[3].width, height);
CPDOMDisplayServerSetStyleSize(_DOMImageParts[4], width, height);
CPDOMDisplayServerSetStyleSize(_DOMImageParts[5], _DOMImageSizes[5].width, height);
CPDOMDisplayServerSetStyleSize(_DOMImageParts[7], width, _DOMImageSizes[7].height);
CPDOMDisplayServerSetStyleLeftTop(_DOMImageParts[0], NULL, 0.0, 0.0);
CPDOMDisplayServerSetStyleLeftTop(_DOMImageParts[1], NULL, _DOMImageSizes[0].width, 0.0);
CPDOMDisplayServerSetStyleRightTop(_DOMImageParts[2], NULL, 0.0, 0.0);
CPDOMDisplayServerSetStyleLeftTop(_DOMImageParts[3], NULL, 0.0, _DOMImageSizes[1].height);
CPDOMDisplayServerSetStyleLeftTop(_DOMImageParts[4], NULL, _DOMImageSizes[0].width, _DOMImageSizes[0].height);
CPDOMDisplayServerSetStyleRightTop(_DOMImageParts[5], NULL, 0.0, _DOMImageSizes[1].height);
CPDOMDisplayServerSetStyleLeftBottom(_DOMImageParts[6], NULL, 0.0, 0.0);
CPDOMDisplayServerSetStyleLeftBottom(_DOMImageParts[7], NULL, _DOMImageSizes[6].width, 0.0);
CPDOMDisplayServerSetStyleRightBottom(_DOMImageParts[8], NULL, 0.0, 0.0);
}
else if (_backgroundType == BackgroundVerticalThreePartImage)
{
CPDOMDisplayServerSetStyleSize(_DOMImageParts[1], frameSize.width, frameSize.height - _DOMImageSizes[0].height - _DOMImageSizes[2].height);
CPDOMDisplayServerSetStyleLeftTop(_DOMImageParts[0], NULL, 0.0, 0.0);
CPDOMDisplayServerSetStyleLeftTop(_DOMImageParts[1], NULL, 0.0, _DOMImageSizes[0].height);
CPDOMDisplayServerSetStyleLeftBottom(_DOMImageParts[2], NULL, 0.0, 0.0);
}
else if (_backgroundType == BackgroundHorizontalThreePartImage)
{
CPDOMDisplayServerSetStyleSize(_DOMImageParts[1], frameSize.width - _DOMImageSizes[0].width - _DOMImageSizes[2].width, frameSize.height);
CPDOMDisplayServerSetStyleLeftTop(_DOMImageParts[0], NULL, 0.0, 0.0);
CPDOMDisplayServerSetStyleLeftTop(_DOMImageParts[1], NULL, _DOMImageSizes[0].width, 0.0);
CPDOMDisplayServerSetStyleRightTop(_DOMImageParts[2], NULL, 0.0, 0.0);
}
}
#endif
}
/*!
Returns the background color of the receiver
*/
- (CPColor)backgroundColor
{
return _backgroundColor;
}
// Converting Coordinates
/*!
Converts aPoint from the coordinate space of aView to the coordinate space of the receiver.
@param aPoint the point to convert
@param aView the view space to convert from
@return the converted point
*/
- (CGPoint)convertPoint:(CGPoint)aPoint fromView:(CPView)aView
{
return CGPointApplyAffineTransform(aPoint, _CPViewGetTransform(aView, self));
}
/*!
Converts aPoint from the receiver's coordinate space to the coordinate space of aView.
@param aPoint the point to convert
@param aView the coordinate space to which the point will be converted
@return the converted point
*/
- (CGPoint)convertPoint:(CGPoint)aPoint toView:(CPView)aView
{
return CGPointApplyAffineTransform(aPoint, _CPViewGetTransform(self, aView));
}
/*!
Convert's aSize from aView's coordinate space to the receiver's coordinate space.
@param aSize the size to convert
@param aView the coordinate space to convert from
@return the converted size
*/
- (CGSize)convertSize:(CGSize)aSize fromView:(CPView)aView
{
return CGSizeApplyAffineTransform(aSize, _CPViewGetTransform(aView, self));
}
/*!
Convert's aSize from the receiver's coordinate space to aView's coordinate space.
@param aSize the size to convert
@param the coordinate space to which the size will be converted
@return the converted size
*/
- (CGSize)convertSize:(CGSize)aSize toView:(CPView)aView
{
return CGSizeApplyAffineTransform(aSize, _CPViewGetTransform(self, aView));
}
/*!
Converts aRect from aView's coordinate space to the receiver's space.
@param aRect the rectangle to convert
@param aView the coordinate space from which to convert
@return the converted rectangle
*/
- (CGRect)convertRect:(CGRect)aRect fromView:(CPView)aView
{
return CGRectApplyAffineTransform(aRect, _CPViewGetTransform(aView, self));
}
/*!
Converts aRect from the receiver's coordinate space to aView's coordinate space.
@param aRect the rectangle to convert
@param aView the coordinate space to which the rectangle will be converted
@return the converted rectangle
*/
- (CGRect)convertRect:(CGRect)aRect toView:(CPView)aView
{
return CGRectApplyAffineTransform(aRect, _CPViewGetTransform(self, aView));
}
/*!
Sets whether the receiver posts a NO.
Methods that could cause a frame change notification are:
setFrame: setFrameSize: setFrameOrigin:@param shouldPostFrameChangedNotifications
YES makes the receiver post
notifications on frame changes (size or origin)
*/
- (void)setPostsFrameChangedNotifications:(BOOL)shouldPostFrameChangedNotifications
{
if (_postsFrameChangedNotifications == shouldPostFrameChangedNotifications)
return;
_postsFrameChangedNotifications = shouldPostFrameChangedNotifications;
if (_postsFrameChangedNotifications)
[_CPViewNotificationCenter postNotificationName:CPViewFrameDidChangeNotification object:self];
}
/*!
Returns YES if the receiver posts a NO.
Methods that could cause a bounds change notification are:
setBounds: setBoundsSize: setBoundsOrigin:@param shouldPostBoundsChangedNotifications
YES makes the receiver post
notifications on bounds changes
*/
- (void)setPostsBoundsChangedNotifications:(BOOL)shouldPostBoundsChangedNotifications
{
if (_postsBoundsChangedNotifications == shouldPostBoundsChangedNotifications)
return;
_postsBoundsChangedNotifications = shouldPostBoundsChangedNotifications;
if (_postsBoundsChangedNotifications)
[_CPViewNotificationCenter postNotificationName:CPViewBoundsDidChangeNotification object:self];
}
/*!
Returns YES if the receiver posts a
anImage
@param mouseOffset the distance from the mouseDown: location and the current location
@param anEvent the mouseDown: that triggered the drag
@param aPastebaord the pasteboard that holds the drag data
@param aSourceObject the drag operation controller
@param slideBack Whether the image should 'slide back' if the drag is rejected
*/
- (void)dragImage:(CPImage)anImage at:(CGPoint)aLocation offset:(CGSize)mouseOffset event:(CPEvent)anEvent pasteboard:(CPPasteboard)aPasteboard source:(id)aSourceObject slideBack:(BOOL)slideBack
{
[_window dragImage:anImage at:[self convertPoint:aLocation toView:nil] offset:mouseOffset event:anEvent pasteboard:aPasteboard source:aSourceObject slideBack:slideBack];
}
/*!
Initiates a drag operation from the receiver to another view that accepts dragged data.
@param aView the view to be dragged
@param aLocation the lower-left corner coordinate of aView
@param mouseOffset the distance from the mouseDown: location and the current location
@param anEvent the mouseDown: that triggered the drag
@param aPastebaord the pasteboard that holds the drag data
@param aSourceObject the drag operation controller
@param slideBack Whether the view should 'slide back' if the drag is rejected
*/
- (void)dragView:(CPView)aView at:(CPPoint)aLocation offset:(CPSize)mouseOffset event:(CPEvent)anEvent pasteboard:(CPPasteboard)aPasteboard source:(id)aSourceObject slideBack:(BOOL)slideBack
{
[_window dragView:aView at:[self convertPoint:aLocation toView:nil] offset:mouseOffset event:anEvent pasteboard:aPasteboard source:aSourceObject slideBack:slideBack];
}
/*!
Sets the receiver's list of acceptable data types for a dragging operation.
@param pasteboardTypes an array of aRect. This method should be overridden by subclasses.
@param aRect the area that should be drawn into
*/
- (void)drawRect:(CPRect)aRect
{
}
// Displaying
/*!
Marks the entire view as dirty, and needing a redraw.
*/
- (void)setNeedsDisplay:(BOOL)aFlag
{
if (aFlag)
[self setNeedsDisplayInRect:[self bounds]];
#if PLATFORM(DOM)
else
CPDOMDisplayServerRemoveView(self);
#endif
}
/*!
Marks the area denoted by aRect as dirty, and initiates a redraw on it.
@param aRect the area that needs to be redrawn
*/
- (void)setNeedsDisplayInRect:(CPRect)aRect
{
#if PLATFORM(DOM)
var hash = [[self class] hash],
hasCustomDrawRect = CustomDrawRectViews[hash];
if (!hasCustomDrawRect && typeof hasCustomDrawRect === "undefined")
{
hasCustomDrawRect = [self methodForSelector:@selector(drawRect:)] != [CPView instanceMethodForSelector:@selector(drawRect:)];
CustomDrawRectViews[hash] = hasCustomDrawRect;
}
if (!hasCustomDrawRect)
return;
#endif
if (_CGRectIsEmpty(aRect))
return;
if (_dirtyRect && !_CGRectIsEmpty(_dirtyRect))
_dirtyRect = CGRectUnion(aRect, _dirtyRect);
else
_dirtyRect = _CGRectMakeCopy(aRect);
#if PLATFORM(DOM)
CPDOMDisplayServerAddView(self);
#endif
}
- (BOOL)needsDisplay
{
return _dirtyRect && !_CGRectIsEmpty(_dirtyRect);
}
/*!
Displays the receiver and any of its subviews that need to be displayed.
*/
- (void)displayIfNeeded
{
if ([self needsDisplay])
[self displayRect:_dirtyRect];
}
/*!
Draws the entire area of the receiver as defined by its bounds.
*/
- (void)display
{
[self displayRect:[self visibleRect]];
}
- (void)displayIfNeededInRect:(CGRect)aRect
{
if ([self needsDisplay])
[self displayRect:aRect];
}
/*!
Draws the receiver into the area defined by aRect.
@param aRect the area to be drawn
*/
- (void)displayRect:(CPRect)aRect
{
[self viewWillDraw];
[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)viewWillDraw
{
}
/*!
Locks focus on the receiver, so drawing commands apply to it.
*/
- (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]);
}
/*!
Takes focus away from the receiver, and restores it to the previous view.
*/
- (void)unlockFocus
{
CGContextRestoreGState([_graphicsContext graphicsPort]);
[CPGraphicsContext setCurrentContext:nil];
}
/*!
Returns whether the receiver is completely opaque. By default, returns NO.
*/
- (BOOL)isOpaque
{
return NO;
}
/*!
Returns the rectangle of the receiver not clipped by its superview.
*/
- (CGRect)visibleRect
{
if (!_superview)
return _bounds;
return CGRectIntersection([self convertRect:[_superview visibleRect] fromView:_superview], _bounds);
}
// Scrolling
/* @ignore */
- (CPScrollView)_enclosingClipView
{
var superview = _superview,
clipViewClass = [CPClipView class];
while(superview && ![superview isKindOfClass:clipViewClass])
superview = superview._superview;
return superview;
}
/*!
Changes the receiver's frame origin to a 'constrained' aPoint.
@param aPoint the proposed frame origin
*/
- (void)scrollPoint:(CGPoint)aPoint
{
var clipView = [self _enclosingClipView];
if (!clipView)
return;
[clipView scrollToPoint:[self convertPoint:aPoint toView:clipView]];
}
/*!
Scrolls the nearest ancestor aRect can become visible.
@param aRect the area to become visible
@return NO otherwise.
*/
- (BOOL)scrollRectToVisible:(CGRect)aRect
{
var visibleRect = [self visibleRect];
// Make sure we have a rect that exists.
aRect = CGRectIntersection(aRect, _bounds);
// If aRect is empty or is already visible then no scrolling required.
if (_CGRectIsEmpty(aRect) || CGRectContainsRect(visibleRect, aRect))
return NO;
var enclosingClipView = [self _enclosingClipView];
// If we're not in a clip view, then there isn't much we can do.
if (!enclosingClipView)
return NO;
var scrollPoint = _CGPointMakeCopy(visibleRect.origin);
// One of the following has to be true since our current visible rect didn't contain aRect.
if (_CGRectGetMinX(aRect) <= _CGRectGetMinX(visibleRect))
scrollPoint.x = _CGRectGetMinX(aRect);
else if (_CGRectGetMaxX(aRect) > _CGRectGetMaxX(visibleRect))
scrollPoint.x += _CGRectGetMaxX(aRect) - _CGRectGetMaxX(visibleRect);
if (_CGRectGetMinY(aRect) <= _CGRectGetMinY(visibleRect))
scrollPoint.y = CGRectGetMinY(aRect);
else if (_CGRectGetMaxY(aRect) > _CGRectGetMaxY(visibleRect))
scrollPoint.y += _CGRectGetMaxY(aRect) - _CGRectGetMaxY(visibleRect);
[enclosingClipView scrollToPoint:CGPointMake(scrollPoint.x, scrollPoint.y)];
return YES;
}
/*
FIXME Not yet implemented
*/
- (BOOL)autoscroll:(CPEvent)anEvent
{
// FIXME: Implement.
return NO;
}
/*!
Subclasses can override this to modify the visible rectangle after a
scrolling operation. The default implementation simply returns the provided rectangle.
@param proposedVisibleRect the rectangle to alter
@return the same adjusted rectangle
*/
- (CGRect)adjustScroll:(CGRect)proposedVisibleRect
{
return proposedVisibleRect;
}
/*!
Should be overridden by subclasses.
*/
- (void)scrollRect:(CGRect)aRect by:(float)anAmount
{
}
/*!
Returns the YES means the receiver wants a layer.
*/
- (void)setWantsLayer:(BOOL)aFlag
{
_wantsLayer = aFlag;
}
/*!
Returns YES if the receiver uses a YES if the receiver uses a