Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

liptonlover

macrumors 6502a
Original poster
Mar 13, 2008
989
0
I'm getting down and dirty learning how to use structs as it is important if I want to move windows, resize windows, and a million other things. I got the basics working, for instance in my test I successfully moved a window. But to resize it I'm bumping my head against a wall. First I tried doing it without structs by doing setContentSize. But this did absolutely nothing, so I'm not sure why it's even listed lol. (It's under NSWindow>sizing content)
So next I settled on setFrame:display: as the next likely candidate. It says it will resize the window. But I ran into trouble...

Here's some code:

NSRect windowBounds;
NSPoint windowOrigin;
NSSize windowSize;

So I have three structs here. Trouble is, the NSPoint and the NSSize are supposed to be "children" of the NSRect. They are what make up an NSRect. How do I go about "putting" them into the NSRect, then editing their content.

Here's what I tried doing:
windowBounds.windowSize.width = 50;

error: 'NSRect' has no member named 'windowSize'

Can anyone help? Thanks!
 

Cromulent

macrumors 604
Oct 2, 2006
6,816
1,100
The Land of Hope and Glory
You seem to be confused what a struct is.

Here is an example struct:

Code:
typedef struct example
{
     int test;
     char *another;
     float whatever;
} example;

You use it by doing the following:

Code:
example randomStruct;

randomStruct.test = 20;
randomStruct.another = (char *)malloc (128);
randonStruct.whatever = 28.93;

You see what you have to do?
 

liptonlover

macrumors 6502a
Original poster
Mar 13, 2008
989
0
I know what a struct is... here's my successful code for moving the window.

NSPoint location;
location.x=5;
location.y=5;
[window setFrameOrigin:location];

This works just fine. It moves the window.

The problem is, with setFrame: display: I need an NSRect. That's fine, except that NSRect's values are an NSPoint and an NSSize.

typedef struct _NSRect {
NSPoint origin;
NSSize size;
} NSRect;

So how do I specify that the NSPoint and NSSize I created are part of the NSRect? Or am I completely on the wrong track?
 

Cromulent

macrumors 604
Oct 2, 2006
6,816
1,100
The Land of Hope and Glory
I know what a struct is... here's my successful code for moving the window.

NSPoint location;
location.x=5;
location.y=5;
[window setFrameOrigin:location];

This works just fine. It moves the window.

The problem is, with setFrame: display: I need an NSRect. That's fine, except that NSRect's values are an NSPoint and an NSSize.

typedef struct _NSRect {
NSPoint origin;
NSSize size;
} NSRect;

So how do I specify that the NSPoint and NSSize I created are part of the NSRect? Or am I completely on the wrong track?

I don't see what the problem is? If you have a struct called NSRect you just access the members of that struct in the same way that I did above.

Accessing members is no different just because the variables are of a different type.

In your example above you would just do:

Code:
NSRect randomStruct;

randomStruct.origin = 0.0;
randomStruct.size = 10;
 

liptonlover

macrumors 6502a
Original poster
Mar 13, 2008
989
0
I don't think I'm getting through to you with the problem I'm having :mad: so lets try this. How would you define an NSRect and then use it? Sorry I have a problem explaining things :(
 

liptonlover

macrumors 6502a
Original poster
Mar 13, 2008
989
0
did I just miss that last bit or did you edit? Anyways, thanks.

My problem was that I was trying to make an NSPoint and NSSize and then access their values with a long whatever-you-call-it line.

So I just need to define an NSRect, not the NSPoint and NSSize?
 

yoavcs

macrumors regular
Apr 7, 2004
220
96
Israel
Referring to your original example, that would be:

windowBounds.origin.x

and

windowBounds.size.width

use the inner struct names defined in the NSRect definition (origin and size) and not the names of the variables you defined (windowSize, etc...)

to elaborate:

windowBounds.size.width = windowSize.width;

or

windowBounds.size = windowSize;
 

liptonlover

macrumors 6502a
Original poster
Mar 13, 2008
989
0
I hate going back over old territory... but I suppose what must be done must be done. *sigh* so long then...
Anyways thanks a million :) until my next question then!
Nate
 

Cromulent

macrumors 604
Oct 2, 2006
6,816
1,100
The Land of Hope and Glory
I hate going back over old territory... but I suppose what must be done must be done. *sigh* so long then...
Anyways thanks a million :) until my next question then!
Nate

Believe me you'll be going back over C for a while :). I still am, in fact I was caught out by a (looking back on it) simple struct problem myself this weekend.

Ask questions, read what you can, buy The C Programming Language and you'll get the hang of it sooner or later.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.