2012-12-20 23:42:36 +01:00
|
|
|
//
|
|
|
|
// RecentItemsMenu.m
|
|
|
|
// Pcsxr
|
|
|
|
//
|
|
|
|
// Created by Nicolas Pepin-Perreault on 12-12-16.
|
|
|
|
//
|
|
|
|
//
|
|
|
|
|
|
|
|
#import "RecentItemsMenu.h"
|
2013-01-10 21:46:32 +01:00
|
|
|
#import "PcsxrController.h"
|
2012-12-20 23:42:36 +01:00
|
|
|
|
|
|
|
@implementation RecentItemsMenu
|
|
|
|
|
2013-09-13 23:31:56 +02:00
|
|
|
@synthesize pcsxr;
|
|
|
|
|
2012-12-20 23:42:36 +01:00
|
|
|
// Initialization
|
|
|
|
- (void)awakeFromNib
|
|
|
|
{
|
|
|
|
[self setAutoenablesItems:YES];
|
|
|
|
|
|
|
|
// Populate the menu
|
|
|
|
NSArray* recentDocuments = [[NSDocumentController sharedDocumentController] recentDocumentURLs];
|
|
|
|
NSInteger index = 0;
|
2013-01-29 20:16:02 +01:00
|
|
|
for (NSURL* url in recentDocuments) {
|
|
|
|
NSMenuItem *tempItem = [self newMenuItem:url];
|
2013-01-09 02:50:38 +01:00
|
|
|
[self addMenuItem:tempItem atIndex:index];
|
2012-12-20 23:42:36 +01:00
|
|
|
index++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)addRecentItem:(NSURL*)documentURL
|
|
|
|
{
|
|
|
|
[[NSDocumentController sharedDocumentController] noteNewRecentDocumentURL:documentURL];
|
|
|
|
|
2013-09-10 20:18:15 +02:00
|
|
|
NSMenuItem* item = [self findMenuItemByURL:documentURL];
|
2013-01-29 20:16:02 +01:00
|
|
|
if (item != nil) {
|
2012-12-20 23:42:36 +01:00
|
|
|
[self removeItem:item];
|
|
|
|
[self insertItem:item atIndex:0];
|
2013-01-20 18:26:34 +01:00
|
|
|
} else {
|
2013-01-29 20:16:02 +01:00
|
|
|
NSMenuItem *newitem = [self newMenuItem:documentURL];
|
2013-01-09 02:50:38 +01:00
|
|
|
[self addMenuItem:newitem];
|
2012-12-20 23:42:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (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];
|
2013-01-29 20:16:02 +01:00
|
|
|
if (([self numberOfItems] - 2) > maxNumItems) {
|
2012-12-20 23:42:36 +01:00
|
|
|
[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
|
|
|
|
}
|
|
|
|
|
2013-01-29 20:16:02 +01:00
|
|
|
- (NSMenuItem*)newMenuItem:(NSURL*)documentURL
|
2012-12-20 23:42:36 +01:00
|
|
|
{
|
2015-01-19 00:39:02 +01:00
|
|
|
NSString *documentPath = [documentURL path];
|
|
|
|
NSString *lastName = [[NSFileManager defaultManager] displayNameAtPath:documentPath];
|
|
|
|
NSImage *fileImage = [[NSWorkspace sharedWorkspace] iconForFile:documentPath];
|
|
|
|
fileImage.size = NSMakeSize(16, 16);
|
2013-02-28 17:54:50 +01:00
|
|
|
|
|
|
|
NSMenuItem *newItem = [[NSMenuItem alloc] initWithTitle:lastName action:@selector(openRecentItem:) keyEquivalent:@""];
|
2012-12-20 23:42:36 +01:00
|
|
|
[newItem setRepresentedObject:documentURL];
|
2015-01-19 00:39:02 +01:00
|
|
|
newItem.image = fileImage;
|
2012-12-20 23:42:36 +01:00
|
|
|
[newItem setTarget:self];
|
|
|
|
|
|
|
|
return newItem;
|
|
|
|
}
|
|
|
|
|
2015-01-19 00:39:02 +01:00
|
|
|
- (IBAction)openRecentItem:(NSMenuItem*)sender
|
2012-12-20 23:42:36 +01:00
|
|
|
{
|
|
|
|
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
|
|
|
|
{
|
2013-02-28 17:54:50 +01:00
|
|
|
NSMutableArray *removeItemsArray = [[NSMutableArray alloc] initWithCapacity:10];
|
2013-01-29 20:16:02 +01:00
|
|
|
for (NSMenuItem* item in [self itemArray]) {
|
|
|
|
if([item tag] == 0) {
|
|
|
|
[removeItemsArray addObject:item];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (NSMenuItem *item in removeItemsArray) {
|
|
|
|
[self removeItem:item];
|
|
|
|
}
|
2012-12-20 23:42:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|