From 0a6a53a530c6fead2ed807b08838db3d095e6393 Mon Sep 17 00:00:00 2001 From: stuconnolly Date: Sat, 19 Mar 2011 18:53:25 +0000 Subject: Remove sub-menus from default favorite popup button. --- Source/SPGeneralPreferencePane.h | 2 + Source/SPGeneralPreferencePane.m | 80 +++++++++++++++++++++++++++++----------- 2 files changed, 61 insertions(+), 21 deletions(-) (limited to 'Source') diff --git a/Source/SPGeneralPreferencePane.h b/Source/SPGeneralPreferencePane.h index 7d14492e..c573b84a 100644 --- a/Source/SPGeneralPreferencePane.h +++ b/Source/SPGeneralPreferencePane.h @@ -34,6 +34,8 @@ */ @interface SPGeneralPreferencePane : SPPreferencePane { + NSImage *folderImage; + IBOutlet NSPopUpButton *defaultFavoritePopup; } diff --git a/Source/SPGeneralPreferencePane.m b/Source/SPGeneralPreferencePane.m index ecc3172a..d8daa229 100644 --- a/Source/SPGeneralPreferencePane.m +++ b/Source/SPGeneralPreferencePane.m @@ -29,14 +29,27 @@ #import "SPFavoriteNode.h" #import "SPGroupNode.h" +static NSString *SPDatabaseImage = @"database-small"; + @interface SPGeneralPreferencePane () -- (NSMenuItem *)_constructMenuItemForNode:(SPTreeNode *)node; +- (NSArray *)_constructMenuItemsForNode:(SPTreeNode *)node atLevel:(NSUInteger)level; @end @implementation SPGeneralPreferencePane +#pragma mark - +#pragma mark Initialisation + +- (void)awakeFromNib +{ + // Generic folder image for use in the outline view's groups + folderImage = [[[NSWorkspace sharedWorkspace] iconForFileType:NSFileTypeForHFSTypeCode(kGenericFolderIcon)] retain]; + + [folderImage setSize:NSMakeSize(16, 16)]; +} + #pragma mark - #pragma mark IB action methods @@ -44,7 +57,15 @@ * Updates the default favorite. */ - (IBAction)updateDefaultFavorite:(id)sender -{ +{ + for (NSMenuItem *item in [defaultFavoritePopup itemArray]) + { + [item setState:NSOffState]; + } + + [sender setState:NSOnState]; + [defaultFavoritePopup setTitle:[sender title]]; + [prefs setBool:([defaultFavoritePopup indexOfSelectedItem] == 0) forKey:SPSelectLastFavoriteUsed]; [prefs setInteger:[sender tag] forKey:SPDefaultFavorite]; @@ -60,18 +81,18 @@ { [defaultFavoritePopup removeAllItems]; - // Use the last used favorite [defaultFavoritePopup addItemWithTitle:NSLocalizedString(@"Last Used", @"Last Used entry in favorites menu")]; [[defaultFavoritePopup menu] addItem:[NSMenuItem separatorItem]]; // Add all favorites to the menu for (SPTreeNode *node in [[[[[SPFavoritesController sharedFavoritesController] favoritesTree] childNodes] objectAtIndex:0] childNodes]) { - NSMenuItem *menuItem = [self _constructMenuItemForNode:node]; - - [[defaultFavoritePopup menu] addItem:menuItem]; + NSArray *items = [self _constructMenuItemsForNode:node atLevel:0]; - [menuItem release]; + for (NSMenuItem *item in items) + { + [[defaultFavoritePopup menu] addItem:item]; + } } // Select the default favorite from prefs @@ -93,40 +114,48 @@ * * @return The menu item */ -- (NSMenuItem *)_constructMenuItemForNode:(SPTreeNode *)node +- (NSArray *)_constructMenuItemsForNode:(SPTreeNode *)node atLevel:(NSUInteger)level { - NSMenuItem *menuItem = nil; + NSMutableArray *items = [NSMutableArray array]; if ([node isGroup]) { + level++; + SPGroupNode *groupNode = (SPGroupNode *)[node representedObject]; - menuItem = [[NSMenuItem alloc] initWithTitle:[groupNode nodeName] action:NULL keyEquivalent:@""]; + NSMenuItem *groupItem = [[NSMenuItem alloc] initWithTitle:[groupNode nodeName] action:NULL keyEquivalent:@""]; - NSMenu *subMenu = [[NSMenu alloc] initWithTitle:[groupNode nodeName]]; + [groupItem setEnabled:NO]; + [groupItem setImage:folderImage]; + + [items addObject:groupItem]; + + [groupItem release]; for (SPTreeNode *childNode in [node childNodes]) { - NSMenuItem *innerItem = [self _constructMenuItemForNode:childNode]; - - [subMenu addItem:innerItem]; - - [innerItem release]; + NSArray *innerItems = [self _constructMenuItemsForNode:childNode atLevel:level]; + + [items addObjectsFromArray:innerItems]; } - - [menuItem setSubmenu:subMenu]; } else { NSDictionary *favorite = [(SPFavoriteNode *)[node representedObject] nodeFavorite]; - menuItem = [[NSMenuItem alloc] initWithTitle:[favorite objectForKey:SPFavoriteNameKey] action:@selector(updateDefaultFavorite:) keyEquivalent:@""]; + NSMenuItem *menuItem = [[NSMenuItem alloc] initWithTitle:[favorite objectForKey:SPFavoriteNameKey] action:@selector(updateDefaultFavorite:) keyEquivalent:@""]; [menuItem setTag:[[favorite objectForKey:SPFavoriteIDKey] integerValue]]; - + [menuItem setImage:[NSImage imageNamed:SPDatabaseImage]]; + [menuItem setIndentationLevel:level]; [menuItem setTarget:self]; + + [items addObject:menuItem]; + + [menuItem release]; } - return menuItem; + return items; } #pragma mark - @@ -162,4 +191,13 @@ return NO; } +#pragma mark - + +- (void)dealloc +{ + [folderImage release], folderImage = nil; + + [super dealloc]; +} + @end -- cgit v1.2.3