38 CPArray _documentTypes;
46 + (id)sharedDocumentController
63 _documents = [[CPArray alloc] init];
85 count = [_documents count];
87 for (; index < count; ++index)
89 var theDocument = _documents[index];
91 if ([[theDocument fileURL]
isEqual:aURL])
103 - (void)openUntitledDocumentOfType:(
CPString)aType display:(BOOL)shouldDisplay
112 [theDocument makeWindowControllers];
113 [theDocument showWindows];
152 else if (shouldDisplay)
153 [result showWindows];
180 - (
CPDocument)makeDocumentWithContentsOfURL:(
CPURL)anAbsoluteURL ofType:(
CPString)aType delegate:(
id)aDelegate didReadSelector:(
SEL)aSelector contextInfo:(
id)aContextInfo
182 return [[[
self documentClassForType:aType] alloc] initWithContentsOfURL:anAbsoluteURL ofType:aType delegate:aDelegate didReadSelector:aSelector contextInfo:aContextInfo];
196 - (
CPDocument)makeDocumentForURL:(
CPURL)anAbsoluteURL withContentsOfURL:(
CPURL)absoluteContentsURL ofType:(
CPString)aType delegate:(
id)aDelegate didReadSelector:(
SEL)aSelector contextInfo:(
id)aContextInfo
198 return [[[
self documentClassForType:aType] alloc] initForURL:anAbsoluteURL withContentsOfURL:absoluteContentsURL ofType:aType delegate:aDelegate didReadSelector:aSelector contextInfo:aContextInfo];
205 - (void)document:(
CPDocument)aDocument didRead:(BOOL)didRead contextInfo:(
id)aContextInfo
210 [aDocument makeWindowControllers];
212 if ([aContextInfo objectForKey:
@"shouldDisplay"])
213 [aDocument showWindows];
220 - (CFAction)newDocument:(
id)aSender
225 - (void)openDocument:(
id)aSender
229 [openPanel runModal];
231 var URLs = [openPanel URLs],
233 count = [URLs count];
235 for (; index < count; ++index)
236 [
self openDocumentWithContentsOfURL:[
CPURL URLWithString:URLs[index]] display:YES error:nil];
255 return [[[CPApp mainWindow] windowController] document];
264 [_documents addObject:aDocument];
273 [_documents removeObjectIdenticalTo:aDocument];
282 return [[aWindow windowController] document];
289 - (BOOL)hasEditedDocuments
291 var iter = [_documents objectEnumerator],
294 while ((obj = [iter nextObject]) !== nil)
296 if ([obj isDocumentEdited])
305 return [_documentTypes[0] objectForKey:@"CPBundleTypeName"];
311 count = _documentTypes.length,
316 for (; index < count; ++index)
318 var documentType = _documentTypes[index],
319 extensions = [documentType objectForKey:@"CFBundleTypeExtensions"],
321 extensionCount = extensions.length;
323 for (; extensionIndex < extensionCount; ++extensionIndex)
325 var thisExtension = [extensions[extensionIndex] lowercaseString];
326 if (thisExtension === extension)
327 return [documentType objectForKey:@"CPBundleTypeName"];
329 if (thisExtension ===
"****")
330 starType = [documentType objectForKey:@"CPBundleTypeName"];
343 count = [_documentTypes count];
345 for (;i < count; ++i)
347 var documentType = _documentTypes[i];
349 if ([documentType objectForKey:
@"CPBundleTypeName"] == aType)
363 var className = [[
self _infoForType:aType] objectForKey:@"CPDocumentClass"];
372 - (void)closeAllDocumentsWithDelegate:(
id)aDelegate didCloseAllSelector:(
SEL)didCloseSelector contextInfo:(Object)info
376 selector: didCloseSelector,
380 [
self _closeDocumentsStartingWith:nil shouldClose:YES context:context];
384 - (void)_closeDocumentsStartingWith:(
CPDocument)aDocument shouldClose:(BOOL)shouldClose context:(Object)context
390 if ([[
self documents] count] > 0)
392 [[[
self documents] lastObject] canCloseDocumentWithDelegate:self
393 shouldCloseSelector:@selector(_closeDocumentsStartingWith:shouldClose:context:)
394 contextInfo:context];
399 if ([context.delegate respondsToSelector:context.selector])
400 objj_msgSend(context.delegate, context.selector,
self, [[
self documents] count] === 0, context.context);
407 - (CPArray)recentDocumentURLs
410 if (typeof window[
"cpRecentDocumentURLs"] ===
'function')
411 return window.cpRecentDocumentURLs();
416 - (void)clearRecentDocuments:(
id)sender
418 if (typeof window[
"cpClearRecentDocuments"] ===
'function')
419 window.cpClearRecentDocuments();
421 [
self _updateRecentDocumentsMenu];
429 - (void)noteNewRecentDocumentURL:(
CPURL)aURL
431 var urlAsString = [aURL isKindOfClass:CPString] ? aURL : [aURL
absoluteString];
432 if (typeof window[
"cpNoteNewRecentDocumentPath"] ===
'function')
433 window.cpNoteNewRecentDocumentPath(urlAsString);
435 [
self _updateRecentDocumentsMenu];
438 - (void)_removeAllRecentDocumentsFromMenu:(
CPMenu)aMenu
441 count = [items count];
445 var item = items[count];
447 if ([item action] ===
@selector(_openRecentDocument:))
452 - (void)_updateRecentDocumentsMenu
454 var
menu = [[CPApp mainMenu] _menuWithName:@"_CPRecentDocumentsMenu"],
455 recentDocuments = [
self recentDocumentURLs],
456 menuItems = [menu itemArray],
457 documentCount = [recentDocuments count],
458 menuItemCount = [menuItems count];
460 [
self _removeAllRecentDocumentsFromMenu:menu];
466 if ([menuItems[0] isSeparatorItem])
467 [menu removeItemAtIndex:0];
471 if (![menuItems[0] isSeparatorItem])
472 [menu insertItem:[
CPMenuItem separatorItem] atIndex:0];
476 while (documentCount--)
478 var path = recentDocuments[documentCount],
479 item = [[
CPMenuItem alloc] initWithTitle:[path lastPathComponent] action:@selector(_openRecentDocument:) keyEquivalent:nil];
482 [menu insertItem:item atIndex:0];
486 - (void)_openRecentDocument:(
id)sender
488 [
self openDocumentWithContentsOfURL:[sender tag] display:YES error:nil];