8

I want to highlight the selected NSToolbarItem like e.g. in Adium (see screenshot).

highlight http://a2.s3.p.quickshareit.com/files/screenshot_b28b67ba9411513d6.png

Is there an easy way? If not, tell me the difficult one. =)

duskwuff -inactive-
  • 171,163
  • 27
  • 219
  • 269
papr
  • 4,477
  • 5
  • 28
  • 38

3 Answers3

14

To expand upon Chuck's answer, you simply need to make your controller the delegate of your NSToolBar and implement the toolbarSelectableItemIdentifiers: delegate method in it. For example, the following implementation will let you retain the selection highlight on every toolbar item except for the one labeled "Inspect":

- (NSArray *)toolbarSelectableItemIdentifiers:(NSToolbar *)toolbar
{
    NSMutableArray *allIdentifiers = [[NSMutableArray alloc] init];

    for (NSToolbarItem *toolbarItem in [toolbar items])
    {
        if (![[toolbarItem label] isEqualToString:@"Inspect"])
            [allIdentifiers addObject:[toolbarItem itemIdentifier]];
    }

    return [allIdentifiers autorelease];
}

I cache the allIdentifiers array in an instance variable when I do something like this, so that I only have to do the array construction once.

Brad Larson
  • 168,330
  • 45
  • 388
  • 563
  • 1
    In interface builder for Xcode 4 there is now a little checkbox when you click on a toolbar item that says selectable, but checking it seems to do nothing. any ideas? – Tony Jan 31 '12 at 22:34
  • @Tony - Just tried this myself in Interface Builder and it worked fine for my application, even without the above code. Not sure why it doesn't work in your case. – Brad Larson Feb 10 '12 at 22:15
  • 2
    turns out you actually need to hook up all the tool bar items to an action so that they become 'enabled' – Tony Feb 12 '12 at 01:42
  • In my case, NSWindows display the toolbar selection with just the Selectable checkbox, but NSPanels do not, and require the above code (Thanks!). – Peter N Lewis Dec 14 '12 at 06:28
10

If you made your toolbar in Interface Builder, you can click on the individual NSToolbarItems and check the Selectable box in the Inspector for the ones you want to have that look. No code needed.

sam
  • 3,263
  • 3
  • 32
  • 48
  • 1
    I have it built in interface builder and checked the is selectable checkbox, but nothing seems to happen. is there anything else that needs to be done? – Tony Jan 31 '12 at 22:34
  • 1
    No idea, it seems to be broken in most cases when setting it in IB i can only get it to work with toolbarSelectableItemIdentifiers – valexa Jan 23 '14 at 08:23
6

See Selectable Toolbar Items in the Cocoa documentation.

Chuck
  • 222,660
  • 29
  • 289
  • 383