From 5a494b609ad166dacdb29f2a20650f18c7a847d8 Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Fri, 14 Jan 2011 09:22:14 -0500 Subject: [PATCH] Implement |-mouseDown:| in BSLineNumberRulerView to get breakpoints working again --- Source/BSLineNumberRulerView.h | 5 ++++ Source/BSLineNumberRulerView.mm | 41 ++++++++++++++++++++++++++++++++- 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/Source/BSLineNumberRulerView.h b/Source/BSLineNumberRulerView.h index 9b1fd38..80b36bc 100644 --- a/Source/BSLineNumberRulerView.h +++ b/Source/BSLineNumberRulerView.h @@ -33,6 +33,11 @@ // Designated initializer. - (id)initWithSourceView:(BSSourceView*)sourceView; +// Performs layout and redraws the line number view. - (void)performLayout; +// Returns the line number (1-based) at the given point. |point| should be in +// the receiver's coordinate system. +- (NSUInteger)lineNumberAtPoint:(NSPoint)point; + @end diff --git a/Source/BSLineNumberRulerView.mm b/Source/BSLineNumberRulerView.mm index aad921b..d4b102a 100644 --- a/Source/BSLineNumberRulerView.mm +++ b/Source/BSLineNumberRulerView.mm @@ -90,7 +90,7 @@ const CGFloat kRulerRightPadding = 2.5; const NSRange kNullRange = NSMakeRange(NSNotFound, 0); const CGFloat yOffset = [textView textContainerInset].height; - size_t lineCount = lineIndex_.size(); + const size_t lineCount = lineIndex_.size(); std::vector::iterator element = std::lower_bound(lineIndex_.begin(), lineIndex_.end(), @@ -149,6 +149,45 @@ const CGFloat kRulerRightPadding = 2.5; [self setNeedsDisplay:YES]; } +- (NSUInteger)lineNumberAtPoint:(NSPoint)point +{ + // Get some common elements of the source view. + NSTextView* textView = [sourceView_ textView]; + NSLayoutManager* layoutManager = [textView layoutManager]; + NSTextContainer* textContainer = [textView textContainer]; + NSRect visibleRect = [[[self scrollView] contentView] bounds]; + point.y += NSMinY(visibleRect); // Adjust for scroll offset. + + const CGFloat kWidth = NSWidth([self bounds]); + const NSRange kNullRange = NSMakeRange(NSNotFound, 0); + const size_t lineCount = lineIndex_.size(); + for (NSUInteger line = 0; line < lineCount; ++line) { + NSUInteger firstCharacterIndex = lineIndex_[line]; + + NSUInteger rectCount; + NSRectArray frameRects = + [layoutManager rectArrayForCharacterRange:NSMakeRange(firstCharacterIndex, 0) + withinSelectedCharacterRange:kNullRange + inTextContainer:textContainer + rectCount:&rectCount]; + for (NSUInteger i = 0; i < rectCount; ++i) { + frameRects[i].size.width = kWidth; + if (NSPointInRect(point, frameRects[i])) { + return line + 1; + } + } + } + return NSNotFound; +} + +- (void)mouseDown:(NSEvent*)theEvent +{ + NSPoint point = [theEvent locationInWindow]; + point = [self convertPoint:point fromView:nil]; + NSUInteger line = [self lineNumberAtPoint:point]; + [sourceView_.delegate gutterClickedAtLine:line forFile:sourceView_.file]; +} + // Private ///////////////////////////////////////////////////////////////////// /** -- 2.43.5