Resizing Window and View

Sage
Posts: 1,066
Joined: 2004.07
Post: #1
I'm trying to get my preferences to work and resize my window and view but it currently only updates the window. Here's the code that's handling it all:
Code:
- (void)resize
{
    NSWindow    *mainWindow = [self window];
    NSRect        newSize = [mainWindow frame];
    switch(windowSize)
    {
        case w600x400 :
        {
            newSize.size.width = 600;
            newSize.size.height = 400;
            [self setFrame:newSize];
            [mainWindow setFrame:newSize
                         display:YES];
            break;
        }
        case w640x426 :
        {
            newSize.size.width = 640;
            newSize.size.height = 426;
            [self setFrame:newSize];
            [mainWindow setFrame:newSize
                         display:YES];
            break;
        }
        case w800x533 :
        {
            newSize.size.width = 800;
            newSize.size.height = 533;
            [self setFrame:newSize];
            [mainWindow setFrame:newSize
                         display:YES];
            break;
        }
        case w1024x682 :
        {
            newSize.size.width = 1024;
            newSize.size.height = 682;
            [self setFrame:newSize];
            [mainWindow setFrame:newSize
                         display:YES];
            break;
        }
        case w1152x768 :
        {
            newSize.size.width = 1152;
            newSize.size.height = 768;
            [self setFrame:newSize];
            [mainWindow setFrame:newSize
                         display:YES];
            break;
        }
        case w1280x854 :
        {
            newSize.size.width = 1280;
            newSize.size.height = 854;
            [self setFrame:newSize];
            [mainWindow setFrame:newSize
                         display:YES];
            break;
        }
    }            
}
It will resize the window correctly but the view stays the same 600x400 rectangle in the lower left corner. Any ideas?
Quote this message in a reply
Member
Posts: 257
Joined: 2004.06
Post: #2
You can't use the Auto Save Name field of the window? In Interface Builder, if you put a name in there, the app will automatically save the window size and position to the user's preferences file.

If you can't, I think the problem you're having is that you think the following holds true for positioning your window:
(0, 0) ----->
|
|
|
V

when in reality, windows are positioned using this coordinate system:

^
|
|
|
(0, 0) ----->

So what happens is when you resize your window, the top and right sides come towards the lower right which is (0, 0) of the coordinate system instead of the bottom and left sides coming towards the upper right which is what you're expecting to happen, I think.

So how to fix that? I think what I've done in the past is I hold on to the original dimensions of the window before I resize it, resize the window then get the differences in the dimensions, then adjust the position by those differences. So if your window is 25 pixels smaller, then you have to increase the y position by 25 and so on. Frankly, it's just easier to use the Auto Save Name field if you can.

The brains and fingers behind Malarkey Software (plus caretaker of the world's two brattiest felines).
Quote this message in a reply
Sage
Posts: 1,199
Joined: 2004.10
Post: #3
What if, instead of resizing your view manually, you set the view's resizing mode in Interface Builder? Then it'd keep track for free.
Quote this message in a reply
Sage
Posts: 1,066
Joined: 2004.07
Post: #4
@Malarkey:That's not really what I needed help with. My window resizes fine. That's not the problem. My view, on the other hand, (what is referred to as self in the code), is not resizing to the proper size. It just remains at 600x400 no matter what. That's what I'm trying to fix. But thanks for trying.

@Tomorrow:How do I do that? I remember once doing it, but all current attempts just keep failing.

p.s. I'm not letting the user free resize the window, just in case anyone thought that. There is only a limited number of choices for size.
Quote this message in a reply
Member
Posts: 257
Joined: 2004.06
Post: #5
Nick Wrote:@Malarkey:That's not really what I needed help with. My window resizes fine. That's not the problem. My view, on the other hand, (what is referred to as self in the code), is not resizing to the proper size. It just remains at 600x400 no matter what. That's what I'm trying to fix. But thanks for trying.

Doh! Yeah, I totally misread your post. Well, one thing that I've found helpful in figuring out how to do what TomorrowPlusX says is to download and build the Sproing project and play around with it.

http://developer.apple.com/samplecode/Sp...roing.html

The brains and fingers behind Malarkey Software (plus caretaker of the world's two brattiest felines).
Quote this message in a reply
Sage
Posts: 1,199
Joined: 2004.10
Post: #6
Did you create your window and view in IB, or are you doing it programmatically? If the former, it's just a matter of selecting your view, hitting command-3, and clicking the center springs on the inspector.

If it's the latter, you want to look up NSView's - (void)setAutoresizingMask:(unsigned int)mask.

I haven't manually set resizing modes in a while, but you'd just set:

Code:
[someView setAutoresizingMask: NSViewWidthSizable | NSViewHeightSizable];

And, yeah, Malarky's link.
Quote this message in a reply
Member
Posts: 257
Joined: 2004.06
Post: #7
Yeah, the nice thing about Sproing is that you can visually see how the different options (aka, the springs) work. Plus, it will tell you what flags you need to set if you're doing it programmatically instead of through IB.

The brains and fingers behind Malarkey Software (plus caretaker of the world's two brattiest felines).
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Programically creating window + openGl view hangt5 6 3,625 Oct 15, 2005 04:17 PM
Last Post: unknown
  Translating a window coordinate to a view coordinate emileej 3 2,649 Jun 20, 2005 11:25 AM
Last Post: emileej
  resizing window skyhawk 4 3,468 Nov 2, 2002 03:09 AM
Last Post: Hog