Convert the remaining DebuggerController IBOutlets to properties.
[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 #include <algorithm>
20
21 #import "Breakpoint.h"
22 #import "BSSourceView.h"
23
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;
33 @end
34
35 // Constants {{
36
37 // The default width of the ruler.
38 const CGFloat kDefaultWidth = 30.0;
39
40 // Padding between the right edge of the ruler and the line number string.
41 const CGFloat kRulerRightPadding = 2.5;
42
43 // }}
44
45
46 @implementation BSLineNumberRulerView
47
48 - (id)initWithSourceView:(BSSourceView*)sourceView
49 {
50 if (self = [super initWithScrollView:[sourceView scrollView]
51 orientation:NSVerticalRuler]) {
52 sourceView_ = sourceView;
53 [self setClientView:[[sourceView_ scrollView] documentView]];
54 [self setRuleThickness:kDefaultWidth];
55 }
56 return self;
57 }
58
59 - (void)awakeFromNib
60 {
61 [self setClientView:[[sourceView_ scrollView] documentView]];
62 [self setRuleThickness:kDefaultWidth];
63 }
64
65 - (void)drawHashMarksAndLabelsInRect:(NSRect)rect
66 {
67 // Draw the background color.
68 [[NSColor colorWithDeviceRed:0.871 green:0.871 blue:0.871 alpha:1] set];
69 [NSBezierPath fillRect:rect];
70
71 // Draw the right stroke.
72 [[NSColor grayColor] setStroke];
73 [NSBezierPath strokeLineFromPoint:NSMakePoint(NSMaxX(rect), NSMinY(rect))
74 toPoint:NSMakePoint(NSMaxX(rect), NSMaxY(rect))];
75
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];
81
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];
87
88 // Load any markers. The superview takes care of filtering out for just the
89 // curently displayed file.
90 NSSet<NSNumber*>* markers = [sourceView_ markers];
91
92 // Go through the lines.
93 const NSRange kNullRange = NSMakeRange(NSNotFound, 0);
94 const CGFloat yOffset = [textView textContainerInset].height;
95
96 const size_t lineCount = lineIndex_.size();
97 std::vector<NSUInteger>::iterator element =
98 std::lower_bound(lineIndex_.begin(),
99 lineIndex_.end(),
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))
106 break;
107
108 NSUInteger rectCount;
109 NSRectArray frameRects = [layoutManager rectArrayForCharacterRange:NSMakeRange(firstCharacterIndex, 0)
110 withinSelectedCharacterRange:kNullRange
111 inTextContainer:textContainer
112 rectCount:&rectCount];
113 if (frameRects) {
114 NSUInteger lineNumber = line + 1;
115 NSAttributedString* lineNumberString =
116 [self attributedStringForLineNumber:lineNumber];
117 NSSize stringSize = [lineNumberString size];
118
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];
125
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);
129
130 if ([markers containsObject:@(lineNumber)]) {
131 [self drawBreakpointInRect:drawRect];
132 }
133 if (sourceView_.markedLine == lineNumber) {
134 [self drawProgramCounterInRect:drawRect];
135 }
136 }
137 }
138 }
139
140 - (void)performLayout
141 {
142 [self computeLineIndex];
143
144 // Determine the width of the ruler based on the line count.
145 NSUInteger lastElement = lineIndex_.back() + 1;
146 NSAttributedString* lastElementString = [self attributedStringForLineNumber:lastElement];
147 NSSize boundingSize = [lastElementString size];
148 [self setRuleThickness:std::max(kDefaultWidth, boundingSize.width)];
149
150 [self setNeedsDisplay:YES];
151 }
152
153 - (NSUInteger)lineNumberAtPoint:(NSPoint)point
154 {
155 // Get some common elements of the source view.
156 NSTextView* textView = [sourceView_ textView];
157 NSLayoutManager* layoutManager = [textView layoutManager];
158 NSTextContainer* textContainer = [textView textContainer];
159 NSRect visibleRect = [[[self scrollView] contentView] bounds];
160 point.y += NSMinY(visibleRect); // Adjust for scroll offset.
161
162 const CGFloat kWidth = NSWidth([self bounds]);
163 const NSRange kNullRange = NSMakeRange(NSNotFound, 0);
164 const size_t lineCount = lineIndex_.size();
165 for (NSUInteger line = 0; line < lineCount; ++line) {
166 NSUInteger firstCharacterIndex = lineIndex_[line];
167
168 NSUInteger rectCount;
169 NSRectArray frameRects =
170 [layoutManager rectArrayForCharacterRange:NSMakeRange(firstCharacterIndex, 0)
171 withinSelectedCharacterRange:kNullRange
172 inTextContainer:textContainer
173 rectCount:&rectCount];
174 for (NSUInteger i = 0; i < rectCount; ++i) {
175 frameRects[i].size.width = kWidth;
176 if (NSPointInRect(point, frameRects[i])) {
177 return line + 1;
178 }
179 }
180 }
181 return NSNotFound;
182 }
183
184 - (void)mouseDown:(NSEvent*)theEvent
185 {
186 NSPoint point = [theEvent locationInWindow];
187 point = [self convertPoint:point fromView:nil];
188 NSUInteger line = [self lineNumberAtPoint:point];
189 if (line != NSNotFound)
190 [sourceView_.delegate gutterClickedAtLine:line forFile:sourceView_.file];
191 }
192
193 // Private /////////////////////////////////////////////////////////////////////
194
195 /**
196 * Iterates over the text storage system and computes a map of line numbers to
197 * first character index for a line's frame rectangle.
198 */
199 - (void)computeLineIndex
200 {
201 lineIndex_.clear();
202
203 NSString* text = [[sourceView_ textView] string];
204 NSUInteger stringLength = [text length];
205 NSUInteger index = 0;
206
207 while (index < stringLength) {
208 lineIndex_.push_back(index);
209 index = NSMaxRange([text lineRangeForRange:NSMakeRange(index, 0)]);
210 }
211
212 NSUInteger lineEnd, contentEnd;
213 [text getLineStart:NULL
214 end:&lineEnd
215 contentsEnd:&contentEnd
216 forRange:NSMakeRange(lineIndex_.back(), 0)];
217 if (contentEnd < lineEnd)
218 lineIndex_.push_back(index);
219 }
220
221 /**
222 * Takes in a line number and returns a formatted attributed string, usable
223 * for drawing.
224 */
225 - (NSAttributedString*)attributedStringForLineNumber:(unsigned long)line
226 {
227 NSString* format = [NSString stringWithFormat:@"%lu", line];
228 return [[[NSAttributedString alloc] initWithString:format
229 attributes:[self fontAttributes]] autorelease];
230 }
231
232 /**
233 * Returns the dictionary for an NSAttributedString with which the line numbers
234 * will be drawn.
235 */
236 - (NSDictionary*)fontAttributes
237 {
238 NSFont* font = [NSFont fontWithName:@"Menlo" size:10.0];
239 if (!font)
240 font = [NSFont fontWithName:@"Monaco" size:10.0];
241 return [NSDictionary dictionaryWithObjectsAndKeys:
242 font, NSFontAttributeName,
243 [NSColor grayColor], NSForegroundColorAttributeName,
244 nil
245 ];
246 }
247
248 /**
249 * Draws a breakpoint (a blue arrow) in the specified rectangle.
250 */
251 - (void)drawBreakpointInRect:(NSRect)rect
252 {
253 [self drawMarkerInRect:rect
254 fillColor:[NSColor colorWithDeviceRed:0.004 green:0.557 blue:0.851 alpha:1.0]
255 strokeColor:[NSColor colorWithDeviceRed:0.0 green:0.404 blue:0.804 alpha:1.0]];
256 }
257
258 /**
259 * Draws the program counter (a red arrow) in the specified rectangle.
260 */
261 - (void)drawProgramCounterInRect:(NSRect)rect
262 {
263 [self drawMarkerInRect:rect
264 fillColor:[[NSColor redColor] colorWithAlphaComponent:0.5]
265 strokeColor:[NSColor colorWithDeviceRed:0.788 green:0 blue:0 alpha:1.0]];
266 }
267
268 /**
269 * Draws the arrow shape in a given color.
270 */
271 - (void)drawMarkerInRect:(NSRect)rect
272 fillColor:(NSColor*)fill
273 strokeColor:(NSColor*)stroke
274 {
275 [[NSGraphicsContext currentContext] saveGraphicsState];
276
277 NSBezierPath* path = [NSBezierPath bezierPath];
278
279 const CGFloat kPadding = 2.0;
280 const CGFloat kArrowWidth = 7.0;
281 const CGFloat minX = NSMinX(rect) + kPadding;
282 const CGFloat maxX = NSMaxX(rect);
283 const CGFloat minY = NSMinY(rect) + kPadding;
284
285 [path moveToPoint:NSMakePoint(minX, minY)]; // initial origin
286 [path lineToPoint:NSMakePoint(maxX - kArrowWidth, minY)]; // upper right
287 [path lineToPoint:NSMakePoint(maxX - kPadding, NSMidY(rect))]; // point
288 [path lineToPoint:NSMakePoint(maxX - kArrowWidth, NSMaxY(rect) - kPadding)]; // lower right
289 [path lineToPoint:NSMakePoint(minX, NSMaxY(rect) - kPadding)]; // lower left
290 [path lineToPoint:NSMakePoint(minX, minY - 1)]; // upper left
291
292 [fill set];
293 [path fill];
294
295 [stroke set];
296 [path setLineWidth:2];
297 [path stroke];
298
299 [[NSGraphicsContext currentContext] restoreGraphicsState];
300 }
301
302 @end