Re: Restrain size of NSWindow to NSToolbar only
Re: Restrain size of NSWindow to NSToolbar only
- Subject: Re: Restrain size of NSWindow to NSToolbar only
- From: Daryn <email@hidden>
- Date: Mon, 30 Jun 2003 23:19:16 -0500
Hi Rolf,
The examples given below are not entirely correct. In particular, the
sample for calculating titlebarHeight is wrong. It will return the
titlebar + toolbar combined heights.
In addition, you may be quickly headed for frustration due to:
1) setMinSize must NOT include the size of the toolbar
2) minSize does not account for the toolbar
I suppose these inconsistencies were necessary to transparently support
toolbars, but it sure doesn't seem to be documented! Here's my
NSWindow category that you may find very useful:
@implementation NSWindow (MyWindowAdditions)
- (NSSize)nonContentSize {
NSRect windowRect = [self frame];
NSRect contentRect = [[self contentView] frame];
float deltaWidth = NSWidth(windowRect) - NSWidth(contentRect);
float deltaHeight = NSHeight(windowRect) - NSHeight(contentRect);
return NSMakeSize(deltaWidth,deltaHeight);
}
- (float)toolbarHeight {
NSRect contentRect = [NSWindow contentRectForFrameRect:[self frame]
styleMask:[self
styleMask]];
return NSHeight(contentRect) - NSHeight([[self contentView] frame]);
}
- (float)titlebarHeight {
return [self nonContentSize].height - [self toolbarHeight];
}
- (NSSize)minContentSize {
NSSize minSize = [self minSize];
minSize.height -= [self titlebarHeight];
return minSize;
}
- (void)setMinContentSize:(NSSize)minContentSize {
minContentSize.height += [self titlebarHeight];
[self setMinSize:minContentSize];
}
@end
On Monday, June 30, 2003, at 06:48 PM, Rolf wrote:
Hi
Thanks for your help :-)
/Rolf
30.06.2003 22:23:42, skrev Matt Gemmell <email@hidden>:
On 30/6/03 at 8:48 pm, Rolf said:
My app has an NSWindow that contains only a NSToolbar. The NSWindow
is supposed to have a fixed height that is equal to the NSToolbar +
Window Title bar height, and a variable width from X upto the width
of the toolbar. How do I do that ? I guess the correct way of
limiting NSWindow size is to use the setMinSize: and setMaxSize:
methods, but I haven't been able to find a way of determining the
NSToolbar size and the Window Titale bar height.
To find the window titlebar height, assuming your window is "win":
[win frame].size.height - [[win contentView] frame].size.height
To find the height of the toolbar attached to a given window, use this
function from Apple:
/* --- begin code --- */
float ToolbarHeightForWindow(NSWindow *window)
{
NSToolbar *toolbar;
float toolbarHeight = 0.0;
NSRect windowFrame;
toolbar = [window toolbar];
if (toolbar && [toolbar isVisible])
{
windowFrame = [NSWindow contentRectForFrameRect:[window frame]
styleMask:[window
styleMask]];
toolbarHeight = NSHeight(windowFrame)
- NSHeight([[window contentView] frame]);
}
return toolbarHeight;
}
/* --- end code --- */
No idea how you'd find the width of a toolbar. It varies depending on
number of items, display mode, size mode (which depends on the version
of OS X), and the labels of items (if displayed).
You could probably write a (nightmarish) routine to calculate the
size,
but you'd be hard-coding all kinds of values, including the font used
to
draw the toolbar labels, spacing between items, edge spacing, and so
on.
I'd just allow your window to resize horizontally as far as it likes
(set the maximum width to FLT_MAX).
If you're desperate, I suppose you could keep the window off-screen,
and
repeatedly increase its size until the toolbar's -visibleItems match
its
-items. Then use [NSWindow -setMaxSize:] to not allow it to get any
wider. That's quite an ugly way to do it, though. Maybe I've missed
something (it's happened before).
Cheers,
-Matt
--
Matt Gemmell
Scotland Software
http://www.scotlandsoftware.com/
_______________________________________________
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.
_______________________________________________
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.
Daryn
_______________________________________________
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.