summaryrefslogtreecommitdiff
path: root/macosx/RecentItemsMenu.m
blob: d50abeff4768ae01d7b613bca84d59a1bf97b9bf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
//
//  RecentItemsMenu.m
//  Pcsxr
//
//  Created by Nicolas Pepin-Perreault on 12-12-16.
//
//

#import "RecentItemsMenu.h"
#import "PcsxrController.h"
#import "ARCBridge.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) {
		NSMenuItem *tempItem = [self createMenuItem:url];
        [self addMenuItem:tempItem atIndex:index];
		RELEASEOBJ(tempItem);
        index++;
    }
}

- (void)addRecentItem:(NSURL*)documentURL
{
    [[NSDocumentController sharedDocumentController] noteNewRecentDocumentURL:documentURL];
    
    NSMenuItem* item = RETAINOBJ([self findMenuItemByURL:documentURL]);
    if(item != nil) {
        [self removeItem:item];
        [self insertItem:item atIndex:0];
		RELEASEOBJ(item);
    }
    else {
		NSMenuItem *newitem = [self createMenuItem:documentURL];
        [self addMenuItem:newitem];
		RELEASEOBJ(newitem);
    }
}

- (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
}

- (NSMenuItem*)createMenuItem:(NSURL*)documentURL
{
    NSMenuItem *newItem = [[NSMenuItem alloc] initWithTitle:[documentURL lastPathComponent] action:@selector(openRecentItem:) keyEquivalent:@""];
    [newItem setRepresentedObject:documentURL];
    //[newItem setEnabled:YES];
    [newItem setTarget:self];
    
    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