3 * Copyright (c) 2007 - 2011, Blue Static <http://www.bluestatic.org>
5 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU
6 * General Public License as published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
10 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along with this program; if not,
14 * write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
17 #import "BSLineNumberRulerView.h"
19 @interface BSLineNumberRulerView (Private)
20 - (void)computeLineIndex;
24 @implementation BSLineNumberRulerView
26 - (id)initWithScrollView:(NSScrollView*)scrollView
28 if (self = [super initWithScrollView:scrollView orientation:NSVerticalRuler]) {
29 [self setClientView:[scrollView documentView]];
36 [self setClientView:[[self scrollView] documentView]];
39 - (void)drawHashMarksAndLabelsInRect:(NSRect)rect
41 // Draw the background color.
42 [[NSColor colorWithDeviceRed:0.871 green:0.871 blue:0.871 alpha:1] set];
43 [NSBezierPath fillRect:rect];
45 // Draw the right stroke.
46 [[NSColor grayColor] setStroke];
47 [NSBezierPath strokeLineFromPoint:NSMakePoint(NSMaxX(rect), NSMinY(rect))
48 toPoint:NSMakePoint(NSMaxX(rect), NSMaxY(rect))];
53 [self computeLineIndex];
56 // Private /////////////////////////////////////////////////////////////////////
59 * Iterates over the text storage system and computes a map of line numbers to
60 * first character index for a line's frame rectangle.
62 - (void)computeLineIndex
66 NSView* view = [self clientView];
67 if (![view isKindOfClass:[NSTextView class]])
70 NSString* text = [(NSTextView*)view string];
71 NSUInteger stringLength = [text length];
74 while (index < stringLength) {
75 lineIndex_.push_back(index);
76 index = NSMaxRange([text lineRangeForRange:NSMakeRange(index, 0)]);
79 NSUInteger lineEnd, contentEnd;
80 [text getLineStart:NULL
82 contentsEnd:&contentEnd
83 forRange:NSMakeRange(lineIndex_.back(), 0)];
84 if (contentEnd < lineEnd)
85 lineIndex_.push_back(index);
87 NSLog(@"line count = %d", lineIndex_.size());