Re: Open new document in first (empty) window
Re: Open new document in first (empty) window
- Subject: Re: Open new document in first (empty) window
- From: PGM <email@hidden>
- Date: Tue, 5 Jun 2007 16:56:31 -0400
Hey guys. My app is of the standard document type and it opens a
new, empty document on launch. My users have requested that the
when they open a saved document at this point, it should open in
that blank window, instead of creating a new one. The only other
app I've seen do this is Apple's TextEdit.
Is there a supported way to do this? I've googled and searched but
I can't find anything and I'd rather not hack my own solution if
there's a better way.
There is a page on cocodev on this subject, though it is not very
extensive (you would have to do a search for the exact page). I took
another route as outlined there as I thought that approach required
too much rewriting of my existing code. My own approach seems very
hackish though.
At the end of my NSDocument subclass awakeFromNib method I have the
following code.
MyDocument *firstDoc = [[[NSDocumentController
sharedDocumentController] documents] objectAtIndex:0];
if([firstDoc isFirstDocument]){
[self performSelector:@selector(closeFirstWindow:)
withObject:firstDoc afterDelay:0.001];
NSDisableScreenUpdates();
}
The isFirstDocument method checks whether there is only one window
open and whether that document was the first untitled document and
has never been changed. This code is specific for my app, so not much
use to show here.
the closeFirstWindow: method is as follows:
-(void)closeFirstWindow:(MyDocument *)firstDoc
{
[[self window] setFrame:[[firstDoc window] frame] display:YES];
[firstDoc close];
NSEnableScreenUpdates();
}
So basically what I do is draw the new document at exactly the same
position and size as the empty one and then close the empty one in
the background. The delay and disabling of screen updates is
necessary to avoid ugly flashes of the saved document at its original
position.
Good luck, Patrick
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden