Use new NSTextAttachment class

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gui/trunk@6806 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
FredKiefer 2000-06-26 19:54:09 +00:00
parent 3ccb4049bd
commit c805ceef35

View file

@ -84,33 +84,39 @@ wordCSet()
return cset; return cset;
} }
@interface NSAttributedString(AttributedStringRTFDAdditions) /*
* Returns a String containing the attachment character
*/
static NSString *attachmentString()
{
static NSString *attach = nil;
if (attach == nil)
{
unichar ch = NSAttachmentCharacter;
attach = [NSString stringWithCharacters: &ch
length: 1];
}
return attach;
}
- (NSString*) RTFHeaderStringWithContext: (NSMutableDictionary*) contextDict;
- (NSString*) RTFTrailerStringWithContext: (NSMutableDictionary*) contextDict;
- (NSString*) RTFBodyStringWithContext: (NSMutableDictionary*) contextDict;
- (NSString*) RTFDStringFromRange: (NSRange)range
documentAttributes: (NSDictionary*)dict;
@end
@implementation NSAttributedString (AppKit) @implementation NSAttributedString (AppKit)
+ (NSAttributedString *)attributedStringWithAttachment:(NSTextAttachment *)attachment + (NSAttributedString *)attributedStringWithAttachment:(NSTextAttachment *)attachment
{ {
unichar ch = NSAttachmentCharacter;
NSString *string = [NSString stringWithCharacters: &ch
length: 1];
NSDictionary *attributes = [NSDictionary dictionaryWithObject: attachment NSDictionary *attributes = [NSDictionary dictionaryWithObject: attachment
forKey: NSAttachmentAttributeName]; forKey: NSAttachmentAttributeName];
return [[self alloc] initWithString: string return AUTORELEASE([[self alloc] initWithString: attachmentString()
attributes: attributes]; attributes: attributes]);
} }
- (BOOL) containsAttachments - (BOOL) containsAttachments
{ {
// FIXME: Currently there are no attachment in GNUstep. NSRange aRange = [[self string] rangeOfString: attachmentString()];
return NO;
return aRange.length;
} }
- (NSDictionary*) fontAttributesInRange: (NSRange)range - (NSDictionary*) fontAttributesInRange: (NSRange)range
@ -427,16 +433,17 @@ documentAttributes: (NSDictionary**)dict
NSMutableDictionary *fileDict = [NSMutableDictionary dictionary]; NSMutableDictionary *fileDict = [NSMutableDictionary dictionary];
NSFileWrapper *txt = [[NSFileWrapper alloc] NSFileWrapper *txt = [[NSFileWrapper alloc]
initRegularFileWithContents: initRegularFileWithContents:
[self RTFDFromRange: range [self RTFFromRange: range
documentAttributes: dict]]; documentAttributes: dict]];
// FIXME: We have to add the attachements to the directory file wrapper
[fileDict setObject: txt forKey: @"TXT.rtf"]; [fileDict setObject: txt forKey: @"TXT.rtf"];
// FIXME: We have to add the attachments to the directory file wrapper
return [[NSFileWrapper alloc] initDirectoryWithFileWrappers: fileDict]; return [[NSFileWrapper alloc] initDirectoryWithFileWrappers: fileDict];
} }
else else
return [[NSFileWrapper alloc] initRegularFileWithContents: return [[NSFileWrapper alloc] initRegularFileWithContents:
[self RTFDFromRange: range [self RTFFromRange: range
documentAttributes: dict]]; documentAttributes: dict]];
} }
@end @end
@ -658,51 +665,99 @@ documentAttributes: (NSDictionary**)dict
} }
} }
- (void) fixAttachmentAttributeInRange: (NSRange)range - (void) fixAttachmentAttributeInRange: (NSRange)aRange
{ {
unsigned location = range.location; NSString *string = [self string];
unsigned end = NSMaxRange(range); unsigned location = aRange.location;
unsigned end = NSMaxRange(aRange);
if (NSMaxRange(range) > [self length]) if (end > [self length])
{ {
[NSException raise: NSRangeException [NSException raise: NSRangeException
format: @"RangeError in method -fixAttachmentAttributeInRange:"]; format: @"RangeError in method -fixAttachmentAttributeInRange:"];
} }
// Check for attachments with the wrong character
while (location < end) while (location < end)
{ {
NSDictionary *attr; NSDictionary *attr;
NSRange range;
attr = [self attributesAtIndex: location effectiveRange: &range]; attr = [self attributesAtIndex: location effectiveRange: &range];
if ([attr objectForKey: NSAttachmentAttributeName] != nil) if ([attr objectForKey: NSAttachmentAttributeName] != nil)
{ {
unichar buf[range.length]; unichar buf[range.length];
unsigned pos = 0; unsigned pos = 0;
unsigned start = range.location;
[[self string] getCharacters: buf range: range]; // Leave only one character with the attachment
while (pos < range.length) [string getCharacters: buf range: range];
{ while (pos < range.length && buf[pos] != NSAttachmentCharacter)
unsigned start; pos++;
unsigned end; if (pos)
[self removeAttribute: NSAttachmentAttributeName
while (pos < range.length && buf[pos] == NSAttachmentCharacter) range: NSMakeRange(start, pos)];
pos++; pos++;
start = pos; if (pos < range.length)
while (pos < range.length && buf[pos] == NSAttachmentCharacter) [self removeAttribute: NSAttachmentAttributeName
pos++; range: NSMakeRange(start+pos, range.length - pos)];
end = pos;
if (start != end)
[self removeAttribute: NSAttachmentAttributeName
range: NSMakeRange(start, end - start)];
}
} }
location = NSMaxRange(range); location = NSMaxRange(range);
} }
// Check for attachment characters without attachments
location = aRange.location;
while (location < end)
{
NSRange range = [string rangeOfString: attachmentString()
options: NSLiteralSearch
range: NSMakeRange(location, end - location)];
NSTextAttachment *attachment;
if (!range.length)
break;
attachment = [self attribute: NSAttachmentAttributeName
atIndex: range.location
effectiveRange: NULL];
if (attachment == nil)
{
[self deleteCharactersInRange: NSMakeRange(range.location, 1)];
range.length--;
}
location = NSMaxRange(range);
}
} }
- (void)updateAttachmentsFromPath:(NSString *)path - (void)updateAttachmentsFromPath:(NSString *)path
{ {
// FIXME: Still missing NSString *string = [self string];
unsigned location = 0;
unsigned end = [string length];
while (location < end)
{
NSRange range = [string rangeOfString: attachmentString()
options: NSLiteralSearch
range: NSMakeRange(location, end - location)];
NSTextAttachment *attachment;
NSFileWrapper *fileWrapper;
if (!range.length)
break;
attachment = [self attribute: NSAttachmentAttributeName
atIndex: range.location
effectiveRange: NULL];
fileWrapper = [attachment fileWrapper];
// FIXME: Is this the correct thing to do?
[fileWrapper updateFromPath: [path stringByAppendingPathComponent:
[fileWrapper filename]]];
location = NSMaxRange(range);
}
} }
@end @end