• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Adding an icon to the Window title bar (like Safari does with its Locked icon)
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Adding an icon to the Window title bar (like Safari does with its Locked icon)


  • Subject: Re: Adding an icon to the Window title bar (like Safari does with its Locked icon)
  • From: Mark Alldritt <email@hidden>
  • Date: Thu, 11 Nov 2004 11:25:26 -0800

>> I want to add a small icon to the right-hand edge of my window's title
>> bar.
>> This status icon is not clickable.  I'm thinking of something like the
>> locked icon that appears in Safari when visiting a secure web page.
>> How can
>> this be done?
>
> I would get one of the standard window buttons and then add a new view
> to the button's superview...
>
> {
> id superview = [[yourWindow standardWindowButton:NSWindowCloseButton]
> superview];
>
> [superview addSubview: yourView];
> }

Thanks, this was just the clue I needed.  Others have asked me off-list to
post my findings, so here is the code I worked up which can be added as an
NSWindow category:

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

- (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

- (NSImageView*) addIconToTitleBar:(NSImage*) icon
{
    id superview = [[self standardWindowButton:NSWindowToolbarButton]
superview];
    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;
}


NOTE: this code presumes that the window has a Toolbar button.

Cheers
-Mark

---------------------------------------------------------------------
Mark Alldritt                         Late Night Software Ltd.
Phone: 250-380-1725                   333 Moss Street
FAX:   250-383-3204                   Victoria, B.C.
WEB:   http://www.latenightsw.com/    CANADA  V8V-4M9

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

References: 
 >Re: Adding an icon to the Window title bar (like Safari does with its Locked icon) (From: Ryan Stevens <email@hidden>)

  • Prev by Date: Re: Launching System Prefs
  • Next by Date: Re: Launching System Prefs
  • Previous by thread: Re: Adding an icon to the Window title bar (like Safari does with its Locked icon)
  • Next by thread: Binding questions
  • Index(es):
    • Date
    • Thread