Window title bar accessory view

  • Hi!

    I would like to replicate the accessory views found in the top right
    of window title bars. Examples include Coda, iCal, Safari,...

    How can I add subviews to the window's title bar?

    Pierre
  • On Nov 18, 2008, at 6:26 PM, Houdah - ML Pierre Bernard wrote:

    > Hi!
    >
    > I would like to replicate the accessory views found in the top right
    > of window title bars. Examples include Coda, iCal, Safari,...
    >
    > How can I add subviews to the window's title bar?
    >
    > Pierre

    Greetings, Pierre,

    I think that what you're referring to is the toolbar (class
    NSToolbar), as I don't see anything besides that at the top of my iCal
    or Safari windows.  (On Leopard, the Unified Title/Toolbar setting in
    Interface Builder, or through code, makes the title bar, which is
    actually separate, look connected to the toolbar.)

    If you can afford to target Leopard-only and are willing to put up
    with a slightly buggy interface, you can use Interface Builder to set
    up the toolbar in the window itself and store it in the nib or xib
    that way.  Otherwise, if for whatever reason that won't work for you*,
    you may wish to use a toolbar delegate to handle the configuration of
    the toolbar.  The docs have much more info, as well as "dos" and
    "dont's" of toolbars (for instance: anything in a toolbar, last I
    checked, should have a menu item equivalent; thus if you have a Reset
    button or a Send button or whatnot in your toolbar, there should be a
    Reset or Send menu item, accessible from the menu bar).  Hope this
    helps!

    * Selectable toolbar items are one example of why the Interface
    Builder approach may no work for you -- such items are not currently
    configurable in IB.  (They're the sort of item used in the iTunes
    preferences window, where they act like push-buttons of which only one
    can be selected at a time.)

    Cheers,
    Andrew
  • On Tue, Nov 18, 2008 at 9:26 PM, Houdah - ML Pierre Bernard
    <pierre.bernard...> wrote:
    > I would like to replicate the accessory views found in the top right of
    > window title bars. Examples include Coda, iCal, Safari,...

    Are you talking about things like the little lock icon in HTTPS Safari
    windows?  If so, use -[NSWindow standardWindowButton:] to get an
    NSButton instance, and then use -[NSView superview] to get at the
    window frame view.  Then use -[NSView addSubview:] to add your custom
    view and position it accordingly.

    --Kyle Sluder
  • Hi Andrew,

    No I am not talking about the toolbar.

    I am referring to the lock icon and the new green certificate
    information text displayed by Safari in the window's title bar.
    iCal displays a pop-up menu where one can pick the time zone.

    Pierre

    On 19 Nov 2008, at 03:52, Andrew Merenbach wrote:

    > On Nov 18, 2008, at 6:26 PM, Houdah - ML Pierre Bernard wrote:
    >
    >> Hi!
    >>
    >> I would like to replicate the accessory views found in the top
    >> right of window title bars. Examples include Coda, iCal, Safari,...
    >>
    >> How can I add subviews to the window's title bar?
    >>
    >> Pierre
    >
    > Greetings, Pierre,
    >
    > I think that what you're referring to is the toolbar (class
    > NSToolbar), as I don't see anything besides that at the top of my
    > iCal or Safari windows.  (On Leopard, the Unified Title/Toolbar
    > setting in Interface Builder, or through code, makes the title bar,
    > which is actually separate, look connected to the toolbar.)
    >
    > If you can afford to target Leopard-only and are willing to put up
    > with a slightly buggy interface, you can use Interface Builder to
    > set up the toolbar in the window itself and store it in the nib or
    > xib that way.  Otherwise, if for whatever reason that won't work for
    > you*, you may wish to use a toolbar delegate to handle the
    > configuration of the toolbar.  The docs have much more info, as well
    > as "dos" and "dont's" of toolbars (for instance: anything in a
    > toolbar, last I checked, should have a menu item equivalent; thus if
    > you have a Reset button or a Send button or whatnot in your toolbar,
    > there should be a Reset or Send menu item, accessible from the menu
    > bar).  Hope this helps!
    >
    > * Selectable toolbar items are one example of why the Interface
    > Builder approach may no work for you -- such items are not currently
    > configurable in IB.  (They're the sort of item used in the iTunes
    > preferences window, where they act like push-buttons of which only
    > one can be selected at a time.)
    >
    > Cheers,
    > Andrew
    >
  • Hi Kyle!

    Thanks a lot.

    This sounds like a great idea. I will give it a try ASAP.

    Pierre

    On 19 Nov 2008, at 04:03, Kyle Sluder wrote:

    > On Tue, Nov 18, 2008 at 9:26 PM, Houdah - ML Pierre Bernard
    > <pierre.bernard...> wrote:
    >> I would like to replicate the accessory views found in the top
    >> right of
    >> window title bars. Examples include Coda, iCal, Safari,...
    >
    > Are you talking about things like the little lock icon in HTTPS Safari
    > windows?  If so, use -[NSWindow standardWindowButton:] to get an
    > NSButton instance, and then use -[NSView superview] to get at the
    > window frame view.  Then use -[NSView addSubview:] to add your custom
    > view and position it accordingly.
    >
    > --Kyle Sluder
  • There is already some info in the list on this.
    http://www.cocoabuilder.com/archive/message/cocoa/2004/11/11/121369

    The following can be added to an NSWindow category

    /*

      window toolbar height

      */
    - (float) toolbarHeight
    {
        return NSHeight([NSWindow contentRectForFrameRect:[self frame]
    styleMask:[self styleMask]]) NSHeight([[self contentView] frame]);
    }

    /*

      window title bar height

      */
    - (float) titleBarHeight
    {
        return NSHeight([self frame]) -
    NSHeight([[self contentView] frame]) -
    [self toolbarHeight];
    }

    #define kIconSpacing 8.0 // h-space between the icon and the toolbar
    button
    /*

      add icon to toolbar

      */
    - (NSImageView*) addIconToTitleBar:(NSImage*) icon
    {
        id superview = [[self standardWindowButton:NSWindowToolbarButton]
        superview];

    // assume toolbarbutton present
        NSRect toolbarButtonFrame = [[self
    standardWindowButton:NSWindowToolbarButton] frame];
        NSRect iconFrame;

        iconFrame.size = [icon size];
        iconFrame.origin.y = NSMaxY([superview frame]) -
    (iconFrame.size.height + ceil(([self titleBarHeight] -
    iconFrame.size.height) / 2.0));
        iconFrame.origin.x = NSMinX(toolbarButtonFrame) -
    iconFrame.size.width -
    kIconSpacing;

        NSImageView* iconView = [[[NSImageView alloc]
    initWithFrame:iconFrame]
                                autorelease];
        [iconView setImage:icon];
        [iconView setEditable:NO];
        [iconView setImageFrameStyle:NSImageFrameNone];
        [iconView setImageScaling:NSScaleNone];
        [iconView setImageAlignment:NSImageAlignCenter];
        [iconView setAutoresizingMask:NSViewMinXMargin | NSViewMinYMargin];
        [superview addSubview:iconView];

        return iconView;
    }

    /*

      add icon to toolbar

      */
    - (void) addViewToTitleBar:(NSView*)view xoffset:(CGFloat)xoffset
    {
        id superview = [[self standardWindowButton:NSWindowToolbarButton]
    superview];
        NSRect toolbarButtonFrame = [[self
    standardWindowButton:NSWindowToolbarButton] frame];
        NSRect iconFrame;

        iconFrame.size = [view bounds].size;
        iconFrame.origin.y = NSMaxY([superview frame]) -
    (iconFrame.size.height + ceil(([self titleBarHeight] -
    iconFrame.size.height) / 2.0));
        iconFrame.origin.x = NSMinX(toolbarButtonFrame) -
    iconFrame.size.width - kIconSpacing;

    if (xoffset > 0) {
      iconFrame.origin.x -= (xoffset + kIconSpacing);
    }

        [view setFrame:iconFrame];
        [view setAutoresizingMask:NSViewMinXMargin | NSViewMinYMargin];
        [superview addSubview:view];

        return;
    }

    Jonathan Mitchell

    Central Conscious Unit
    http://www.mugginsoft.com