#import "BPToolbarController.h" #import "BPDocument.h" #import "BPNoteDocument.h" #import "BPPilesDisplayController.h" #import "BPSearcher.h" // Thanks to Apple & CocoaDevCentral toolbar examples @implementation BPToolbarController static NSToolbarItem* addToolbarItem(NSString *itemIdentifier, NSString *label, NSString *toolTip, id imageOrView) { NSToolbarItem *item = [[[NSToolbarItem alloc] initWithItemIdentifier:itemIdentifier] autorelease]; [item setLabel:label]; [item setPaletteLabel:label]; [item setToolTip:toolTip]; if ([imageOrView isKindOfClass:[NSImage class]]) [item setImage:imageOrView]; else if ([imageOrView isKindOfClass:[NSView class]]) [item setView:imageOrView]; [itemsAndIdentifiers setObject:item forKey:itemIdentifier]; return item; } + (void)setupConstants { TB_DELETE = @"delete"; TB_REPLY = @"reply"; TB_REPLY_ALL = @"replyAll"; TB_FORWARD = @"forward"; TB_ATTACH = @"attach"; TB_SEND = @"send"; TB_SEND_AS_EMAIL = @"sendAsEmail"; TB_SAVE_AS_DRAFT = @"saveAsDraft"; TB_MARK = @"markItem"; TB_NEW_EMAIL = @"newEmail"; TB_NEW_NOTE = @"newNote"; TB_GET_MAIL = @"getMail"; TB_FILTER_MAIL = @"mailDisplayFilter"; TB_SEARCH = @"searchBar"; } + (void)setupToolbars { // add standard toolbar items //NSToolbarItem *print = [[[NSToolbarItem alloc] initWithItemIdentifier:NSToolbarPrintItemIdentifier] autorelease]; NSToolbarItem *separator = [[[NSToolbarItem alloc] initWithItemIdentifier:NSToolbarSeparatorItemIdentifier] autorelease]; NSToolbarItem *flexibleSpace = [[[NSToolbarItem alloc] initWithItemIdentifier:NSToolbarFlexibleSpaceItemIdentifier] autorelease]; //[itemsAndIdentifiers setObject:print forKey:NSToolbarPrintItemIdentifier]; [itemsAndIdentifiers setObject:separator forKey:NSToolbarSeparatorItemIdentifier]; [itemsAndIdentifiers setObject:flexibleSpace forKey:NSToolbarFlexibleSpaceItemIdentifier]; // add custom toolbar items NSToolbarItem *delete = addToolbarItem(TB_DELETE, @"Delete", @"Move this item to the trash pile", [NSImage imageNamed:@"toolbar_delete"]), *reply = addToolbarItem(TB_REPLY, @"Reply", @"Reply to the sender of this email", [NSImage imageNamed:@"toolbar_reply"]), *replyAll = addToolbarItem(TB_REPLY_ALL, @"Reply all", @"Reply to all recipients for this email", [NSImage imageNamed:@"toolbar_replyAll"]), *forward = addToolbarItem(TB_FORWARD, @"Forward", @"Forward this email", [NSImage imageNamed:@"toolbar_forward"]), *attach = addToolbarItem(TB_ATTACH, @"Attach", @"Attach a file to this email", [NSImage imageNamed:@"toolbar_attach"]), *send = addToolbarItem(TB_SEND, @"Send", @"Send this email", [NSImage imageNamed:@"toolbar_send"]), *sendAsEmail = addToolbarItem(TB_SEND_AS_EMAIL, @"Send as email", @"Send this note's contents as an email", [NSImage imageNamed:@"toolbar_send"]), *saveAsDraft = addToolbarItem(TB_SAVE_AS_DRAFT, @"Save as draft", @"Save this email into the Drafts pile", [NSImage imageNamed:@"toolbar_saveAsDraft"]), *markItem = addToolbarItem(TB_MARK, @"Mark flagged", @"Mark email as flagged", [NSImage imageNamed:@"toolbar_mark"]); // set out toolbar items for each different toolbar NSArray *defaultToolbarItems_pendingEmail = [NSArray arrayWithObjects: [markItem itemIdentifier], [separator itemIdentifier], [send itemIdentifier], [attach itemIdentifier], [saveAsDraft itemIdentifier], [flexibleSpace itemIdentifier], [delete itemIdentifier], nil]; NSArray *defaultToolbarItems_notPendingEmail = [NSArray arrayWithObjects: [markItem itemIdentifier], [separator itemIdentifier], [reply itemIdentifier], [replyAll itemIdentifier], [forward itemIdentifier], [flexibleSpace itemIdentifier], [delete itemIdentifier], nil]; NSArray *defaultToolbarItems_note = [NSArray arrayWithObjects: [markItem itemIdentifier], [separator itemIdentifier], [saveAsDraft itemIdentifier], [flexibleSpace itemIdentifier], [delete itemIdentifier], nil]; // store all toolbar items dictionaries in a dictionary, mapping // toolbar identifier -> default toolbar items' identifiers toolbarsAndItemsIdentifiers = [[NSDictionary dictionaryWithObjectsAndKeys: defaultToolbarItems_pendingEmail, @"PendingEmail", defaultToolbarItems_notPendingEmail, @"NotPendingEmail", defaultToolbarItems_note, @"Note", NULL] retain]; } + (void)initialize { itemsAndIdentifiers = [[NSMutableDictionary alloc] initWithCapacity:25]; [BPToolbarController setupConstants]; [BPToolbarController setupToolbars]; } - (void)setupMainToolbar { [mainWindowToolbarPopUpButton setHidden:NO]; [mainWindowToolbarPopUpButton setAction:@selector(filterMailDisplay:)]; [mainWindowToolbarSearchField setHidden:NO]; [mainWindowToolbarSearchField setAction:@selector(search:)]; // for main window toolbar NSToolbarItem *newEmail = addToolbarItem(TB_NEW_EMAIL, @"Write an email", @"Write an email", [NSImage imageNamed:@"toolbar_newEmail"]), *newNote = addToolbarItem(TB_NEW_NOTE, @"Write a note", @"Write a note", [NSImage imageNamed:@"toolbar_newNote"]), *getMail = addToolbarItem(TB_GET_MAIL, @"Get mail", @"Get mail", [NSImage imageNamed:@"getNewMail"]), *mailDisplayFilter = addToolbarItem(TB_FILTER_MAIL, @"Mail to display", @"Show or hide emails", mainWindowToolbarPopUpButton), *searchBar = addToolbarItem(TB_SEARCH, @"Search", @"Search all messages", mainWindowToolbarSearchField); [newEmail setTarget:mailSender]; [newEmail setAction:@selector(writeNewEmail:)]; [newNote setTarget:noteWriter]; [newNote setAction:@selector(writeNewNote:)]; [getMail setTarget:newMailGetter]; [getMail setAction:@selector(getMail:)]; // must set min sizes for views within toolbar items otherwise they don't get displayed!!! [mailDisplayFilter setMinSize:[[mailDisplayFilter view] bounds].size]; [searchBar setMinSize:[[searchBar view] bounds].size]; mainWindowToolbar = [[NSToolbar alloc] initWithIdentifier:@"MainWindowToolbar"]; mainWindowToolbarItems = [[NSArray arrayWithObjects: [newEmail itemIdentifier], [newNote itemIdentifier], NSToolbarSeparatorItemIdentifier, [getMail itemIdentifier], NSToolbarFlexibleSpaceItemIdentifier, [mailDisplayFilter itemIdentifier], [searchBar itemIdentifier], nil] retain]; [mainWindowToolbar setAllowsUserCustomization:YES]; [mainWindowToolbar setAutosavesConfiguration:YES]; [mainWindowToolbar setDisplayMode:NSToolbarDisplayModeIconAndLabel]; [mainWindowToolbar setDelegate:self]; } + (NSToolbar *)toolbarForDocument:(NSObject *)aDocument { NSString *identifier; if ([aDocument isKindOfClass:[BPNoteDocument class]]) { identifier = @"Note"; } else { if ([aDocument isEditable]) { identifier = @"PendingEmail"; } else { identifier = @"NotPendingEmail"; } } NSToolbar *tb = [[[NSToolbar alloc] initWithIdentifier:identifier] autorelease]; [tb setAllowsUserCustomization:YES]; [tb setAutosavesConfiguration:YES]; [tb setDisplayMode:NSToolbarDisplayModeIconAndLabel]; [tb setSizeMode:NSToolbarSizeModeSmall]; return tb; } #pragma mark - - (id)initWithDelegate:(id)anActionDelegate { [super init]; actionDelegate = anActionDelegate; toolbarItemWasClickedSel = @selector(toolbarItemWasClicked:); return self; } - (void)updateForDocument:(NSObject *)aDocument { NSToolbar *newToolbar = [[BPToolbarController toolbarForDocument:aDocument] retain]; [toolbar release]; toolbar = newToolbar; [toolbar setDelegate:self]; // list of the item identifiers used in this toolbar toolbarItemIdentifiers = [toolbarsAndItemsIdentifiers objectForKey:[toolbar identifier]]; // set the toolbar items for this toolbar, by copying the toolbar items // appropriate for this toolbar (must copy so you aren't modifying other toolbars' items!) toolbarItemsWithIdentifiers = [[NSMutableDictionary alloc] initWithCapacity:[toolbarItemIdentifiers count]]; NSToolbarItem *item; int i; for (i=0; i<[toolbarItemIdentifiers count]; i++) { item = [itemsAndIdentifiers objectForKey:[toolbarItemIdentifiers objectAtIndex:i]]; [toolbarItemsWithIdentifiers setObject:[item copy] forKey:[item itemIdentifier]]; } } - (NSToolbar *)mainWindowToolbar { if (mainWindowToolbar == nil) [self setupMainToolbar]; return mainWindowToolbar; } - (NSToolbar *)toolbar { return toolbar; } - (void)clickedToolbarItem:(id)sender { if ([actionDelegate respondsToSelector:toolbarItemWasClickedSel]) [actionDelegate performSelector:toolbarItemWasClickedSel withObject:sender]; } #pragma mark - #pragma mark - toolbar delegate methods - // used by the toolbar to obtain the toolbar items when it initially builds the // toolbar or when an item is added to a toolbar, and when the customization // sheet is opened - (NSToolbarItem *)toolbar:(NSToolbar *)tb itemForItemIdentifier:(NSString *)itemIdentifier willBeInsertedIntoToolbar:(BOOL)flag { //return [itemsAndIdentifiers objectForKey:itemIdentifier]; if (tb == mainWindowToolbar) { return [itemsAndIdentifiers objectForKey:itemIdentifier]; } else { return [toolbarItemsWithIdentifiers objectForKey:itemIdentifier]; } } // returns an array that contains the identifier for each of all the items that // are allowed to be displayed in the toolbar - (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar *)tb { //return [toolbarsAndItems objectForKey:[toolbar identifier]]; if (tb == mainWindowToolbar) return mainWindowToolbarItems; else return toolbarItemIdentifiers; } // returns an array of identifiers for the items that you want to use as the // default set - (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar *)tb { //return [toolbarsAndItems objectForKey:[toolbar identifier]]; if (tb == mainWindowToolbar) return mainWindowToolbarItems; else return toolbarItemIdentifiers; } #pragma mark - optional toolbar delegate methods // called right before an item is added to a toolbar // Use this method to do any initilization of the toolbar item that it needs - (void)toolbarWillAddItem:(NSNotification *) notification { NSToolbarItem *item = [[notification userInfo] objectForKey:@"item"]; // set up the item here //if (![[item itemIdentifier] isEqual:TB_FILTER_MAIL] && ![[item itemIdentifier] isEqual:TB_SEARCH]) { if (![mainWindowToolbarItems containsObject:[item itemIdentifier]]) { // set target/action [item setTarget:self]; [item setAction:@selector(clickedToolbarItem:)]; [item setEnabled:YES]; } } - (void)dealloc { NSLog(@"destroying %@", self); /* [toolbarsAndItems release]; [toolbar_pendingEmail release]; [toolbar_notPendingEmail release]; [toolbar_note release]; */ [toolbar release]; [toolbarItemsWithIdentifiers release]; [super dealloc]; } @end