NSToolbar does not currently provide a method for returning its height. The Objective-C function in Listing 1 calculates the height of the toolbar in a window, returning 0 if the toolbar is hidden.
Listing 1 Objective-C function to calculate toolbar height
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; |
} |
In Java, you can calculate a toolbar’s height using the ToolbarHeightForWindow static method using the class in Listing 2.
Listing 2 Java static method to calculate toolbar height
public class ToolbarHeightCalculator |
{ |
public static float ToolbarHeightForWindow(NSWindow window) |
{ |
NSToolbar toolbar = window.toolbar(); |
float toolbarHeight = (float)0.0; |
if(toolbar != null && toolbar.isVisible()) |
{ |
NSRect windowFrame = NSWindow.contentRectForFrameRect( |
window.frame(), window.styleMask()); |
toolbarHeight = windowFrame.height() |
- window.contentView().frame().height(); |
} |
return toolbarHeight; |
} |
} |
Last updated: 2007-01-08