PDA

View Full Version : Constrain Window Proportions


honkFactory
2004.08.05, 11:21 AM
I want to have a window that is resizeable but is constrained to certain proportions. When I subclass NSWindow, what methods should I override to do this properly?

skyhawk
2004.08.05, 11:56 AM
these are the 3 functions I use (and I just use delegation)

- (NSSize)windowWillResize:(NSWindow *)sender toSize:(NSSize)proposedFrameSize
{
proposedFrameSize.height = proposedFrameSize.width * 3/4;
return [NSWindow frameRectForContentRect:(NSRect){[self frame].origin,proposedFrameSize} styleMask:[sender styleMask]].size;
}

- (NSRect)windowWillUseStandardFrame:(NSWindow *)sender defaultFrame:(NSRect)df
{
int n=df.size.height*4/3;
if (n < df.size.width)
{
df.origin.x += (df.size.width-n)/2;
df.size.width = n;
} else {
df.size.height = df.size.width*3/4;
}
return df;
}

- (void)newsize:(NSNotification *)nsn
{
NSOpenGLContext *c = [self openGLContext];
[c clearDrawable];
[c setView:self];
}

Sohta
2004.08.05, 12:33 PM
Hum...


[ self setContentAspectRatio: NSMakeSize( 4 , 3 ) ];

?

honkFactory
2004.08.05, 02:18 PM
In the initWithFrame: method of the openGL view subclass which is in the window whose aspect I wish to constrain I tried
{
NSSize windowSize;
windowSize.height=3;
windowSize.width=4;
[[self window] setResizeIncrements:windowSize];
}
But it doesn't work. Why not?

imported_kelvin
2004.08.05, 02:21 PM
In the initWithFrame: method of the openGL view subclass which is in the window whose aspect I wish to constrain I tried
{
NSSize windowSize;
windowSize.height=3;
windowSize.width=4;
[[self window] setResizeIncrements:windowSize];
}
But it doesn't work. Why not? I believe you're not going to get good sizing because the title bar adds some height that changes the ratio.

honkFactory
2004.08.05, 02:28 PM
Kelvin. Thats not the problem. The problem is that the size isn't constrained in any way at all. I tried both setResizeIncrements: and setAspectRatio: in both the initWithFrame: and awakeFromNib: methods and neither seem to have any effect on the window. Do I need to call these at a certain time for them to be effective?

honkFactory
2004.08.05, 02:36 PM
Wait. setAspectRation does work in the awakeFromNib method. However setResizeIncrements: (which is the method I really want) does not.

arekkusu
2004.08.05, 03:02 PM
Resize increments and aspect ratio are mutually exclusive attributes.

10.3 adds setContentAspectRatio: which is what most people really want. On 10.2 you have to subtract the widget size (currently just the menubar height), fix the aspect, and then add the widgets back. See Shoot Things source.