RE: Getting the rect of a character in an NSTextView
RE: Getting the rect of a character in an NSTextView
- Subject: RE: Getting the rect of a character in an NSTextView
- From: Keith Blount <email@hidden>
- Date: Tue, 25 Jan 2005 10:59:35 -0800 (PST)
- Comment: DomainKeys? See http://antispam.yahoo.com/domainkeys
Many thanks for your reply, much appreciated.
Sorry if I'm being dumb, but although it makes sense
that I am creating an infinite recursion somewhere, I
can't see how - maybe I am making some false
assumptions about the best way of syncing the resizing
of views.
The way my view works is this: there are really three
main views - a container view that contains a text
view and a margin view. In turn, these are contained
inside a scroll view. One of my problems has always
been how to make sure the container view is always the
optimal size to fit its sub views (the margin view and
text view), and to make sure that the text view and
margin view are always the same height - whichever is
biggest. To achieve this, I am listening for an
NSViewFrameDidChangeNotification, and then checking if
it is one of these views that changed size. If so, I
then change the other view sizes as appropriate (or,
at this stage, try to rather inaccurately).
None of this seems to be causing a problem - apart
from slowing down the live resizing process. Ripping
out code here and there I know that this part isn't
causing a crash. But my margin view can contain any
number of subviews (notes), which have to be aligned
with certain character indices in the text view. To do
this, my margin view also listens for
NSFrameDidChangeNotification and if the sender is the
associated text view, it cycles through all of the
cards and makes sure they are still aligned.
I just can't see where I'm creating an infinite
recursion here... I thought it might be a clash
between the container view and the margin view both
operating on the same notification, but if I remove
the container view as an observer, the margin view
still crashes the app. Here are my margin view methods
for altering the positions of the note views on
receiving an NSViewFrameDidChangeNotification from the
text view - the crash occurs in
rectForCharacterIndex:, as I said in my first post.
And this all works fine for a small number of note
views...:
// Method for updating the position of the margin
cards, in case we need to force an update manually
- (void)updateMarginCardPositions
{
HEMMarginCardView *card;
NSEnumerator *enumerator = [[self subviews]
objectEnumerator];
while (card = [enumerator nextObject])
{
// Calculate the new frame
NSRect newFrame = [card frame]; // get the old frame
// Get the new Y position by finding the new place
of the character and minusing the drag bar height
newFrame.origin.y = [self
rectForCharacterIndex:[card charIndex]].origin.y;
if (newFrame.origin.y < 0.0) newFrame.origin.y =
0.0; // Make sure the card doesn't go off the top
if ( (newFrame.origin.y + newFrame.size.height) >
([self frame].size.height) )
{
NSRect marginFrame = [self frame];
marginFrame.size.height = newFrame.origin.y +
newFrame.size.height;
[self setFrame:marginFrame];
[self display];
}
// Move the card
[card setFrame:newFrame];
}
[self setNeedsDisplay:YES]; // Redraw
}
// Whenever the text view associataed with the margin
changes size, we need to recalculate the positions of
all
// the margin cards so that they are still aligned
with the characters to which they are attached
- (void)updateMarginCardPositions:(NSNotification
*)notification
{
if ([notification object] == textView)
{
[self updateMarginCardPositions];
}
}
Sorry for posting so much code, but I am really
stumped...
Many thanks again,
Keith
--- Douglas Davidson <email@hidden> wrote:
> On 2005-01-24 12:07:00 -0800 Keith Blount
> <email@hidden>
> wrote:
>
> >
> > Here is a snippet of my backtrace - the backtrace
> is
> > long, but it is basically a repeat of what is
> below
> > for every margin card that has raised an error. It
> > seems to be an EXC_BAD_ACCESS error. I don't know
> if
> > this helps.
>
> This is an infinite recursion--you are changing the
> view size in
> response to a change in view size, and so on ad
> infinitum. You need
> to figure out how not to do that.
>
> Douglas Davidson
>
>
__________________________________
Do you Yahoo!?
The all-new My Yahoo! - Get yours free!
http://my.yahoo.com
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden