Speculative fix for a crash if a handler cannot be found for a transaction.
[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 windowBackgroundColor] 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 if (lineIndex_.empty()) {
146 [self setRuleThickness:kDefaultWidth];
147 } else {
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)];
152 }
153
154 [self setNeedsDisplay:YES];
155 }
156
157 - (NSUInteger)lineNumberAtPoint:(NSPoint)point
158 {
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.
165
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];
171
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])) {
181 return line + 1;
182 }
183 }
184 }
185 return NSNotFound;
186 }
187
188 - (void)mouseDown:(NSEvent*)theEvent
189 {
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];
195 }
196
197 // Private /////////////////////////////////////////////////////////////////////
198
199 /**
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.
202 */
203 - (void)computeLineIndex
204 {
205 lineIndex_.clear();
206
207 NSString* text = [[sourceView_ textView] string];
208 NSUInteger stringLength = [text length];
209 NSUInteger index = 0;
210
211 while (index < stringLength) {
212 lineIndex_.push_back(index);
213 index = NSMaxRange([text lineRangeForRange:NSMakeRange(index, 0)]);
214 }
215
216 if (lineIndex_.empty())
217 return;
218
219 NSUInteger lineEnd, contentEnd;
220 [text getLineStart:NULL
221 end:&lineEnd
222 contentsEnd:&contentEnd
223 forRange:NSMakeRange(lineIndex_.back(), 0)];
224 if (contentEnd < lineEnd)
225 lineIndex_.push_back(index);
226 }
227
228 /**
229 * Takes in a line number and returns a formatted attributed string, usable
230 * for drawing.
231 */
232 - (NSAttributedString*)attributedStringForLineNumber:(unsigned long)line
233 {
234 NSString* format = [NSString stringWithFormat:@"%lu", line];
235 return [[NSAttributedString alloc] initWithString:format
236 attributes:[self fontAttributes]];
237 }
238
239 /**
240 * Returns the dictionary for an NSAttributedString with which the line numbers
241 * will be drawn.
242 */
243 - (NSDictionary*)fontAttributes
244 {
245 NSFont* font = [NSFont fontWithDescriptor:[[BSSourceView sourceFont] fontDescriptor] size:11.0];
246 return @{
247 NSFontAttributeName : font,
248 NSForegroundColorAttributeName : [NSColor grayColor],
249 };
250 }
251
252 /**
253 * Draws a breakpoint (a blue arrow) in the specified rectangle.
254 */
255 - (void)drawBreakpointInRect:(NSRect)rect
256 {
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]];
260 }
261
262 /**
263 * Draws the program counter (a red arrow) in the specified rectangle.
264 */
265 - (void)drawProgramCounterInRect:(NSRect)rect
266 {
267 [self drawMarkerInRect:rect
268 fillColor:[[NSColor redColor] colorWithAlphaComponent:0.5]
269 strokeColor:[NSColor colorWithDeviceRed:0.788 green:0 blue:0 alpha:1.0]];
270 }
271
272 /**
273 * Draws the arrow shape in a given color.
274 */
275 - (void)drawMarkerInRect:(NSRect)rect
276 fillColor:(NSColor*)fill
277 strokeColor:(NSColor*)stroke
278 {
279 [[NSGraphicsContext currentContext] saveGraphicsState];
280
281 NSBezierPath* path = [NSBezierPath bezierPath];
282
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;
288
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
295
296 [fill set];
297 [path fill];
298
299 [stroke set];
300 [path setLineWidth:2];
301 [path stroke];
302
303 [[NSGraphicsContext currentContext] restoreGraphicsState];
304 }
305
306 @end