[SOLVED] Re: Character at point during perform drag
[SOLVED] Re: Character at point during perform drag
- Subject: [SOLVED] Re: Character at point during perform drag
- From: Philip Dow <email@hidden>
- Date: Wed, 15 Mar 2006 10:31:05 +0100
After doing some more searching on this issue, I cam across the
LayoutManagerDemo. Rather than use characterIndexForPoint I am now
calculating the index in a more roundabout way. The system provided
methods still produces a problem when the drop occurs after the last
character, but at least now I can check to see if the mouse point is
contained within glyph rect at the (incorrectly returned) second to
last character index. When it is not, I believe I can safely assume
the user intends the drop to occur after the last character. Code
follows. If anyone has a better solution, I'm still interested.
- (unsigned int) _charIndexForDraggingLoc:(NSPoint)point {
NSPoint localPoint = [self convertPoint:point fromView:nil];
// Convert view coordinates to container coordinates
localPoint.x -= [self textContainerOrigin].x;
localPoint.y -= [self textContainerOrigin].y;
// Convert those coordinates to the nearest glyph index
unsigned glyphIndex = [[self layoutManager]
glyphIndexForPoint:localPoint inTextContainer:[self textContainer]];
// For checking to see whether the mouse actually lies over the
glyph it is nearest to
NSRect glyphRect = [[self layoutManager]
boundingRectForGlyphRange:NSMakeRange(glyphIndex, 1) inTextContainer:
[self textContainer]];
// Convert the glyph index to a character index
unsigned charIndex = [[self layoutManager]
characterIndexForGlyphAtIndex:glyphIndex];
// make sure the char index is not beyond what is available to the
text view
// and fix the last char index problem
if ( charIndex == NSNotFound || charIndex > [[self textStorage]
length] ||
( ! NSPointInRect(localPoint,glyphRect) && charIndex >= [[self
textStorage] length] - 1 ) )
charIndex = [[self textStorage] length];
return charIndex;
}
-Phil
On Mar 14, 2006, at 12:37 PM, Philip Dow wrote:
There are circumstances in a subclass of nstextview when I need to
override the default performDragOperation: behavior. For instance,
I need to link to files rather than read their contents into the
textview's data, or I need to resize pictures before placing them
in the view.
During a drag, the text view updates what is seems to be a
temporary insertion point. When the drag is performed, calling
super's performDragOperation: ensures that the data is dropped at
the correct location. However, when I need to handle the drag on my
own, I have found that inserting the data at the
rangeForUserTextChange point does not insert it in the location
indicated by the drag. Rather, I must calculate the insertion point
myself.
I can do this. The following code works *except* when the drop is
to occur at the very last character of the text view. In this case,
the data is consistently inserted at the second to last character.
If the last character is a newline, and the temporary caret
indicates the drop will occur on this new line, when I handle the
drop myself, it occurs at the end of the line above. Or if the last
character is a period and the caret indicates the drop will occur
after it, when I handle the drop myself, it occurs between the
period and the character before it.
Why is this? Can I edit the following code to get the correct
insertion point even at the last character, or is there a better
way to do this?
- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender {
...
NSPoint mouseLoc = [sender draggingLocation];
NSPoint charLoc = [[self window] convertBaseToScreen:mouseLoc];
int charIndex = [self characterIndexForPoint:charLoc];
[self setSelectedRange:NSMakeRange(charIndex,0)];
...
}
-Phil
_______________________________________________
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
_______________________________________________
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