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