As a learning experience, I'm trying to make a trivial document-based app: a 
window with a text view that can edit and save RTF files.

What I have now almost works, except that whenever I save changes, the text 
view scrolls to the top.  Needless to say, this would be very annoying in a 
real text-editing app.  I suspect it's something trivial about document-based 
apps and/or bindings that I just don't understand.  Can someone explain what's 
going on?


--- The setup ---

I barely wrote ten lines of code.  I created a new document-based project and 
added this ivar to MyDocument.h:

    NSMutableAttributedString *myText;

(I see the document class in TextEdit uses an NSTextStorage.  But the docs say 
NSTextView can be bound to an attributed string, so I figured I should be able 
to use one.)

I wrote three methods, two of which the project template had stubbed out for me:

- (NSDictionary *)myDocumentAttributes
{
    return [NSDictionary dictionaryWithObject:NSRTFTextDocumentType
                                       forKey:NSDocumentTypeDocumentAttribute];
}

- (NSData *)dataOfType:(NSString *)typeName error:(NSError **)outError
{
    return [myText dataFromRange:NSMakeRange(0, [myText length])
              documentAttributes:[self myDocumentAttributes]
                           error:outError];
}

- (BOOL)readFromData:(NSData *)data ofType:(NSString *)typeName error:(NSError 
**)outError
{
    [myText release];
    myText = [[NSMutableAttributedString alloc] initWithData:data
                                                     options:[self 
myDocumentAttributes]
                                          documentAttributes:nil
                                                       error:outError];
    return (myText != nil);
}

In IB, I put an NSTextView in the window and bound its Attributed String to the 
File's Owner with "myText" as the model key path.

In the Info.plist, I specified rtf as the document type.


--- The weirdness ---

I paste a longish amount of text into the text view and hit Command-S.  While 
the sheet for the save panel is sliding down, I can see the text view scroll to 
the top of the text.  Aside from this, saving and opening files seems to work.


--- Attempt to diagnose ---

I created a subview of NSTextView and put a breakpoint in scrollPoint:.  When I 
hit Save, I get the stack trace below.

* The point being passed to scrollPoint: is {0, 467} which doesn't seem to 
correspond to anything.  The scroll view's height is 599.
* Why is the text view being sent a setTextColor: message?

#0    0x100001b72 in -[MyTextView scrollPoint:] at MyTextView.m:16
#1    0x7fff81626fb3 in -[NSLayoutManager(NSPrivate) 
_resizeTextViewForTextContainer:]
#2    0x7fff81625758 in -[NSLayoutManager(NSPrivate) 
_recalculateUsageForTextContainerAtIndex:]
#3    0x7fff8162c099 in -[NSLayoutManager 
textStorage:edited:range:changeInLength:invalidatedRange:]
#4    0x7fff8162be59 in -[NSTextStorage 
_notifyEdited:range:changeInLength:invalidatedRange:]
#5    0x7fff81686f3c in -[NSTextStorage processEditing]
#6    0x7fff816879e3 in -[NSTextStorage endEditing]
#7    0x7fff86bde763 in -[NSMutableAttributedString removeAttribute:range:]
#8    0x7fff816a2315 in -[NSTextView setTextColor:]
#9    0x7fff81b15c33 in -[_NSTextPlugin showValue:inObject:]
#10    0x7fff816d8e38 in -[NSValueBinder discardEditing]
#11    0x7fff81adefe2 in -[NSValueBinder 
commitEditingWithDelegate:didCommitSelector:contextInfo:]
#12    0x7fff8184deb2 in -[NSDocument 
saveDocumentWithDelegate:didSaveSelector:contextInfo:]
#13    0x7fff81677e9a in -[NSApplication sendAction:to:from:]
#14    0x7fff8169c41e in -[NSMenuItem _corePerformAction]
#15    0x7fff8169c188 in -[NSCarbonMenuImpl 
performActionWithHighlightingForItemAtIndex:]
#16    0x7fff81681e09 in -[NSMenu performKeyEquivalent:]
#17    0x7fff81680bb1 in -[NSApplication _handleKeyEquivalent:]
#18    0x7fff81551645 in -[NSApplication sendEvent:]
#19    0x7fff814e84da in -[NSApplication run]
#20    0x7fff814e11a8 in NSApplicationMain
#21    0x100001b50 in main at main.m:13


--Andy

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to