Displaying NSPanel as a sheet creating offset

520 views Asked by At

I'm using this code to show a panel :

- (void)displayPanelWithView:(NSView *)view andTitle:(NSString *)title {
    [panel setTitle:title];    
    if ([windows count] < 1) {
        [panel setContentSize:[view frame].size];
        [panel setContentView:view];
        [panel center];
        [panel makeKeyAndOrderFront:nil];
    }
    else {
        [panel setContentSize:[view frame].size];
        [panel setContentView:view];
        [NSApp beginSheet:panel modalForWindow:[windows objectAtIndex:0] modalDelegate:self     didEndSelector:nil contextInfo:NULL];
    }
}

panel is an IBOutlet (it is instantiated in IB). It is a subclass of NSPanel.

windows is an array of windows. If there are no windows, the panel is shown as a new window.

The problem

When I display a panel with a certain view, and windows is not empty, the panel is shown as a sheet. The first time this happens, there is always an offset. What follows are screenshots of the first time the method is called, and the second time it is called (both with windows not empty) :

First time

Second time

This error can go away if I use this :

[panel setStyleMask:NSBorderlessWindowMask];

In that case the size is correct, BUT the sheet is not focused anymore and stays unfocused (so the blue button in the above screenshots is white all the time). What can I do to get the sheet to display right every time? What happens that first time that doesn't happen the next time?

1

There are 1 answers

0
Fatso On

I've managed to solve the issue by using this code instead :

- (void)displayPanelWithView:(NSView *)view andTitle:(NSString *)title {
    [panel setTitle:title];    
    if (![defaults floatForKey:@"SS_Interface_Mode"] == 0 || [windows count] < 1) {
        [panel setContentSize:[view frame].size];
        [[panel contentView] addSubview:view];
        [panel center];
        [panel makeKeyAndOrderFront:nil];
    }
    else {
        [panel setContentSize:[view frame].size];
        [[panel contentView] addSubview:view];
        [NSApp beginSheet:panel modalForWindow:[windows objectAtIndex:0] modalDelegate:self didEndSelector:nil contextInfo:NULL];
    }
}
- (IBAction)endSheet:(id)sender {
    [NSApp endSheet:panel];
    [panel orderOut:nil];
    for (NSView *view in [[panel contentView] subviews])
        [view removeFromSuperview];
}

As you can see, I add the view to the current content view of the panel instead of assigning it as the new content view of the panel.