diff options
| author | SND\MaddTheSane_cp <SND\MaddTheSane_cp@e17a0e51-4ae3-4d35-97c3-1a29b211df97> | 2012-12-20 22:42:36 +0000 |
|---|---|---|
| committer | SND\MaddTheSane_cp <SND\MaddTheSane_cp@e17a0e51-4ae3-4d35-97c3-1a29b211df97> | 2012-12-20 22:42:36 +0000 |
| commit | ec03001040a634987b658e4b79e377ec4fd2e959 (patch) | |
| tree | d7a140be2703c4a92de736203ef02c70afb57787 /macosx/RecentItemsMenu.m | |
| parent | b35a42b0a1f8424b3e92b66bd315274ce748c2bb (diff) | |
| download | pcsxr-ec03001040a634987b658e4b79e377ec4fd2e959.tar.gz | |
Add a recent menu.
Ignoring Xcode 4 generated files.
git-svn-id: https://pcsxr.svn.codeplex.com/svn/pcsxr@81905 e17a0e51-4ae3-4d35-97c3-1a29b211df97
Diffstat (limited to 'macosx/RecentItemsMenu.m')
| -rw-r--r-- | macosx/RecentItemsMenu.m | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/macosx/RecentItemsMenu.m b/macosx/RecentItemsMenu.m new file mode 100644 index 00000000..aaf3f01b --- /dev/null +++ b/macosx/RecentItemsMenu.m @@ -0,0 +1,103 @@ +// +// RecentItemsMenu.m +// Pcsxr +// +// Created by Nicolas Pepin-Perreault on 12-12-16. +// +// + +#import "RecentItemsMenu.h" + +@implementation RecentItemsMenu + +// Initialization +- (void)awakeFromNib +{ + [self setAutoenablesItems:YES]; + + // Populate the menu + NSArray* recentDocuments = [[NSDocumentController sharedDocumentController] recentDocumentURLs]; + NSInteger index = 0; + for(NSURL* url in recentDocuments) { + [self addMenuItem:[self createMenuItem:url] atIndex:index]; + index++; + } +} + +- (void)addRecentItem:(NSURL*)documentURL +{ + [[NSDocumentController sharedDocumentController] noteNewRecentDocumentURL:documentURL]; + + NSMenuItem* item = [self findMenuItemByURL:documentURL]; + if(item != nil) { + [self removeItem:item]; + [self insertItem:item atIndex:0]; + } + else { + [self addMenuItem:[self createMenuItem:documentURL]]; + } +} + +- (void)addMenuItem:(NSMenuItem*)item +{ + [self addMenuItem:item atIndex:0]; + + // Prevent menu from overflowing; the -2 accounts for the "Clear..." and the separator items + NSInteger maxNumItems = [[NSDocumentController sharedDocumentController] maximumRecentDocumentCount]; + if(([self numberOfItems]-2) > maxNumItems) { + [self removeItemAtIndex:maxNumItems]; + } +} + +- (NSMenuItem*)findMenuItemByURL:(NSURL*)url +{ + for(NSMenuItem* item in [self itemArray]) { + if([[item representedObject] isEqual:url]) { + return item; + } + } + + return nil; +} + +- (void)addMenuItem:(NSMenuItem*)item atIndex:(NSInteger)index +{ + [self insertItem:item atIndex:index]; // insert at the top + [item release]; +} + +- (NSMenuItem*)createMenuItem:(NSURL*)documentURL +{ + NSMenuItem *newItem = [[NSMenuItem alloc] initWithTitle:[documentURL relativePath] action:@selector(openRecentItem:) keyEquivalent:@""]; + [newItem setRepresentedObject:documentURL]; + [newItem setEnabled:YES]; + [newItem setTarget:self]; + [newItem setTag:0]; + + return newItem; +} + +- (void)openRecentItem:(NSMenuItem*)sender +{ + NSURL* url = [sender representedObject]; + [self addRecentItem:url]; + [pcsxr runURL:url]; +} + +- (IBAction)clearRecentDocuments:(id)sender +{ + [self removeDocumentItems]; + [[NSDocumentController sharedDocumentController] clearRecentDocuments:sender]; +} + +// Document items are menu items with tag 0 +- (void)removeDocumentItems +{ + for(NSMenuItem* item in [self itemArray]) { + if([item tag] == 0) { + [self removeItem:item]; + } + } +} + +@end |
