views:

175

answers:

1

I have an NSMatrix that is filled dynamically with some form items. Now, I can conveniently call [theMatrix sizeToCells] and then pass it into the panel to be displayed.

Now, I want the NSPanel object which contains this NSMatrix to resize to wrap nicely around it. The NSPanel also has a button at the bottom which should be under the NSMatrix.

I have been trying many things with getting bounds and setting frames and have been having much confusion.

Is there any standard or correct way of sizing a panel to it's contents?

As a side question: Does a frame's origin refer to it's top left or bottom left? Is it always consistent?

Thanks

A: 

The origin of an NSPanel/NSWindow frame is always the bottom-left corner, it's measured from the screen origin.

Whether the origin of a view is the top left or bottom left depends on whether or not its superview is flipped. Flipped views have their bounds origin in the top left.

To do what you want, you need to get the frame size of the NSMatrix, then recalculate the layout of the panel.

Something like this (written in here, untested!):

//NSMatrix* matrix;
//NSPanel* panel;
CGFloat panelMargin = 10.0;
CGFloat matrixBottomMargin = 30.0;

[matrix sizeToCells];
NSRect matrixFrame = [matrix frame];

NSRect panelFrame = [panel frame];

NSSize newPanelSize = NSMakeSize(NSWidth(matrixFrame) + 2.0 * panelMargin, 
       NSHeight(matrixFrame) + 2.0 * panelMargin + matrixBottomMargin);

CGFloat yDelta = newPanelSize.height - NSHeight(panelFrame);

panelFrame = NSMakeRect(panelFrame.origin.x, 
         panelFrame.origin.y - yDelta, 
         newPanelSize.width, 
         newPanelSize.height);
[panel setFrame:panelFrame display:YES];
Rob Keniger