Women in Technology

Hear us Roar



Article:
  Bitmap Image Filters
Subject:   Centering the view.
Date:   2002-08-12 05:02:38
From:   infallible
Response to: Centering the view.

I am looking for a solution for that problem also.
1. Fixing lower-left corner problem is easy, just overwrite one method:


- (BOOL)isFlipped
{
return YES;
}


now about centering the view :/....


I have 1 idea:
1. make a new subclass from NSView called MyPositioningView or something.
2. add a MyImageView subview to MyPositioningView
3. then check if the clipview is larger than the image * scaleFactor. If it is set MyPositioningView's frame as big as clipView's frame, but if not then make it as big as your image * scaleFactor.
4. change MyImageView's frame origin.


I am gonna try it this week[when i got time], but maybe somebody knows a better way...


Oh easiest way would be just make a subclass of NSScrollView and overwrite the (void)tile method, but its not a good solution[ euw ugly!] :( ].


btw. sorry about my poor english. And good luck with your book Mike :).


Full Threads Oldest First

Showing messages 1 through 2 of 2.

  • Solution here:
    2002-08-12 07:30:31  infallible [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
    • Oh almost forgot
      2002-08-12 07:53:03  infallible [View]

      This how it should look(without that popup button ofcourse)
      http://www.hot.ee/priiware/shot0001.gif

      and there may be some bugs when the image width or height isn't a whole number, i fixed that problem with the round() command.

      /Infallible da n00b coder