Start drawing breakpoints in the BSLineNumberRulerView
[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 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 // Private /////////////////////////////////////////////////////////////////////
153
154 /**
155 * Iterates over the text storage system and computes a map of line numbers to
156 * first character index for a line's frame rectangle.
157 */
158 - (void)computeLineIndex
159 {
160 lineIndex_.clear();
161
162 NSString* text = [[sourceView_ textView] string];
163 NSUInteger stringLength = [text length];
164 NSUInteger index = 0;
165
166 while (index < stringLength) {
167 lineIndex_.push_back(index);
168 index = NSMaxRange([text lineRangeForRange:NSMakeRange(index, 0)]);
169 }
170
171 NSUInteger lineEnd, contentEnd;
172 [text getLineStart:NULL
173 end:&lineEnd
174 contentsEnd:&contentEnd
175 forRange:NSMakeRange(lineIndex_.back(), 0)];
176 if (contentEnd < lineEnd)
177 lineIndex_.push_back(index);
178 }
179
180 /**
181 * Takes in a line number and returns a formatted attributed string, usable
182 * for drawing.
183 */
184 - (NSAttributedString*)attributedStringForLineNumber:(NSUInteger)line
185 {
186 NSString* format = [NSString stringWithFormat:@"%d", line];
187 return [[[NSAttributedString alloc] initWithString:format
188 attributes:[self fontAttributes]] autorelease];
189 }
190
191 /**
192 * Returns the dictionary for an NSAttributedString with which the line numbers
193 * will be drawn.
194 */
195 - (NSDictionary*)fontAttributes
196 {
197 return [NSDictionary dictionaryWithObjectsAndKeys:
198 [NSFont fontWithName:@"Monaco" size:9.0], NSFontAttributeName,
199 [NSColor grayColor], NSForegroundColorAttributeName,
200 nil
201 ];
202 }
203
204 /**
205 * Draws a breakpoint (a blue arrow) in the specified rectangle.
206 */
207 - (void)drawBreakpointInRect:(NSRect)rect
208 {
209 [[NSGraphicsContext currentContext] saveGraphicsState];
210
211 NSBezierPath* path = [NSBezierPath bezierPath];
212
213 [path moveToPoint:NSMakePoint(rect.origin.x + 2, rect.origin.y + 2)]; // initial origin
214 [path lineToPoint:NSMakePoint(rect.size.width - 7, rect.origin.y + 2)]; // upper right
215 [path lineToPoint:NSMakePoint(rect.size.width - 2, rect.origin.y + (rect.size.height / 2))]; // point
216 [path lineToPoint:NSMakePoint(rect.size.width - 7, rect.origin.y + rect.size.height - 2)]; // lower right
217 [path lineToPoint:NSMakePoint(rect.origin.x + 2, rect.origin.y + rect.size.height - 2)]; // lower left
218 [path lineToPoint:NSMakePoint(rect.origin.x + 2, rect.origin.y + 1)]; // upper left
219
220 [[NSColor colorWithDeviceRed:0.004 green:0.557 blue:0.851 alpha:1.0] set];
221 [path fill];
222
223 [[NSColor colorWithDeviceRed:0.0 green:0.404 blue:0.804 alpha:1.0] set];
224 [path setLineWidth:2];
225 [path stroke];
226
227 [[NSGraphicsContext currentContext] restoreGraphicsState];
228 }
229
230 /**
231 * Draws the program counter (a red arrow) in the specified rectangle.
232 */
233 - (void)drawProgramCounterInRect:(NSRect)rect
234 {
235 [[NSGraphicsContext currentContext] saveGraphicsState];
236
237 const CGFloat kArrowWidth = 10.0;
238 const CGFloat kMaxX = NSMaxX(rect);
239 const CGFloat kMidY = NSMidY(rect);
240
241 NSBezierPath* path = [NSBezierPath bezierPath];
242 [path moveToPoint:NSMakePoint(kMaxX, kMidY)];
243 [path lineToPoint:NSMakePoint(kMaxX - kArrowWidth, NSMaxY(rect))];
244 [path lineToPoint:NSMakePoint(kMaxX - kArrowWidth, NSMinY(rect))];
245 [path lineToPoint:NSMakePoint(kMaxX, kMidY)];
246
247 [[[NSColor redColor] colorWithAlphaComponent:0.5] set];
248 [path fill];
249
250 [[NSColor redColor] setStroke];
251 [path stroke];
252
253 [[NSGraphicsContext currentContext] restoreGraphicsState];
254 }
255
256 @end