//
//  SEDrawingClipView.m
//  Structure Edit
//
//  Created by Ian Jackson on 2/09/05.
//  Copyright 2005 __MyCompanyName__. All rights reserved.
//

#import "SEDrawingClipView.h"
#import "SEDrawingView.h"

@implementation SEDrawingClipView

- (void)drawRect:(NSRect)rect {
	
	[[NSColor lightGrayColor] set];
	NSRectFill([self bounds]);
	NSShadow *shadow = [[[NSShadow alloc] init] autorelease];
	[shadow setShadowColor:[NSColor colorWithCalibratedRed:0 green:0 blue:0 alpha:1]];
    [shadow setShadowOffset:NSMakeSize(3, -3)];
    [shadow setShadowBlurRadius:8];    
	[shadow set];
	NSRect docRect = [[self documentView] frame];
	NSBezierPath *docRectPath = [NSBezierPath bezierPathWithRect:docRect];
	[[NSColor lightGrayColor] set];
	[docRectPath fill];

}

- (SEDrawingView *)view {
	return view;
}

- (void)setView:(SEDrawingView *)theView {
	view = theView;
}


-(void)centerDocument
{
	NSRect docRect = [[self documentView] frame];
	NSRect clipRect = [self bounds];
	
	// We can leave these values as integers (don't need the "2.0")
	if( docRect.size.width < clipRect.size.width )
		clipRect.origin.x = roundf( ( docRect.size.width - clipRect.size.width ) / 2.0 );
	
	if( docRect.size.height < clipRect.size.height )
		clipRect.origin.y = roundf( ( docRect.size.height - clipRect.size.height ) / 2.0 );
	
	// Probably the most efficient way to move the bounds origin.
	[self scrollToPoint:clipRect.origin];
	
	// We could use this instead since it allows a scroll view to coordinate scrolling between multiple clip views.
	// [[self superview] scrollClipView:self toPoint:clipRect.origin];
	
	//if(( docRect.size.width < clipRect.size.width ) ||( docRect.size.height < clipRect.size.height ))
	[self setNeedsDisplay:YES];
	[[self view] reestablishAllTrackingRectDictionaryItems];

}

// ----------------------------------------
// We need to override this so that the superclass doesn't override our new origin point.

-(NSPoint)constrainScrollPoint:(NSPoint)proposedNewOrigin
{
	NSRect docRect = [[self documentView] frame];
	NSRect clipRect = [self bounds];
	NSPoint newScrollPoint = proposedNewOrigin;
	float maxX = docRect.size.width - clipRect.size.width;
	float maxY = docRect.size.height - clipRect.size.height;
	
	// If the clip view is wider than the doc, we can't scroll horizontally
	if( docRect.size.width < clipRect.size.width )
		newScrollPoint.x = roundf( maxX / 2.0 );
	else
		newScrollPoint.x = roundf( MAX(0,MIN(newScrollPoint.x,maxX)) );
	
	// If the clip view is taller than the doc, we can't scroll vertically
	if( docRect.size.height < clipRect.size.height )
		newScrollPoint.y = roundf( maxY / 2.0 );
	else
		newScrollPoint.y = roundf( MAX(0,MIN(newScrollPoint.y,maxY)) );
	
	return newScrollPoint;
}

// ----------------------------------------
// These two methods get called whenever the subview changes

-(void)viewBoundsChanged:(NSNotification *)notification
{
	[super viewBoundsChanged:notification];
	NSLog(@"bounds changed with zoom");
	[self centerDocument];
}

-(void)viewFrameChanged:(NSNotification *)notification
{
	[super viewFrameChanged:notification];
	NSLog(@"frame changed with zoom");
	[self centerDocument];
}

// ----------------------------------------
// These superclass methods change the bounds rect directly without sending any notifications,
// so we're not sure what other work they silently do for us. As a result, we let them do their
// work and then swoop in behind to change the bounds origin ourselves. This appears to work
// just fine without us having to reinvent the methods from scratch.

- (void)setFrame:(NSRect)frameRect
{
	[super setFrame:frameRect];
	//NSLog(@"frame changed");
	//[[self view] reestablishAllTrackingRectDictionaryItems];
	[self centerDocument];
	
}

- (void)setBounds:(NSRect)boundsRect {
	
	[super setBounds:boundsRect];
	NSLog(@"bounds changed");
	[self centerDocument];
	
}

- (void)setFrameOrigin:(NSPoint)newOrigin
{
	[super setFrameOrigin:newOrigin];
	//NSLog(@"frame origin changed");
	[self centerDocument];
	
}


@end
