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 #import "Breakpoint.h"
22 #import "BSSourceView.h"
24 @interface BSLineNumberRulerView (Private)
25 - (void)computeLineIndex;
26 - (NSAttributedString*)attributedStringForLineNumber:(NSUInteger)line;
27 - (NSDictionary*)fontAttributes;
28 - (void)drawBreakpointInRect:(NSRect)rect;
29 - (void)drawProgramCounterInRect:(NSRect)rect;
30 - (void)drawMarkerInRect:(NSRect)rect
31 fillColor:(NSColor*)fill
32 strokeColor:(NSColor*)stroke;
37 // The default width of the ruler.
38 const CGFloat kDefaultWidth = 30.0;
40 // Padding between the right edge of the ruler and the line number string.
41 const CGFloat kRulerRightPadding = 2.5;
46 @implementation BSLineNumberRulerView
48 - (id)initWithSourceView:(BSSourceView*)sourceView
50 if (self = [super initWithScrollView:[sourceView scrollView]
51 orientation:NSVerticalRuler]) {
52 sourceView_ = sourceView;
53 [self setClientView:[[sourceView_ scrollView] documentView]];
54 [self setRuleThickness:kDefaultWidth];
61 [self setClientView:[[sourceView_ scrollView] documentView]];
62 [self setRuleThickness:kDefaultWidth];
65 - (void)drawHashMarksAndLabelsInRect:(NSRect)rect
67 // Draw the background color.
68 [[NSColor windowBackgroundColor] set];
69 [NSBezierPath fillRect:rect];
71 // Draw the right stroke.
72 [[NSColor grayColor] setStroke];
73 [NSBezierPath strokeLineFromPoint:NSMakePoint(NSMaxX(rect), NSMinY(rect))
74 toPoint:NSMakePoint(NSMaxX(rect), NSMaxY(rect))];
76 // Get some common elements of the source view.
77 NSTextView* textView = [sourceView_ textView];
78 NSLayoutManager* layoutManager = [textView layoutManager];
79 NSTextContainer* textContainer = [textView textContainer];
80 NSRect visibleRect = [[[self scrollView] contentView] bounds];
82 // Get the visible glyph range, as NSRulerView only draws in the visible rect.
83 NSRange visibleGlyphRange = [layoutManager glyphRangeForBoundingRect:visibleRect
84 inTextContainer:textContainer];
85 NSRange characterRange = [layoutManager characterRangeForGlyphRange:visibleGlyphRange
86 actualGlyphRange:NULL];
88 // Load any markers. The superview takes care of filtering out for just the
89 // curently displayed file.
90 NSSet<NSNumber*>* markers = [sourceView_ markers];
92 // Go through the lines.
93 const NSRange kNullRange = NSMakeRange(NSNotFound, 0);
94 const CGFloat yOffset = [textView textContainerInset].height;
96 const size_t lineCount = lineIndex_.size();
97 std::vector<NSUInteger>::iterator element =
98 std::lower_bound(lineIndex_.begin(),
100 characterRange.location);
101 for (NSUInteger line = std::distance(lineIndex_.begin(), element);
102 line < lineCount; ++line) {
103 NSUInteger firstCharacterIndex = lineIndex_[line];
104 // Stop after iterating past the end of the visible range.
105 if (firstCharacterIndex > NSMaxRange(characterRange))
108 NSUInteger rectCount;
109 NSRectArray frameRects = [layoutManager rectArrayForCharacterRange:NSMakeRange(firstCharacterIndex, 0)
110 withinSelectedCharacterRange:kNullRange
111 inTextContainer:textContainer
112 rectCount:&rectCount];
114 NSUInteger lineNumber = line + 1;
115 NSAttributedString* lineNumberString =
116 [self attributedStringForLineNumber:lineNumber];
117 NSSize stringSize = [lineNumberString size];
119 CGFloat yCoord = yOffset + NSMinY(frameRects[0]) - NSMinY(visibleRect);
120 NSRect drawRect = NSMakeRect(NSWidth(rect) - stringSize.width - kRulerRightPadding,
121 yCoord + (NSHeight(frameRects[0]) - stringSize.height) / 2.0,
122 NSWidth(rect) - kRulerRightPadding,
123 NSHeight(frameRects[0]));
124 [lineNumberString drawInRect:drawRect];
126 // Draw any markers. Adjust the drawRect to be the entire width of the
127 // ruler, rather than just the width of the string.
128 drawRect.origin.x = NSMinX(rect);
130 if ([markers containsObject:@(lineNumber)]) {
131 [self drawBreakpointInRect:drawRect];
133 if (sourceView_.markedLine == lineNumber) {
134 [self drawProgramCounterInRect:drawRect];
140 - (void)performLayout
142 [self computeLineIndex];
144 // Determine the width of the ruler based on the line count.
145 if (lineIndex_.empty()) {
146 [self setRuleThickness:kDefaultWidth];
148 NSUInteger lastElement = lineIndex_.back() + 1;
149 NSAttributedString* lastElementString = [self attributedStringForLineNumber:lastElement];
150 NSSize boundingSize = [lastElementString size];
151 [self setRuleThickness:std::max(kDefaultWidth, boundingSize.width)];
154 [self setNeedsDisplay:YES];
157 - (NSUInteger)lineNumberAtPoint:(NSPoint)point
159 // Get some common elements of the source view.
160 NSTextView* textView = [sourceView_ textView];
161 NSLayoutManager* layoutManager = [textView layoutManager];
162 NSTextContainer* textContainer = [textView textContainer];
163 NSRect visibleRect = [[[self scrollView] contentView] bounds];
164 point.y += NSMinY(visibleRect); // Adjust for scroll offset.
166 const CGFloat kWidth = NSWidth([self bounds]);
167 const NSRange kNullRange = NSMakeRange(NSNotFound, 0);
168 const size_t lineCount = lineIndex_.size();
169 for (NSUInteger line = 0; line < lineCount; ++line) {
170 NSUInteger firstCharacterIndex = lineIndex_[line];
172 NSUInteger rectCount;
173 NSRectArray frameRects =
174 [layoutManager rectArrayForCharacterRange:NSMakeRange(firstCharacterIndex, 0)
175 withinSelectedCharacterRange:kNullRange
176 inTextContainer:textContainer
177 rectCount:&rectCount];
178 for (NSUInteger i = 0; i < rectCount; ++i) {
179 frameRects[i].size.width = kWidth;
180 if (NSPointInRect(point, frameRects[i])) {
188 - (void)mouseDown:(NSEvent*)theEvent
190 NSPoint point = [theEvent locationInWindow];
191 point = [self convertPoint:point fromView:nil];
192 NSUInteger line = [self lineNumberAtPoint:point];
193 if (line != NSNotFound)
194 [sourceView_.delegate gutterClickedAtLine:line forFile:sourceView_.file];
197 // Private /////////////////////////////////////////////////////////////////////
200 * Iterates over the text storage system and computes a map of line numbers to
201 * first character index for a line's frame rectangle.
203 - (void)computeLineIndex
207 NSString* text = [[sourceView_ textView] string];
208 NSUInteger stringLength = [text length];
209 NSUInteger index = 0;
211 while (index < stringLength) {
212 lineIndex_.push_back(index);
213 index = NSMaxRange([text lineRangeForRange:NSMakeRange(index, 0)]);
216 if (lineIndex_.empty())
219 NSUInteger lineEnd, contentEnd;
220 [text getLineStart:NULL
222 contentsEnd:&contentEnd
223 forRange:NSMakeRange(lineIndex_.back(), 0)];
224 if (contentEnd < lineEnd)
225 lineIndex_.push_back(index);
229 * Takes in a line number and returns a formatted attributed string, usable
232 - (NSAttributedString*)attributedStringForLineNumber:(unsigned long)line
234 NSString* format = [NSString stringWithFormat:@"%lu", line];
235 return [[NSAttributedString alloc] initWithString:format
236 attributes:[self fontAttributes]];
240 * Returns the dictionary for an NSAttributedString with which the line numbers
243 - (NSDictionary*)fontAttributes
245 NSFont* font = [NSFont fontWithDescriptor:[[BSSourceView sourceFont] fontDescriptor] size:11.0];
247 NSFontAttributeName : font,
248 NSForegroundColorAttributeName : [NSColor grayColor],
253 * Draws a breakpoint (a blue arrow) in the specified rectangle.
255 - (void)drawBreakpointInRect:(NSRect)rect
257 [self drawMarkerInRect:rect
258 fillColor:[NSColor colorWithDeviceRed:0.004 green:0.557 blue:0.851 alpha:1.0]
259 strokeColor:[NSColor colorWithDeviceRed:0.0 green:0.404 blue:0.804 alpha:1.0]];
263 * Draws the program counter (a red arrow) in the specified rectangle.
265 - (void)drawProgramCounterInRect:(NSRect)rect
267 [self drawMarkerInRect:rect
268 fillColor:[[NSColor redColor] colorWithAlphaComponent:0.5]
269 strokeColor:[NSColor colorWithDeviceRed:0.788 green:0 blue:0 alpha:1.0]];
273 * Draws the arrow shape in a given color.
275 - (void)drawMarkerInRect:(NSRect)rect
276 fillColor:(NSColor*)fill
277 strokeColor:(NSColor*)stroke
279 [[NSGraphicsContext currentContext] saveGraphicsState];
281 NSBezierPath* path = [NSBezierPath bezierPath];
283 const CGFloat kPadding = 2.0;
284 const CGFloat kArrowWidth = 7.0;
285 const CGFloat minX = NSMinX(rect) + kPadding;
286 const CGFloat maxX = NSMaxX(rect);
287 const CGFloat minY = NSMinY(rect) + kPadding;
289 [path moveToPoint:NSMakePoint(minX, minY)]; // initial origin
290 [path lineToPoint:NSMakePoint(maxX - kArrowWidth, minY)]; // upper right
291 [path lineToPoint:NSMakePoint(maxX - kPadding, NSMidY(rect))]; // point
292 [path lineToPoint:NSMakePoint(maxX - kArrowWidth, NSMaxY(rect) - kPadding)]; // lower right
293 [path lineToPoint:NSMakePoint(minX, NSMaxY(rect) - kPadding)]; // lower left
294 [path lineToPoint:NSMakePoint(minX, minY - 1)]; // upper left
300 [path setLineWidth:2];
303 [[NSGraphicsContext currentContext] restoreGraphicsState];