Sign In/My Account | View Cart  

advertisement

AddThis Social Bookmark Button

Article:
  Bitmap Image Filters
Subject:   Centering the view.
Date:   2002-08-10 12:28:06
From:   henrihansen
Well, this question has already been asked in the "working with bitmaps" article but remained unanswered so far.


So, I wanted to take the following approach: when the window is resized (for example the user makes the window larger then the view should be placed in the center of the window) there should be an NSWindowDidResize notification flying around. My goal is to register the ImageView for this notification in order to catch it and make some adjustments on the placement of the ImageView.


I have no problems, registering and unregistering the ImageView with the default notificationCenter, but it looks like there is no NSWindowDidResize notification, well at least my method to handle the event is not invoked, which I checked with the help of NSLog.


The only idea I have, is that it is not sufficient to register the ImageView with the notificationCenter? Might it be, that the windows aren't automatically sending these notifications?


Any help is appreciated


greetings Henri

Full Threads Oldest First

Showing messages 1 through 3 of 3.

  • Centering the view.
    2002-08-12 05:02:38  infallible [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 :).

    • 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