Changes to allow proper document type selection in the save panel for document oriented applications.

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gui/trunk@23013 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
gcasa 2006-06-02 00:31:14 +00:00
parent 85ece0acc5
commit 242e1f7ee9
5 changed files with 136 additions and 49 deletions

View file

@ -1,3 +1,18 @@
2006-06-01 00:13 Gregory John Casamento <greg_casamento@yahoo.com>
* Headers/AppKit/NSDocumentFrameworkPrivate.h: Addition of new
private methods to manage file types.
* Headers/AppKit/NSDocument.h: New ivar _saveType.
* Source/NSDocumentController.m: Implementation of new private
methods.
* Source/NSDocument.m: Changed writeWithBackupToFile:ofType:
saveOperation: to use new methods to get the human readable
filetypes to be listed in the dropdown in the savepanel and to
properly retrieve the extension for the specified filetype
and apply it to the backup filename and filename being saved.
Also modified changeSaveType: to update the save panel's
requiredFileType when the type is changed.
2006-05-21 Richard Frith-Macdonald <rfm@gnu.org> 2006-05-21 Richard Frith-Macdonald <rfm@gnu.org>
* Source/NSApplication.m: Implement MacOS-X behavior (as determined * Source/NSApplication.m: Implement MacOS-X behavior (as determined

View file

@ -80,6 +80,7 @@ typedef enum _NSSaveOperationType {
NSPopUpButton *spaButton; // outlet for "the File Format:" button in the save panel. NSPopUpButton *spaButton; // outlet for "the File Format:" button in the save panel.
int _documentIndex; // Untitled index int _documentIndex; // Untitled index
NSUndoManager *_undoManager; // Undo manager for this document NSUndoManager *_undoManager; // Undo manager for this document
NSString *_saveType; // the currently selected extension.
struct __docFlags { struct __docFlags {
unsigned int inClose:1; unsigned int inClose:1;
unsigned int hasUndoManager:1; unsigned int hasUndoManager:1;

View file

@ -34,6 +34,9 @@
- (NSArray *)_editorAndViewerTypesForClass:(Class)documentClass; - (NSArray *)_editorAndViewerTypesForClass:(Class)documentClass;
- (NSArray *)_editorTypesForClass:(Class)fp12; - (NSArray *)_editorTypesForClass:(Class)fp12;
- (NSArray *)_exportableTypesForClass:(Class)documentClass; - (NSArray *)_exportableTypesForClass:(Class)documentClass;
- (NSString *)_nameForHumanReadableType: (NSString *)type;
- (NSArray *)_displayNamesForTypes: (NSArray *)types;
- (NSArray *)_displayNamesForClass: (Class)documentClass;
@end @end

View file

@ -78,6 +78,7 @@
if ([fileTypes count]) if ([fileTypes count])
{ {
[self setFileType: [fileTypes objectAtIndex: 0]]; [self setFileType: [fileTypes objectAtIndex: 0]];
ASSIGN(_saveType, [fileTypes objectAtIndex: 0]);
} }
} }
return self; return self;
@ -146,6 +147,7 @@
RELEASE(_printInfo); RELEASE(_printInfo);
RELEASE(savePanelAccessory); RELEASE(savePanelAccessory);
RELEASE(spaButton); RELEASE(spaButton);
RELEASE(_saveType);
[super dealloc]; [super dealloc];
} }
@ -457,7 +459,17 @@
- (IBAction)changeSaveType: (id)sender - (IBAction)changeSaveType: (id)sender
{ {
[self setFileType: [sender titleOfSelectedItem]]; NSDocumentController *controller =
[NSDocumentController sharedDocumentController];
NSArray *extensions = nil;
ASSIGN(_saveType, [controller _nameForHumanReadableType:
[sender titleOfSelectedItem]]);
extensions = [controller fileExtensionsFromType: _saveType];
if([extensions count] > 0)
{
[(NSSavePanel *)[sender window] setRequiredFileType: [extensions objectAtIndex:0]];
}
} }
- (int)runModalSavePanel: (NSSavePanel *)savePanel - (int)runModalSavePanel: (NSSavePanel *)savePanel
@ -509,7 +521,9 @@
// if we have some items, select the current filetype. // if we have some items, select the current filetype.
if(i > 0) if(i > 0)
{ {
[spaButton selectItemWithTitle: [self fileType]]; NSString *title = [[NSDocumentController sharedDocumentController]
displayNameForType: [self fileType]];
[spaButton selectItemWithTitle: title];
} }
} }
@ -518,25 +532,32 @@
NSView *accessory = nil; NSView *accessory = nil;
NSString *title; NSString *title;
NSString *directory; NSString *directory;
NSArray *extensions; NSArray *displayNames;
NSDocumentController *controller; NSDocumentController *controller;
NSSavePanel *savePanel = [NSSavePanel savePanel]; NSSavePanel *savePanel = [NSSavePanel savePanel];
controller = [NSDocumentController sharedDocumentController]; controller = [NSDocumentController sharedDocumentController];
extensions = [controller fileExtensionsFromType:[self fileType]]; displayNames = [controller _displayNamesForClass: [self class]];
if ([self shouldRunSavePanelWithAccessoryView]) if ([self shouldRunSavePanelWithAccessoryView])
{ {
if (savePanelAccessory == nil) if (savePanelAccessory == nil)
[self _createPanelAccessory]; [self _createPanelAccessory];
[self _addItemsToSpaButtonFromArray: extensions]; [self _addItemsToSpaButtonFromArray: displayNames];
accessory = savePanelAccessory; accessory = savePanelAccessory;
} }
if ([extensions count] > 0) if ([displayNames count] > 0)
[savePanel setRequiredFileType:[extensions objectAtIndex:0]]; {
NSArray *extensions = [[NSDocumentController sharedDocumentController]
fileExtensionsFromType: [self fileTypeFromLastRunSavePanel]];
if([extensions count] > 0)
{
[savePanel setRequiredFileType:[extensions objectAtIndex:0]];
}
}
switch (saveOperation) switch (saveOperation)
{ {
@ -678,8 +699,7 @@
- (NSString *)fileTypeFromLastRunSavePanel - (NSString *)fileTypeFromLastRunSavePanel
{ {
// return [self fileType]; return _saveType;
return [spaButton title];
} }
- (NSDictionary *)fileAttributesToWriteToFile: (NSString *)fullDocumentPath - (NSDictionary *)fileAttributesToWriteToFile: (NSString *)fullDocumentPath
@ -707,51 +727,59 @@
if (fileName) if (fileName)
{ {
if ([fileManager fileExistsAtPath: fileName]) NSArray *extensions = [[NSDocumentController sharedDocumentController]
fileExtensionsFromType: fileType];
if([extensions count] > 0)
{ {
NSString *extension = [fileName pathExtension]; NSString *extension = [extensions objectAtIndex: 0];
NSString *newFileName = [[fileName stringByDeletingPathExtension]
stringByAppendingPathExtension: extension];
backupFilename = [fileName stringByDeletingPathExtension]; if ([fileManager fileExistsAtPath: newFileName])
backupFilename = [backupFilename stringByAppendingString:@"~"]; {
backupFilename = [backupFilename stringByAppendingPathExtension: extension]; backupFilename = [newFileName stringByDeletingPathExtension];
backupFilename = [backupFilename stringByAppendingString:@"~"];
/* Save panel has already asked if the user wants to replace it */ backupFilename = [backupFilename stringByAppendingPathExtension: extension];
/* 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])
{
int result = NSRunAlertPanel(_(@"File Error"),
_(@"Can't create backup file. Save anyways?"),
_(@"Save"), _(@"Cancel"), nil);
if (result != NSAlertDefaultReturn) return NO; /* Save panel has already asked if the user wants to replace it */
}
} /* NSFileManager movePath: will fail if destination exists */
if ([self writeToFile: fileName if ([fileManager fileExistsAtPath: backupFilename])
ofType: fileType [fileManager removeFileAtPath: backupFilename handler: nil];
originalFile: backupFilename
saveOperation: saveOp]) // Move or copy?
{ if (![fileManager movePath: newFileName toPath: backupFilename handler: nil] &&
if (saveOp != NSSaveToOperation) [self keepBackupFile])
{ {
[self setFileName: fileName]; int result = NSRunAlertPanel(_(@"File Error"),
[self setFileType: fileType]; _(@"Can't create backup file. Save anyways?"),
[self updateChangeCount: NSChangeCleared]; _(@"Save"), _(@"Cancel"), nil);
if (result != NSAlertDefaultReturn) return NO;
}
} }
if ([self writeToFile: fileName
// FIXME: Should set the file attributes ofType: fileType
originalFile: backupFilename
if (backupFilename && ![self keepBackupFile]) saveOperation: saveOp])
{ {
[fileManager removeFileAtPath: backupFilename handler: nil]; if (saveOp != NSSaveToOperation)
{
[self setFileName: newFileName];
[self setFileType: fileType];
[self updateChangeCount: NSChangeCleared];
}
// FIXME: Should set the file attributes
if (backupFilename && ![self keepBackupFile])
{
[fileManager removeFileAtPath: backupFilename handler: nil];
}
return YES;
} }
return YES;
} }
} }

View file

@ -53,6 +53,7 @@ static NSString *NSDefaultOpenDirectory = @"NSDefaultOpenDirectory";
static NSDocumentController *sharedController = nil; static NSDocumentController *sharedController = nil;
#define TYPE_INFO(name) TypeInfoForName(_types, name) #define TYPE_INFO(name) TypeInfoForName(_types, name)
#define HR_TYPE_INFO(name) TypeInfoForHumanReadableName(_types, name)
static NSDictionary *TypeInfoForName (NSArray *types, NSString *typeName) static NSDictionary *TypeInfoForName (NSArray *types, NSString *typeName)
{ {
@ -70,6 +71,22 @@ static NSDictionary *TypeInfoForName (NSArray *types, NSString *typeName)
return nil; return nil;
} }
static NSDictionary *TypeInfoForHumanReadableName (NSArray *types, NSString *typeName)
{
int i, count = [types count];
for (i = 0; i < count; i++)
{
NSDictionary *dict = [types objectAtIndex: i];
if ([[dict objectForKey: NSHumanReadableNameKey] isEqualToString: typeName])
{
return dict;
}
}
return nil;
}
/** <p> /** <p>
NSDocumentController is a class that controls a set of NSDocuments NSDocumentController is a class that controls a set of NSDocuments
for an application. As an application delegate, it responds to the for an application. As an application delegate, it responds to the
@ -488,7 +505,7 @@ static NSDictionary *TypeInfoForName (NSArray *types, NSString *typeName)
- (BOOL) reviewUnsavedDocumentsWithAlertTitle: (NSString *)title - (BOOL) reviewUnsavedDocumentsWithAlertTitle: (NSString *)title
cancellable: (BOOL)cancellable cancellable: (BOOL)cancellable
{ {
NSString *cancelString = (cancellable)? _(@"Cancel") : nil; NSString *cancelString = (cancellable)? ((NSString *)_(@"Cancel")) : ((NSString *)nil);
int result; int result;
/* Probably as good a place as any to do this */ /* Probably as good a place as any to do this */
@ -846,5 +863,28 @@ static NSString *NSViewerRole = @"Viewer";
return [self _editorTypesForClass: documentClass]; return [self _editorTypesForClass: documentClass];
} }
- (NSString *) _nameForHumanReadableType: (NSString *)type
{
return [HR_TYPE_INFO(type) objectForKey: NSNameKey];
}
- (NSArray *) _displayNamesForTypes: (NSArray *)types
{
NSEnumerator *en = [types objectEnumerator];
NSString *type = nil;
NSMutableArray *result = [NSMutableArray arrayWithCapacity: 10];
while((type = (NSString *)[en nextObject]) != nil)
{
NSString *name = [self displayNameForType: type];
[result addObject: name];
}
return result;
}
- (NSArray *) _displayNamesForClass: (Class)documentClass
{
return [self _displayNamesForTypes:
[self _editorTypesForClass: documentClass]];
}
@end @end