diff --git a/MathPad/MPExpressionLayout.m b/MathPad/MPExpressionLayout.m index 4e83690..ccb008a 100644 --- a/MathPad/MPExpressionLayout.m +++ b/MathPad/MPExpressionLayout.m @@ -184,17 +184,20 @@ elementBounds.origin.x += elementOffset.x; elementBounds.origin.y += elementOffset.y; + // Only the horizontal location is to consider for hit testing + elementBounds.size.height = CGFLOAT_MAX; + id element = [self.expression elementAtIndex:index]; if (NSMouseInRect(point, elementBounds, self.flipped)) { + NSPoint pointInElement = NSMakePoint(point.x - elementOffset.x, point.y + elementOffset.y); if ([element isString]) { CTLineRef line = [self lineForElementAtIndex:index]; CFRetain(line); - CFIndex localIndex = CTLineGetStringIndexForPosition(line, point); + CFIndex localIndex = CTLineGetStringIndexForPosition(line, pointInElement); CFRelease(line); return [NSIndexPath indexPathWithIndex:currentPosition+localIndex]; } else { - NSPoint pointInFunction = NSMakePoint(point.x - elementOffset.x, point.y + elementOffset.y); - NSIndexPath *subPath = [[self childLayoutAtIndex:index] indexPathForMousePoint:pointInFunction]; + NSIndexPath *subPath = [[self childLayoutAtIndex:index] indexPathForMousePoint:pointInElement]; if (subPath.length == 1) { // A single index is used to communicate back wether the // selection should be before or after the function. diff --git a/MathPad/MPLayout.h b/MathPad/MPLayout.h index c053a45..c211246 100644 --- a/MathPad/MPLayout.h +++ b/MathPad/MPLayout.h @@ -40,6 +40,8 @@ replacementLength:(NSUInteger)replacementLength; - (void)invalidate; +- (MPLayout *)childLayoutAtIndexPath:(NSIndexPath *)indexPath; + #pragma mark Calculation and Drawing Methods - (CTLineRef)createLineForString:(NSString *)aString; diff --git a/MathPad/MPLayout.m b/MathPad/MPLayout.m index 49b5b4f..a7e9492 100644 --- a/MathPad/MPLayout.m +++ b/MathPad/MPLayout.m @@ -127,6 +127,15 @@ [self.parent invalidate]; } +- (MPLayout *)childLayoutAtIndexPath:(NSIndexPath *)indexPath +{ + if (indexPath.length == 0) { + return self; + } + MPLayout *child = [self childLayoutAtIndex:indexPath.firstIndex]; + return [child childLayoutAtIndexPath:[indexPath indexPathByRemovingFirstIndex]]; +} + #pragma mark Calculation and Drawing Methods - (CTLineRef)createLineForString:(NSString *)aString {