Avoid a bogus warning that was shown when a Save panel requires a file

type extension and the user has entered a file name without an
extension. In addition fix the filename method so that setting
allowsOtherFileTypes to YES works as advertised.


git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gui/trunk@29126 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
wlux 2009-12-15 22:52:40 +00:00
parent 39a91b9021
commit e93f94e5ab
2 changed files with 83 additions and 52 deletions

View file

@ -1,3 +1,18 @@
2009-12-15 Wolfgang Lux <wolfgang.lux@gmail.com>
* Source/NSSavePanel.m (-ok:): Avoid the bogus warning that was
shown when the panel requires a file type extension and the user
has entered a file name without an extension.
* Source/NSSavePanel.m (-filename): Do not replace an existing
file type extension if allowsOtherFileTypes is set.
* Source/NSSavePanel.m (-runModalForDirectory:file,
-runModalForDirectory:file:relativeToWindow:,
-beginSheetForDirectory:file:modalForWindow:modalDelegate:...):
Initially enable the panel's OK button if a file name is provided
to the modal session.
2009-12-15 Riccardo Mottola <rmottola@users.sf.net> 2009-12-15 Riccardo Mottola <rmottola@users.sf.net>
* Source/NSOutlineView.m: removed c99-isms * Source/NSOutlineView.m: removed c99-isms

View file

@ -1082,6 +1082,8 @@ selectCellWithString: (NSString*)title
- (int) runModalForDirectory: (NSString*)path file: (NSString*)filename - (int) runModalForDirectory: (NSString*)path file: (NSString*)filename
{ {
[self _setupForDirectory: path file: filename]; [self _setupForDirectory: path file: filename];
if ([filename length] > 0)
[_okButton setEnabled: YES];
return [NSApp runModalForWindow: self]; return [NSApp runModalForWindow: self];
} }
@ -1090,6 +1092,8 @@ selectCellWithString: (NSString*)title
relativeToWindow: (NSWindow*)window relativeToWindow: (NSWindow*)window
{ {
[self _setupForDirectory: path file: filename]; [self _setupForDirectory: path file: filename];
if ([filename length] > 0)
[_okButton setEnabled: YES];
return [NSApp runModalForWindow: self return [NSApp runModalForWindow: self
relativeToWindow: window]; relativeToWindow: window];
} }
@ -1102,6 +1106,8 @@ selectCellWithString: (NSString*)title
contextInfo: (void *)contextInfo contextInfo: (void *)contextInfo
{ {
[self _setupForDirectory: path file: filename]; [self _setupForDirectory: path file: filename];
if ([filename length] > 0)
[_okButton setEnabled: YES];
[NSApp beginSheet: self [NSApp beginSheet: self
modalForWindow: docWindow modalForWindow: docWindow
modalDelegate: delegate modalDelegate: delegate
@ -1136,18 +1142,21 @@ selectCellWithString: (NSString*)title
[_allowedFileTypes indexOfObject: @""] != NSNotFound) [_allowedFileTypes indexOfObject: @""] != NSNotFound)
return _fullFileName; return _fullFileName;
/* add filetype extension only if the filename does not include an /* add file type extension if the file name does not have an extension or
allowed one already */ the file name's extension is not one of the allowed extensions and the
save panel does not allow other extensions */
fileType = [_fullFileName pathExtension]; fileType = [_fullFileName pathExtension];
if ([_allowedFileTypes indexOfObject: fileType] != NSNotFound) if ([fileType length] == 0 ||
{ ((!_allowsOtherFileTypes &&
return _fullFileName; [_allowedFileTypes indexOfObject: fileType] == NSNotFound)))
}
else
{ {
fileType = [_allowedFileTypes objectAtIndex: 0]; fileType = [_allowedFileTypes objectAtIndex: 0];
return [_fullFileName stringByAppendingPathExtension: fileType]; return [_fullFileName stringByAppendingPathExtension: fileType];
} }
else
{
return _fullFileName;
}
} }
- (NSURL *) URL - (NSURL *) URL
@ -1175,7 +1184,7 @@ selectCellWithString: (NSString*)title
{ {
NSMatrix *matrix; NSMatrix *matrix;
NSBrowserCell *selectedCell; NSBrowserCell *selectedCell;
NSString *filename, *ext, *req; NSString *filename;
BOOL isDir = NO; BOOL isDir = NO;
matrix = [_browser matrixInColumn: [_browser lastColumn]]; matrix = [_browser matrixInColumn: [_browser lastColumn]];
@ -1216,21 +1225,25 @@ selectCellWithString: (NSString*)title
} }
/* Warn user if a wrong extension was entered */ /* Warn user if a wrong extension was entered */
req = [self requiredFileType]; if (_allowedFileTypes != nil &&
ext = [_fullFileName pathExtension]; [_allowedFileTypes indexOfObject: @""] == NSNotFound)
if ([req length] > 0 && ![ext isEqualToString: req]) {
NSString *fileType = [_fullFileName pathExtension];
if ([fileType length] != 0 &&
[_allowedFileTypes indexOfObject: fileType] == NSNotFound)
{ {
int result; int result;
NSString *msgFormat, *butFormat; NSString *msgFormat, *butFormat;
NSString *altExt; NSString *altType, *requiredType;
requiredType = [self requiredFileType];
if ([self allowsOtherFileTypes]) if ([self allowsOtherFileTypes])
{ {
msgFormat = msgFormat =
_(@"You have used the extension '.%@'.\n" _(@"You have used the extension '.%@'.\n"
@"The standard extension is '.%@'.'"); @"The standard extension is '.%@'.'");
butFormat = _(@"Use .%@"); butFormat = _(@"Use .%@");
altExt = ext; altType = fileType;
} }
else else
{ {
@ -1238,29 +1251,31 @@ selectCellWithString: (NSString*)title
_(@"You cannot save this document with extension '.%@'.\n" _(@"You cannot save this document with extension '.%@'.\n"
@"The required extension is '.%@'."); @"The required extension is '.%@'.");
butFormat = _(@"Use .%@"); butFormat = _(@"Use .%@");
altExt = [ext stringByAppendingPathExtension: req]; altType = [fileType stringByAppendingPathExtension: requiredType];
} }
result = NSRunAlertPanel(_(@"Save"), result = NSRunAlertPanel(_(@"Save"),
msgFormat, msgFormat,
[NSString stringWithFormat: butFormat, req], [NSString stringWithFormat: butFormat, requiredType],
_(@"Cancel"), _(@"Cancel"),
[NSString stringWithFormat: butFormat, altExt], [NSString stringWithFormat: butFormat, altType],
ext, req); fileType, requiredType);
switch (result) switch (result)
{ {
case NSAlertDefaultReturn: case NSAlertDefaultReturn:
filename = [_fullFileName stringByDeletingPathExtension]; filename = [_fullFileName stringByDeletingPathExtension];
filename = [filename stringByAppendingPathExtension: req]; filename =
[filename stringByAppendingPathExtension: requiredType];
ASSIGN (_fullFileName, filename); ASSIGN (_fullFileName, filename);
setPath(_browser, _fullFileName); setPath(_browser, _fullFileName);
[self _setFileName: [_fullFileName lastPathComponent]]; [self _setFileName: [_fullFileName lastPathComponent]];
break; break;
case NSAlertOtherReturn: case NSAlertOtherReturn:
if (altExt != ext) if (altType != fileType)
{ {
filename = [_fullFileName stringByAppendingPathExtension: req]; filename =
[_fullFileName stringByAppendingPathExtension: requiredType];
ASSIGN (_fullFileName, filename); ASSIGN (_fullFileName, filename);
setPath(_browser, _fullFileName); setPath(_browser, _fullFileName);
@ -1271,6 +1286,7 @@ selectCellWithString: (NSString*)title
return; return;
} }
} }
}
filename = [_fullFileName stringByDeletingLastPathComponent]; filename = [_fullFileName stringByDeletingLastPathComponent];
if ([_fm fileExistsAtPath: filename isDirectory: &isDir] == NO) if ([_fm fileExistsAtPath: filename isDirectory: &isDir] == NO)