NSText can save rtf

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gui/trunk@6407 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
richard 2000-04-02 05:58:50 +00:00
parent 84606cb733
commit f6bf8a8acd
3 changed files with 170 additions and 114 deletions

View file

@ -1,3 +1,9 @@
2000-04-01 18:00 Fred Kiefer <FredKiefer@gmx.de>
* Source/NSText.m: Now really uses a text storage instead of an
attributed string (old patch missed this)
* Source/NSAttributedString: Use NSFileWrapper to write RTF
2000-03-31 Adam Fedor <fedor@gnu.org> 2000-03-31 Adam Fedor <fedor@gnu.org>
* Headers/gnustep/gui/NSFont.h: Conform to NSCopying. * Headers/gnustep/gui/NSFont.h: Conform to NSCopying.

View file

@ -75,6 +75,15 @@ paraBreakCSet()
return cset; return cset;
} }
@interface NSAttributedString(AttributedStringRTFDAdditions)
- (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)
- (BOOL) containsAttachments - (BOOL) containsAttachments
@ -282,7 +291,9 @@ paraBreakCSet()
- (id) initWithRTFD: (NSData*)data - (id) initWithRTFD: (NSData*)data
documentAttributes: (NSDictionary**)dict documentAttributes: (NSDictionary**)dict
{ {
return self; // FIXME: We use RTF, as there are currently no additional images
return [self initWithRTF: data
documentAttributes: dict];
} }
- (id) initWithPath: (NSString*)path - (id) initWithPath: (NSString*)path
@ -319,19 +330,24 @@ documentAttributes: (NSDictionary**)dict
- (NSData*) RTFFromRange: (NSRange)range - (NSData*) RTFFromRange: (NSRange)range
documentAttributes: (NSDictionary*)dict documentAttributes: (NSDictionary*)dict
{ {
return (NSData *)self; // FIXME: We use RTFD, as there are currently no additional images
return [self RTFDFromRange: range
documentAttributes: dict];
} }
- (NSData*) RTFDFromRange: (NSRange)range - (NSData*) RTFDFromRange: (NSRange)range
documentAttributes: (NSDictionary*)dict documentAttributes: (NSDictionary*)dict
{ {
return (NSData *)self; return [[self RTFDStringFromRange: range documentAttributes: dict]
dataUsingEncoding: NSNEXTSTEPStringEncoding];
} }
- (NSFileWrapper*) RTFDFileWrapperFromRange: (NSRange)range - (NSFileWrapper*) RTFDFileWrapperFromRange: (NSRange)range
documentAttributes: (NSDictionary*)dict documentAttributes: (NSDictionary*)dict
{ {
return (NSFileWrapper *)self; return [[NSFileWrapper alloc] initRegularFileWithContents:
[self RTFDFromRange: range
documentAttributes: dict]];
} }
@end @end
@ -847,11 +863,4 @@ documentAttributes: (NSDictionary**)dict
[output appendString: trailerString]; [output appendString: trailerString];
return (NSString*)output; return (NSString*)output;
} }
- (NSData*) RTFDFromRange: (NSRange)range
documentAttributes: (NSDictionary*)dict
{
return [[self RTFDStringFromRange: range documentAttributes: dict]
dataUsingEncoding: NSNEXTSTEPStringEncoding];
}
@end @end

View file

@ -261,8 +261,6 @@ static NSRange MakeRangeFromAbs (int a1,int a2) // not the same as NSMakeRange!
*/ */
- (unsigned) characterIndexForPoint: (NSPoint)point; - (unsigned) characterIndexForPoint: (NSPoint)point;
- (NSRect) rectForCharacterIndex: (unsigned)index; - (NSRect) rectForCharacterIndex: (unsigned)index;
- (void) _editedRange: (NSRange)aRange
withDelta: (int)delta;
- (void) _buildUpLayout; - (void) _buildUpLayout;
- (void) drawRect: (NSRect)rect - (void) drawRect: (NSRect)rect
withSelection: (NSRange)range; withSelection: (NSRange)range;
@ -300,16 +298,14 @@ static NSRange MakeRangeFromAbs (int a1,int a2) // not the same as NSMakeRange!
// contains private _GNULineLayoutInfo objects // contains private _GNULineLayoutInfo objects
NSMutableArray *lineLayoutInformation; NSMutableArray *lineLayoutInformation;
NSText *_textHolder; NSText *_textHolder;
NSAttributedString *_textStorage; NSTextStorage *_textStorage;
} }
- (id) initForText: (NSText*) aTextHolder - (id) initForText: (NSText*) aTextHolder;
withAttributedString: (NSAttributedString*) aString; - (void) setTextStorage: (NSTextStorage*) aTextStorage;
- (void) setAttributedString: (NSAttributedString*) aString;
- (NSSize) _sizeOfRange: (NSRange) range; - (NSSize) _sizeOfRange: (NSRange) range;
- (NSRect) _textBounds; - (NSRect) _textBounds;
- (unsigned) characterIndexForPoint: (NSPoint)point; - (unsigned) characterIndexForPoint: (NSPoint)point;
- (NSRect) rectForCharacterIndex: (unsigned) index; - (NSRect) rectForCharacterIndex: (unsigned) index;
- (NSRange) characterRangeForBoundingRect: (NSRect)bounds; - (NSRange) characterRangeForBoundingRect: (NSRect)bounds;
@ -322,8 +318,11 @@ withAttributedString: (NSAttributedString*) aString;
- (NSRange) characterRangeForLineLayoutRange: (NSRange) aRange; - (NSRange) characterRangeForLineLayoutRange: (NSRange) aRange;
- (void) setNeedsDisplayForLineRange: (NSRange) redrawLineRange; - (void) setNeedsDisplayForLineRange: (NSRange) redrawLineRange;
- (void) _editedRange: (NSRange) aRange - (void)textStorage:(NSTextStorage *)aTextStorage
withDelta: (int) delta; edited:(unsigned int)mask
range:(NSRange)range
changeInLength:(int)lengthChange
invalidatedRange:(NSRange)invalidatedCharRange;
- (int) rebuildLineLayoutInformation; - (int) rebuildLineLayoutInformation;
// override for special layout of text // override for special layout of text
- (int) rebuildLineLayoutInformationStartingAtLine: (int)aLine - (int) rebuildLineLayoutInformationStartingAtLine: (int)aLine
@ -336,16 +335,14 @@ withAttributedString: (NSAttributedString*) aString;
@implementation GSSimpleLayoutManager @implementation GSSimpleLayoutManager
- (id) initForText: (NSText*)aTextHolder - (id) initForText: (NSText*)aTextHolder
withAttributedString: (NSAttributedString*)aString
{ {
_textHolder = aTextHolder; _textHolder = aTextHolder;
[self setAttributedString: aString];
return self; return self;
} }
- (void) setAttributedString: (NSAttributedString*)aString - (void) setTextStorage: (NSTextStorage*)aTextStorage
{ {
ASSIGN(_textStorage, aString); ASSIGN(_textStorage, aTextStorage);
[self rebuildLineLayoutInformation]; [self rebuildLineLayoutInformation];
} }
@ -619,8 +616,11 @@ withAttributedString: (NSAttributedString*) aString;
} }
} }
- (void) _editedRange: (NSRange)aRange - (void)textStorage:(NSTextStorage *)aTextStorage
withDelta: (int)delta edited:(unsigned int)mask
range:(NSRange)aRange
changeInLength:(int)delta
invalidatedRange:(NSRange)invalidatedCharRange;
{ {
int start = [self lineLayoutIndexForCharacterIndex: aRange.location]; int start = [self lineLayoutIndexForCharacterIndex: aRange.location];
int count; int count;
@ -1328,16 +1328,15 @@ scanRange(NSScanner *scanner, NSCharacterSet* aSet)
- (void) replaceCharactersInRange: (NSRange)aRange - (void) replaceCharactersInRange: (NSRange)aRange
withString: (NSString*)aString withString: (NSString*)aString
{ {
[_textStorage beginEditing];
[_textStorage replaceCharactersInRange: aRange withString: aString]; [_textStorage replaceCharactersInRange: aRange withString: aString];
[_textStorage endEditing];
[self _editedRange: aRange
withDelta: [aString length] - aRange.length];
} }
- (void) setString: (NSString*)aString - (void) setString: (NSString*)aString
{ {
RELEASE(_textStorage); RELEASE(_textStorage);
_textStorage = [[NSMutableAttributedString alloc] _textStorage = [[NSTextStorage alloc]
initWithString: aString initWithString: aString
attributes: [self defaultTypingAttributes]]; attributes: [self defaultTypingAttributes]];
@ -1558,7 +1557,6 @@ scanRange(NSScanner *scanner, NSCharacterSet* aSet)
if (didLock) if (didLock)
{ {
[self unlockFocus]; [self unlockFocus];
[_window flushWindow];
} }
} }
@ -1697,6 +1695,7 @@ scanRange(NSScanner *scanner, NSCharacterSet* aSet)
NSRange foundRange; NSRange foundRange;
int maxSelRange; int maxSelRange;
[_textStorage beginEditing];
for (maxSelRange = NSMaxRange(_selected_range); for (maxSelRange = NSMaxRange(_selected_range);
searchRange.location < maxSelRange; searchRange.location < maxSelRange;
searchRange = NSMakeRange (NSMaxRange (foundRange), searchRange = NSMakeRange (NSMaxRange (foundRange),
@ -1712,25 +1711,25 @@ scanRange(NSScanner *scanner, NSCharacterSet* aSet)
ofRange: foundRange]; ofRange: foundRange];
} }
} }
[_textStorage endEditing];
} }
else else
{ {
[self setFont: [sender convertFont: _default_font]]; [self setFont: [sender convertFont: _default_font]];
} }
[self setNeedsDisplay: YES];
} }
} }
- (void) setFont: (NSFont*)font - (void) setFont: (NSFont*)font
{ {
NSRange fullRange = NSMakeRange(0, [[self string] length]); NSRange fullRange = NSMakeRange(0, [_textStorage length]);
ASSIGN(_default_font, font); ASSIGN(_default_font, font);
[_textStorage beginEditing];
[_textStorage addAttribute: NSFontAttributeName [_textStorage addAttribute: NSFontAttributeName
value: font value: font
range: fullRange]; range: fullRange];
[self _editedRange: fullRange [_textStorage endEditing];
withDelta: 0];
} }
- (void) setFont: (NSFont*)font - (void) setFont: (NSFont*)font
@ -1740,11 +1739,11 @@ scanRange(NSScanner *scanner, NSCharacterSet* aSet)
{ {
if (font != nil) if (font != nil)
{ {
[_textStorage beginEditing];
[_textStorage addAttribute: NSFontAttributeName [_textStorage addAttribute: NSFontAttributeName
value: font value: font
range: aRange]; range: aRange];
[self _editedRange: aRange [_textStorage endEditing];
withDelta: 0];
} }
} }
} }
@ -1760,16 +1759,20 @@ scanRange(NSScanner *scanner, NSCharacterSet* aSet)
- (void) setAlignment: (NSTextAlignment) mode - (void) setAlignment: (NSTextAlignment) mode
{ {
_alignment = mode; _alignment = mode;
[self setNeedsDisplay: YES]; [_textStorage beginEditing];
[_textStorage setAlignment: NSCenterTextAlignment
range: NSMakeRange(0, [_textStorage length])];
[_textStorage endEditing];
} }
- (void) alignCenter: (id) sender - (void) alignCenter: (id) sender
{ {
if ([self isRichText]) if ([self isRichText])
{ {
[_textStorage beginEditing];
[_textStorage setAlignment: NSCenterTextAlignment [_textStorage setAlignment: NSCenterTextAlignment
range: _selected_range]; range: _selected_range];
[self setNeedsDisplay: YES]; [_textStorage endEditing];
} }
else else
[self setAlignment: NSCenterTextAlignment]; [self setAlignment: NSCenterTextAlignment];
@ -1779,9 +1782,10 @@ scanRange(NSScanner *scanner, NSCharacterSet* aSet)
{ {
if ([self isRichText]) if ([self isRichText])
{ {
[_textStorage beginEditing];
[_textStorage setAlignment: NSLeftTextAlignment [_textStorage setAlignment: NSLeftTextAlignment
range: _selected_range]; range: _selected_range];
[self setNeedsDisplay: YES]; [_textStorage endEditing];
} }
else else
[self setAlignment: NSLeftTextAlignment]; [self setAlignment: NSLeftTextAlignment];
@ -1791,9 +1795,10 @@ scanRange(NSScanner *scanner, NSCharacterSet* aSet)
{ {
if ([self isRichText]) if ([self isRichText])
{ {
[_textStorage beginEditing];
[_textStorage setAlignment: NSRightTextAlignment [_textStorage setAlignment: NSRightTextAlignment
range: _selected_range]; range: _selected_range];
[self setNeedsDisplay: YES]; [_textStorage endEditing];
} }
else else
[self setAlignment: NSRightTextAlignment]; [self setAlignment: NSRightTextAlignment];
@ -1812,10 +1817,19 @@ scanRange(NSScanner *scanner, NSCharacterSet* aSet)
{ {
if ([self isRichText]) if ([self isRichText])
{ {
[_textStorage beginEditing];
if (color != nil) if (color != nil)
[_textStorage addAttribute: NSForegroundColorAttributeName {
value: color [_textStorage addAttribute: NSForegroundColorAttributeName
range: aRange]; value: color
range: aRange];
}
else
{
[_textStorage removeAttribute: NSForegroundColorAttributeName
range: aRange];
}
[_textStorage endEditing];
} }
} }
@ -1827,9 +1841,22 @@ scanRange(NSScanner *scanner, NSCharacterSet* aSet)
- (void) setTextColor: (NSColor*) color - (void) setTextColor: (NSColor*) color
{ {
NSRange fullRange = NSMakeRange(0, [_textStorage length]);
ASSIGN (_text_color, color); ASSIGN (_text_color, color);
if (![self isRichText]) [_textStorage beginEditing];
[self setNeedsDisplay: YES]; if (color != nil)
{
[_textStorage addAttribute: NSForegroundColorAttributeName
value: color
range: fullRange];
}
else
{
[_textStorage removeAttribute: NSForegroundColorAttributeName
range: fullRange];
}
[_textStorage endEditing];
} }
// //
@ -1837,66 +1864,97 @@ scanRange(NSScanner *scanner, NSCharacterSet* aSet)
// //
- (void) subscript: (id)sender - (void) subscript: (id)sender
{ {
if ([self isRichText]) NSRange aRange;
if (_tf.is_rich_text)
aRange = _selected_range;
else
aRange = NSMakeRange(0, [_textStorage length]);
if (aRange.length)
{ {
if (_selected_range.length) [_textStorage beginEditing];
{ [_textStorage subscriptRange: aRange];
[_textStorage subscriptRange: _selected_range]; [_textStorage endEditing];
[self _editedRange: _selected_range }
withDelta: 0]; else
} {
// FIXME: Set the typing attributes
/* [[self typingAttributes]
setObject: [NSNumber numberWithInt: ]
forKey: NSSuperScriptAttributeName]; */
} }
} }
- (void) superscript: (id)sender - (void) superscript: (id)sender
{ {
if ([self isRichText]) NSRange aRange;
if (_tf.is_rich_text)
aRange = _selected_range;
else
aRange = NSMakeRange(0, [_textStorage length]);
if (aRange.length)
{ {
if (_selected_range.length) [_textStorage beginEditing];
{ [_textStorage superscriptRange: aRange];
[_textStorage superscriptRange: _selected_range]; [_textStorage endEditing];
[self _editedRange: _selected_range }
withDelta: 0]; else
} {
// FIXME: Set the typing attributes
} }
} }
- (void) unscript: (id)sender - (void) unscript: (id)sender
{ {
if ([self isRichText]) NSRange aRange;
if (_tf.is_rich_text)
aRange = _selected_range;
else
aRange = NSMakeRange(0, [_textStorage length]);
if (aRange.length)
{ {
if (_selected_range.length) [_textStorage beginEditing];
{ [_textStorage unscriptRange: aRange];
[_textStorage unscriptRange: _selected_range]; [_textStorage endEditing];
[self _editedRange: _selected_range }
withDelta: 0]; else
} {
// FIXME: Set the typing attributes
} }
} }
- (void) underline: (id)sender - (void) underline: (id)sender
{ {
if ([self isRichText]) NSRange aRange;
{ BOOL doUnderline = YES;
BOOL doUnderline = YES;
if ([[_textStorage attribute: NSUnderlineStyleAttributeName
atIndex: _selected_range.location
effectiveRange: NULL] intValue])
doUnderline = NO;
if (_selected_range.length) if (_tf.is_rich_text)
{ aRange = _selected_range;
[_textStorage addAttribute: NSUnderlineStyleAttributeName else
value: [NSNumber numberWithInt: doUnderline] aRange = NSMakeRange(0, [_textStorage length]);
range: _selected_range];
[self _editedRange: _selected_range if ([[_textStorage attribute: NSUnderlineStyleAttributeName
withDelta: 0]; atIndex: aRange.location
} effectiveRange: NULL] intValue])
else // no redraw necess. doUnderline = NO;
[[self typingAttributes]
setObject: [NSNumber numberWithInt: doUnderline] if (aRange.length)
forKey: NSUnderlineStyleAttributeName]; {
[_textStorage beginEditing];
[_textStorage addAttribute: NSUnderlineStyleAttributeName
value: [NSNumber numberWithInt: doUnderline]
range: aRange];
[_textStorage endEditing];
} }
else // no redraw necess.
[[self typingAttributes]
setObject: [NSNumber numberWithInt: doUnderline]
forKey: NSUnderlineStyleAttributeName];
} }
// //
@ -1946,13 +2004,6 @@ scanRange(NSScanner *scanner, NSCharacterSet* aSet)
// //
// Sizing the Frame Rectangle // Sizing the Frame Rectangle
// //
- (void) setFrame: (NSRect) frameRect
{
// FIXME: This should clear the now empty space,
// when shrinking
[super setFrame: frameRect];
}
- (BOOL) isHorizontallyResizable - (BOOL) isHorizontallyResizable
{ {
return _tf.is_horizontally_resizable; return _tf.is_horizontally_resizable;
@ -2207,7 +2258,7 @@ scanRange(NSScanner *scanner, NSCharacterSet* aSet)
- (void) keyDown: (NSEvent*)theEvent - (void) keyDown: (NSEvent*)theEvent
{ {
// If not editable then don't recognize the key down // If not editable, don't recognize the key down
if (!_tf.is_editable) if (!_tf.is_editable)
{ {
[super keyDown: theEvent]; [super keyDown: theEvent];
@ -2546,7 +2597,7 @@ scanRange(NSScanner *scanner, NSCharacterSet* aSet)
_default_font = RETAIN([aDecoder decodeObject]); _default_font = RETAIN([aDecoder decodeObject]);
[aDecoder decodeValueOfObjCType: @encode(NSRange) at: &_selected_range]; [aDecoder decodeValueOfObjCType: @encode(NSRange) at: &_selected_range];
// build upt the layout information that dont get stored // build up the layout information that dont get stored
[self _buildUpLayout]; [self _buildUpLayout];
return self; return self;
} }
@ -2581,15 +2632,14 @@ scanRange(NSScanner *scanner, NSCharacterSet* aSet)
- (void) replaceRange: (NSRange) aRange - (void) replaceRange: (NSRange) aRange
withAttributedString: (NSAttributedString*) attrString withAttributedString: (NSAttributedString*) attrString
{ {
[_textStorage beginEditing];
if ([self isRichText]) if ([self isRichText])
[_textStorage replaceCharactersInRange: aRange [_textStorage replaceCharactersInRange: aRange
withAttributedString: attrString]; withAttributedString: attrString];
else else
[_textStorage replaceCharactersInRange: aRange [_textStorage replaceCharactersInRange: aRange
withString: [attrString string]]; withString: [attrString string]];
[_textStorage endEditing];
[self _editedRange: aRange
withDelta: [attrString length] - aRange.length];
} }
- (unsigned) textLength - (unsigned) textLength
@ -2677,9 +2727,9 @@ scanRange(NSScanner *scanner, NSCharacterSet* aSet)
NSRange longestRange; NSRange longestRange;
currentFont = [_textStorage attribute: NSFontAttributeName currentFont = [_textStorage attribute: NSFontAttributeName
atIndex: _selected_range.location atIndex: _selected_range.location
longestEffectiveRange: &longestRange longestEffectiveRange: &longestRange
inRange: _selected_range]; inRange: _selected_range];
if (NSEqualRanges (longestRange, _selected_range)) if (NSEqualRanges (longestRange, _selected_range))
isMultiple = NO; isMultiple = NO;
@ -2728,7 +2778,6 @@ scanRange(NSScanner *scanner, NSCharacterSet* aSet)
if (didLock) if (didLock)
{ {
[self unlockFocus]; [self unlockFocus];
[_window flushWindow];
} }
} }
@ -3039,9 +3088,9 @@ scanRange(NSScanner *scanner, NSCharacterSet* aSet)
deleteRange = NSMakeRange (MAX (0, aRange.location - 1), 1); deleteRange = NSMakeRange (MAX (0, aRange.location - 1), 1);
} }
[_textStorage beginEditing];
[_textStorage deleteCharactersInRange: deleteRange]; [_textStorage deleteCharactersInRange: deleteRange];
[_textStorage endEditing];
[self _editedRange: deleteRange withDelta: -deleteRange.length ];
// ScrollView interaction // ScrollView interaction
[self sizeToFit]; [self sizeToFit];
@ -3072,17 +3121,9 @@ scanRange(NSScanner *scanner, NSCharacterSet* aSet)
{ {
if (_layoutManager == nil) if (_layoutManager == nil)
_layoutManager = [[GSSimpleLayoutManager alloc] _layoutManager = [[GSSimpleLayoutManager alloc]
initForText: self initForText: self];
withAttributedString: _textStorage];
else
[_layoutManager setAttributedString: _textStorage];
}
- (void) _editedRange: (NSRange) aRange [_textStorage addLayoutManager: _layoutManager];
withDelta: (int) delta
{
[_layoutManager _editedRange: aRange
withDelta: delta];
} }
// Returns the currently used bounds for all the text // Returns the currently used bounds for all the text