From b02f4e360684797d4c861ccce860ba20462e05a3 Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Wed, 12 Jan 2011 17:35:31 -0500 Subject: [PATCH] BSLineNumberRulerView should take the BSSourceView rather than just a generic NSScrollView --- Source/BSLineNumberRulerView.h | 8 +++++++- Source/BSLineNumberRulerView.mm | 23 ++++++++++------------- Source/BSSourceView.mm | 2 +- 3 files changed, 18 insertions(+), 15 deletions(-) diff --git a/Source/BSLineNumberRulerView.h b/Source/BSLineNumberRulerView.h index ece4300..9b1fd38 100644 --- a/Source/BSLineNumberRulerView.h +++ b/Source/BSLineNumberRulerView.h @@ -17,15 +17,21 @@ #import #include +@class BSSourceView; + // The NSRulerView that draws line numbers on the BSSourceView. @interface BSLineNumberRulerView : NSRulerView { @private + BSSourceView* sourceView_; // Weak, owns this. + + // A vector (thus 0-based) map of line numbers (indices) to character indices + // in the text storage. std::vector lineIndex_; } // Designated initializer. -- (id)initWithScrollView:(NSScrollView*)scrollView; +- (id)initWithSourceView:(BSSourceView*)sourceView; - (void)performLayout; diff --git a/Source/BSLineNumberRulerView.mm b/Source/BSLineNumberRulerView.mm index 554183a..f0227e3 100644 --- a/Source/BSLineNumberRulerView.mm +++ b/Source/BSLineNumberRulerView.mm @@ -18,6 +18,8 @@ #include +#include "BSSourceView.h" + @interface BSLineNumberRulerView (Private) - (void)computeLineIndex; - (NSAttributedString*)attributedStringForLineNumber:(NSUInteger)line; @@ -37,10 +39,12 @@ const CGFloat kRulerRightPadding = 2.5; @implementation BSLineNumberRulerView -- (id)initWithScrollView:(NSScrollView*)scrollView +- (id)initWithSourceView:(BSSourceView*)sourceView { - if (self = [super initWithScrollView:scrollView orientation:NSVerticalRuler]) { - [self setClientView:[scrollView documentView]]; + if (self = [super initWithScrollView:[sourceView scrollView] + orientation:NSVerticalRuler]) { + sourceView_ = sourceView; + [self setClientView:[[sourceView_ scrollView] documentView]]; [self setRuleThickness:kDefaultWidth]; } return self; @@ -48,7 +52,7 @@ const CGFloat kRulerRightPadding = 2.5; - (void)awakeFromNib { - [self setClientView:[[self scrollView] documentView]]; + [self setClientView:[[sourceView_ scrollView] documentView]]; [self setRuleThickness:kDefaultWidth]; } @@ -64,10 +68,7 @@ const CGFloat kRulerRightPadding = 2.5; toPoint:NSMakePoint(NSMaxX(rect), NSMaxY(rect))]; // Get some common elements of the source view. - NSView* view = [self clientView]; - if (![view isKindOfClass:[NSTextView class]]) - return; - NSTextView* textView = (NSTextView*)view; + NSTextView* textView = [sourceView_ textView]; NSLayoutManager* layoutManager = [textView layoutManager]; NSTextContainer* textContainer = [textView textContainer]; NSRect visibleRect = [[[self scrollView] contentView] bounds]; @@ -137,11 +138,7 @@ const CGFloat kRulerRightPadding = 2.5; { lineIndex_.clear(); - NSView* view = [self clientView]; - if (![view isKindOfClass:[NSTextView class]]) - return; - - NSString* text = [(NSTextView*)view string]; + NSString* text = [[sourceView_ textView] string]; NSUInteger stringLength = [text length]; NSUInteger index = 0; diff --git a/Source/BSSourceView.mm b/Source/BSSourceView.mm index 31ab6cd..25e9a4c 100644 --- a/Source/BSSourceView.mm +++ b/Source/BSSourceView.mm @@ -213,7 +213,7 @@ [scrollView_ setDocumentView:textView_]; // Set up the ruler. - ruler_ = [[[BSLineNumberRulerView alloc] initWithScrollView:scrollView_] autorelease]; + ruler_ = [[[BSLineNumberRulerView alloc] initWithSourceView:self] autorelease]; [scrollView_ setVerticalRulerView:ruler_]; [scrollView_ setHasHorizontalRuler:NO]; [scrollView_ setHasVerticalRuler:YES]; -- 2.43.5