-
Notifications
You must be signed in to change notification settings - Fork 41
/
NSDocumentController+FrontOrder.m
87 lines (76 loc) · 3.01 KB
/
NSDocumentController+FrontOrder.m
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
#import "NSDocumentController+FrontOrder.h"
#import "NSDocumentController+DisambiguateForUTI.h"
@implementation NSDocumentController (FrontOrder)
- (NSArray*)frontOrderDocuments {
NSArray* documents = [self documents] ;
NSInteger count = [documents count] ;
if (count > 1) {
NSDocument* frontDoc = [self currentDocument];
/* frontDoc will be nil if a non-document window such as Preferences
is frontmost. */
if (frontDoc) {
if ([documents objectAtIndex:0] != frontDoc) {
// Need to fix the order
NSMutableArray* mutant = [documents mutableCopy] ;
[mutant removeObject:frontDoc] ;
[mutant insertObject:frontDoc
atIndex:0] ;
documents = [[mutant copy] autorelease] ;
[mutant release] ;
}
}
}
return documents ;
}
- (NSArray*)defaultDocuments {
Class defaultDocumentClass = [self defaultDocumentClass] ;
NSMutableArray* defaultDocuments = [[NSMutableArray alloc] init] ;
for (NSDocument* document in [self documents]) {
if ([document isKindOfClass:defaultDocumentClass]) {
[defaultDocuments addObject:document] ;
}
}
NSArray* answer = [defaultDocuments copy] ;
[answer autorelease] ;
[defaultDocuments release] ;
return answer ;
}
- (NSDocument*)currentDefaultDocumentAggressively {
NSDocument* currentDocument = [self currentDocument] ;
if (!currentDocument) {
NSInteger docCount = [[self defaultDocuments] count] ;
if (docCount == 1) {
currentDocument = [[self defaultDocuments] firstObject] ;
}
else if ((docCount > 0) && !currentDocument) {
NSDocument* candidate ;
Class defaultDocumentClass = [self defaultDocumentClass] ;
candidate = [[[NSApp mainWindow] windowController] document] ;
if ([candidate isKindOfClass:defaultDocumentClass]) {
currentDocument = candidate ;
}
if (!currentDocument) {
candidate = [[[NSApp keyWindow] windowController] document] ;
if ([candidate isKindOfClass:defaultDocumentClass]) {
currentDocument = candidate ;
}
if (!currentDocument) {
for (NSWindow* window in [NSApp orderedWindows]) {
candidate = [[window windowController] document] ;
if ([candidate isKindOfClass:defaultDocumentClass]) {
currentDocument = candidate ;
}
if (currentDocument) {
break ;
}
}
}
if (!currentDocument) {
NSLog(@"Internal Error 501-3831 Can't discern current: %@", [self documents]) ;
}
}
}
}
return currentDocument ;
}
@end