File: RecentItemsMenu.m

package info (click to toggle)
pcsxr 1.9.94-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 10,500 kB
  • ctags: 16,110
  • sloc: ansic: 117,276; objc: 8,159; cpp: 1,140; makefile: 316; asm: 145; pascal: 30; sh: 20
file content (114 lines) | stat: -rw-r--r-- 2,881 bytes parent folder | download | duplicates (3)
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
109
110
111
112
113
114
//
//  RecentItemsMenu.m
//  Pcsxr
//
//  Created by Nicolas Pepin-Perreault on 12-12-16.
//
//

#import "RecentItemsMenu.h"
#import "PcsxrController.h"

@implementation RecentItemsMenu

@synthesize pcsxr;

// 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 newMenuItem:url];
        [self addMenuItem:tempItem 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 {
		NSMenuItem *newitem = [self newMenuItem:documentURL];
        [self addMenuItem: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*)newMenuItem:(NSURL*)documentURL
{
    NSString *lastName = nil;
	[documentURL getResourceValue:&lastName forKey:NSURLLocalizedNameKey error:NULL];
	if (!lastName) {
		lastName = [documentURL lastPathComponent];
	}
	
	NSMenuItem *newItem = [[NSMenuItem alloc] initWithTitle:lastName action:@selector(openRecentItem:) keyEquivalent:@""];
    [newItem setRepresentedObject:documentURL];
    [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
{
	NSMutableArray *removeItemsArray = [[NSMutableArray alloc] initWithCapacity:10];
	for (NSMenuItem* item in [self itemArray]) {
		if([item tag] == 0) {
			[removeItemsArray addObject:item];
		}
	}
	for (NSMenuItem *item in removeItemsArray) {
		[self removeItem:item];
	}
}

@end