[Biococoa-dev] Re: Weird bug with selection in Textview with custom textcontainer [solved]

Alexander Griekspoor mek at mekentosj.com
Tue Apr 18 14:12:13 EDT 2006

Hi Douglas,

Many thanks for the advice, I've got it to work!
Below is the implementation of a custom layoutmanager class that  
fixes the problem. The nice thing is that it will get self- 
deactivated when you've fixed the problem ;-) I know if I have  
correctly implemented the fractionOfDistanceThroughGlyph part of the  
method, I'm not well known with c pointers, but that's anyway not  
really needed anyway for me.
Once again, thanks for the help!

#import "KDLayoutManager.h"

#define COLUMN_WIDTH 10

@implementation KDLayoutManager

- (unsigned)glyphIndexForPoint:(NSPoint)aPoint inTextContainer: 
(NSTextContainer *)aTextContainer fractionOfDistanceThroughGlyph: 
(float *)partialFraction{
	unsigned idx = [super glyphIndexForPoint: aPoint
							 inTextContainer: aTextContainer
			  fractionOfDistanceThroughGlyph: partialFraction];
	// because of a bug in this method we check by hand	if wrong value  
is given
	if(idx == COLUMN_WIDTH && !NSPointInRect(aPoint, [self  
boundingRectForGlyphRange: NSMakeRange(COLUMN_WIDTH,1)  
inTextContainer: aTextContainer])){
		int i;
		for (i=0; i<COLUMN_WIDTH; i++){
			NSRect glyphRect = [self boundingRectForGlyphRange: NSMakeRange(i, 
1) inTextContainer: aTextContainer];
			if(NSPointInRect(aPoint, glyphRect)){
				//calculate fraction
				float fraction = (aPoint.x - NSMinX(glyphRect)) / NSWidth 
				partialFraction = &fraction;
				return i;
		// if not found, check whether before or after
		if(aPoint.x < NSMinX([self boundingRectForGlyphRange: NSMakeRange 
(0,1) inTextContainer: aTextContainer])) {
			float fraction = 0.0;
			partialFraction = &fraction;
			return 0;
		else {
			float fraction = 1.0;
			partialFraction = &fraction;
			return COLUMN_WIDTH;
	return idx;


On 18-apr-2006, at 17:30, Douglas Davidson wrote:

> On Apr 18, 2006, at 12:43 AM, Alexander Griekspoor wrote:
>> I've thought a bit about it and I see one potential workaround.  
>> Can you tell me whether it's likely to work?
>> Here's what I had in mind:
>> 1) as you suggested, override:  - (unsigned)glyphIndexForPoint: 
>> (NSPoint)aPoint inTextContainer:(NSTextContainer *)aTextContainer  
>> fractionOfDistanceThroughGlyph:(float *)partialFraction
>> 2) in it, find out if the point is within the first 10 characters  
>> and there is only one line (the conditions that the bug presents  
>> itself). <- is there an easy way to do this?
> You can find out from the layout manager how many fragments there  
> are, using the effective glyph range argument to  
> lineFragmentRectForGlyphAtIndex:effectiveRange: and iterating, and  
> I think you have a fixed maximum number of fragments per line.   
> However, in your case I think just counting characters will suffice  
> to determine how many lines there are.  Then I would check to see  
> whether the super method  
> glyphIndexForPoint:inTextContainer:fractionOfDistanceThroughGlyph:  
> points at the first glyph in the second fragment, which is what  
> happens in the error case.
>> 3) if we are dealing with the above, use - (NSRectArray) 
>> rectArrayForGlyphRange:(NSRange)glyphRange  
>> withinSelectedGlyphRange:(NSRange)selGlyphRange inTextContainer: 
>> (NSTextContainer *)aTextContainer rectCount:(unsigned *)rectCount   
>> with glyphrange 1-10, iterate over the rect array to see if the  
>> point falls within each rect and return the one where we have bingo.
> I would use locationForGlyphAtIndex:, and if it falls before one of  
> the locations of the glyphs in the first fragment, handle it  
> accordingly, and otherwise just use the super method's values.
> Douglas Davidson

                     ** Alexander Griekspoor **
               The Netherlands Cancer Institute
               Department of Tumorbiology (H4)
          Plesmanlaan 121, 1066 CX, Amsterdam
                   Tel:  + 31 20 - 512 2023
                   Fax:  + 31 20 - 512 2029
                   AIM: mekentosj at mac.com
                   E-mail: a.griekspoor at nki.nl
               Web: http://www.mekentosj.com

                             iRNAi, do you?


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.bioinformatics.org/pipermail/biococoa-dev/attachments/20060418/3f5c049f/attachment.html>

More information about the Biococoa-dev mailing list