# NSToolbar



## Drarok (Jun 28, 2005)

OK, so, looking at this page: http://developer.apple.com/document...bars/index.html#//apple_ref/doc/uid/10000109i

There's an NSToolbar class. However! There's no mention of it in the Interface Builder (Classes Tab). 

WTF is going on?

Oh, oh! And NSDrawer... I can't work it at all.


----------



## Viro (Jun 29, 2005)

That's because you are meant to programmatically instantiate it. That's why the article is so long and goes into so much detail .


----------



## Drarok (Jun 29, 2005)

Arses! I need to set aside a few days and plough through my books. 

Damn! Cocoa is hard.


----------



## Viro (Jun 29, 2005)

I don't see what you're complaining about. Did you come from a Delphi or VB background?


----------



## btoth (Jun 29, 2005)

Draws are pretty easy: http://developer.apple.com/document...wers/index.html#//apple_ref/doc/uid/10000001i

Toolbars are easy once you do a few of them, you just can't do them in Interface Builder for whatever reason.  The documentation from Apple looks overwhelming, but check out: http://www.cocoadevcentral.com/articles/000037.php

Here's some code from my app (I removed unnecessary code to make it shorter):


```
#define TOOLBAR_IDENTIFIER		@"Main toolbar"
#define PREV_BUTTON				@"Previous button"
#define	NEXT_BUTTON				@"Next button"
#define TRASH_BUTTON			@"Delete button"
#define TAG_BUTTON				@"Tag button"
#define REMOVE_BUTTON			@"Remove Button"
#define SHRINK_BUTTON			@"Shrink Button"
#define INFO_BUTTON				@"Get Info Button"

@implementation ToolbarController


- (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar *)toolbar
{
	NSArray * toolBarIdentifiers = [NSArray arrayWithObjects:
		PREV_BUTTON,
		NEXT_BUTTON,
		TRASH_BUTTON,
		TAG_BUTTON,
		REMOVE_BUTTON,	
		SHRINK_BUTTON,
		INFO_BUTTON,
		NSToolbarSpaceItemIdentifier,
		NSToolbarSeparatorItemIdentifier,
		NSToolbarFlexibleSpaceItemIdentifier,
		NSToolbarCustomizeToolbarItemIdentifier,
		nil];
	
	return toolBarIdentifiers;
}


- (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar *)toolbar
{
	NSArray * defaultItems = [NSArray arrayWithObjects:
		PREV_BUTTON,			
		NEXT_BUTTON,
		TAG_BUTTON,
		INFO_BUTTON,
		NSToolbarSeparatorItemIdentifier,
		SHRINK_BUTTON,
		NSToolbarFlexibleSpaceItemIdentifier,
		REMOVE_BUTTON,
		TRASH_BUTTON,
		nil];
	
	return defaultItems;
}



- (NSToolbarItem *)toolbar:(NSToolbar *)toolbar itemForItemIdentifier:(NSString *)itemIdentifier willBeInsertedIntoToolbar:(BOOL)flag
{
	NSToolbarItem * item = [[NSToolbarItem alloc] initWithItemIdentifier:itemIdentifier];
	
		
	if ( [itemIdentifier isEqualToString:SHRINK_BUTTON] ) {
		[item setImage:[NSImage imageNamed:@"Shrink.png"]];
		[item setLabel:@"Full Screen"];
		[item setPaletteLabel:@"Toggle Full Screen"];
		[item setAction:@selector(toggleFullScreen:)];
		[item setToolTip:@"Switch to full screen mode"];
		[item setTarget:nil];
	}
	if ( [itemIdentifier isEqualToString:INFO_BUTTON] ) {
		[item setImage:[NSImage imageNamed:@"info.icns"]];
		[item setLabel:@"Image Info"];
		[item setPaletteLabel:@"Image Info"];
		[item setAction:@selector(getImageInfo:)];
		[item setToolTip:@"Get image info"];
		[item setTarget:nil];
	}
	
	return [item autorelease];
}

@end
```

nil targeted actions behave like menu items... they target the first responder and then work back up the responder chain, so they're more flexible than setting a specific target.


----------



## Drarok (Jun 30, 2005)

Viro: Started in BASIC on the Commodore 64, then QBasic on DOS, then VB on Windows - was NOT impressed, moved to Delphi about 4 years ago, been there since.

btoth: Thanks for the example, I'll have a bash at a toolbar tonight. Really disappointed you have to do it all in code, though.


----------



## Viro (Jun 30, 2005)

OK, that explains why you're looking for a IB palette for the toolbar .


----------



## Drarok (Jun 30, 2005)

Yeah, everything I've ever used that's got an IDE to make the interface includes toolbars in that, seeing as they're visual controls. Ah well. Yet something else to get used to


----------

