structs, members etc.

Discussion in 'Mac Programming' started by liptonlover, Jul 6, 2008.

  1. liptonlover macrumors 6502a

    Joined:
    Mar 13, 2008
    #1
    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!
     
  2. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #2
    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?
     
  3. liptonlover thread starter macrumors 6502a

    Joined:
    Mar 13, 2008
    #3
    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?
     
  4. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #4
    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;
     
  5. liptonlover thread starter macrumors 6502a

    Joined:
    Mar 13, 2008
    #5
    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 :(
     
  6. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #6
    See my edited post.
     
  7. liptonlover thread starter macrumors 6502a

    Joined:
    Mar 13, 2008
    #7
    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?
     
  8. yoavcs macrumors regular

    Joined:
    Apr 7, 2004
    Location:
    Israel
    #8
    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;
     
  9. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #9
    This is where you should go back and look at your C. You have already declared the NSPoint and NSSize in the definition of the struct. In this case Apple have provided it for you, but it is no different to doing it yourself.
     
  10. liptonlover thread starter macrumors 6502a

    Joined:
    Mar 13, 2008
    #10
    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
     
  11. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #11
    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.
     

Share This Page