advertisement

Article:
  Bitmap Image Filters
Subject:   Solution here:
Date:   2002-08-12 07:30:31
From:   infallible
Response to: Centering the view.

Ok this is how you can do it:


1. Make a subclass of NSScrollView
2. overwrite drawRect, its not needed, but i wanted to make gray background for the image :]. If you won't overwrite it then it will be white


- (void)drawRect:(NSRect)rect
{
[[NSColor whiteColor] set];
NSRectFill(rect);
[[NSColor grayColor] set];
NSRectFill(NSMakeRect(0,0,rect.size.width - 15,rect.size.height - 15));
[self tile];
}


hmm 15 pixels is scroller's width ofcourse, you can make the code sigtly better ofcourse. Like: float scrollerWidth = NSHeight([[self horizontalScroller] frame]);


3. overwrite (void)tile


- (void)tile
{
NSRect newContentRect = [self frame];
NSRect documentRect = [[self documentView] frame];
float scrollerWidth = NSHeight([[self horizontalScroller] frame]); // 15 pixels for normal scrollers


// assuming that both scrollers are always visible! Modify if needed :]
newContentRect = NSMakeRect(0,0,newContentRect.size.width - scrollerWidth, newContentRect.size.height - scrollerWidth);


// use if([self hasHorizontalScroller])/if([self hasVerticalScroller]) also if you need to check if the scrollView has to draw the scrollers


[[self horizontalScroller] setFrame:NSMakeRect(0,newContentRect.size.height,newContentRect.size.width,scrollerWidth)];
[[self verticalScroller] setFrame:NSMakeRect(newContentRect.size.width,0,scrollerWidth,newContentRect.size.height)];


// Now check if centering is needed at all


if( newContentRect.size.width >= documentRect.size.width )
{
newContentRect.origin.x = (newContentRect.size.width - documentRect.size.width) / 2;
newContentRect.size.width = documentRect.size.width;
}


if( newContentRect.size.height >= documentRect.size.height )
{
newContentRect.origin.y = (newContentRect.size.height - documentRect.size.height) / 2;
newContentRect.size.height = documentRect.size.height;
}


// draw the content view
[[self contentView] setFrame:newContentRect];
}


/Infallible da n00b coder


1 to 1 of 1
  1. Oh almost forgot
    2002-08-12 07:53:03  infallible [View]

1 to 1 of 1