• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: programatically resizing a sheet, visually from the bottom up
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: programatically resizing a sheet, visually from the bottom up


  • Subject: Re: programatically resizing a sheet, visually from the bottom up
  • From: Jerry Krinock <email@hidden>
  • Date: Fri, 23 Mar 2007 05:25:56 -0700
  • Thread-topic: programatically resizing a sheet, visually from the bottom up

on 07/03/22 16:28, Brian Amerige at email@hidden wrote:

> it resizes from the top down --- what I'm interested in is decreasing
> the height of the sheet so that visually it appears that it is
> getting smaller from the bottom.

When resizing an NSView from the bottom, you have to move all of the
subviews, something like SSResizeViewMovingSubviews() below.  I've used the
following code to resize windows, but the same/idea should work for sheets.

void SSMoveView(NSView* view, float dX, float dY) {
    NSRect frame = [view frame] ;
    frame.origin.x += dX ;
    frame.origin.y += dY ;
    [view setFrame:frame] ;
}

void SSResizeView(NSView* view, float dX, float dY) {
    NSRect frame = [view frame] ;
    frame.size.width += dX ;
    frame.size.height += dY ;
    [view setFrame:frame] ;
}

void SSResizeViewMovingSubviews(NSView* view, float dXLeft, float dXRight,
float dYTop, float dYBottom) {
    SSResizeView(view, dXLeft + dXRight, dYTop + dYBottom) ;

    NSArray* subviews = [view subviews] ;
    NSEnumerator* e ;
    NSView* subview ;

    // If we wanted to change the "left", move all existing
    // subviews to the right
    if (dXLeft != 0.0) {
        e = [subviews objectEnumerator] ;
        while ((subview = [e nextObject])) {
            SSMoveView(subview, dXLeft, 0.0) ;
        }
    }

    // If we wanted to change the "bottom", move all existing
    // subviews up
    if (dYBottom != 0.0) {
        e = [subviews objectEnumerator] ;
        while ((subview = [e nextObject])) {
            SSMoveView(subview, 0.0, dYBottom) ;
        }
    }
    [view display] ;
}

NSView* SSResizeWindowAndContent(NSWindow* window, float dXLeft, float
dXRight, float dYTop, float dYBottom, BOOL moveSubviews) {
    NSView* view = [window contentView] ;
    if (moveSubviews) {
        SSResizeViewMovingSubviews(view, dXLeft, dXRight, dYTop, dYBottom) ;
    }
    else {
        SSResizeView(view, dXLeft + dXRight, dYTop + dYBottom) ;
    }
    NSRect frame = [window frame] ;
    frame.size.width += (dXLeft + dXRight) ;
    frame.size.height += (dYTop + dYBottom) ;
    // Since window origin is at the bottom, and we want
    // the bottom to move instead of the top, we also
    // adjust the origin.y
    frame.origin.y -= (dYTop + dYBottom) ;
        // since screen y is measured from the top, we have to
        // subtract instead of add
    [window setFrame:frame display:YES] ;

    return view ;  // because often this is handy
}

and then for even more fun....


@implementation NSView (LayoutHelp)

- (float)leftEdge {
    return [self frame].origin.x ;
}

- (float)rightEdge {
    return [self frame].origin.x + [self width] ;
}

- (float)centerX {
    return ([self frame].origin.x + [self width]/2) ;
}

- (void)setLeftEdge:(float)t {
    NSRect frame = [self frame] ;
    frame.origin.x = t ;
    [self setFrame:frame] ;
}

- (void)setRightEdge:(float)t {
    NSRect frame = [self frame] ;
    frame.origin.x = t - [self width] ;
    [self setFrame:frame] ;
}

- (void)setCenterX:(float)t {
    float center = [self centerX] ;
    float adjustment = t - center ;

    NSRect frame = [self frame] ;
    frame.origin.x += adjustment ;
    [self setFrame:frame] ;
}

- (float)bottom {
    return [self frame].origin.y ;
}

- (float)top {
    return [self frame].origin.y + [self height] ;
}

- (float)centerY {
    return ([self frame].origin.y + [self height]/2) ;
}

- (void)setBottom:(float)t {
    NSRect frame = [self frame] ;
    frame.origin.y = t ;
    [self setFrame:frame] ;
}

- (void)setTop:(float)t {
    NSRect frame = [self frame] ;
    frame.origin.y = t - [self height] ;
    [self setFrame:frame] ;
}

- (void)setCenterY:(float)t {
    float center = [self centerY] ;
    float adjustment = t - center ;

    NSRect frame = [self frame] ;
    frame.origin.y += adjustment ;
    [self setFrame:frame] ;
}

- (float)width {
    return [self frame].size.width ;
}

- (float)height {
    return [self frame].size.height ;
}

- (void)setWidth:(float)t {
    NSRect frame = [self frame] ;
    frame.size.width = t ;
    [self setFrame:frame] ;
}

- (void)setHeight:(float)t {
    NSRect frame = [self frame] ;
    frame.size.height = t ;
    [self setFrame:frame] ;
}

@end

@implementation NSWindow (Sizing)

- (void)setFrameToFitContentViewDisplay:(BOOL)display {
    NSRect frameW = NSMakeRect(0,0,255,255) ;
    NSRect frameC = [self contentRectForFrameRect:frameW] ;
    float titleToolBarHeight = frameW.size.height - frameC.size.height ;

    frameC = [[self contentView] frame] ;
    frameW = [self frame] ;

    float newHeight = frameC.size.height + titleToolBarHeight ;
    float dY = newHeight - frameW.size.height ;

    frameW.size.width = frameC.size.width ;
    frameW.size.height = newHeight ;
    // Since window origin is at the bottom, and we want
    // the bottom to move instead of the top, we also
    // adjust the origin.y.  However, since screen y is
    // measured from the top, we must subtract instead of add
    frameW.origin.y -= dY ;

    [self setFrame:frameW display:display] ;
}

@end

(If you REALLY want to have fun, ask for my functions for resizing text
views to fit the text).


_______________________________________________

Cocoa-dev mailing list (email@hidden)

Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

References: 
 >programatically resizing a sheet, visually from the bottom up (From: Brian Amerige <email@hidden>)

  • Prev by Date: Re: Twain, Image Capture, Intel, PPC
  • Next by Date: Re: filteredArrayUsingPredicate - need help with run time error
  • Previous by thread: Re: programatically resizing a sheet, visually from the bottom up
  • Next by thread: Re: programatically resizing a sheet, visually from the bottom up
  • Index(es):
    • Date
    • Thread