2009-06-05 03:16:46 +00:00
|
|
|
/* Copyright (c) 2006-2007 Christopher J. W. Lloyd <cjwl@objc.net>
|
2006-12-22 04:41:44 +00:00
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
|
|
|
|
#import <AppKit/NSClipView.h>
|
|
|
|
#import <AppKit/NSColor.h>
|
|
|
|
#import <AppKit/NSGraphics.h>
|
|
|
|
#import <AppKit/NSScrollView.h>
|
|
|
|
#import <AppKit/NSEvent.h>
|
2007-11-08 15:58:54 +00:00
|
|
|
#import <AppKit/NSCursor.h>
|
2009-06-19 18:12:28 +00:00
|
|
|
#import <Foundation/NSKeyedArchiver.h>
|
2009-06-05 03:16:46 +00:00
|
|
|
#import <AppKit/NSRaise.h>
|
2006-12-22 04:41:44 +00:00
|
|
|
|
|
|
|
@implementation NSClipView
|
|
|
|
|
|
|
|
-(void)encodeWithCoder:(NSCoder *)coder {
|
|
|
|
NSUnimplementedMethod();
|
|
|
|
}
|
|
|
|
|
|
|
|
-initWithCoder:(NSCoder *)coder {
|
|
|
|
[super initWithCoder:coder];
|
|
|
|
|
2009-06-19 17:31:04 +00:00
|
|
|
if([coder allowsKeyedCoding]){
|
|
|
|
NSKeyedUnarchiver *keyed=(NSKeyedUnarchiver *)coder;
|
2010-03-04 02:58:41 +00:00
|
|
|
unsigned flags=[keyed decodeIntForKey:@"NScvFlags"];
|
2006-12-22 04:41:44 +00:00
|
|
|
|
2010-03-04 02:58:41 +00:00
|
|
|
_drawsBackground=(flags&0x04)?YES:NO;
|
2006-12-22 04:41:44 +00:00
|
|
|
_backgroundColor=[[keyed decodeObjectForKey:@"NSBGColor"] retain];
|
|
|
|
_docView=[[keyed decodeObjectForKey:@"NSDocView"] retain];
|
|
|
|
|
|
|
|
if(_docView!=nil)
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
|
|
selector:@selector(viewFrameChanged:)
|
|
|
|
name:NSViewFrameDidChangeNotification object:_docView];
|
|
|
|
}
|
|
|
|
else {
|
2009-04-07 00:17:26 +00:00
|
|
|
[NSException raise:NSInvalidArgumentException format:@"-[%@ %s] is not implemented for coder %@",isa,sel_getName(_cmd),coder];
|
2006-12-22 04:41:44 +00:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
-initWithFrame:(NSRect)frame {
|
|
|
|
[super initWithFrame:frame];
|
|
|
|
_backgroundColor=[[NSColor controlBackgroundColor] retain];
|
2010-03-04 02:58:41 +00:00
|
|
|
_drawsBackground=YES;
|
2006-12-22 04:41:44 +00:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
-(void)dealloc {
|
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
|
|
|
[_backgroundColor release];
|
|
|
|
[_docView release];
|
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
2007-11-08 15:58:54 +00:00
|
|
|
-(BOOL)drawsBackground {
|
|
|
|
return _drawsBackground;
|
|
|
|
}
|
|
|
|
|
|
|
|
-(BOOL)copiesOnScroll {
|
|
|
|
return _copiesOnScroll;
|
|
|
|
}
|
|
|
|
|
2006-12-22 04:41:44 +00:00
|
|
|
-(NSColor *)backgroundColor {
|
|
|
|
return _backgroundColor;
|
|
|
|
}
|
|
|
|
|
2007-11-08 15:58:54 +00:00
|
|
|
-(NSCursor *)documentCursor {
|
|
|
|
return _documentCursor;
|
|
|
|
}
|
|
|
|
|
2006-12-22 04:41:44 +00:00
|
|
|
-(id)documentView {
|
|
|
|
return _docView;
|
|
|
|
}
|
|
|
|
|
2007-11-08 15:58:54 +00:00
|
|
|
-(void)setDrawsBackground:(BOOL)value {
|
|
|
|
_drawsBackground=value;
|
|
|
|
[self setNeedsDisplay:YES];
|
|
|
|
}
|
|
|
|
|
|
|
|
-(void)setCopiesOnScroll:(BOOL)value {
|
|
|
|
_copiesOnScroll=value;
|
2006-12-22 04:41:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
-(void)setBackgroundColor:(NSColor *)color {
|
|
|
|
color=[color copy];
|
|
|
|
[_backgroundColor release];
|
|
|
|
_backgroundColor=color;
|
|
|
|
}
|
|
|
|
|
2007-11-08 15:58:54 +00:00
|
|
|
-(void)setDocumentCursor:(NSCursor *)value {
|
|
|
|
value=[value retain];
|
|
|
|
[_documentCursor release];
|
|
|
|
_documentCursor=value;
|
|
|
|
NSUnimplementedMethod();
|
|
|
|
}
|
|
|
|
|
2006-12-22 04:41:44 +00:00
|
|
|
-(void)setDocumentView:(NSView *)view {
|
|
|
|
if(_docView!=nil){
|
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self
|
|
|
|
name:NSViewFrameDidChangeNotification object:_docView];
|
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self
|
|
|
|
name:NSViewBoundsDidChangeNotification object:_docView];
|
|
|
|
}
|
|
|
|
|
|
|
|
[_docView removeFromSuperview];
|
|
|
|
|
|
|
|
view=[view retain];
|
|
|
|
[_docView release];
|
|
|
|
_docView=view;
|
|
|
|
|
|
|
|
[self addSubview:view];
|
|
|
|
|
|
|
|
if(_docView!=nil){
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
|
|
selector:@selector(viewFrameChanged:)
|
|
|
|
name:NSViewFrameDidChangeNotification object:_docView];
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
|
|
selector:@selector(viewBoundsChanged:)
|
|
|
|
name: NSViewBoundsDidChangeNotification object:_docView];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-11-08 15:58:54 +00:00
|
|
|
-(NSRect)documentRect {
|
|
|
|
NSUnimplementedMethod();
|
|
|
|
return NSMakeRect(0,0,0,0);
|
2006-12-22 04:41:44 +00:00
|
|
|
}
|
|
|
|
|
2007-11-08 15:58:54 +00:00
|
|
|
-(NSRect)documentVisibleRect {
|
|
|
|
return [self convertRect:[self bounds] toView:_docView];
|
2006-12-22 04:41:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
-(NSPoint)constrainScrollPoint:(NSPoint)point {
|
|
|
|
NSRect bounds=[self bounds];
|
|
|
|
NSRect docFrame=[[self documentView] frame];
|
|
|
|
|
|
|
|
if(point.y<docFrame.origin.y)
|
|
|
|
point.y=docFrame.origin.y;
|
|
|
|
|
|
|
|
if(point.x<docFrame.origin.x)
|
|
|
|
point.x=docFrame.origin.x;
|
|
|
|
|
|
|
|
if(docFrame.size.height<bounds.size.height)
|
|
|
|
point.y=docFrame.origin.y;
|
|
|
|
else if(point.y+bounds.size.height>NSMaxY(docFrame))
|
|
|
|
point.y=NSMaxY(docFrame)-bounds.size.height;
|
|
|
|
|
|
|
|
if(docFrame.size.width<bounds.size.width)
|
|
|
|
point.x=docFrame.origin.x;
|
|
|
|
else if(point.x+bounds.size.width>NSMaxX(docFrame))
|
|
|
|
point.x=NSMaxX(docFrame)-bounds.size.width;
|
|
|
|
|
|
|
|
return point;
|
|
|
|
}
|
|
|
|
|
2007-11-08 15:58:54 +00:00
|
|
|
-(NSPoint)_scrollPoint {
|
|
|
|
return [self bounds].origin;
|
|
|
|
}
|
|
|
|
|
|
|
|
-(void)viewBoundsChanged:(NSNotification *)note {
|
|
|
|
[self scrollToPoint:[self _scrollPoint]];
|
|
|
|
}
|
|
|
|
|
|
|
|
-(void)viewFrameChanged:(NSNotification *)note {
|
|
|
|
[self scrollToPoint:[self _scrollPoint]];
|
|
|
|
}
|
|
|
|
|
2006-12-22 04:41:44 +00:00
|
|
|
-(BOOL)isOpaque {
|
2010-03-04 02:58:41 +00:00
|
|
|
return _drawsBackground;
|
2006-12-22 04:41:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
-(BOOL)isFlipped {
|
|
|
|
return [_docView isFlipped];
|
|
|
|
}
|
|
|
|
|
|
|
|
-(void)drawRect:(NSRect)rect {
|
|
|
|
if([_docView isOpaque]){
|
|
|
|
NSRect bounds=[self bounds];
|
|
|
|
NSRect frame=[_docView frame];
|
|
|
|
NSRect intersection=NSIntersectionRect(bounds,frame);
|
|
|
|
|
2010-03-04 02:58:41 +00:00
|
|
|
// if the docview completely fills the clip view, don't draw the background
|
2006-12-22 04:41:44 +00:00
|
|
|
if(NSEqualRects(bounds,intersection))
|
|
|
|
return;
|
|
|
|
}
|
2010-03-04 02:58:41 +00:00
|
|
|
|
|
|
|
if([self drawsBackground]){
|
|
|
|
[_backgroundColor setFill];
|
|
|
|
NSRectFill(rect);
|
|
|
|
}
|
|
|
|
|
2006-12-22 04:41:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
-(BOOL)autoscroll:(NSEvent *)event {
|
|
|
|
NSRect bounds=[self bounds];
|
|
|
|
NSPoint point=[self convertPoint:[event locationInWindow] fromView:nil];
|
|
|
|
int deltax=0,deltay=0;
|
|
|
|
NSView *superview=[self superview];
|
|
|
|
|
|
|
|
if(NSMouseInRect(point,bounds,[self isFlipped]))
|
|
|
|
return NO;
|
|
|
|
|
|
|
|
if(![superview isKindOfClass:[NSScrollView class]] ||
|
|
|
|
[(NSScrollView *)[self superview] hasVerticalScroller]){
|
|
|
|
if(point.y<NSMinY(bounds))
|
|
|
|
deltay=NSMinY(bounds)-point.y;
|
|
|
|
else if(point.y>NSMaxY(bounds))
|
|
|
|
deltay=NSMaxY(bounds)-point.y;
|
|
|
|
if(deltay<-bounds.size.height)
|
|
|
|
deltay=-bounds.size.height;
|
|
|
|
if(deltay>bounds.size.height)
|
|
|
|
deltay=bounds.size.height;
|
|
|
|
}
|
|
|
|
if(![superview isKindOfClass:[NSScrollView class]] ||
|
|
|
|
[(NSScrollView *)[self superview] hasHorizontalScroller]){
|
|
|
|
if(point.x<NSMinX(bounds))
|
|
|
|
deltax=NSMinX(bounds)-point.x;
|
|
|
|
else if(point.x>NSMaxX(bounds))
|
|
|
|
deltax=NSMaxX(bounds)-point.x;
|
|
|
|
if(deltax<-bounds.size.width)
|
|
|
|
deltax=-bounds.size.width;
|
|
|
|
if(deltax>bounds.size.width)
|
|
|
|
deltax=bounds.size.width;
|
|
|
|
}
|
|
|
|
|
2011-07-15 13:06:41 +00:00
|
|
|
// "Returns YES if any scrolling is performed; otherwise returns NO." - AppKit documentation
|
|
|
|
if (deltax != 0.f || deltay != 0.f) {
|
|
|
|
bounds.origin.y-=deltay;
|
|
|
|
bounds.origin.x-=deltax;
|
|
|
|
[self scrollToPoint:bounds.origin];
|
|
|
|
return YES;
|
|
|
|
} else {
|
|
|
|
return NO;
|
|
|
|
}
|
2006-12-22 04:41:44 +00:00
|
|
|
}
|
|
|
|
|
2007-11-08 15:58:54 +00:00
|
|
|
-(void)scrollToPoint:(NSPoint)point {
|
|
|
|
point=[self constrainScrollPoint:point];
|
|
|
|
[self setBoundsOrigin:point];
|
|
|
|
[self setNeedsDisplay:YES];
|
|
|
|
|
|
|
|
if([[self superview] isKindOfClass:[NSScrollView class]])
|
|
|
|
[[self superview] reflectScrolledClipView:self];
|
|
|
|
}
|
|
|
|
|
2006-12-22 04:41:44 +00:00
|
|
|
@end
|