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:
Wolfgang Lux 2009-12-15 22:52:40 +00:00
parent c99387f5f5
commit 103671a71e
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>
* Source/NSOutlineView.m: removed c99-isms

View file

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