PDA

View Full Version : C Struct Question




MadDoc
Nov 16, 2007, 02:44 PM
I'm learning OnjC and Cocoa. My C is not my strongest point. I am trying to build a simple single window app to teach me about using delegates.

All the app needs to do is keep the window twice as tall as it is wide when it is resized. I have one class (AppController) that has been defined and set as the delegate for the NSWindow.

This is the windowWillResize method

#import "AppController.h"

@implementation AppController

// Delegate methods
-(NSSize) windowWillResize: (NSWindow *) sender toSize: (NSSize) frameSize
{
// Make sure that the window remains twice as tall as it is wide
NSSize newSize;

newSize.width = frameSize.Width;
newSize.height = frameSize.width * 2;

return newSize;
}

@end

This doesn't compile however as I get this error:'NSSize' has no member named 'Width'.

I don't fully understand what I've done wrong - I thought NSSize was a C struct with 2 variables (height and width).

What do I need to change?

MadDoc



antibact1
Nov 16, 2007, 02:48 PM
Lower case 'W' for width.

kainjow
Nov 16, 2007, 02:48 PM
It's case sensitive. "width", not "Width".

MadDoc
Nov 16, 2007, 05:16 PM
Ooops.

Thanks!

BTW, will my approach to the problem work with this method (can't check 'til I finish work in 10 hours!).

MadDoc,

kainjow
Nov 16, 2007, 05:20 PM
Just use the built-in NSWindow method setAspectRatio:

MadDoc
Nov 16, 2007, 05:44 PM
Is setAspectRatio a delegate method too? If not, I am just trying to learn about delegates (it's a new concept to me). Would my method (would the corrected 'W'idth) work?

Thanks for the tip.

MadDoc,

Krevnik
Nov 16, 2007, 06:47 PM
Just use the built-in NSWindow method setAspectRatio:

I wouldn't use it, it doesn't behave as one expects. What the OP is trying to do is the best way to keep it smooth and not run into issues with setAspectRatio: causing odd resizing behavior.

EDIT: One thing I /would/ change is instead of calculating the height from the width, calculate the width from the height. Introduce as little deviation with where the mouse pointer is (compared to the resize control's location) as possible.