Window (with Toolbar) Resize To Sheet?

Discussion in 'Mac Programming' started by Darkroom, Jul 12, 2008.

  1. Darkroom Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #1
    i can't figure out how to make my mainWindow (with a toolbar) to resize itself to my mainSheet (in other words, make the sheet fit perfectly inside the mainWindow's view)... i'm using setFrame:animate:display for the resize, but it's not taking into account that there's a toolbar on the mainWindow, so the resize is the same width of the mainSheet, but a little shorter:

    Code:
    [mainWindow setFrame:[mainSheet frame] display:YES animate:YES];
    [NSApp mainSheet modalForWindow:mainWindow modalDelegate:nil didEndSelector:NULL contextInfo:NULL];
    
    please help... :(
     
  2. Darkroom thread starter Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #2
    ok... 10 years later :rolleyes: ...

    this code could probably be more optimized, but it seems to work well...

    Code:
    NSSize SheetSize = [Sheet frame].size;
    NSPoint SheetPoint = [Sheet frame].origin;
    NSToolbar *toolbar = [mainWindow toolbar];
    			
    	if(toolbar && [toolbar isVisible])
    		{
    		[mainWindow setFrame:NSMakeRect(SheetPoint.x, SheetPoint.y, SheetSize.width, SheetSize.height + 55) display:YES animate:YES];
    		[NSApp beginSheet:Sheet modalForWindow:mainWindow modalDelegate:nil didEndSelector:NULL contextInfo:NULL];
    		}
    		else
    		{
    		[mainWindow setFrame:NSMakeRect(SheetPoint.x, SheetPoint.y, SheetSize.width, SheetSize.height) display:YES animate:YES];
    		[NSApp beginSheet:Sheet modalForWindow:mainWindow modalDelegate:nil didEndSelector:NULL contextInfo:NULL];
    		}
    
    through trial and error i figured out that my toolbar is 55 pixels in height... the toolbar's height will never change in my app so even though it's kinda ghetto to get the toolbar's height this way, it's fine for me... for learning purposes, i'm very open to knowing a better way to write this method if anyone has any thoughts or examples...
     
  3. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #3
    You could try subtracting the window's content height from its frame height. See NSWindow's frameRectForContentRect: and contentRectForFrameRect: methods.
     
  4. Darkroom thread starter Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #4
    wow that really did make the coding a lot easier ;)... thanks for that... the following code seems perfect now, and i no longer have to ask if the toolbar is visible or not...:

    Code:
    NSSize SheetSize = [sheet contentRectForFrameRect:[sheet frame]].size;
    NSSize MainWindowFrame = [mainWindow frame].size;
    NSSize MainWindowContent = [mainWindow contentRectForFrameRect:[mainWindow frame]].size;
    NSPoint SheetPoint = [mainWindow contentRectForFrameRect:[mainWindow frame]].origin;
    
    [mainWindow setFrame:NSMakeRect(SheetPoint.x, SheetPoint.y, SheetSize.width, (SheetSize.height + (MainWindowFrame.height - MainWindowContent.height))) display:YES animate:YES];
    [NSApp beginSheet:sheet modalForWindow:mainWindow modalDelegate:nil didEndSelector:NULL contextInfo:NULL];
    
    tasty... ;)
     

Share This Page