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"
21 #include "BSSourceView.h"
23 @interface BSLineNumberRulerView (Private)
24 - (void)computeLineIndex;
25 - (NSAttributedString*)attributedStringForLineNumber:(NSUInteger)line;
26 - (NSDictionary*)fontAttributes;
31 // The default width of the ruler.
32 const CGFloat kDefaultWidth = 30.0;
34 // Padding between the right edge of the ruler and the line number string.
35 const CGFloat kRulerRightPadding = 2.5;
40 @implementation BSLineNumberRulerView
42 - (id)initWithSourceView:(BSSourceView*)sourceView
44 if (self = [super initWithScrollView:[sourceView scrollView]
45 orientation:NSVerticalRuler]) {
46 sourceView_ = sourceView;
47 [self setClientView:[[sourceView_ scrollView] documentView]];
48 [self setRuleThickness:kDefaultWidth];
55 [self setClientView:[[sourceView_ scrollView] documentView]];
56 [self setRuleThickness:kDefaultWidth];
59 - (void)drawHashMarksAndLabelsInRect:(NSRect)rect
61 // Draw the background color.
62 [[NSColor colorWithDeviceRed:0.871 green:0.871 blue:0.871 alpha:1] set];
63 [NSBezierPath fillRect:rect];
65 // Draw the right stroke.
66 [[NSColor grayColor] setStroke];
67 [NSBezierPath strokeLineFromPoint:NSMakePoint(NSMaxX(rect), NSMinY(rect))
68 toPoint:NSMakePoint(NSMaxX(rect), NSMaxY(rect))];
70 // Get some common elements of the source view.
71 NSTextView* textView = [sourceView_ textView];
72 NSLayoutManager* layoutManager = [textView layoutManager];
73 NSTextContainer* textContainer = [textView textContainer];
74 NSRect visibleRect = [[[self scrollView] contentView] bounds];
76 // Get the visible glyph range, as NSRulerView only draws in the visible rect.
77 NSRange visibleGlyphRange = [layoutManager glyphRangeForBoundingRect:visibleRect
78 inTextContainer:textContainer];
79 NSRange characterRange = [layoutManager characterRangeForGlyphRange:visibleGlyphRange
80 actualGlyphRange:NULL];
82 // Go through the lines.
83 const NSRange kNullRange = NSMakeRange(NSNotFound, 0);
84 const CGFloat yOffset = [textView textContainerInset].height;
86 size_t lineCount = lineIndex_.size();
87 std::vector<NSUInteger>::iterator element =
88 std::lower_bound(lineIndex_.begin(),
90 characterRange.location);
91 for (NSUInteger line = std::distance(lineIndex_.begin(), element);
92 line < lineCount; ++line) {
93 NSUInteger firstCharacterIndex = lineIndex_[line];
94 NSLog(@"line = %d @ %d / %d", line, firstCharacterIndex, lineCount);
95 // Stop after iterating past the end of the visible range.
96 if (firstCharacterIndex > NSMaxRange(characterRange))
100 NSRectArray frameRects = [layoutManager rectArrayForCharacterRange:NSMakeRange(firstCharacterIndex, 0)
101 withinSelectedCharacterRange:kNullRange
102 inTextContainer:textContainer
103 rectCount:&rectCount];
105 NSUInteger lineNumber = line + 1;
106 NSAttributedString* lineNumberString =
107 [self attributedStringForLineNumber:lineNumber];
108 NSSize stringSize = [lineNumberString size];
110 CGFloat yCoord = yOffset + NSMinY(frameRects[0]) - NSMinY(visibleRect);
111 NSRect drawRect = NSMakeRect(NSWidth(rect) - stringSize.width - kRulerRightPadding,
112 yCoord + (NSHeight(frameRects[0]) - stringSize.height) / 2.0,
113 NSWidth(rect) - kRulerRightPadding,
114 NSHeight(frameRects[0]));
115 [lineNumberString drawInRect:drawRect];
120 - (void)performLayout
122 [self computeLineIndex];
124 // Determine the width of the ruler based on the line count.
125 NSUInteger lastElement = lineIndex_.back() + 1;
126 NSAttributedString* lastElementString = [self attributedStringForLineNumber:lastElement];
127 NSSize boundingSize = [lastElementString size];
128 [self setRuleThickness:std::max(kDefaultWidth, boundingSize.width)];
131 // Private /////////////////////////////////////////////////////////////////////
134 * Iterates over the text storage system and computes a map of line numbers to
135 * first character index for a line's frame rectangle.
137 - (void)computeLineIndex
141 NSString* text = [[sourceView_ textView] string];
142 NSUInteger stringLength = [text length];
143 NSUInteger index = 0;
145 while (index < stringLength) {
146 lineIndex_.push_back(index);
147 index = NSMaxRange([text lineRangeForRange:NSMakeRange(index, 0)]);
150 NSUInteger lineEnd, contentEnd;
151 [text getLineStart:NULL
153 contentsEnd:&contentEnd
154 forRange:NSMakeRange(lineIndex_.back(), 0)];
155 if (contentEnd < lineEnd)
156 lineIndex_.push_back(index);
158 NSLog(@"line count = %d", lineIndex_.size());
162 * Takes in a line number and returns a formatted attributed string, usable
165 - (NSAttributedString*)attributedStringForLineNumber:(NSUInteger)line
167 NSString* format = [NSString stringWithFormat:@"%d", line];
168 return [[[NSAttributedString alloc] initWithString:format
169 attributes:[self fontAttributes]] autorelease];
173 * Returns the dictionary for an NSAttributedString with which the line numbers
176 - (NSDictionary*)fontAttributes
178 return [NSDictionary dictionaryWithObjectsAndKeys:
179 [NSFont fontWithName:@"Monaco" size:9.0], NSFontAttributeName,
180 [NSColor grayColor], NSForegroundColorAttributeName,