NSToolbar Problems...
NSToolbar Problems...
- Subject: NSToolbar Problems...
- From: Albert Atkinson <email@hidden>
- Date: Wed, 15 May 2002 08:33:05 -0500
Hello!
For some reason the following code is not working and not setting
up my toolbar. Could someone review the following code and see if
I did anything wrong? It has been partially commented...
Thanks!
Albert
Code---->
#import "MainController.h"
#import "HotController.h"
#import "HotFavorite.h"
static NSString* MyDocToolbarIdentifier = @"My Document Toolbar";
static NSString* ConnectToolbarIdentifier = @"Connect Toolbar";
@interface MainController (Private)
- (void)setupToolbar;
@end
@implementation MainController
- (id)init
{
if (self=[super init]) {
favList = [NSUnarchiver unarchiveObjectWithFile: kFavoriteDir];
if (favList)
[favList retain];
else
favList = [[NSMutableArray alloc] init];
}
[[NSNotificationCenter defaultCenter] addObserver: self
selector:@selector(addThisServerToFav:)
name:@"AddThisServerToFav"
object:nil];
return self;
}
- (void)awakeFromNib
{
[self setupFavoriteMenu];
[self setupToolbar];
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[favList retain];
[super dealloc];
}
- (void)setupFavoriteMenu
{
int i,menuIndex=[[NSApp mainMenu]
indexOfItemWithTitle:@"Favorites"];
NSMenuItem *favMenuItem = [[[NSMenuItem alloc] init] autorelease];
NSMenu *newFavMenu = [[[NSMenu alloc] init] autorelease];
[favMenuItem setTitle: @"Servers"];
[newFavMenu setTitle: @"Servers"];
[newFavMenu addItemWithTitle: @"Edit Server List..."
action: @selector( editFavorites: ) keyEquivalent: @""];
[newFavMenu addItemWithTitle: @"Add Server To List"
action: @selector( addServerToFav: ) keyEquivalent: @"A"];
[newFavMenu addItem: [NSMenuItem separatorItem]];
for (i=0;i<[favList count];i++)
[[newFavMenu addItemWithTitle: [[favList objectAtIndex:i] name]
action: @selector( connectToFav: ) keyEquivalent: @""]
setTag:i];
[favMenuItem setSubmenu: newFavMenu];
[[NSApp mainMenu] removeItemAtIndex: menuIndex];
[[NSApp mainMenu] insertItem: favMenuItem atIndex: menuIndex];
}
- (IBAction)connectToFav:(id)sender
{
HotFavorite *fav = [favList objectAtIndex: [sender tag]];
if (fav) {
HotController *controller = [HotController newController];
[controller setOwnServer:fav];
[controller conConnect:self];
} else
NSBeep();
}
- (IBAction)editFavorites:(id)sender
{
}
- (IBAction)newConnection:(id)sender
{
HotController *controller = [HotController newController];
[controller connect:self];
}
-
(NSApplicationTerminateReply)applicationShouldTerminate:
(NSApplication *)sender
{
[NSArchiver archiveRootObject: favList toFile: kFavoriteDir];
return NSTerminateNow;
}
- (void)addThisServerToFav:(NSNotification *)notifi
{
HotFavorite *favItem = [notifi object];
[favList addObject:favItem];
[self setupFavoriteMenu];
}
/////////////////////////////
// NSToolbar Code
/////////////////////////////
- (void) setupToolbar {
// Create a new toolbar instance, and attach it to our document
window
NSToolbar *toolbar = [[[NSToolbar alloc] initWithIdentifier:
MyDocToolbarIdentifier] autorelease];
// Set up toolbar properties: Allow customization, give a
default display mode, and remember state in user defaults
[toolbar setAllowsUserCustomization: YES];
[toolbar setAutosavesConfiguration: YES];
[toolbar setDisplayMode: NSToolbarDisplayModeIconOnly];
// We are the delegate
[toolbar setDelegate: self];
// Attach the toolbar to the document window
[documentWindow setToolbar: toolbar];
}
- (NSToolbarItem *) toolbar: (NSToolbar *)toolbar
itemForItemIdentifier: (NSString *) itemIdent
willBeInsertedIntoToolbar:(BOOL) willBeInserted {
// Required delegate method Given an item identifier, self
method returns an item
// The toolbar will use self method to obtain toolbar items
that can be displayed in the customization sheet, or in the toolbar
itself
NSToolbarItem *toolbarItem = [[[NSToolbarItem alloc]
initWithItemIdentifier: itemIdent] autorelease];
if ([itemIdent isEqual: ConnectToolbarIdentifier]) {
// Set the text label to be displayed in the toolbar and
customization palette
[toolbarItem setLabel: @"Connect To"];
[toolbarItem setPaletteLabel: @"Connect To"];
// Set up a reasonable tooltip, and image Note, these aren't
localized, but you will likely want to localize many of the item's
properties
[toolbarItem setToolTip: @"Connect To a Server"];
[toolbarItem setImage: [NSImage imageNamed: @"Connect"]];
// Tell the item what message to send when it is clicked
[toolbarItem setTarget: self];
[toolbarItem setAction: @selector(saveDocument:)];
} else {
// itemIdent refered to a toolbar item that is not provide or
supported by us or cocoa
// Returning nil will inform the toolbar self kind of item is
not supported
toolbarItem = nil;
}
return toolbarItem;
}
- (NSArray *) toolbarDefaultItemIdentifiers: (NSToolbar *) toolbar {
// Required delegate method Returns the ordered list of items
to be shown in the toolbar by default
// If during the toolbar's initialization, no overriding values
are found in the user defaults, or if the
// user chooses to revert to the default items self set will be used
return [NSArray arrayWithObjects: ConnectToolbarIdentifier,
NSToolbarFlexibleSpaceItemIdentifier, nil];
}
- (NSArray *) toolbarAllowedItemIdentifiers: (NSToolbar *) toolbar {
// Required delegate method Returns the list of all allowed
items by identifier By default, the toolbar
// does not assume any items are allowed, even the separator
So, every allowed item must be explicitly listed
// The set of allowed items is used to construct the
customization palette
return [NSArray arrayWithObjects: ConnectToolbarIdentifier,
NSToolbarCustomizeToolbarItemIdentifier,
NSToolbarFlexibleSpaceItemIdentifier, NSToolbarSpaceItemIdentifier,
NSToolbarSeparatorItemIdentifier, nil];
}
@end
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.