Solved

Retaining an NSWindow size and state - looking for an elegant way to do this

Posted on 2010-08-13
4
652 Views
Last Modified: 2013-12-26
I have a controller class which spawns a window when a certain condition occurs. This window can be closed by the user, moved around or resized.

Ideally, when this condition occurs, I'd like the window to re-open in the same spot the user closed it last time.

Looking for an elegant way to do this. Pointers (or references) will be most welcome.
0
Comment
Question by:ronenmagid1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 33

Expert Comment

by:pgnatyuk
ID: 33430319
I'm not sure I understand. You want to know how to move? setFrame, I think. The parameter is NSRect.

0
 

Author Comment

by:ronenmagid1
ID: 33430359
No, I can do that myself.

This is why I said "elegant". I thought there might be some Cocoa class that takes the snapshot of the windows location and/or if it's currently miniaturized and saves it into a pfile or something.
0
 
LVL 33

Accepted Solution

by:
pgnatyuk earned 500 total points
ID: 33430432
Animation?
I attached an action method as an example.
Full example is here:
http://www.wmdeveloper.com/2010/07/cocoa-implicit-animation.html

- (IBAction)move: (id) sender
{
    // The button frame.
    NSRect senderFrame = [sender frame];
    
    //The view bounds. The button belongs to this view.
    NSRect superBounds = [[sender superview] bounds];
    
    // Calculate new position within the view bounds.
    senderFrame.origin.x = (superBounds.size.width -
                            senderFrame.size.width) * drand48();
    senderFrame.origin.y = (superBounds.size.height -
                            senderFrame.size.height) * drand48();
    
    // Move the button.
    //[sender setFrame: senderFrame];
    [[sender animator] setFrame: senderFrame];
}

Open in new window

0
 
LVL 33

Expert Comment

by:pgnatyuk
ID: 33430515
Do you need to store the rectangle somewhere and then read it and restore the view position?
Use NSUserDefaults. An example attached.


#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) 
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:[NSDate date] forKey:@"Date and Time"];
    [defaults synchronize];
	
    NSDate *today = [defaults objectForKey:@"Date and Time"];
    NSLog(@"Saved data: %@", today);
	
    [pool drain];
    return 0;
}

Open in new window

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
Do you come here a lot? Are you lazy like me and don't want to go through the "trouble" of having to click your Dock's Safari icon and then having to click your Experts Exchange Favorites bookmark to get here? Well then this article is for you.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question