• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Displaying multiple windows in one sheet
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Displaying multiple windows in one sheet


  • Subject: Re: Displaying multiple windows in one sheet
  • From: Chris Giordano <email@hidden>
  • Date: Fri, 28 Feb 2003 18:07:40 -0500

Sebastian,

First, let me state my assumptions about your question. My understanding is that you're basically looking for a way of showing a sheet whose contents can be any of a set of pre-defined views. You're also looking to be able to load those views on demand.

Your task is made much easier if your views are always the same size. If they are then all you need to do is load them and then call NSWindow's -setContentView: method -- you don't have to deal with changing the frame of the sheet. If they're not the same size, then you'll need the code below. It is out of a preference controller that I've been working on for a while. I've managed to rewrite portions of it in significant ways which have caused various things to break, but I think this code should still be fairly functional. There are probably also more efficient ways of doing some of the calculations, but this seems to work. I'll worry about elegance when I get the rest of my code functional.

As for loading the views/windows, I'd probably suggest building a common controller object for all of the views, storing the views in separate nibs, and using +[NSApplication loadNibNamed:owner:] to load the nibs. The owner (controller) object wouldn't need to do much more than be able to hand off the view to whatever object needed it, although you could use it to handle whatever you wanted the sheet to do. You'd then want to keep your collection of these controllers in some collection such that you could determine whether the nib had been loaded and either get the view or load the nib. (I'll take a test crack at some code for this, but it will be very rough and written in Mail, so probably won't work.)

The code for changing the content view of a sheet is below.

Hope this helps.

chris

On Friday, February 28, 2003, at 03:55 PM, Sebastian Celis wrote:

For my purposes, this wouldn't be very efficient. There are going to be a lot of possible displays within the sheet, and loading all of them at the same time is something I'd like to avoid. Many of the displays will never be used, so I'd also only like to load those that the user will need. I was thinking I could sent the sheet a blank window and then maybe just change it's contents with NSWindow's setContentView: method. I'm just having trouble sending a blank window to the beginSheet: method, changing the contents of the sheet, and passing data from one possible contents of the sheet to another.

Sebastian


On Friday, February 28, 2003, at 02:31 PM, Thomas Harrington wrote:

On Friday, February 28, 2003, at 12:27 PM, Sebastian Celis wrote:

Sorry for both the newbie-ish question as well as for asking the same question again, but does anyone know how to do this? I think if I can get my head wrapped around the OS X GUI programming concepts a little more, everything would be so much easier for me. What I am trying to do seems to embody a lot, and I think that if I could get some help understanding how to do it, I would be all set.

If I understand your description correctly, you might want to consider putting an NSTabView in the sheet, with the tabs hidden. Each view in the NSTabView would have its own contents, and you could set up a pop-up menu or something to control switching between them.


// in your document/app object:

// you'll need something that at minimum has a -view method
@class MyViewController;

NSMutableDictionary * viewControllerRepository; // allocate, init, etc.

- (NSView *) viewForKey:(NSString *)viewKey
{
NSView * theView;
MyViewController * = theController;
theController = [viewControllerRepository objectForKey:viewKey];
if (nil == theController)
{
theController = [[MyViewController alloc] init];
[NSBundle loadNibNamed:@"<you'll have to figure this outs>" owner:theController];
}
return [theController view];
}


// Sets the content view of the window theWindow to the view passed in as newView.
// If theWindow is a sheet (based on the [self windowIsSheet] method), it adjusts
// the sheet so that it is still centered on the window.
- (void) setMyContentView:(NSView *)newView
{
NSRect oldFrame;
NSRect newFrame;
NSSize oldContentSize;
NSSize newSize;
NSPoint topLeftPoint;
NSView * newView;

// if no view passed in then don't do anything.
// if view being displayed is the current one then don't do anything.
if (newView == nil)
return;

// get some info about the old state
oldFrame = [theWindow frame];
oldContentSize = [[theWindow contentView] frame].size;
topLeftPoint = NSMakePoint(NSMinX(oldFrame), NSMaxY(oldFrame));

// figure out the new state
newSize = [newView frame].size;

if (oldContentSize.height < 0.0) oldContentSize.height = 0.0;

newFrame = NSMakeRect(topLeftPoint.x, NSMinY(oldFrame) + oldContentSize.height - NSHeight([newView frame]),
NSWidth([newView frame]), NSHeight(oldFrame) - oldContentSize.height + NSHeight([newView frame]));

if ([self windowIsSheet])
{
newFrame.origin.x = NSMidX(oldFrame) - (newSize.width * 0.500);
}

// if we haven't showed the window yet, then center it on the main screen. But only do if not a sheet.
else if (!windowHasBeenShown)
{
NSRect screenFrame = [[NSScreen mainScreen] frame];
newFrame.origin.x = NSMidX(screenFrame) - (NSWidth(newFrame) * 0.5);
newFrame.origin.y = (NSMaxY(screenFrame) - NSHeight(newFrame)) * 0.7;
windowHasBeenShown = YES;
}

// Use an empty view at the old frame to blank the window before resizing so we don't
// resize with the old content. Instead, we resize with a blank window, and then
// the new content appears.
// NOTE: [[theWindow contentView] frame] needs not to have changed.
NSView *tempView = [[NSView alloc] initWithFrame:[[theWindow contentView] frame]];
[theWindow setContentView:tempView];
[theWindow display];
[theWindow setBackgroundColor:[NSColor windowBackgroundColor]];
[theWindow setFrame:newFrame display:YES animate:YES];
[theWindow setContentView:newView];
[tempView release];

}
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

References: 
 >Re: Displaying multiple windows in one sheet (From: Sebastian Celis <email@hidden>)

  • Prev by Date: RE: [Newbie] writing plain text to a file
  • Next by Date: Re: (SEL) and @selector questions
  • Previous by thread: Re: Displaying multiple windows in one sheet
  • Next by thread: returning char* from an obj-c method
  • Index(es):
    • Date
    • Thread