Making an NSTextView have a data source
Making an NSTextView have a data source
- Subject: Making an NSTextView have a data source
- From: Mikey <email@hidden>
- Date: Tue, 7 May 2002 23:33:06 -0500
I have an app where an NSTextView needs to be updated based on the user's
selection in an NSTableView. It's easy to tell when the user selects
something because the table view's delegate gets a
-tableViewSelectionDidChange: message; however I was having more trouble
with getting the text view updated.
At first I tried to do the updating right in the
-tableViewSelectionDidChange: method, but that didn't work very well
because the GUI wouldn't always get updated correctly (sometimes some of
the text was missing, sometimes the scrollbar was wrong, etc.). I was kind
of surprised by this because I thought that I was receiving the
-tableViewSelectionDidChange: method within the context of the main thread
where drawing is usually okay.
My next thought was to move the drawing to another thread and trigger it
with a semaphore (i.e. an NSLock). While planning this out, I started
thinking about how much easier this would be if NSTextViews had a data
source like NSTableViews do. Then I could just connect the data source in
IB and notify the view whenever the data changed. I decided to do that
instead and it even appears to work ;) but I'm worried about two things:
- I originally wanted to put my data source pull code in the -display
method, but when I tried it, it wasn't being invoked at all. I ended
up moving it to the -drawRect: method instead, making sure to put a check
in to avoid unnecessary pulls during spurious drawing. This seems kind
of yucky to me so I was looking for some feedback from others.
- The scrollbar flickers momentarily during the drawing: once with a
large scroll thumb and then again with a smaller scroll thumb -- as
if it's trying to size itself in realtime as the data is being
loaded. I was surprised by this since I load the data in a single
method call. Does anyone know how to make the flicker go away?
Here's the -drawRect: code if it helps:
- (void) drawRect:(NSRect)aRect
{
// If our current text is stale and we have a data source where we can
// get new text, then do it.
if ( mIsStale && mDataSource )
{
// The storage used by the text view. Changing this changes what's
// displayed.
NSTextStorage* vTextStorage = [self textStorage];
// A range that represents all the text in the view.
NSRange vDeleteRange = NSMakeRange( 0, ( [vTextStorage length] ) );
// Replace all our text with the text provided by the data source.
[vTextStorage replaceCharactersInRange:vDeleteRange
withAttributedString:[mDataSource dataForTextView:self]];
// Yea, we're fresh again.
mIsStale = NO;
};
// Call the super class.
[super drawRect:aRect];
};
Michael Sullivan
email@hidden
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.