mirror of
https://github.com/gnustep/libs-gui.git
synced 2025-05-29 20:47:38 +00:00
Added NSWindowController and the NSDocument* classes.
git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gui/trunk@4868 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
parent
33dbd8054f
commit
dd6369058e
12 changed files with 1841 additions and 2 deletions
|
@ -65,6 +65,8 @@ NSCustomImageRep.m \
|
|||
NSDataLink.m \
|
||||
NSDataLinkManager.m \
|
||||
NSDataLinkPanel.m \
|
||||
NSDocument.m \
|
||||
NSDocumentController.m \
|
||||
NSEPSImageRep.m \
|
||||
NSEvent.m \
|
||||
NSFileWrapper.m \
|
||||
|
@ -125,6 +127,7 @@ NSTextStorage.m \
|
|||
NSTextView.m \
|
||||
NSView.m \
|
||||
NSWindow.m \
|
||||
NSWindowController.m \
|
||||
NSWorkspace.m \
|
||||
GSComboSupport.m \
|
||||
GSHelpManagerPanel.m \
|
||||
|
@ -171,6 +174,9 @@ AppKit/NSCustomImageRep.h \
|
|||
AppKit/NSDataLink.h \
|
||||
AppKit/NSDataLinkManager.h \
|
||||
AppKit/NSDataLinkPanel.h \
|
||||
AppKit/NSDocument.h \
|
||||
AppKit/NSDocumentController.h \
|
||||
AppKit/NSDocumentFrameworkPrivate.h \
|
||||
AppKit/NSDragging.h \
|
||||
AppKit/NSEPSImageRep.h \
|
||||
AppKit/NSEvent.h \
|
||||
|
@ -239,12 +245,14 @@ AppKit/NSTextStorage.h \
|
|||
AppKit/NSTextView.h \
|
||||
AppKit/NSView.h \
|
||||
AppKit/NSWindow.h \
|
||||
AppKit/NSWindowController.h \
|
||||
AppKit/NSWorkspace.h \
|
||||
AppKit/GSTrackingRect.h \
|
||||
AppKit/nsimage-tiff.h \
|
||||
AppKit/GSMethodTable.h \
|
||||
AppKit/DPSOperators.h \
|
||||
AppKit/PSOperators.h \
|
||||
AppKit/GSHelpManagerPanel.h \
|
||||
AppKit/GSPasteboardServer.h \
|
||||
AppKit/GSServicesManager.h \
|
||||
AppKit/GSWraps.h
|
||||
|
|
|
@ -52,6 +52,7 @@
|
|||
|
||||
#include <AppKit/NSGraphicsContext.h>
|
||||
#include <AppKit/NSApplication.h>
|
||||
#include <AppKit/NSDocumentController.h>
|
||||
#include <AppKit/NSPopUpButton.h>
|
||||
#include <AppKit/NSPasteboard.h>
|
||||
#include <AppKit/NSPanel.h>
|
||||
|
@ -217,6 +218,10 @@ NSApplication *NSApp = nil;
|
|||
{
|
||||
[delegate application: self openFile: filePath];
|
||||
}
|
||||
else
|
||||
{
|
||||
[[NSDocumentController sharedDocumentController] openDocumentWithContentsOfFile:filePath display:YES];
|
||||
}
|
||||
}
|
||||
else if ((filePath = [defs stringForKey: @"GSTempPath"]) != nil)
|
||||
{
|
||||
|
@ -224,6 +229,10 @@ NSApplication *NSApp = nil;
|
|||
{
|
||||
[delegate application: self openTempFile: filePath];
|
||||
}
|
||||
else
|
||||
{
|
||||
[[NSDocumentController sharedDocumentController] openDocumentWithContentsOfFile:filePath display:YES];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1448,6 +1457,9 @@ NSAssert([event retainCount] > 0, NSInternalInconsistencyException);
|
|||
|
||||
if ([delegate respondsToSelector: @selector(applicationShouldTerminate:)])
|
||||
shouldTerminate = [delegate applicationShouldTerminate: sender];
|
||||
else
|
||||
shouldTerminate = [[NSDocumentController sharedDocumentController] reviewUnsavedDocumentsWithAlertTitle:@"Quit" cancellable:YES];
|
||||
|
||||
if (shouldTerminate)
|
||||
{
|
||||
app_should_quit = YES;
|
||||
|
|
653
Source/NSDocument.m
Normal file
653
Source/NSDocument.m
Normal file
|
@ -0,0 +1,653 @@
|
|||
#import <AppKit/NSDocument.h>
|
||||
#import <Foundation/NSData.h>
|
||||
#import <AppKit/NSFileWrapper.h>
|
||||
#import <AppKit/NSSavePanel.h>
|
||||
#import <AppKit/NSPrintInfo.h>
|
||||
#import <AppKit/NSPageLayout.h>
|
||||
#import <AppKit/NSView.h>
|
||||
#import <AppKit/NSPopUpButton.h>
|
||||
#import <AppKit/NSDocumentFrameworkPrivate.h>
|
||||
|
||||
|
||||
@implementation NSDocument
|
||||
|
||||
+ (void)initialize
|
||||
{
|
||||
}
|
||||
|
||||
+ (NSArray *)readableTypes
|
||||
{
|
||||
return [[NSDocumentController sharedDocumentController]
|
||||
_editorAndViewerTypesForClass:self];
|
||||
}
|
||||
|
||||
+ (NSArray *)writableTypes
|
||||
{
|
||||
return [[NSDocumentController sharedDocumentController] _editorTypesForClass:self];
|
||||
}
|
||||
|
||||
+ (BOOL)isNativeType:(NSString *)type
|
||||
{
|
||||
return ([[self readableTypes] containsObject:type] &&
|
||||
[[self writableTypes] containsObject:type]);
|
||||
}
|
||||
|
||||
|
||||
- (id)init
|
||||
{
|
||||
static int untitledCount = 1;
|
||||
|
||||
[super init];
|
||||
_documentIndex = untitledCount++;
|
||||
return self;
|
||||
}
|
||||
|
||||
- (id)initWithContentsOfFile:(NSString *)fileName ofType:(NSString *)fileType
|
||||
{
|
||||
[super init];
|
||||
|
||||
if ([self readFromFile:fileName ofType:fileType])
|
||||
{
|
||||
[self setFileType:fileType];
|
||||
[self setFileName:fileName];
|
||||
}
|
||||
else
|
||||
{
|
||||
[self release];
|
||||
return nil;
|
||||
}
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
- (id)initWithContentsOfURL:(NSURL *)url ofType:(NSString *)fileType
|
||||
{
|
||||
[super init];
|
||||
|
||||
if ([self readFromURL:url ofType:fileType])
|
||||
{
|
||||
[self setFileType:fileType];
|
||||
[self setFileName:[url path]];
|
||||
}
|
||||
else
|
||||
{
|
||||
[self release];
|
||||
return nil;
|
||||
}
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)dealloc
|
||||
{
|
||||
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
||||
[(NSObject*)_undoManager release];
|
||||
[_fileName release];
|
||||
[_fileType release];
|
||||
[_windowControllers release];
|
||||
[_window release];
|
||||
[_printInfo release];
|
||||
[savePanelAccessory release];
|
||||
[spaButton release];
|
||||
[super dealloc];
|
||||
}
|
||||
|
||||
- (NSString *)fileName
|
||||
{
|
||||
return _fileName;
|
||||
}
|
||||
|
||||
- (void)setFileName:(NSString *)fileName
|
||||
{
|
||||
[fileName retain];
|
||||
[_fileName release];
|
||||
_fileName = fileName;
|
||||
|
||||
[_windowControllers makeObjectsPerformSelector:
|
||||
@selector(_synchronizeWindowTitleWithDocumentName)];
|
||||
}
|
||||
|
||||
- (NSString *)fileType
|
||||
{
|
||||
return _fileType;
|
||||
}
|
||||
|
||||
- (void)setFileType:(NSString *)type
|
||||
{
|
||||
[type retain];
|
||||
[_fileType release];
|
||||
_fileType = type;
|
||||
}
|
||||
|
||||
- (NSArray *)windowControllers
|
||||
{
|
||||
return _windowControllers;
|
||||
}
|
||||
|
||||
- (void)addWindowController:(NSWindowController *)windowController
|
||||
{
|
||||
if (_windowControllers == nil) _windowControllers = [[NSMutableArray alloc] init];
|
||||
|
||||
[_windowControllers addObject:windowController];
|
||||
if ([windowController document] != self)
|
||||
[windowController setDocument:self];
|
||||
}
|
||||
|
||||
- (void)_removeWindowController:(NSWindowController *)windowController
|
||||
{
|
||||
if ([_windowControllers containsObject:windowController])
|
||||
{
|
||||
BOOL autoClose = [windowController shouldCloseDocument];
|
||||
|
||||
[windowController setDocument:nil];
|
||||
[_windowControllers removeObject:windowController];
|
||||
|
||||
if (autoClose || [_windowControllers count] == 0)
|
||||
{
|
||||
[self close];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
- (NSString *)windowNibName
|
||||
{
|
||||
return nil;
|
||||
}
|
||||
|
||||
// private; called during nib load. // we do not retain the window, since it should
|
||||
// already have a retain from the nib.
|
||||
- (void)setWindow:(NSWindow *)window
|
||||
{
|
||||
_window = window;
|
||||
}
|
||||
|
||||
/*
|
||||
* This private method is used to transfer window ownership to the
|
||||
* NSWindowController in situations (such as the default) where the
|
||||
* document is set to the nib owner, and thus owns the window immediately
|
||||
* following the loading of the nib.
|
||||
*/
|
||||
- (NSWindow *)_transferWindowOwnership
|
||||
{
|
||||
NSWindow *window = _window;
|
||||
_window = nil;
|
||||
return [window autorelease];
|
||||
}
|
||||
|
||||
- (void)makeWindowControllers
|
||||
{
|
||||
NSString *name = [self windowNibName];
|
||||
|
||||
if ([name length] > 0)
|
||||
{
|
||||
NSWindowController *controller;
|
||||
controller = [[NSWindowController alloc] initWithWindowNibName:name owner:self];
|
||||
[self addWindowController:controller];
|
||||
[controller release];
|
||||
}
|
||||
else
|
||||
{
|
||||
[NSException raise:NSInternalInconsistencyException
|
||||
format:@"%@ must override either -windowNibName or -makeWindowControllers",
|
||||
NSStringFromClass([self class])];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)showWindows
|
||||
{
|
||||
[_windowControllers makeObjectsPerformSelector:@selector(showWindow:) withObject:self];
|
||||
}
|
||||
|
||||
- (BOOL)isDocumentEdited
|
||||
{
|
||||
return _changeCount != 0;
|
||||
}
|
||||
|
||||
- (void)updateChangeCount:(NSDocumentChangeType)change
|
||||
{
|
||||
int i, count = [_windowControllers count];
|
||||
BOOL isEdited;
|
||||
|
||||
switch (change)
|
||||
{
|
||||
case NSChangeDone: _changeCount++; break;
|
||||
case NSChangeUndone: _changeCount--; break;
|
||||
case NSChangeCleared: _changeCount = 0; break;
|
||||
}
|
||||
|
||||
/*
|
||||
* NOTE: Apple's implementation seems to not call -isDocumentEdited
|
||||
* here but directly checks to see if _changeCount == 0. It seems it
|
||||
* would be better to call the method in case it's overridden by a
|
||||
* subclass, but we may want to keep Apple's behavior.
|
||||
*/
|
||||
isEdited = [self isDocumentEdited];
|
||||
|
||||
for (i=0; i<count; i++)
|
||||
{
|
||||
[[[_windowControllers objectAtIndex:i] window] setDocumentEdited:isEdited];
|
||||
}
|
||||
}
|
||||
|
||||
- (BOOL)canCloseDocument
|
||||
{
|
||||
int result;
|
||||
|
||||
if (![self isDocumentEdited]) return YES;
|
||||
|
||||
//FIXME -- localize.
|
||||
result = NSRunAlertPanel(@"Close", @"%@ has changed. Save?",
|
||||
@"Save", @"Cancel", @"Don't Save", [self displayName]);
|
||||
|
||||
#define Save NSAlertDefaultReturn
|
||||
#define Cancel NSAlertAlternateReturn
|
||||
#define DontSave NSAlertOtherReturn
|
||||
|
||||
switch (result)
|
||||
{
|
||||
// return NO if save failed
|
||||
case Save: [self saveDocument:nil]; return ![self isDocumentEdited];
|
||||
case DontSave: return YES;
|
||||
case Cancel:
|
||||
default: return NO;
|
||||
}
|
||||
}
|
||||
|
||||
- (BOOL)shouldCloseWindowController:(NSWindowController *)windowController
|
||||
{
|
||||
if (![_windowControllers containsObject:windowController]) return YES;
|
||||
|
||||
/* If it's the last window controller, pop up a warning */
|
||||
/* maybe we should count only loaded window controllers (or visible windows). */
|
||||
if ([windowController shouldCloseDocument] || [_windowControllers count] == 1)
|
||||
{
|
||||
return [self canCloseDocument];
|
||||
}
|
||||
|
||||
return YES;
|
||||
}
|
||||
|
||||
|
||||
- (NSString *)displayName
|
||||
{
|
||||
if ([self fileName] != nil)
|
||||
{
|
||||
return [[[self fileName] lastPathComponent] stringByDeletingPathExtension];
|
||||
}
|
||||
else
|
||||
{
|
||||
//FIXME -- localize.
|
||||
return [NSString stringWithFormat:@"Untitled-%d", _documentIndex];
|
||||
}
|
||||
}
|
||||
|
||||
- (BOOL)keepBackupFile
|
||||
{
|
||||
return NO;
|
||||
}
|
||||
|
||||
- (NSData *)dataRepresentationOfType:(NSString *)type
|
||||
{
|
||||
[NSException raise:NSInternalInconsistencyException format:@"%@ must implement %@",
|
||||
NSStringFromClass([self class]), NSStringFromSelector(_cmd)];
|
||||
return nil;
|
||||
}
|
||||
|
||||
- (BOOL)loadDataRepresentation:(NSData *)data ofType:(NSString *)type
|
||||
{
|
||||
[NSException raise:NSInternalInconsistencyException format:@"%@ must implement %@",
|
||||
NSStringFromClass([self class]), NSStringFromSelector(_cmd)];
|
||||
return NO;
|
||||
}
|
||||
|
||||
- (NSFileWrapper *)fileWrapperRepresentationOfType:(NSString *)type
|
||||
{
|
||||
NSData *data = [self dataRepresentationOfType:type];
|
||||
|
||||
if (data == nil) return nil;
|
||||
|
||||
return [[[NSFileWrapper alloc] initRegularFileWithContents:data] autorelease];
|
||||
}
|
||||
|
||||
- (BOOL)loadFileWrapperRepresentation:(NSFileWrapper *)wrapper ofType:(NSString *)type
|
||||
{
|
||||
if ([wrapper isRegularFile])
|
||||
{
|
||||
return [self loadDataRepresentation:[wrapper regularFileContents] ofType:type];
|
||||
}
|
||||
|
||||
/*
|
||||
* This even happens on a symlink. May want to use
|
||||
* -stringByResolvingAllSymlinksInPath somewhere, but Apple doesn't.
|
||||
*/
|
||||
NSLog(@"%@ must be overridden if your document deals with file packages.",
|
||||
NSStringFromSelector(_cmd));
|
||||
|
||||
return NO;
|
||||
}
|
||||
|
||||
- (BOOL)writeToFile:(NSString *)fileName ofType:(NSString *)type
|
||||
{
|
||||
return [[self fileWrapperRepresentationOfType:type]
|
||||
writeToFile:fileName atomically:YES updateFilenames:YES];
|
||||
}
|
||||
|
||||
- (BOOL)readFromFile:(NSString *)fileName ofType:(NSString *)type
|
||||
{
|
||||
NSFileWrapper *wrapper = [[[NSFileWrapper alloc] initWithPath:fileName] autorelease];
|
||||
return [self loadFileWrapperRepresentation:wrapper ofType:type];
|
||||
}
|
||||
|
||||
- (BOOL)revertToSavedFromFile:(NSString *)fileName ofType:(NSString *)type
|
||||
{
|
||||
return [self readFromFile:fileName ofType:type];
|
||||
}
|
||||
|
||||
- (IBAction)changeSaveType:(id)sender
|
||||
{ //FIXME if we have accessory -- store the desired save type somewhere.
|
||||
}
|
||||
|
||||
- (int)runModalSavePanel:(NSSavePanel *)savePanel withAccessoryView:(NSView *)accessoryView
|
||||
{
|
||||
[savePanel setAccessoryView:accessoryView];
|
||||
return [savePanel runModal];
|
||||
}
|
||||
|
||||
- (BOOL)shouldRunSavePanelWithAccessoryView
|
||||
{
|
||||
return YES;
|
||||
}
|
||||
|
||||
- (void)_loadPanelAccessoryNib
|
||||
{
|
||||
// FIXME. We need to load the pop-up button
|
||||
}
|
||||
- (void)_addItemsToSpaButtonFromArray:(NSArray *)types
|
||||
{
|
||||
// FIXME. Add types to popup.
|
||||
}
|
||||
|
||||
- (NSString *)fileNameFromRunningSavePanelForSaveOperation:(NSSaveOperationType)saveOperation
|
||||
{
|
||||
NSView *accessory = nil;
|
||||
NSString *title = @"save";
|
||||
NSSavePanel *savePanel = [NSSavePanel savePanel];
|
||||
NSArray *extensions = [[NSDocumentController sharedDocumentController]
|
||||
fileExtensionsFromType:[self fileType]];
|
||||
|
||||
if ([self shouldRunSavePanelWithAccessoryView])
|
||||
{
|
||||
if (savePanelAccessory == nil)
|
||||
[self _loadPanelAccessoryNib];
|
||||
|
||||
[self _addItemsToSpaButtonFromArray:extensions];
|
||||
|
||||
accessory = savePanelAccessory;
|
||||
}
|
||||
|
||||
if ([extensions count] > 0)
|
||||
[savePanel setRequiredFileType:[extensions objectAtIndex:0]];
|
||||
|
||||
switch (saveOperation)
|
||||
{
|
||||
// FIXME -- localize.
|
||||
case NSSaveOperation: title = @"Save"; break;
|
||||
case NSSaveAsOperation: title = @"Save As"; break;
|
||||
case NSSaveToOperation: title = @"Save To"; break;
|
||||
}
|
||||
|
||||
[savePanel setTitle:title];
|
||||
if ([self fileName])
|
||||
[savePanel setDirectory:[[self fileName] stringByDeletingLastPathComponent]];
|
||||
|
||||
if ([self runModalSavePanel:savePanel withAccessoryView:accessory])
|
||||
{
|
||||
return [savePanel filename];
|
||||
}
|
||||
|
||||
return nil;
|
||||
}
|
||||
|
||||
- (BOOL)shouldChangePrintInfo:(NSPrintInfo *)newPrintInfo
|
||||
{
|
||||
return YES;
|
||||
}
|
||||
|
||||
- (NSPrintInfo *)printInfo
|
||||
{
|
||||
return _printInfo? _printInfo : [NSPrintInfo sharedPrintInfo];
|
||||
}
|
||||
|
||||
- (void)setPrintInfo:(NSPrintInfo *)printInfo
|
||||
{
|
||||
[printInfo retain];
|
||||
[_printInfo release];
|
||||
_printInfo = printInfo;
|
||||
}
|
||||
|
||||
|
||||
// Page layout panel (Page Setup)
|
||||
|
||||
- (int)runModalPageLayoutWithPrintInfo:(NSPrintInfo *)printInfo
|
||||
{
|
||||
return [[NSPageLayout pageLayout] runModalWithPrintInfo:printInfo];
|
||||
}
|
||||
|
||||
- (IBAction)runPageLayout:(id)sender
|
||||
{
|
||||
NSPrintInfo *printInfo = [self printInfo];
|
||||
|
||||
if ([self runModalPageLayoutWithPrintInfo:printInfo] &&
|
||||
[self shouldChangePrintInfo:printInfo])
|
||||
{
|
||||
[self setPrintInfo:printInfo];
|
||||
[self updateChangeCount:NSChangeDone];
|
||||
}
|
||||
}
|
||||
|
||||
/* This is overridden by subclassers; the default implementation does nothing. */
|
||||
- (void)printShowingPrintPanel:(BOOL)flag
|
||||
{
|
||||
}
|
||||
|
||||
- (IBAction)printDocument:(id)sender
|
||||
{
|
||||
[self printShowingPrintPanel:YES];
|
||||
}
|
||||
|
||||
- (BOOL)validateMenuItem:(NSMenuItem *)anItem
|
||||
{
|
||||
if ([anItem action] == @selector(revertDocumentToSaved:))
|
||||
return ([self fileName] != nil && [self isDocumentEdited]);
|
||||
|
||||
// FIXME should validate spa popup items; return YES if it's a native type.
|
||||
|
||||
return YES;
|
||||
}
|
||||
|
||||
- (NSString *)saveFileType
|
||||
{
|
||||
// FIXME this should return type picked on save accessory
|
||||
// return [spaPopupButton title];
|
||||
return [self fileType];
|
||||
}
|
||||
|
||||
- (void)_doSaveAs:(NSSaveOperationType)saveOperation
|
||||
{
|
||||
NSFileManager *fileManager = [NSFileManager defaultManager];
|
||||
NSString *filename = [self fileName];
|
||||
NSString *backupFilename = nil;
|
||||
|
||||
if (filename == nil || saveOperation != NSSaveOperation)
|
||||
{
|
||||
filename = [self fileNameFromRunningSavePanelForSaveOperation:saveOperation];
|
||||
if (saveOperation == NSSaveOperation) saveOperation = NSSaveAsOperation;
|
||||
}
|
||||
|
||||
if (filename)
|
||||
{
|
||||
if ([fileManager fileExistsAtPath:filename])
|
||||
{
|
||||
NSString *extension = [filename pathExtension];
|
||||
|
||||
backupFilename = [filename stringByDeletingPathExtension];
|
||||
backupFilename = [backupFilename stringByAppendingString:@"~"];
|
||||
backupFilename = [backupFilename stringByAppendingPathExtension:extension];
|
||||
|
||||
/* Save panel has already asked if the user wants to replace it */
|
||||
|
||||
/* NSFileManager movePath: will fail if destination exists */
|
||||
if ([fileManager fileExistsAtPath:backupFilename])
|
||||
[fileManager removeFileAtPath:backupFilename handler:nil];
|
||||
|
||||
// Move or copy?
|
||||
if (![fileManager movePath:filename toPath:backupFilename handler:nil] &&
|
||||
[self keepBackupFile])
|
||||
{
|
||||
//FIXME -- localize.
|
||||
int result = NSRunAlertPanel(@"File Error",
|
||||
@"Can't create backup file. Save anyways?",
|
||||
@"Save", @"Cancel", nil);
|
||||
|
||||
if (result != NSAlertDefaultReturn) return;
|
||||
}
|
||||
}
|
||||
if ([self writeToFile:filename ofType:[self saveFileType]])
|
||||
{
|
||||
if (saveOperation != NSSaveToOperation)
|
||||
{
|
||||
[self setFileName:filename];
|
||||
[self setFileType:[self saveFileType]];
|
||||
[self updateChangeCount:NSChangeCleared];
|
||||
}
|
||||
|
||||
if (backupFilename && ![self keepBackupFile])
|
||||
{
|
||||
[fileManager removeFileAtPath:backupFilename handler:nil];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
- (IBAction)saveDocument:(id)sender
|
||||
{
|
||||
[self _doSaveAs:NSSaveOperation];
|
||||
}
|
||||
|
||||
- (IBAction)saveDocumentAs:(id)sender
|
||||
{
|
||||
[self _doSaveAs:NSSaveAsOperation];
|
||||
}
|
||||
|
||||
- (IBAction)saveDocumentTo:(id)sender
|
||||
{
|
||||
[self _doSaveAs:NSSaveToOperation];
|
||||
}
|
||||
|
||||
- (IBAction)revertDocumentToSaved:(id)sender
|
||||
{
|
||||
int result;
|
||||
|
||||
//FIXME -- localize.
|
||||
result = NSRunAlertPanel(@"Revert",
|
||||
@"%@ has been edited. Are you sure you want to undo changes?",
|
||||
@"Revert", @"Cancel", nil, [self displayName]);
|
||||
|
||||
if (result == NSAlertDefaultReturn &&
|
||||
[self revertToSavedFromFile:[self fileName] ofType:[self fileType]])
|
||||
{
|
||||
[self updateChangeCount:NSChangeCleared];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)close
|
||||
{
|
||||
// We have an _docFlags.inClose flag, but I don't think we need to use it.
|
||||
[_windowControllers makeObjectsPerformSelector:@selector(close)];
|
||||
[[NSDocumentController sharedDocumentController] _removeDocument:self];
|
||||
}
|
||||
|
||||
- (void)windowControllerWillLoadNib:(NSWindowController *)windowController {}
|
||||
- (void)windowControllerDidLoadNib:(NSWindowController *)windowController {}
|
||||
|
||||
- (NSUndoManager *)undoManager
|
||||
{
|
||||
if (_undoManager == nil && [self hasUndoManager])
|
||||
{
|
||||
[self setUndoManager:[[[NSUndoManager alloc] init] autorelease]];
|
||||
}
|
||||
|
||||
return _undoManager;
|
||||
}
|
||||
|
||||
- (void)setUndoManager:(NSUndoManager *)undoManager
|
||||
{
|
||||
if (undoManager != _undoManager)
|
||||
{
|
||||
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
|
||||
|
||||
if (_undoManager)
|
||||
{
|
||||
[center removeObserver:self
|
||||
name:NSUndoManagerWillCloseUndoGroupNotification
|
||||
object:_undoManager];
|
||||
[center removeObserver:self
|
||||
name:NSUndoManagerDidUndoChangeNotification
|
||||
object:_undoManager];
|
||||
[center removeObserver:self
|
||||
name:NSUndoManagerDidRedoChangeNotification
|
||||
object:_undoManager];
|
||||
}
|
||||
|
||||
[(NSObject*)undoManager retain];
|
||||
[(NSObject*)_undoManager release];
|
||||
_undoManager = undoManager;
|
||||
|
||||
if (_undoManager == nil)
|
||||
{
|
||||
[self setHasUndoManager:NO];
|
||||
}
|
||||
else
|
||||
{
|
||||
[center addObserver:self
|
||||
selector:@selector(_changeWasDone:)
|
||||
name:NSUndoManagerWillCloseUndoGroupNotification
|
||||
object:_undoManager];
|
||||
[center addObserver:self
|
||||
selector:@selector(_changeWasUndone:)
|
||||
name:NSUndoManagerDidUndoChangeNotification
|
||||
object:_undoManager];
|
||||
[[NSNotificationCenter defaultCenter]
|
||||
addObserver:self
|
||||
selector:@selector(_changeWasRedone:)
|
||||
name:NSUndoManagerDidRedoChangeNotification
|
||||
object:_undoManager];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
- (BOOL)hasUndoManager
|
||||
{
|
||||
return _docFlags.hasUndoManager;
|
||||
}
|
||||
|
||||
- (void)setHasUndoManager:(BOOL)flag
|
||||
{
|
||||
if (_undoManager && !flag)
|
||||
[self setUndoManager:nil];
|
||||
|
||||
_docFlags.hasUndoManager = flag;
|
||||
}
|
||||
|
||||
- (void)_changeWasDone:(NSNotification *)notification
|
||||
{ [self updateChangeCount:NSChangeDone];
|
||||
}
|
||||
- (void)_changeWasUndone:(NSNotification *)notification
|
||||
{ [self updateChangeCount:NSChangeUndone];
|
||||
}
|
||||
- (void)_changeWasRedone:(NSNotification *)notification
|
||||
{ [self updateChangeCount:NSChangeDone];
|
||||
}
|
||||
|
||||
@end
|
||||
|
528
Source/NSDocumentController.m
Normal file
528
Source/NSDocumentController.m
Normal file
|
@ -0,0 +1,528 @@
|
|||
|
||||
|
||||
#import <AppKit/NSDocumentController.h>
|
||||
#import <AppKit/NSOpenPanel.h>
|
||||
#import <AppKit/NSApplication.h>
|
||||
#import <AppKit/NSMenuItem.h>
|
||||
#import <AppKit/NSWorkspace.h>
|
||||
#import <AppKit/NSDocumentFrameworkPrivate.h>
|
||||
|
||||
|
||||
static NSString *NSTypesKey = @"NSTypes";
|
||||
static NSString *NSNameKey = @"NSName";
|
||||
static NSString *NSRoleKey = @"NSRole";
|
||||
static NSString *NSHumanReadableNameKey = @"NSHumanReadableName";
|
||||
static NSString *NSUnixExtensionsKey = @"NSUnixExtensions";
|
||||
static NSString *NSDOSExtensionsKey = @"NSDOSExtensions";
|
||||
static NSString *NSMacOSTypesKey = @"NSMacOSTypes";
|
||||
static NSString *NSMIMETypesKey = @"NSMIMETypes";
|
||||
static NSString *NSDocumentClassKey = @"NSDocumentClass";
|
||||
|
||||
#define TYPE_INFO(name) TypeInfoForName(_types, name)
|
||||
|
||||
static NSDictionary *TypeInfoForName(NSArray *types, NSString *typeName)
|
||||
{
|
||||
int i, count = [types count];
|
||||
for (i=0; i<count;i++)
|
||||
{
|
||||
NSDictionary *dict = [types objectAtIndex:i];
|
||||
if ([[dict objectForKey:NSNameKey] isEqualToString:typeName])
|
||||
return dict;
|
||||
}
|
||||
|
||||
return nil;
|
||||
}
|
||||
|
||||
@implementation NSDocumentController
|
||||
|
||||
+ (void)initialize
|
||||
{
|
||||
}
|
||||
|
||||
+ (id)documentController //private
|
||||
{
|
||||
return [self sharedDocumentController];
|
||||
}
|
||||
|
||||
+ (id)allocWithZone:(NSZone *)zone
|
||||
{
|
||||
return [self sharedDocumentController];
|
||||
}
|
||||
+ (id)sharedDocumentController
|
||||
{
|
||||
static id instance = nil;
|
||||
|
||||
if (instance == nil) instance = [[super allocWithZone:NULL] init];
|
||||
return instance;
|
||||
}
|
||||
|
||||
- init
|
||||
{
|
||||
NSDictionary *customDict = [[NSBundle mainBundle] infoDictionary];
|
||||
|
||||
_types = [[customDict objectForKey:NSTypesKey] retain];
|
||||
_documents = [[NSMutableArray alloc] init];
|
||||
[self setShouldCreateUI:YES];
|
||||
|
||||
[[[NSWorkspace sharedWorkspace] notificationCenter]
|
||||
addObserver:self
|
||||
selector:@selector(_workspaceWillPowerOff:)
|
||||
name:NSWorkspaceWillPowerOffNotification
|
||||
object:nil];
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)dealloc
|
||||
{
|
||||
[[[NSWorkspace sharedWorkspace] notificationCenter] removeObserver:self];
|
||||
[_documents release];
|
||||
[_types release];
|
||||
[super dealloc];
|
||||
}
|
||||
|
||||
- (BOOL)shouldCreateUI
|
||||
{
|
||||
return _controllerFlags.shouldCreateUI;
|
||||
}
|
||||
|
||||
- (void)setShouldCreateUI:(BOOL)flag
|
||||
{
|
||||
_controllerFlags.shouldCreateUI = flag;
|
||||
}
|
||||
|
||||
- (id)makeUntitledDocumentOfType:(NSString *)type
|
||||
{
|
||||
Class documentClass = [self documentClassForType:type];
|
||||
return [[[documentClass alloc] init] autorelease];
|
||||
}
|
||||
|
||||
- (id)makeDocumentWithContentsOfFile:(NSString *)fileName ofType:(NSString *)type
|
||||
{
|
||||
Class documentClass = [self documentClassForType:type];
|
||||
return [[[documentClass alloc] initWithContentsOfFile:fileName ofType:type] autorelease];
|
||||
}
|
||||
|
||||
- (id)makeDocumentWithContentsOfURL:(NSURL *)url ofType:(NSString *)type
|
||||
{
|
||||
Class documentClass = [self documentClassForType:type];
|
||||
return [[[documentClass alloc] initWithContentsOfURL:url ofType:type] autorelease];
|
||||
}
|
||||
|
||||
- _defaultType
|
||||
{
|
||||
if ([_types count] == 0) return nil; // raise exception?
|
||||
return [[_types objectAtIndex:0] objectForKey:NSNameKey];
|
||||
}
|
||||
|
||||
/* These next two should really have been public. */
|
||||
- (void)_addDocument:(NSDocument *)document
|
||||
{
|
||||
[_documents addObject:document];
|
||||
}
|
||||
|
||||
- (void)_removeDocument:(NSDocument *)document
|
||||
{
|
||||
[_documents removeObject:document];
|
||||
}
|
||||
|
||||
- (id)openUntitledDocumentOfType:(NSString*)type display:(BOOL)display
|
||||
{
|
||||
NSDocument *document = [self makeUntitledDocumentOfType:type];
|
||||
|
||||
if (document == nil) return nil;
|
||||
|
||||
[self _addDocument:document];
|
||||
if ([self shouldCreateUI])
|
||||
{
|
||||
[document makeWindowControllers];
|
||||
if (display)
|
||||
[document showWindows];
|
||||
}
|
||||
|
||||
return document;
|
||||
}
|
||||
|
||||
- (id)openDocumentWithContentsOfFile:(NSString *)fileName display:(BOOL)display
|
||||
{
|
||||
NSDocument *document = [self documentForFileName:fileName];
|
||||
|
||||
if (document == nil)
|
||||
{
|
||||
NSString *type = [self typeFromFileExtension:[fileName pathExtension]];
|
||||
|
||||
if ((document = [self makeDocumentWithContentsOfFile:fileName ofType:type]))
|
||||
[self _addDocument:document];
|
||||
if ([self shouldCreateUI])
|
||||
[document makeWindowControllers];
|
||||
}
|
||||
|
||||
if (display && [self shouldCreateUI])
|
||||
{
|
||||
[document showWindows];
|
||||
}
|
||||
|
||||
return document;
|
||||
}
|
||||
|
||||
- (id)openDocumentWithContentsOfURL:(NSURL *)url display:(BOOL)display
|
||||
{
|
||||
// Should we only do this if [url isFileURL] is YES?
|
||||
NSDocument *document = [self documentForFileName:[url path]];
|
||||
|
||||
if (document == nil)
|
||||
{
|
||||
NSString *type = [self typeFromFileExtension:[[url path] pathExtension]];
|
||||
|
||||
document = [self makeDocumentWithContentsOfURL:url ofType:type];
|
||||
if (document == nil) return nil;
|
||||
|
||||
[self _addDocument:document];
|
||||
if ([self shouldCreateUI])
|
||||
{
|
||||
[document makeWindowControllers];
|
||||
}
|
||||
}
|
||||
|
||||
if (display && [self shouldCreateUI])
|
||||
{
|
||||
[document showWindows];
|
||||
}
|
||||
|
||||
return document;
|
||||
}
|
||||
|
||||
- _setupOpenPanel
|
||||
{
|
||||
NSOpenPanel *openPanel = [NSOpenPanel openPanel];
|
||||
[openPanel setDirectory:[self currentDirectory]];
|
||||
[openPanel setAllowsMultipleSelection:YES];
|
||||
return openPanel;
|
||||
}
|
||||
|
||||
- (int)runModalOpenPanel:(NSOpenPanel *)openPanel forTypes:(NSArray *)openableFileExtensions
|
||||
{
|
||||
return [openPanel runModalForTypes:openableFileExtensions];
|
||||
}
|
||||
|
||||
- (NSArray *)_openableFileExtensions
|
||||
{
|
||||
int i, count = [_types count];
|
||||
NSMutableArray *array = [NSMutableArray arrayWithCapacity:count];
|
||||
|
||||
for (i=0; i<count; i++)
|
||||
{
|
||||
NSDictionary *typeInfo = [_types objectAtIndex:i];
|
||||
[array addObjectsFromArray:[typeInfo objectForKey:NSUnixExtensionsKey]];
|
||||
[array addObjectsFromArray:[typeInfo objectForKey:NSDOSExtensionsKey]];
|
||||
}
|
||||
|
||||
return array;
|
||||
}
|
||||
|
||||
- (NSArray *)fileNamesFromRunningOpenPanel
|
||||
{
|
||||
NSArray *types = [self _openableFileExtensions];
|
||||
NSOpenPanel *openPanel = [self _setupOpenPanel];
|
||||
|
||||
if ([self runModalOpenPanel:openPanel forTypes:types])
|
||||
{
|
||||
return [openPanel filenames];
|
||||
}
|
||||
|
||||
return nil;
|
||||
}
|
||||
|
||||
- (NSArray *)URLsFromRunningOpenPanel
|
||||
{
|
||||
NSArray *types = [self _openableFileExtensions];
|
||||
NSOpenPanel *openPanel = [self _setupOpenPanel];
|
||||
|
||||
if ([self runModalOpenPanel:openPanel forTypes:types])
|
||||
{
|
||||
return [openPanel URLs];
|
||||
}
|
||||
|
||||
return nil;
|
||||
}
|
||||
|
||||
|
||||
- (IBAction)saveAllDocuments:(id)sender
|
||||
{
|
||||
NSDocument *document;
|
||||
NSEnumerator *docEnum = [_documents objectEnumerator];
|
||||
|
||||
while ((document = [docEnum nextObject]))
|
||||
{
|
||||
if ([document isDocumentEdited]) //maybe we should save regardless...
|
||||
{
|
||||
[document saveDocument:sender];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
- (IBAction)openDocument:(id)sender
|
||||
{
|
||||
NSEnumerator *fileEnum = [[self fileNamesFromRunningOpenPanel] objectEnumerator];
|
||||
NSString *filename;
|
||||
|
||||
while ((filename = [fileEnum nextObject]))
|
||||
{
|
||||
[self openDocumentWithContentsOfFile:filename display:YES];
|
||||
}
|
||||
}
|
||||
|
||||
- (IBAction)newDocument:(id)sender
|
||||
{
|
||||
[self openUntitledDocumentOfType:[self _defaultType] display:YES];
|
||||
}
|
||||
|
||||
|
||||
- (BOOL)closeAllDocuments
|
||||
{
|
||||
NSDocument *document;
|
||||
NSEnumerator *docEnum = [_documents objectEnumerator];
|
||||
|
||||
while ((document = [docEnum nextObject]))
|
||||
{
|
||||
if (![document canCloseDocument]) return NO;
|
||||
[document close];
|
||||
[self _removeDocument:document];
|
||||
}
|
||||
|
||||
return YES;
|
||||
}
|
||||
|
||||
- (BOOL)reviewUnsavedDocumentsWithAlertTitle:(NSString *)title cancellable:(BOOL)cancellable
|
||||
{
|
||||
//FIXME -- localize.
|
||||
NSString *cancelString = (cancellable)? @"Cancel" : nil;
|
||||
int result;
|
||||
|
||||
if (![self hasEditedDocuments]) return YES;
|
||||
|
||||
result = NSRunAlertPanel(title, @"You have unsaved documents.",
|
||||
@"Review Unsaved", cancelString, @"Quit Anyways");
|
||||
|
||||
#define ReviewUnsaved NSAlertDefaultReturn
|
||||
#define Cancel NSAlertAlternateReturn
|
||||
#define QuitAnyways NSAlertOtherReturn
|
||||
|
||||
switch (result)
|
||||
{
|
||||
case ReviewUnsaved: return [self closeAllDocuments];
|
||||
case QuitAnyways: return YES;
|
||||
case Cancel:
|
||||
default: return NO;
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef OPENSTEP_ONLY
|
||||
/*
|
||||
* App delegate methods. Apple doesn't have these, but they put code
|
||||
* into NSApplication to call the corresponding NSDocumentController
|
||||
* methods if the app delegate didn't implement a given delegate method.
|
||||
*/
|
||||
- (BOOL)application:(NSApplication *)sender openFile:(NSString *)filename
|
||||
{
|
||||
return [self openDocumentWithContentsOfFile:filename display:YES] ? YES : NO;
|
||||
}
|
||||
|
||||
- (BOOL)application:(NSApplication *)sender openTempFile:(NSString *)filename;
|
||||
{
|
||||
return [self openDocumentWithContentsOfFile:filename display:YES] ? YES : NO;
|
||||
}
|
||||
|
||||
- (BOOL)applicationOpenUntitledFile:(NSApplication *)sender
|
||||
{
|
||||
return [self openUntitledDocumentOfType:[self _defaultType] display:YES] ? YES : NO;
|
||||
}
|
||||
|
||||
- (BOOL)application:(id)sender openFileWithoutUI:(NSString *)filename
|
||||
{
|
||||
return [self openDocumentWithContentsOfFile:filename display:NO] ? YES : NO;
|
||||
}
|
||||
|
||||
- (BOOL)applicationShouldTerminate:(NSApplication *)sender
|
||||
{
|
||||
return [self reviewUnsavedDocumentsWithAlertTitle:@"Quit" cancellable:YES];
|
||||
}
|
||||
#endif
|
||||
|
||||
- (void)_workspaceWillPowerOff:(NSNotification *)notification
|
||||
{
|
||||
// FIXME -- localize.
|
||||
[self reviewUnsavedDocumentsWithAlertTitle:@"Power" cancellable:NO];
|
||||
}
|
||||
|
||||
|
||||
- (NSArray *)documents
|
||||
{
|
||||
return _documents;
|
||||
}
|
||||
|
||||
- (BOOL)hasEditedDocuments;
|
||||
{
|
||||
int i, count = [_documents count];
|
||||
|
||||
for (i=0; i<count; i++)
|
||||
{
|
||||
if ([[_documents objectAtIndex:i] isDocumentEdited]) return YES;
|
||||
}
|
||||
|
||||
return NO;
|
||||
}
|
||||
|
||||
- (id)currentDocument
|
||||
{
|
||||
return [self documentForWindow:[[NSApplication sharedApplication] mainWindow]];
|
||||
}
|
||||
|
||||
- (NSString *)currentDirectory
|
||||
{
|
||||
NSFileManager *manager = [NSFileManager defaultManager];
|
||||
NSDocument *currentDocument = [self currentDocument];
|
||||
NSString *directory = [[currentDocument fileName] stringByDeletingLastPathComponent];
|
||||
BOOL isDir = NO;
|
||||
|
||||
if (directory &&
|
||||
[manager fileExistsAtPath:directory isDirectory:&isDir] && isDir) return directory;
|
||||
//FIXME -- need to remember last saved directory, and return that here.
|
||||
//Only return NSHomeDirectory if nothing's been saved yet.
|
||||
return NSHomeDirectory();
|
||||
}
|
||||
|
||||
- (id)documentForWindow:(NSWindow *)window
|
||||
{
|
||||
id document;
|
||||
|
||||
if (window == nil) return nil;
|
||||
if (![[window windowController] isKindOfClass:[NSWindowController class]]) return nil;
|
||||
|
||||
document = [[window windowController] document];
|
||||
if (![document isKindOfClass:[NSDocument class]]) return nil;
|
||||
return document;
|
||||
}
|
||||
|
||||
- (id)documentForFileName:(NSString *)fileName
|
||||
{
|
||||
int i, count = [_documents count];
|
||||
|
||||
for (i=0; i<count; i++)
|
||||
{
|
||||
NSDocument *document = [_documents objectAtIndex:i];
|
||||
|
||||
if ([[document fileName] isEqualToString:fileName])
|
||||
return document;
|
||||
}
|
||||
|
||||
return nil;
|
||||
}
|
||||
|
||||
- (BOOL)validateMenuItem:(NSMenuItem *)anItem
|
||||
{
|
||||
if ([anItem action] == @selector(saveAllDocuments:))
|
||||
return [self hasEditedDocuments];
|
||||
return YES;
|
||||
}
|
||||
|
||||
- (NSString *)displayNameForType:(NSString *)type
|
||||
{
|
||||
NSString *name = [TYPE_INFO(type) objectForKey:NSHumanReadableNameKey];
|
||||
|
||||
return name? name : type;
|
||||
}
|
||||
|
||||
- (NSString *)typeFromFileExtension:(NSString *)fileExtension
|
||||
{
|
||||
int i, count = [_types count];
|
||||
|
||||
for (i=0; i<count;i++)
|
||||
{
|
||||
NSDictionary *typeInfo = [_types objectAtIndex:i];
|
||||
|
||||
if ([[typeInfo objectForKey:NSUnixExtensionsKey] containsObject:fileExtension] ||
|
||||
[[typeInfo objectForKey:NSDOSExtensionsKey] containsObject:fileExtension])
|
||||
{
|
||||
return [typeInfo objectForKey:NSNameKey];
|
||||
}
|
||||
}
|
||||
|
||||
return nil;
|
||||
}
|
||||
|
||||
- (NSArray *)fileExtensionsFromType:(NSString *)type
|
||||
{
|
||||
NSDictionary *typeInfo = TYPE_INFO(type);
|
||||
NSArray *unixExtensions = [typeInfo objectForKey:NSUnixExtensionsKey];
|
||||
NSArray *dosExtensions = [typeInfo objectForKey:NSDOSExtensionsKey];
|
||||
|
||||
if (!dosExtensions) return unixExtensions;
|
||||
if (!unixExtensions) return dosExtensions;
|
||||
return [unixExtensions arrayByAddingObjectsFromArray:dosExtensions];
|
||||
}
|
||||
|
||||
- (Class)documentClassForType:(NSString *)type
|
||||
{
|
||||
NSString *className = [TYPE_INFO(type) objectForKey:NSDocumentClassKey];
|
||||
|
||||
return className? NSClassFromString(className) : Nil;
|
||||
}
|
||||
|
||||
static NSString *NSEditorRole = @"Editor";
|
||||
static NSString *NSViewerRole = @"Viewer";
|
||||
static NSString *NSNoRole = @"None";
|
||||
|
||||
- (NSArray *)_editorAndViewerTypesForClass:(Class)documentClass
|
||||
{
|
||||
int i, count = [_types count];
|
||||
NSMutableArray *types = [NSMutableArray arrayWithCapacity:count];
|
||||
NSString *docClassName = NSStringFromClass(documentClass);
|
||||
|
||||
for (i=0; i<count; i++)
|
||||
{
|
||||
NSDictionary *typeInfo = [_types objectAtIndex:i];
|
||||
NSString *className = [typeInfo objectForKey:NSDocumentClassKey];
|
||||
NSString *role = [typeInfo objectForKey:NSRoleKey];
|
||||
|
||||
if ([docClassName isEqualToString:className] &&
|
||||
(role == nil || [role isEqual:NSEditorRole] || [role isEqual:NSViewerRole]))
|
||||
{
|
||||
[types addObject:[typeInfo objectForKey:NSNameKey]];
|
||||
}
|
||||
}
|
||||
|
||||
return types;
|
||||
}
|
||||
|
||||
- (NSArray *)_editorTypesForClass:(Class)documentClass
|
||||
{
|
||||
int i, count = [_types count];
|
||||
NSMutableArray *types = [NSMutableArray arrayWithCapacity:count];
|
||||
NSString *docClassName = NSStringFromClass(documentClass);
|
||||
|
||||
for (i=0; i<count; i++)
|
||||
{
|
||||
NSDictionary *typeInfo = [_types objectAtIndex:i];
|
||||
NSString *className = [typeInfo objectForKey:NSDocumentClassKey];
|
||||
NSString *role = [typeInfo objectForKey:NSRoleKey];
|
||||
|
||||
if ([docClassName isEqualToString:className] &&
|
||||
(role == nil || [role isEqual:NSEditorRole]))
|
||||
{
|
||||
[types addObject:[typeInfo objectForKey:NSNameKey]];
|
||||
}
|
||||
}
|
||||
|
||||
return types;
|
||||
}
|
||||
|
||||
- (NSArray *)_exportableTypesForClass:(Class)documentClass
|
||||
{
|
||||
// Dunno what this method is for; maybe looks for filter types
|
||||
return [self _editorTypesForClass:documentClass];
|
||||
}
|
||||
|
||||
@end
|
||||
|
|
@ -42,7 +42,9 @@
|
|||
#include <Foundation/NSLock.h>
|
||||
#include <Foundation/NSUserDefaults.h>
|
||||
|
||||
#include <AppKit/NSDocument.h>
|
||||
#include <AppKit/NSWindow.h>
|
||||
#include <AppKit/NSWindowController.h>
|
||||
#include <AppKit/NSApplication.h>
|
||||
#include <AppKit/NSImage.h>
|
||||
#include <AppKit/NSTextFieldCell.h>
|
||||
|
@ -476,6 +478,19 @@ static NSRecursiveLock *windowsLock;
|
|||
return _fieldEditor;
|
||||
}
|
||||
|
||||
/*
|
||||
* Window controller
|
||||
*/
|
||||
- (void)setWindowController:(NSWindowController *)windowController
|
||||
{
|
||||
ASSIGN(_windowController, windowController);
|
||||
}
|
||||
|
||||
- (id)windowController
|
||||
{
|
||||
return _windowController;
|
||||
}
|
||||
|
||||
/*
|
||||
* Window status and ordering
|
||||
*/
|
||||
|
@ -1942,7 +1957,18 @@ static NSRecursiveLock *windowsLock;
|
|||
- (BOOL) windowShouldClose: (id)sender
|
||||
{
|
||||
if ([delegate respondsToSelector: @selector(windowShouldClose:)])
|
||||
return [delegate windowShouldClose: sender];
|
||||
{
|
||||
BOOL ourReturn;
|
||||
|
||||
ourReturn = [delegate windowShouldClose: sender];
|
||||
|
||||
if (ourReturn)
|
||||
{
|
||||
ourReturn = [[_windowController document] shouldCloseWindowController: _windowController];
|
||||
}
|
||||
|
||||
return ourReturn;
|
||||
}
|
||||
else
|
||||
return YES;
|
||||
}
|
||||
|
|
302
Source/NSWindowController.m
Normal file
302
Source/NSWindowController.m
Normal file
|
@ -0,0 +1,302 @@
|
|||
#import <AppKit/NSWindowController.h>
|
||||
#import <AppKit/NSPanel.h>
|
||||
#import <AppKit/NSNibLoading.h>
|
||||
#import <AppKit/NSDocumentFrameworkPrivate.h>
|
||||
|
||||
@implementation NSWindowController
|
||||
|
||||
- (id)initWithWindowNibName:(NSString *)windowNibName
|
||||
{
|
||||
return [self initWithWindowNibName:windowNibName owner:self];
|
||||
}
|
||||
|
||||
- (id)initWithWindowNibName:(NSString *)windowNibName owner:(id)owner
|
||||
{
|
||||
[self initWithWindow:nil];
|
||||
_windowNibName = [windowNibName retain];
|
||||
_owner = owner;
|
||||
return self;
|
||||
}
|
||||
|
||||
- (id)initWithWindow:(NSWindow *)window
|
||||
{
|
||||
[super init];
|
||||
|
||||
_window = [window retain];
|
||||
_windowFrameAutosaveName = @"";
|
||||
_wcFlags.shouldCascade = YES;
|
||||
_wcFlags.shouldCloseDocument = NO;
|
||||
|
||||
if (_window)
|
||||
{
|
||||
[self _windowDidLoad];
|
||||
}
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
- (id)init
|
||||
{
|
||||
return [self initWithWindowNibName:nil];
|
||||
}
|
||||
|
||||
- (void)dealloc
|
||||
{
|
||||
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
||||
[_windowNibName release];
|
||||
[_windowFrameAutosaveName release];
|
||||
[_topLevelObjects release];
|
||||
[_window autorelease];
|
||||
[super dealloc];
|
||||
}
|
||||
|
||||
- (NSString *)windowNibName
|
||||
{
|
||||
return _windowNibName;
|
||||
}
|
||||
|
||||
- (id)owner
|
||||
{
|
||||
return _owner;
|
||||
}
|
||||
|
||||
- (void)setDocument:(NSDocument *)document
|
||||
{
|
||||
_document = document;
|
||||
[self _synchronizeWindowTitleWithDocumentName];
|
||||
}
|
||||
|
||||
- (id)document
|
||||
{
|
||||
return _document;
|
||||
}
|
||||
|
||||
- (void)setWindowFrameAutosaveName:(NSString *)name
|
||||
{
|
||||
[name retain];
|
||||
[_windowFrameAutosaveName release];
|
||||
_windowFrameAutosaveName = name;
|
||||
|
||||
if ([self isWindowLoaded])
|
||||
{
|
||||
[[self window] setFrameAutosaveName:name? name : @""];
|
||||
}
|
||||
}
|
||||
|
||||
- (NSString *)windowFrameAutosaveName;
|
||||
{
|
||||
return _windowFrameAutosaveName;
|
||||
}
|
||||
|
||||
- (void)setShouldCloseDocument:(BOOL)flag
|
||||
{
|
||||
_wcFlags.shouldCloseDocument = flag;
|
||||
}
|
||||
|
||||
- (BOOL)shouldCloseDocument
|
||||
{
|
||||
return _wcFlags.shouldCloseDocument;
|
||||
}
|
||||
|
||||
- (void)setShouldCascadeWindows:(BOOL)flag
|
||||
{
|
||||
_wcFlags.shouldCascade = flag;
|
||||
}
|
||||
|
||||
- (BOOL)shouldCascadeWindows
|
||||
{
|
||||
return _wcFlags.shouldCascade;
|
||||
}
|
||||
|
||||
- (void)close
|
||||
{
|
||||
[_window close];
|
||||
}
|
||||
|
||||
|
||||
- (void)_windowWillClose:(NSNotification *)notification
|
||||
{
|
||||
if ([notification object] == _window)
|
||||
{
|
||||
if ([_window delegate] == self) [_window setDelegate:nil];
|
||||
if ([_window windowController] == self) [_window setWindowController:nil];
|
||||
|
||||
/*
|
||||
* If the window is set to isReleasedWhenClosed, it will release
|
||||
* itself, so nil out our reference so we don't release it again.
|
||||
* We may want to unilaterally turn off the setting in the NSWindow
|
||||
* instance so it doesn't cause problems.
|
||||
*
|
||||
* Apple's implementation doesn't seem to deal with this case, and
|
||||
* crashes if isReleaseWhenClosed is set.
|
||||
*/
|
||||
if ([_window isReleasedWhenClosed])
|
||||
{
|
||||
_window = nil;
|
||||
}
|
||||
|
||||
[_document _removeWindowController:self];
|
||||
}
|
||||
}
|
||||
|
||||
- (NSWindow *)window
|
||||
{
|
||||
if (_window == nil && ![self isWindowLoaded])
|
||||
{
|
||||
// Do all the notifications. Yes, the docs say this should
|
||||
// be implemented here instead of in -loadWindow itself.
|
||||
[self windowWillLoad];
|
||||
if ([_document respondsToSelector:@selector(windowControllerWillLoadNib:)])
|
||||
[_document windowControllerWillLoadNib:self];
|
||||
|
||||
[self loadWindow];
|
||||
|
||||
[self _windowDidLoad];
|
||||
if ([_document respondsToSelector:@selector(windowControllerDidLoadNib:)])
|
||||
[_document windowControllerDidLoadNib:self];
|
||||
}
|
||||
|
||||
return _window;
|
||||
}
|
||||
|
||||
// Private method; the nib loading will call this.
|
||||
- (void)setWindow:(NSWindow *)aWindow
|
||||
{
|
||||
[_window autorelease];
|
||||
_window = [aWindow retain];
|
||||
}
|
||||
|
||||
- (IBAction)showWindow:(id)sender
|
||||
{
|
||||
NSWindow *window = [self window];
|
||||
|
||||
if ([window isKindOfClass:[NSPanel class]] && [(NSPanel*)window becomesKeyOnlyIfNeeded])
|
||||
[window orderFront:sender];
|
||||
else
|
||||
[window makeKeyAndOrderFront:sender];
|
||||
}
|
||||
|
||||
- (NSString *)windowTitleForDocumentDisplayName:(NSString *)displayName
|
||||
{
|
||||
return displayName;
|
||||
}
|
||||
|
||||
- (void)_synchronizeWindowTitleWithDocumentName
|
||||
{
|
||||
if (_document)
|
||||
{
|
||||
NSString *filename = [_document fileName];
|
||||
NSString *displayName = [_document displayName];
|
||||
NSString *title = [self windowTitleForDocumentDisplayName:displayName];
|
||||
|
||||
/* If they just want to display the filename, use the fancy method */
|
||||
if (filename != nil && [title isEqualToString:filename])
|
||||
{
|
||||
[_window setTitleWithRepresentedFilename:filename];
|
||||
}
|
||||
else
|
||||
{
|
||||
if (filename) [_window setRepresentedFilename:filename];
|
||||
[_window setTitle:title];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
- (BOOL)isWindowLoaded
|
||||
{
|
||||
return _wcFlags.nibIsLoaded;
|
||||
}
|
||||
|
||||
- (void)windowDidLoad
|
||||
{
|
||||
}
|
||||
|
||||
- (void)windowWillLoad
|
||||
{
|
||||
}
|
||||
|
||||
- (void)_windowDidLoad
|
||||
{
|
||||
_wcFlags.nibIsLoaded = YES;
|
||||
|
||||
[_window setWindowController:self];
|
||||
|
||||
[self _synchronizeWindowTitleWithDocumentName];
|
||||
|
||||
[[NSNotificationCenter defaultCenter]
|
||||
addObserver:self
|
||||
selector:@selector(_windowWillClose:)
|
||||
name:NSWindowWillCloseNotification
|
||||
object:_window];
|
||||
|
||||
/* Make sure window sizes itself right */
|
||||
if ([_windowFrameAutosaveName length] > 0)
|
||||
{
|
||||
[_window setFrameUsingName:_windowFrameAutosaveName];
|
||||
[_window setFrameAutosaveName:_windowFrameAutosaveName];
|
||||
}
|
||||
|
||||
if ([self shouldCascadeWindows])
|
||||
{
|
||||
static NSPoint nextWindowLocation = { 0.0, 0.0 };
|
||||
static BOOL firstWindow = YES;
|
||||
|
||||
if (firstWindow)
|
||||
{
|
||||
NSRect windowFrame = [_window frame];
|
||||
|
||||
/* Start with the frame as set */
|
||||
nextWindowLocation = NSMakePoint(NSMinX(windowFrame), NSMaxY(windowFrame));
|
||||
firstWindow = NO;
|
||||
}
|
||||
else
|
||||
{
|
||||
/*
|
||||
* cascadeTopLeftFromPoint will "wrap" the point back to the
|
||||
* top left if the normal cascading will cause the window to go
|
||||
* off the screen. In Apple's implementation, this wraps to the
|
||||
* extreme top of the screen, and offset only a small amount
|
||||
* from the left.
|
||||
*/
|
||||
nextWindowLocation = [_window cascadeTopLeftFromPoint:nextWindowLocation];
|
||||
}
|
||||
}
|
||||
|
||||
[self windowDidLoad];
|
||||
}
|
||||
|
||||
- (void)loadWindow
|
||||
{
|
||||
if ([self isWindowLoaded]) return;
|
||||
|
||||
if ([NSBundle loadNibNamed:_windowNibName owner:_owner])
|
||||
{
|
||||
_wcFlags.nibIsLoaded = YES;
|
||||
|
||||
if (_window == nil && _document && _owner == _document)
|
||||
[self setWindow:[_document _transferWindowOwnership]];
|
||||
}
|
||||
else
|
||||
{
|
||||
NSLog(@"%@: could not load nib named %@.nib", [self class], _windowNibName);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* There's no way I'll ever get these compatible if Apple's versions
|
||||
* actually encode anything, sigh
|
||||
*/
|
||||
- initWithCoder:(NSCoder *)coder
|
||||
{
|
||||
return [self init];
|
||||
}
|
||||
|
||||
- (void)encodeWithCoder:(NSCoder *)coder
|
||||
{
|
||||
// What are we supposed to encode? Window nib name? Or should these
|
||||
// be empty, just to conform to NSCoding, so we do an -init on
|
||||
// unarchival. ?
|
||||
}
|
||||
|
||||
@end
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue