Move |-computeLineIndex| from BSSourceView to BSLineNumberRulerView
[macgdbp.git] / Source / BSLineNumberRulerView.mm
1 /*
2 * MacGDBp
3 * Copyright (c) 2007 - 2011, Blue Static <http://www.bluestatic.org>
4 *
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.
8 *
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.
12 *
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
15 */
16
17 #import "BSLineNumberRulerView.h"
18
19 @interface BSLineNumberRulerView (Private)
20 - (void)computeLineIndex;
21 @end
22
23
24 @implementation BSLineNumberRulerView
25
26 - (id)initWithScrollView:(NSScrollView*)scrollView
27 {
28 if (self = [super initWithScrollView:scrollView orientation:NSVerticalRuler]) {
29 [self setClientView:[scrollView documentView]];
30 }
31 return self;
32 }
33
34 - (void)awakeFromNib
35 {
36 [self setClientView:[[self scrollView] documentView]];
37 }
38
39 - (void)drawHashMarksAndLabelsInRect:(NSRect)rect
40 {
41 // Draw the background color.
42 [[NSColor colorWithDeviceRed:0.871 green:0.871 blue:0.871 alpha:1] set];
43 [NSBezierPath fillRect:rect];
44
45 // Draw the right stroke.
46 [[NSColor grayColor] setStroke];
47 [NSBezierPath strokeLineFromPoint:NSMakePoint(NSMaxX(rect), NSMinY(rect))
48 toPoint:NSMakePoint(NSMaxX(rect), NSMaxY(rect))];
49 }
50
51 - (void)performLayout
52 {
53 [self computeLineIndex];
54 }
55
56 // Private /////////////////////////////////////////////////////////////////////
57
58 /**
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.
61 */
62 - (void)computeLineIndex
63 {
64 lineIndex_.clear();
65
66 NSView* view = [self clientView];
67 if (![view isKindOfClass:[NSTextView class]])
68 return;
69
70 NSString* text = [(NSTextView*)view string];
71 NSUInteger stringLength = [text length];
72 NSUInteger index = 0;
73
74 while (index < stringLength) {
75 lineIndex_.push_back(index);
76 index = NSMaxRange([text lineRangeForRange:NSMakeRange(index, 0)]);
77 }
78
79 NSUInteger lineEnd, contentEnd;
80 [text getLineStart:NULL
81 end:&lineEnd
82 contentsEnd:&contentEnd
83 forRange:NSMakeRange(lineIndex_.back(), 0)];
84 if (contentEnd < lineEnd)
85 lineIndex_.push_back(index);
86
87 NSLog(@"line count = %d", lineIndex_.size());
88 }
89
90 @end