Apple Developer Connection
Member Login Log In | Not a Member? Contact ADC

< Previous PageNext Page > Hide TOC

Calculating a Toolbar’s Height

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;
    }
}


< Previous PageNext Page > Hide TOC


Last updated: 2007-01-08




Did this document help you?
Yes: Tell us what works for you.

It’s good, but: Report typos, inaccuracies, and so forth.

It wasn’t helpful: Tell us what would have helped.
Get information on Apple products.
Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Copyright © 2007 Apple Inc.
All rights reserved. | Terms of use | Privacy Notice