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 colorWithDeviceRed:0.871 green:0.871 blue:0.871 alpha:1] 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* 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 Breakpoint* test = [[[Breakpoint alloc] initWithLine:lineNumber
131 inFile:[sourceView_ file]] autorelease];
132 if ([markers containsObject:test]) {
133 [self drawBreakpointInRect:drawRect];
135 if (sourceView_.markedLine == lineNumber) {
136 [self drawProgramCounterInRect:drawRect];
142 - (void)performLayout
144 [self computeLineIndex];
146 // Determine the width of the ruler based on the line count.
147 NSUInteger lastElement = lineIndex_.back() + 1;
148 NSAttributedString* lastElementString = [self attributedStringForLineNumber:lastElement];
149 NSSize boundingSize = [lastElementString size];
150 [self setRuleThickness:std::max(kDefaultWidth, boundingSize.width)];
152 [self setNeedsDisplay:YES];
155 - (NSUInteger)lineNumberAtPoint:(NSPoint)point
157 // Get some common elements of the source view.
158 NSTextView* textView = [sourceView_ textView];
159 NSLayoutManager* layoutManager = [textView layoutManager];
160 NSTextContainer* textContainer = [textView textContainer];
161 NSRect visibleRect = [[[self scrollView] contentView] bounds];
162 point.y += NSMinY(visibleRect); // Adjust for scroll offset.
164 const CGFloat kWidth = NSWidth([self bounds]);
165 const NSRange kNullRange = NSMakeRange(NSNotFound, 0);
166 const size_t lineCount = lineIndex_.size();
167 for (NSUInteger line = 0; line < lineCount; ++line) {
168 NSUInteger firstCharacterIndex = lineIndex_[line];
170 NSUInteger rectCount;
171 NSRectArray frameRects =
172 [layoutManager rectArrayForCharacterRange:NSMakeRange(firstCharacterIndex, 0)
173 withinSelectedCharacterRange:kNullRange
174 inTextContainer:textContainer
175 rectCount:&rectCount];
176 for (NSUInteger i = 0; i < rectCount; ++i) {
177 frameRects[i].size.width = kWidth;
178 if (NSPointInRect(point, frameRects[i])) {
186 - (void)mouseDown:(NSEvent*)theEvent
188 NSPoint point = [theEvent locationInWindow];
189 point = [self convertPoint:point fromView:nil];
190 NSUInteger line = [self lineNumberAtPoint:point];
191 if (line != NSNotFound)
192 [sourceView_.delegate gutterClickedAtLine:line forFile:sourceView_.file];
195 // Private /////////////////////////////////////////////////////////////////////
198 * Iterates over the text storage system and computes a map of line numbers to
199 * first character index for a line's frame rectangle.
201 - (void)computeLineIndex
205 NSString* text = [[sourceView_ textView] string];
206 NSUInteger stringLength = [text length];
207 NSUInteger index = 0;
209 while (index < stringLength) {
210 lineIndex_.push_back(index);
211 index = NSMaxRange([text lineRangeForRange:NSMakeRange(index, 0)]);
214 NSUInteger lineEnd, contentEnd;
215 [text getLineStart:NULL
217 contentsEnd:&contentEnd
218 forRange:NSMakeRange(lineIndex_.back(), 0)];
219 if (contentEnd < lineEnd)
220 lineIndex_.push_back(index);
224 * Takes in a line number and returns a formatted attributed string, usable
227 - (NSAttributedString*)attributedStringForLineNumber:(NSUInteger)line
229 NSString* format = [NSString stringWithFormat:@"%d", line];
230 return [[[NSAttributedString alloc] initWithString:format
231 attributes:[self fontAttributes]] autorelease];
235 * Returns the dictionary for an NSAttributedString with which the line numbers
238 - (NSDictionary*)fontAttributes
240 NSFont* font = [NSFont fontWithName:@"Menlo" size:10.0];
242 font = [NSFont fontWithName:@"Monaco" size:10.0];
243 return [NSDictionary dictionaryWithObjectsAndKeys:
244 font, NSFontAttributeName,
245 [NSColor grayColor], NSForegroundColorAttributeName,
251 * Draws a breakpoint (a blue arrow) in the specified rectangle.
253 - (void)drawBreakpointInRect:(NSRect)rect
255 [self drawMarkerInRect:rect
256 fillColor:[NSColor colorWithDeviceRed:0.004 green:0.557 blue:0.851 alpha:1.0]
257 strokeColor:[NSColor colorWithDeviceRed:0.0 green:0.404 blue:0.804 alpha:1.0]];
261 * Draws the program counter (a red arrow) in the specified rectangle.
263 - (void)drawProgramCounterInRect:(NSRect)rect
265 [self drawMarkerInRect:rect
266 fillColor:[[NSColor redColor] colorWithAlphaComponent:0.5]
267 strokeColor:[NSColor colorWithDeviceRed:0.788 green:0 blue:0 alpha:1.0]];
271 * Draws the arrow shape in a given color.
273 - (void)drawMarkerInRect:(NSRect)rect
274 fillColor:(NSColor*)fill
275 strokeColor:(NSColor*)stroke
277 [[NSGraphicsContext currentContext] saveGraphicsState];
279 NSBezierPath* path = [NSBezierPath bezierPath];
281 const CGFloat kPadding = 2.0;
282 const CGFloat kArrowWidth = 7.0;
283 const CGFloat minX = NSMinX(rect) + kPadding;
284 const CGFloat maxX = NSMaxX(rect);
285 const CGFloat minY = NSMinY(rect) + kPadding;
287 [path moveToPoint:NSMakePoint(minX, minY)]; // initial origin
288 [path lineToPoint:NSMakePoint(maxX - kArrowWidth, minY)]; // upper right
289 [path lineToPoint:NSMakePoint(maxX - kPadding, NSMidY(rect))]; // point
290 [path lineToPoint:NSMakePoint(maxX - kArrowWidth, NSMaxY(rect) - kPadding)]; // lower right
291 [path lineToPoint:NSMakePoint(minX, NSMaxY(rect) - kPadding)]; // lower left
292 [path lineToPoint:NSMakePoint(minX, minY - 1)]; // upper left
298 [path setLineWidth:2];
301 [[NSGraphicsContext currentContext] restoreGraphicsState];